xnd 0.2.0dev5 → 0.2.0dev6

Sign up to get free protection for your applications and to get access to all the features.
@@ -44,127 +44,18 @@
44
44
  #include "ndtypes.h"
45
45
 
46
46
  /* Raise an error stored in $!. Clears it before raising. */
47
- inline void
48
- raise_error(void)
49
- {
50
- VALUE exeception = rb_errinfo();
47
+ void raise_error(void);
51
48
 
52
- rb_set_errinfo(Qnil);
53
- rb_exc_raise(exeception);
54
- }
49
+ void set_error_info(VALUE err, const char * msg);
55
50
 
56
- inline void
57
- set_error_info(VALUE err, const char * msg)
58
- {
59
- rb_set_errinfo(rb_exc_new2(err, msg));
60
- }
51
+ size_t safe_downcast(int64_t size);
61
52
 
62
- static inline size_t
63
- safe_downcast(int64_t size)
64
- {
65
- #if SIZE_MAX < INT64_MAX
66
- if (size > INT32_MAX) {
67
- rb_raise(rb_eSizeError,
68
- "sizes should never exceed INT32_MAX on 32-bit platforms.");
69
- }
70
- #endif
71
- return (size_t)size;
72
- }
53
+ bool check_invariants(const ndt_t *t);
73
54
 
74
- static inline bool
75
- check_invariants(const ndt_t *t)
76
- {
77
- #if SIZE_MAX < INT64_MAX
78
- return safe_downcast(t->datasize) >= 0;
79
- #else
80
- (void)t;
81
- return 1;
82
- #endif
83
- }
55
+ VALUE array_new(int64_t size);
84
56
 
85
- static inline VALUE
86
- array_new(int64_t size)
87
- {
88
- #if SIZE_MAX < INT64_MAX
89
- size_t n = safe_downcast(size);
90
- return n < 0 ? NULL : rb_ary_new2(n);
91
- #else
92
- return rb_ary_new2(size);
93
- #endif
94
- }
57
+ VALUE bytes_from_string_and_size(const char *str, int64_t size);
95
58
 
96
- static inline VALUE
97
- bytes_from_string_and_size(const char *str, int64_t size)
98
- {
99
- #if SIZE_MAX < INT64_MAX
100
- size_t n = safe_downcast(size);
101
- return n < 0 ? NULL : rb_str_new(str, n);
102
- #else
103
- return rb_str_new(str, size);
104
- #endif
105
- }
106
-
107
- static long long
108
- mod(long long a, long long b)
109
- {
110
- long long r = a % b;
111
- return r < 0 ? r + b : r;
112
- }
113
-
114
- static inline int
115
- rb_range_unpack(VALUE range, long long *begin, long long *end, long long *step, size_t size)
116
- {
117
- /* FIXME: As of 27 Aug. 2018 Ruby trunk implements step as a property of
118
- Range and XND will support it as and when it is available. Maybe for
119
- now we can implement a #step iterator in a separate method.
120
- */
121
- *step = 1;
122
- VALUE rb_begin = rb_funcall(range, rb_intern("begin"), 0, NULL);
123
- VALUE rb_end = rb_funcall(range, rb_intern("end"), 0, NULL);
124
- int exclude_end = RTEST(rb_funcall(range, rb_intern("exclude_end?"), 0, NULL));
125
-
126
- if (RB_TYPE_P(rb_begin, T_FLOAT)) {
127
- double value = RFLOAT_VALUE(rb_begin);
128
-
129
- if (isinf(value)) {
130
- *begin = 0;
131
- }
132
- }
133
- else {
134
- long long temp = NUM2LL(rb_begin);
135
-
136
- if (temp < 0) { /* if negative index map to positive. */
137
- temp = mod(temp, (long long)size);
138
- }
139
-
140
- *begin = temp;
141
- }
142
-
143
- if (RB_TYPE_P(rb_end, T_FLOAT)) {
144
- double value = RFLOAT_VALUE(rb_end);
145
-
146
- if (isinf(value)) {
147
- *end = INT64_MAX;
148
- return;
149
- }
150
- }
151
- else {
152
- long long temp = NUM2LL(rb_end);
153
-
154
- if (temp < 0) { /* if negative index map to ppositive. */
155
- temp = mod(temp, (long long)size);
156
- }
157
-
158
- *end = temp;
159
- }
160
-
161
- /* a[0..0] in Ruby returns the 0th index.
162
- a[0...0] in Ruby returns empty array like a[0:0] in Python.
163
- libxnd does not include the last index by default.
164
- */
165
- if (!exclude_end) {
166
- *end += 1;
167
- }
168
- }
59
+ int ndt_exists(void);
169
60
 
170
61
  #endif /* UTIL_H */
data/lib/xnd.rb CHANGED
@@ -29,18 +29,25 @@
29
29
  # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
30
 
31
31
  require 'ndtypes'
32
- require "ruby_xnd.so"
32
+ begin
33
+ require "ruby_xnd.so"
34
+ rescue LoadError
35
+ require 'ruby_xnd/ruby_xnd.so'
36
+ end
33
37
 
34
- require 'xnd/monkeys'
35
38
  require 'xnd/version'
36
39
 
37
40
  INF = Float::INFINITY
38
41
 
39
- class RubyXND
42
+ class XND
40
43
  class Ellipsis
41
44
  def to_s
42
45
  "..."
43
46
  end
47
+
48
+ def == other
49
+ other.is_a?(XND::Ellipsis) ? true : false
50
+ end
44
51
  end
45
52
  end
46
53
 
@@ -122,6 +129,7 @@ class XND < RubyXND
122
129
  end
123
130
 
124
131
  t = dtype
132
+
125
133
  var = shapes.map { |lst| lst.uniq.size > 1 || nil }.any?
126
134
  shapes.each do |lst|
127
135
  opt = lst.include? nil
@@ -1,6 +1,6 @@
1
1
  class RubyXND
2
2
  # VERSION number should be the version of libxnd this is built against.
3
- VERSION = "0.2.0dev5"
3
+ VERSION = "0.2.0dev6"
4
4
  # Working commit in xnd repo
5
5
  COMMIT = "31ec2d8501af442e1a5054d9531a5e2cefb242cb"
6
6
  end
@@ -37,8 +37,9 @@ Gem::Specification.new do |spec|
37
37
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
38
38
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
39
39
  spec.require_paths = ["lib"]
40
-
41
- spec.add_development_dependency 'rspec', '~> 3.8'
40
+
41
+ spec.add_development_dependency 'minitest', '~> 5.11'
42
+ spec.add_development_dependency 'minitest-hooks'
42
43
  spec.add_development_dependency 'rake-compiler'
43
44
  spec.add_development_dependency 'pry'
44
45
  spec.add_development_dependency 'pry-byebug'
metadata CHANGED
@@ -1,29 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xnd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0dev5
4
+ version: 0.2.0dev6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sameer Deshmukh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-09-25 00:00:00.000000000 Z
11
+ date: 2018-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rspec
14
+ name: minitest
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '3.8'
19
+ version: '5.11'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '3.8'
26
+ version: '5.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest-hooks
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rake-compiler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -101,16 +115,12 @@ files:
101
115
  - ext/ruby_xnd/float_pack_unpack.h
102
116
  - ext/ruby_xnd/gc_guard.c
103
117
  - ext/ruby_xnd/gc_guard.h
104
- - ext/ruby_xnd/include/xnd.h
105
- - ext/ruby_xnd/lib/libxnd.a
106
- - ext/ruby_xnd/lib/libxnd.so
107
- - ext/ruby_xnd/lib/libxnd.so.0
108
- - ext/ruby_xnd/lib/libxnd.so.0.2.0dev3
109
118
  - ext/ruby_xnd/memory_block_object.c
110
119
  - ext/ruby_xnd/memory_block_object.h
111
120
  - ext/ruby_xnd/ruby_xnd.c
112
121
  - ext/ruby_xnd/ruby_xnd.h
113
122
  - ext/ruby_xnd/ruby_xnd_internal.h
123
+ - ext/ruby_xnd/util.c
114
124
  - ext/ruby_xnd/util.h
115
125
  - ext/ruby_xnd/xnd/AUTHORS.txt
116
126
  - ext/ruby_xnd/xnd/INSTALL.txt
@@ -171,16 +181,8 @@ files:
171
181
  - ext/ruby_xnd/xnd/vcbuild/vcbuild64.bat
172
182
  - ext/ruby_xnd/xnd/vcbuild/vcclean.bat
173
183
  - ext/ruby_xnd/xnd/vcbuild/vcdistclean.bat
174
- - lib/ruby_xnd.so
175
184
  - lib/xnd.rb
176
- - lib/xnd/monkeys.rb
177
185
  - lib/xnd/version.rb
178
- - spec/debug_spec.rb
179
- - spec/gc_guard_spec.rb
180
- - spec/leakcheck.rb
181
- - spec/spec_helper.rb
182
- - spec/type_inference_spec.rb
183
- - spec/xnd_spec.rb
184
186
  - xnd.gemspec
185
187
  homepage: https://github.com/plures/xnd
186
188
  licenses:
@@ -206,10 +208,4 @@ rubygems_version: 2.6.14
206
208
  signing_key:
207
209
  specification_version: 4
208
210
  summary: Ruby wrapper over libxnd. A library for typed data containers.
209
- test_files:
210
- - spec/debug_spec.rb
211
- - spec/gc_guard_spec.rb
212
- - spec/leakcheck.rb
213
- - spec/spec_helper.rb
214
- - spec/type_inference_spec.rb
215
- - spec/xnd_spec.rb
211
+ test_files: []
@@ -1,449 +0,0 @@
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 */