xnd 0.2.0dev3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CONTRIBUTING.md +42 -0
- data/Gemfile +3 -0
- data/History.md +0 -0
- data/README.md +7 -0
- data/Rakefile +135 -0
- data/ext/ruby_xnd/extconf.rb +70 -0
- data/ext/ruby_xnd/float_pack_unpack.c +277 -0
- data/ext/ruby_xnd/float_pack_unpack.h +39 -0
- data/ext/ruby_xnd/gc_guard.c +36 -0
- data/ext/ruby_xnd/gc_guard.h +12 -0
- data/ext/ruby_xnd/include/xnd.h +449 -0
- data/ext/ruby_xnd/lib/libxnd.a +0 -0
- data/ext/ruby_xnd/lib/libxnd.so +1 -0
- data/ext/ruby_xnd/lib/libxnd.so.0 +1 -0
- data/ext/ruby_xnd/lib/libxnd.so.0.2.0dev3 +0 -0
- data/ext/ruby_xnd/memory_block_object.c +32 -0
- data/ext/ruby_xnd/memory_block_object.h +33 -0
- data/ext/ruby_xnd/ruby_xnd.c +1953 -0
- data/ext/ruby_xnd/ruby_xnd.h +61 -0
- data/ext/ruby_xnd/ruby_xnd_internal.h +85 -0
- data/ext/ruby_xnd/util.h +170 -0
- data/ext/ruby_xnd/xnd/AUTHORS.txt +5 -0
- data/ext/ruby_xnd/xnd/INSTALL.txt +134 -0
- data/ext/ruby_xnd/xnd/LICENSE.txt +29 -0
- data/ext/ruby_xnd/xnd/MANIFEST.in +3 -0
- data/ext/ruby_xnd/xnd/Makefile.in +80 -0
- data/ext/ruby_xnd/xnd/README.rst +44 -0
- data/ext/ruby_xnd/xnd/config.guess +1530 -0
- data/ext/ruby_xnd/xnd/config.h.in +22 -0
- data/ext/ruby_xnd/xnd/config.sub +1782 -0
- data/ext/ruby_xnd/xnd/configure +4867 -0
- data/ext/ruby_xnd/xnd/configure.ac +164 -0
- data/ext/ruby_xnd/xnd/doc/Makefile +14 -0
- data/ext/ruby_xnd/xnd/doc/_static/copybutton.js +66 -0
- data/ext/ruby_xnd/xnd/doc/conf.py +26 -0
- data/ext/ruby_xnd/xnd/doc/index.rst +44 -0
- data/ext/ruby_xnd/xnd/doc/libxnd/data-structures.rst +186 -0
- data/ext/ruby_xnd/xnd/doc/libxnd/functions.rst +148 -0
- data/ext/ruby_xnd/xnd/doc/libxnd/index.rst +25 -0
- data/ext/ruby_xnd/xnd/doc/releases/index.rst +34 -0
- data/ext/ruby_xnd/xnd/doc/xnd/align-pack.rst +96 -0
- data/ext/ruby_xnd/xnd/doc/xnd/buffer-protocol.rst +42 -0
- data/ext/ruby_xnd/xnd/doc/xnd/index.rst +30 -0
- data/ext/ruby_xnd/xnd/doc/xnd/quickstart.rst +62 -0
- data/ext/ruby_xnd/xnd/doc/xnd/types.rst +674 -0
- data/ext/ruby_xnd/xnd/install-sh +527 -0
- data/ext/ruby_xnd/xnd/libxnd/Makefile.in +102 -0
- data/ext/ruby_xnd/xnd/libxnd/Makefile.vc +112 -0
- data/ext/ruby_xnd/xnd/libxnd/bitmaps.c +345 -0
- data/ext/ruby_xnd/xnd/libxnd/contrib.h +313 -0
- data/ext/ruby_xnd/xnd/libxnd/copy.c +944 -0
- data/ext/ruby_xnd/xnd/libxnd/equal.c +1216 -0
- data/ext/ruby_xnd/xnd/libxnd/inline.h +154 -0
- data/ext/ruby_xnd/xnd/libxnd/overflow.h +147 -0
- data/ext/ruby_xnd/xnd/libxnd/split.c +286 -0
- data/ext/ruby_xnd/xnd/libxnd/tests/Makefile.in +39 -0
- data/ext/ruby_xnd/xnd/libxnd/tests/Makefile.vc +44 -0
- data/ext/ruby_xnd/xnd/libxnd/tests/README.txt +2 -0
- data/ext/ruby_xnd/xnd/libxnd/tests/runtest.c +101 -0
- data/ext/ruby_xnd/xnd/libxnd/tests/test.h +48 -0
- data/ext/ruby_xnd/xnd/libxnd/tests/test_fixed.c +108 -0
- data/ext/ruby_xnd/xnd/libxnd/xnd.c +1304 -0
- data/ext/ruby_xnd/xnd/libxnd/xnd.h +449 -0
- data/ext/ruby_xnd/xnd/python/test_xnd.py +3144 -0
- data/ext/ruby_xnd/xnd/python/xnd/__init__.py +290 -0
- data/ext/ruby_xnd/xnd/python/xnd/_xnd.c +2822 -0
- data/ext/ruby_xnd/xnd/python/xnd/contrib/pretty.py +850 -0
- data/ext/ruby_xnd/xnd/python/xnd/docstrings.h +129 -0
- data/ext/ruby_xnd/xnd/python/xnd/pyxnd.h +200 -0
- data/ext/ruby_xnd/xnd/python/xnd/util.h +182 -0
- data/ext/ruby_xnd/xnd/python/xnd_randvalue.py +1121 -0
- data/ext/ruby_xnd/xnd/python/xnd_support.py +106 -0
- data/ext/ruby_xnd/xnd/setup.py +303 -0
- data/ext/ruby_xnd/xnd/vcbuild/INSTALL.txt +42 -0
- data/ext/ruby_xnd/xnd/vcbuild/runtest32.bat +16 -0
- data/ext/ruby_xnd/xnd/vcbuild/runtest64.bat +14 -0
- data/ext/ruby_xnd/xnd/vcbuild/vcbuild32.bat +29 -0
- data/ext/ruby_xnd/xnd/vcbuild/vcbuild64.bat +29 -0
- data/ext/ruby_xnd/xnd/vcbuild/vcclean.bat +13 -0
- data/ext/ruby_xnd/xnd/vcbuild/vcdistclean.bat +14 -0
- data/lib/ruby_xnd.so +0 -0
- data/lib/xnd.rb +306 -0
- data/lib/xnd/monkeys.rb +29 -0
- data/lib/xnd/version.rb +6 -0
- data/spec/debug_spec.rb +9 -0
- data/spec/gc_guard_spec.rb +10 -0
- data/spec/leakcheck.rb +9 -0
- data/spec/spec_helper.rb +877 -0
- data/spec/type_inference_spec.rb +81 -0
- data/spec/xnd_spec.rb +2921 -0
- data/xnd.gemspec +47 -0
- metadata +215 -0
@@ -0,0 +1,39 @@
|
|
1
|
+
/* BSD 3-Clause License
|
2
|
+
*
|
3
|
+
* Copyright (c) 2018, Quansight and Sameer Deshmukh
|
4
|
+
* All rights reserved.
|
5
|
+
*
|
6
|
+
* Redistribution and use in source and binary forms, with or without
|
7
|
+
* modification, are permitted provided that the following conditions are met:
|
8
|
+
*
|
9
|
+
* * Redistributions of source code must retain the above copyright notice, this
|
10
|
+
* list of conditions and the following disclaimer.
|
11
|
+
*
|
12
|
+
* * Redistributions in binary form must reproduce the above copyright notice,
|
13
|
+
* this list of conditions and the following disclaimer in the documentation
|
14
|
+
* and/or other materials provided with the distribution.
|
15
|
+
*
|
16
|
+
* * Neither the name of the copyright holder nor the names of its
|
17
|
+
* contributors may be used to endorse or promote products derived from
|
18
|
+
* this software without specific prior written permission.
|
19
|
+
*
|
20
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
21
|
+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
22
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
23
|
+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
24
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
25
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
26
|
+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
27
|
+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
28
|
+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
29
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
30
|
+
*/
|
31
|
+
|
32
|
+
/* Functions for packing and unpacking floats from char * arrays.
|
33
|
+
|
34
|
+
Author: Sameer Deshmukh (@v0dro)
|
35
|
+
*/
|
36
|
+
|
37
|
+
#ifdef XND_DEBUG
|
38
|
+
void run_float_pack_unpack_tests(void);
|
39
|
+
#endif
|
@@ -0,0 +1,36 @@
|
|
1
|
+
/* Functions useful for interfacing shared rbuf objects with the Ruby GC. */
|
2
|
+
/* Author: Sameer Deshmukh (@v0dro) */
|
3
|
+
#include "ruby_xnd_internal.h"
|
4
|
+
|
5
|
+
#define GC_GUARD_TABLE_NAME "@__gc_guard_table"
|
6
|
+
|
7
|
+
static ID id_gc_guard_table;
|
8
|
+
|
9
|
+
/* Unregister an NDT object-rbuf pair from the GC guard. */
|
10
|
+
void
|
11
|
+
rb_xnd_gc_guard_unregister(XndObject *xnd)
|
12
|
+
{
|
13
|
+
VALUE table = rb_ivar_get(mRubyXND_GCGuard, id_gc_guard_table);
|
14
|
+
rb_hash_delete(table, PTR2NUM(xnd));
|
15
|
+
}
|
16
|
+
|
17
|
+
/* Register a XND-mblock pair in the GC guard. */
|
18
|
+
void
|
19
|
+
rb_xnd_gc_guard_register(XndObject *xnd, VALUE mblock)
|
20
|
+
{
|
21
|
+
VALUE table = rb_ivar_get(mRubyXND_GCGuard, id_gc_guard_table);
|
22
|
+
if (table == Qnil) {
|
23
|
+
rb_raise(rb_eLoadError, "GC guard not initialized.");
|
24
|
+
}
|
25
|
+
|
26
|
+
rb_hash_aset(table, PTR2NUM(xnd), mblock);
|
27
|
+
}
|
28
|
+
|
29
|
+
/* Initialize the global GC guard table. klass is a VALUE reprensenting NDTypes class. */
|
30
|
+
void
|
31
|
+
rb_xnd_init_gc_guard(void)
|
32
|
+
{
|
33
|
+
id_gc_guard_table = rb_intern(GC_GUARD_TABLE_NAME);
|
34
|
+
rb_ivar_set(mRubyXND_GCGuard, id_gc_guard_table, rb_hash_new());
|
35
|
+
}
|
36
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
/* Header file containing various functions for GC guard table. */
|
2
|
+
|
3
|
+
#ifndef GC_GUARD_H
|
4
|
+
#define GC_GUARD_H
|
5
|
+
|
6
|
+
#include "ruby_xnd_internal.h"
|
7
|
+
|
8
|
+
void rb_xnd_gc_guard_unregister(XndObject *);
|
9
|
+
void rb_xnd_gc_guard_register(XndObject *, VALUE);
|
10
|
+
void rb_xnd_init_gc_guard(void);
|
11
|
+
|
12
|
+
#endif /* GC_GUARD_H */
|
@@ -0,0 +1,449 @@
|
|
1
|
+
/*
|
2
|
+
* BSD 3-Clause License
|
3
|
+
*
|
4
|
+
* Copyright (c) 2017-2018, plures
|
5
|
+
* All rights reserved.
|
6
|
+
*
|
7
|
+
* Redistribution and use in source and binary forms, with or without
|
8
|
+
* modification, are permitted provided that the following conditions are met:
|
9
|
+
*
|
10
|
+
* 1. Redistributions of source code must retain the above copyright notice,
|
11
|
+
* this list of conditions and the following disclaimer.
|
12
|
+
*
|
13
|
+
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
14
|
+
* this list of conditions and the following disclaimer in the documentation
|
15
|
+
* and/or other materials provided with the distribution.
|
16
|
+
*
|
17
|
+
* 3. Neither the name of the copyright holder nor the names of its
|
18
|
+
* contributors may be used to endorse or promote products derived from
|
19
|
+
* this software without specific prior written permission.
|
20
|
+
*
|
21
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
22
|
+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
23
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
24
|
+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
25
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
26
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
27
|
+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
28
|
+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
29
|
+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
30
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
31
|
+
*/
|
32
|
+
|
33
|
+
|
34
|
+
#ifndef XND_H
|
35
|
+
#define XND_H
|
36
|
+
|
37
|
+
|
38
|
+
#include <stdlib.h>
|
39
|
+
#include <stdint.h>
|
40
|
+
#include <string.h>
|
41
|
+
#include <assert.h>
|
42
|
+
#include "ndtypes.h"
|
43
|
+
|
44
|
+
|
45
|
+
#ifdef _MSC_VER
|
46
|
+
#if defined (XND_EXPORT)
|
47
|
+
#define XND_API __declspec(dllexport)
|
48
|
+
#elif defined(XND_IMPORT)
|
49
|
+
#define XND_API __declspec(dllimport)
|
50
|
+
#else
|
51
|
+
#define XND_API
|
52
|
+
#endif
|
53
|
+
#else
|
54
|
+
#define XND_API
|
55
|
+
#endif
|
56
|
+
|
57
|
+
|
58
|
+
#if SIZE_MAX == UINT64_MAX
|
59
|
+
#define XND_SSIZE_MAX INT64_MAX
|
60
|
+
#elif SIZE_MAX == UINT32_MAX
|
61
|
+
#define XND_SSIZE_MAX INT32_MAX
|
62
|
+
#else
|
63
|
+
#error "unsupported platform: need 32-bit or 64-bit size_t"
|
64
|
+
#endif
|
65
|
+
|
66
|
+
|
67
|
+
/*
|
68
|
+
* Ownership flags: The library itself has no notion of how many exported
|
69
|
+
* views a master buffer has. The Python bindings for example use Pythons's
|
70
|
+
* reference counting to to keep track of exported memory blocks.
|
71
|
+
*/
|
72
|
+
#define XND_OWN_TYPE 0x00000001U /* type pointer */
|
73
|
+
#define XND_OWN_DATA 0x00000002U /* data pointer */
|
74
|
+
#define XND_OWN_STRINGS 0x00000004U /* embedded string pointers */
|
75
|
+
#define XND_OWN_BYTES 0x00000008U /* embedded bytes pointers */
|
76
|
+
#define XND_OWN_POINTERS 0x00000010U /* embedded pointers */
|
77
|
+
|
78
|
+
#define XND_OWN_ALL (XND_OWN_TYPE | \
|
79
|
+
XND_OWN_DATA | \
|
80
|
+
XND_OWN_STRINGS | \
|
81
|
+
XND_OWN_BYTES | \
|
82
|
+
XND_OWN_POINTERS)
|
83
|
+
|
84
|
+
#define XND_OWN_EMBEDDED (XND_OWN_DATA | \
|
85
|
+
XND_OWN_STRINGS | \
|
86
|
+
XND_OWN_BYTES | \
|
87
|
+
XND_OWN_POINTERS)
|
88
|
+
|
89
|
+
|
90
|
+
/* Convenience macros to extract embedded values. */
|
91
|
+
#define XND_POINTER_DATA(ptr) (*((char **)ptr))
|
92
|
+
#define XND_BYTES_SIZE(ptr) (((ndt_bytes_t *)ptr)->size)
|
93
|
+
#define XND_BYTES_DATA(ptr) (((ndt_bytes_t *)ptr)->data)
|
94
|
+
|
95
|
+
|
96
|
+
/* Bitmap tree. */
|
97
|
+
typedef struct xnd_bitmap xnd_bitmap_t;
|
98
|
+
|
99
|
+
struct xnd_bitmap {
|
100
|
+
uint8_t *data; /* bitmap */
|
101
|
+
int64_t size; /* number of subtree bitmaps in the "next" array */
|
102
|
+
xnd_bitmap_t *next; /* array of bitmaps for subtrees */
|
103
|
+
};
|
104
|
+
|
105
|
+
/* Typed memory block, usually a view. */
|
106
|
+
typedef struct xnd {
|
107
|
+
xnd_bitmap_t bitmap; /* bitmap tree */
|
108
|
+
int64_t index; /* linear index for var dims */
|
109
|
+
const ndt_t *type; /* type of the data */
|
110
|
+
char *ptr; /* data */
|
111
|
+
} xnd_t;
|
112
|
+
|
113
|
+
/* Master memory block. */
|
114
|
+
typedef struct xnd_master {
|
115
|
+
uint32_t flags; /* ownership flags */
|
116
|
+
xnd_t master; /* typed memory */
|
117
|
+
} xnd_master_t;
|
118
|
+
|
119
|
+
/* Used in indexing and slicing. */
|
120
|
+
enum xnd_key { Index, FieldName, Slice };
|
121
|
+
typedef struct {
|
122
|
+
enum xnd_key tag;
|
123
|
+
union {
|
124
|
+
int64_t Index;
|
125
|
+
const char *FieldName;
|
126
|
+
ndt_slice_t Slice;
|
127
|
+
};
|
128
|
+
} xnd_index_t;
|
129
|
+
|
130
|
+
|
131
|
+
/* Unstable API: view with ownership tracking. */
|
132
|
+
typedef struct xnd_view {
|
133
|
+
uint32_t flags; /* flags that indicate resource ownership by the view */
|
134
|
+
const void *obj; /* object that holds shared resources */
|
135
|
+
xnd_t view; /* typed memory */
|
136
|
+
} xnd_view_t;
|
137
|
+
|
138
|
+
|
139
|
+
/*****************************************************************************/
|
140
|
+
/* Create xnd memory blocks */
|
141
|
+
/*****************************************************************************/
|
142
|
+
|
143
|
+
XND_API xnd_master_t *xnd_empty_from_string(const char *s, uint32_t flags, ndt_context_t *ctx);
|
144
|
+
XND_API xnd_master_t *xnd_empty_from_type(const ndt_t *t, uint32_t flags, ndt_context_t *ctx);
|
145
|
+
XND_API void xnd_del(xnd_master_t *x);
|
146
|
+
|
147
|
+
/* Create and delete pristine xnd_t buffers. */
|
148
|
+
XND_API xnd_master_t *xnd_from_xnd(xnd_t *src, uint32_t flags, ndt_context_t *ctx);
|
149
|
+
XND_API void xnd_del_buffer(xnd_t *x, uint32_t flags);
|
150
|
+
|
151
|
+
|
152
|
+
/*****************************************************************************/
|
153
|
+
/* Traverse xnd memory blocks */
|
154
|
+
/*****************************************************************************/
|
155
|
+
|
156
|
+
XND_API xnd_t xnd_subtree_index(const xnd_t *x, const int64_t *indices, int len,
|
157
|
+
ndt_context_t *ctx);
|
158
|
+
|
159
|
+
XND_API xnd_t xnd_subtree(const xnd_t *x, const xnd_index_t indices[], int len,
|
160
|
+
ndt_context_t *ctx);
|
161
|
+
|
162
|
+
XND_API xnd_t xnd_multikey(const xnd_t *x, const xnd_index_t indices[], int len,
|
163
|
+
ndt_context_t *ctx);
|
164
|
+
|
165
|
+
XND_API xnd_t xnd_subscript(const xnd_t *x, const xnd_index_t indices[], int len,
|
166
|
+
ndt_context_t *ctx);
|
167
|
+
|
168
|
+
XND_API xnd_t *xnd_split(const xnd_t *x, int64_t *n, int max_outer, ndt_context_t *ctx);
|
169
|
+
|
170
|
+
XND_API int xnd_equal(const xnd_t *x, const xnd_t *y, ndt_context_t *ctx);
|
171
|
+
XND_API int xnd_strict_equal(const xnd_t *x, const xnd_t *y, ndt_context_t *ctx);
|
172
|
+
|
173
|
+
XND_API int xnd_copy(xnd_t *y, const xnd_t *x, uint32_t flags, ndt_context_t *ctx);
|
174
|
+
|
175
|
+
|
176
|
+
/*****************************************************************************/
|
177
|
+
/* Bitmaps */
|
178
|
+
/*****************************************************************************/
|
179
|
+
|
180
|
+
XND_API int xnd_bitmap_init(xnd_bitmap_t *b, const ndt_t *t, ndt_context_t *ctx);
|
181
|
+
XND_API void xnd_bitmap_clear(xnd_bitmap_t *b);
|
182
|
+
XND_API xnd_bitmap_t xnd_bitmap_next(const xnd_t *x, int64_t i, ndt_context_t *ctx);
|
183
|
+
XND_API void xnd_set_valid(xnd_t *x);
|
184
|
+
XND_API void xnd_set_na(xnd_t *x);
|
185
|
+
XND_API int xnd_is_valid(const xnd_t *x);
|
186
|
+
XND_API int xnd_is_na(const xnd_t *x);
|
187
|
+
|
188
|
+
|
189
|
+
/*****************************************************************************/
|
190
|
+
/* Error handling */
|
191
|
+
/*****************************************************************************/
|
192
|
+
|
193
|
+
XND_API extern const xnd_t xnd_error;
|
194
|
+
XND_API extern const xnd_bitmap_t xnd_bitmap_empty;
|
195
|
+
|
196
|
+
|
197
|
+
/*****************************************************************************/
|
198
|
+
/* Unstable API */
|
199
|
+
/*****************************************************************************/
|
200
|
+
|
201
|
+
XND_API extern const xnd_view_t xnd_view_error;
|
202
|
+
|
203
|
+
XND_API int xnd_view_err_occurred(const xnd_view_t *x);
|
204
|
+
XND_API void xnd_view_clear(xnd_view_t *x);
|
205
|
+
XND_API xnd_view_t xnd_view_from_xnd(const void *obj, const xnd_t *x);
|
206
|
+
XND_API xnd_view_t xnd_view_subscript(const xnd_view_t *x, const xnd_index_t indices[],
|
207
|
+
int len, ndt_context_t *ctx);
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
/*****************************************************************************/
|
212
|
+
/* Float format */
|
213
|
+
/*****************************************************************************/
|
214
|
+
|
215
|
+
XND_API int xnd_init_float(ndt_context_t *ctx);
|
216
|
+
XND_API bool xnd_float_is_little_endian(void);
|
217
|
+
XND_API bool xnd_float_is_big_endian(void);
|
218
|
+
XND_API bool xnd_double_is_little_endian(void);
|
219
|
+
XND_API bool xnd_double_is_big_endian(void);
|
220
|
+
|
221
|
+
|
222
|
+
/*****************************************************************************/
|
223
|
+
/* Static inline functions */
|
224
|
+
/*****************************************************************************/
|
225
|
+
|
226
|
+
/*
|
227
|
+
* This looks inefficient, but both gcc and clang clean up unused xnd_t members.
|
228
|
+
*/
|
229
|
+
static inline int64_t
|
230
|
+
xnd_ndim(const xnd_t *x)
|
231
|
+
{
|
232
|
+
return x->type->ndim;
|
233
|
+
}
|
234
|
+
|
235
|
+
static inline xnd_t
|
236
|
+
xnd_fixed_dim_next(const xnd_t *x, const int64_t i)
|
237
|
+
{
|
238
|
+
const ndt_t *t = x->type;
|
239
|
+
const ndt_t *u = t->FixedDim.type;
|
240
|
+
const int64_t step = i * t->Concrete.FixedDim.step;
|
241
|
+
xnd_t next;
|
242
|
+
|
243
|
+
assert(t->tag == FixedDim);
|
244
|
+
|
245
|
+
next.bitmap = x->bitmap;
|
246
|
+
next.index = x->index + step;
|
247
|
+
next.type = u;
|
248
|
+
next.ptr = u->ndim==0 ? x->ptr + next.index * next.type->datasize : x->ptr;
|
249
|
+
|
250
|
+
return next;
|
251
|
+
}
|
252
|
+
|
253
|
+
static inline int64_t
|
254
|
+
xnd_fixed_shape(const xnd_t *x)
|
255
|
+
{
|
256
|
+
const ndt_t *t = x->type;
|
257
|
+
assert(t->tag == FixedDim);
|
258
|
+
return t->FixedDim.shape;
|
259
|
+
}
|
260
|
+
|
261
|
+
static inline int64_t
|
262
|
+
xnd_fixed_shape_at(const xnd_t *x, const int i)
|
263
|
+
{
|
264
|
+
const ndt_t *t = x->type;
|
265
|
+
|
266
|
+
assert(0 <= i && i < t->ndim);
|
267
|
+
assert(t->tag == FixedDim);
|
268
|
+
|
269
|
+
for (int k = 0; k < i; k++) {
|
270
|
+
t = t->FixedDim.type;
|
271
|
+
}
|
272
|
+
return t->FixedDim.shape;
|
273
|
+
}
|
274
|
+
|
275
|
+
static inline int64_t
|
276
|
+
xnd_fixed_stride(const xnd_t *x)
|
277
|
+
{
|
278
|
+
const ndt_t *t = x->type;
|
279
|
+
assert(t->tag == FixedDim);
|
280
|
+
return t->Concrete.FixedDim.step * t->Concrete.FixedDim.itemsize;
|
281
|
+
}
|
282
|
+
|
283
|
+
static inline char *
|
284
|
+
xnd_fixed_apply_index(const xnd_t *x)
|
285
|
+
{
|
286
|
+
assert(x->type->tag == FixedDim);
|
287
|
+
return x->ptr + x->index * x->type->Concrete.FixedDim.itemsize;
|
288
|
+
}
|
289
|
+
|
290
|
+
static inline xnd_t
|
291
|
+
xnd_var_dim_next(const xnd_t *x, const int64_t start, const int64_t step,
|
292
|
+
const int64_t i)
|
293
|
+
{
|
294
|
+
const ndt_t *t = x->type;
|
295
|
+
const ndt_t *u = t->VarDim.type;
|
296
|
+
xnd_t next;
|
297
|
+
|
298
|
+
next.bitmap = x->bitmap;
|
299
|
+
next.index = start + i * step;
|
300
|
+
next.type = u;
|
301
|
+
next.ptr = u->ndim==0 ? x->ptr + next.index * next.type->datasize : x->ptr;
|
302
|
+
|
303
|
+
return next;
|
304
|
+
}
|
305
|
+
|
306
|
+
static inline xnd_t
|
307
|
+
xnd_tuple_next(const xnd_t *x, const int64_t i, ndt_context_t *ctx)
|
308
|
+
{
|
309
|
+
const ndt_t *t = x->type;
|
310
|
+
xnd_t next;
|
311
|
+
|
312
|
+
next.bitmap = xnd_bitmap_next(x, i, ctx);
|
313
|
+
if (ndt_err_occurred(ctx)) {
|
314
|
+
return xnd_error;
|
315
|
+
}
|
316
|
+
|
317
|
+
next.index = 0;
|
318
|
+
next.type = t->Tuple.types[i];
|
319
|
+
next.ptr = x->ptr + t->Concrete.Tuple.offset[i];
|
320
|
+
|
321
|
+
return next;
|
322
|
+
}
|
323
|
+
|
324
|
+
static inline xnd_t
|
325
|
+
xnd_record_next(const xnd_t *x, const int64_t i, ndt_context_t *ctx)
|
326
|
+
{
|
327
|
+
const ndt_t *t = x->type;
|
328
|
+
xnd_t next;
|
329
|
+
|
330
|
+
next.bitmap = xnd_bitmap_next(x, i, ctx);
|
331
|
+
if (ndt_err_occurred(ctx)) {
|
332
|
+
return xnd_error;
|
333
|
+
}
|
334
|
+
|
335
|
+
next.index = 0;
|
336
|
+
next.type = t->Record.types[i];
|
337
|
+
next.ptr = x->ptr + t->Concrete.Record.offset[i];
|
338
|
+
|
339
|
+
return next;
|
340
|
+
}
|
341
|
+
|
342
|
+
static inline xnd_t
|
343
|
+
xnd_ref_next(const xnd_t *x, ndt_context_t *ctx)
|
344
|
+
{
|
345
|
+
const ndt_t *t = x->type;
|
346
|
+
xnd_t next;
|
347
|
+
|
348
|
+
next.bitmap = xnd_bitmap_next(x, 0, ctx);
|
349
|
+
if (ndt_err_occurred(ctx)) {
|
350
|
+
return xnd_error;
|
351
|
+
}
|
352
|
+
|
353
|
+
next.index = 0;
|
354
|
+
next.type = t->Ref.type;
|
355
|
+
next.ptr = XND_POINTER_DATA(x->ptr);
|
356
|
+
|
357
|
+
return next;
|
358
|
+
}
|
359
|
+
|
360
|
+
static inline xnd_t
|
361
|
+
xnd_constr_next(const xnd_t *x, ndt_context_t *ctx)
|
362
|
+
{
|
363
|
+
const ndt_t *t = x->type;
|
364
|
+
xnd_t next;
|
365
|
+
|
366
|
+
next.bitmap = xnd_bitmap_next(x, 0, ctx);
|
367
|
+
if (ndt_err_occurred(ctx)) {
|
368
|
+
return xnd_error;
|
369
|
+
}
|
370
|
+
|
371
|
+
next.index = 0;
|
372
|
+
next.type = t->Constr.type;
|
373
|
+
next.ptr = x->ptr;
|
374
|
+
|
375
|
+
return next;
|
376
|
+
}
|
377
|
+
|
378
|
+
static inline xnd_t
|
379
|
+
xnd_nominal_next(const xnd_t *x, ndt_context_t *ctx)
|
380
|
+
{
|
381
|
+
const ndt_t *t = x->type;
|
382
|
+
xnd_t next;
|
383
|
+
|
384
|
+
next.bitmap = xnd_bitmap_next(x, 0, ctx);
|
385
|
+
if (ndt_err_occurred(ctx)) {
|
386
|
+
return xnd_error;
|
387
|
+
}
|
388
|
+
|
389
|
+
next.index = 0;
|
390
|
+
next.type = t->Nominal.type;
|
391
|
+
next.ptr = x->ptr;
|
392
|
+
|
393
|
+
return next;
|
394
|
+
}
|
395
|
+
|
396
|
+
#if NDT_SYS_BIG_ENDIAN == 1
|
397
|
+
#define XND_REV_COND NDT_LITTLE_ENDIAN
|
398
|
+
#else
|
399
|
+
#define XND_REV_COND NDT_BIG_ENDIAN
|
400
|
+
#endif
|
401
|
+
|
402
|
+
static inline void
|
403
|
+
memcpy_rev(char *dest, const char *src, size_t size)
|
404
|
+
{
|
405
|
+
size_t i;
|
406
|
+
|
407
|
+
for (i = 0; i < size; i++) {
|
408
|
+
dest[i] = src[size-1-i];
|
409
|
+
}
|
410
|
+
}
|
411
|
+
|
412
|
+
static inline void
|
413
|
+
bcopy_swap(char *dest, const char *src, size_t size, uint32_t flags)
|
414
|
+
{
|
415
|
+
if (flags & XND_REV_COND) {
|
416
|
+
memcpy_rev(dest, src, size);
|
417
|
+
}
|
418
|
+
else {
|
419
|
+
memcpy(dest, src, size);
|
420
|
+
}
|
421
|
+
}
|
422
|
+
|
423
|
+
static inline int
|
424
|
+
le(uint32_t flags)
|
425
|
+
{
|
426
|
+
#if NDT_SYS_BIG_ENDIAN == 1
|
427
|
+
return flags & NDT_LITTLE_ENDIAN;
|
428
|
+
#else
|
429
|
+
return !(flags & NDT_BIG_ENDIAN);
|
430
|
+
#endif
|
431
|
+
}
|
432
|
+
|
433
|
+
|
434
|
+
#define PACK_SINGLE(ptr, src, type, flags) \
|
435
|
+
do { \
|
436
|
+
type _x; \
|
437
|
+
_x = (type)src; \
|
438
|
+
bcopy_swap(ptr, (const char *)&_x, sizeof _x, flags); \
|
439
|
+
} while (0)
|
440
|
+
|
441
|
+
#define UNPACK_SINGLE(dest, ptr, type, flags) \
|
442
|
+
do { \
|
443
|
+
type _x; \
|
444
|
+
bcopy_swap((char *)&_x, ptr, sizeof _x, flags); \
|
445
|
+
dest = _x; \
|
446
|
+
} while (0)
|
447
|
+
|
448
|
+
|
449
|
+
#endif /* XND_H */
|