numo-narray-alt 0.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +14 -0
  3. data/LICENSE +30 -0
  4. data/README.md +71 -0
  5. data/Rakefile +24 -0
  6. data/ext/numo/narray/SFMT-params.h +97 -0
  7. data/ext/numo/narray/SFMT-params19937.h +48 -0
  8. data/ext/numo/narray/SFMT.c +602 -0
  9. data/ext/numo/narray/SFMT.h +147 -0
  10. data/ext/numo/narray/array.c +575 -0
  11. data/ext/numo/narray/data.c +958 -0
  12. data/ext/numo/narray/extconf.rb +84 -0
  13. data/ext/numo/narray/index.c +1092 -0
  14. data/ext/numo/narray/kwargs.c +142 -0
  15. data/ext/numo/narray/math.c +133 -0
  16. data/ext/numo/narray/narray.c +1976 -0
  17. data/ext/numo/narray/narray.def +28 -0
  18. data/ext/numo/narray/ndloop.c +1840 -0
  19. data/ext/numo/narray/numo/compat.h +23 -0
  20. data/ext/numo/narray/numo/intern.h +115 -0
  21. data/ext/numo/narray/numo/narray.h +480 -0
  22. data/ext/numo/narray/numo/ndloop.h +93 -0
  23. data/ext/numo/narray/numo/template.h +149 -0
  24. data/ext/numo/narray/numo/types/bit.h +38 -0
  25. data/ext/numo/narray/numo/types/complex.h +404 -0
  26. data/ext/numo/narray/numo/types/complex_macro.h +384 -0
  27. data/ext/numo/narray/numo/types/dcomplex.h +42 -0
  28. data/ext/numo/narray/numo/types/dfloat.h +44 -0
  29. data/ext/numo/narray/numo/types/float_def.h +34 -0
  30. data/ext/numo/narray/numo/types/float_macro.h +202 -0
  31. data/ext/numo/narray/numo/types/int16.h +27 -0
  32. data/ext/numo/narray/numo/types/int32.h +23 -0
  33. data/ext/numo/narray/numo/types/int64.h +23 -0
  34. data/ext/numo/narray/numo/types/int8.h +23 -0
  35. data/ext/numo/narray/numo/types/int_macro.h +66 -0
  36. data/ext/numo/narray/numo/types/real_accum.h +481 -0
  37. data/ext/numo/narray/numo/types/robj_macro.h +78 -0
  38. data/ext/numo/narray/numo/types/robject.h +25 -0
  39. data/ext/numo/narray/numo/types/scomplex.h +42 -0
  40. data/ext/numo/narray/numo/types/sfloat.h +45 -0
  41. data/ext/numo/narray/numo/types/uint16.h +24 -0
  42. data/ext/numo/narray/numo/types/uint32.h +20 -0
  43. data/ext/numo/narray/numo/types/uint64.h +20 -0
  44. data/ext/numo/narray/numo/types/uint8.h +20 -0
  45. data/ext/numo/narray/numo/types/uint_macro.h +57 -0
  46. data/ext/numo/narray/numo/types/xint_macro.h +166 -0
  47. data/ext/numo/narray/rand.c +40 -0
  48. data/ext/numo/narray/src/t_bit.c +3236 -0
  49. data/ext/numo/narray/src/t_dcomplex.c +6776 -0
  50. data/ext/numo/narray/src/t_dfloat.c +9417 -0
  51. data/ext/numo/narray/src/t_int16.c +5757 -0
  52. data/ext/numo/narray/src/t_int32.c +5757 -0
  53. data/ext/numo/narray/src/t_int64.c +5759 -0
  54. data/ext/numo/narray/src/t_int8.c +5355 -0
  55. data/ext/numo/narray/src/t_robject.c +5567 -0
  56. data/ext/numo/narray/src/t_scomplex.c +6731 -0
  57. data/ext/numo/narray/src/t_sfloat.c +9374 -0
  58. data/ext/numo/narray/src/t_uint16.c +5753 -0
  59. data/ext/numo/narray/src/t_uint32.c +5753 -0
  60. data/ext/numo/narray/src/t_uint64.c +5755 -0
  61. data/ext/numo/narray/src/t_uint8.c +5351 -0
  62. data/ext/numo/narray/step.c +266 -0
  63. data/ext/numo/narray/struct.c +814 -0
  64. data/lib/numo/narray/extra.rb +1266 -0
  65. data/lib/numo/narray.rb +4 -0
  66. metadata +106 -0
@@ -0,0 +1,23 @@
1
+ #ifndef COMPAT_H
2
+ #define COMPAT_H
3
+
4
+ #if !defined RSTRING_LEN
5
+ #define RSTRING_LEN(a) RSTRING(a)->len
6
+ #endif
7
+ #if !defined RSTRING_PTR
8
+ #define RSTRING_PTR(a) RSTRING(a)->ptr
9
+ #endif
10
+ #if !defined RARRAY_LEN
11
+ #define RARRAY_LEN(a) RARRAY(a)->len
12
+ #endif
13
+ #if !defined RARRAY_PTR
14
+ #define RARRAY_PTR(a) RARRAY(a)->ptr
15
+ #endif
16
+ #if !defined RARRAY_AREF
17
+ #define RARRAY_AREF(a, i) RARRAY_PTR(a)[i]
18
+ #endif
19
+ #if !defined RARRAY_ASET
20
+ #define RARRAY_ASET(a, i, v) (RARRAY_PTR(a)[i] = v)
21
+ #endif
22
+
23
+ #endif /* ifndef COMPAT_H */
@@ -0,0 +1,115 @@
1
+ /*
2
+ intern.h
3
+ Ruby/Numo::NArray - Numerical Array class for Ruby
4
+ Copyright (C) 1999-2020 Masahiro TANAKA
5
+ */
6
+ #ifndef INTERN_H
7
+ #define INTERN_H
8
+
9
+ #define rb_narray_new nary_new
10
+ VALUE nary_new(VALUE elem, int ndim, size_t* shape);
11
+ #define rb_narray_view_new nary_view_new
12
+ VALUE nary_view_new(VALUE elem, int ndim, size_t* shape);
13
+ #define rb_narray_debug_info nary_debug_info
14
+ VALUE nary_debug_info(VALUE);
15
+
16
+ #define na_make_view nary_make_view
17
+ VALUE nary_make_view(VALUE self);
18
+
19
+ #define na_s_allocate nary_s_allocate
20
+ VALUE nary_s_allocate(VALUE klass);
21
+ #define na_s_allocate_view nary_s_allocate_view
22
+ VALUE nary_s_allocate_view(VALUE klass);
23
+ #define na_s_new_like nary_s_new_like
24
+ VALUE nary_s_new_like(VALUE type, VALUE obj);
25
+
26
+ void na_alloc_shape(narray_t* na, int ndim);
27
+ void na_array_to_internal_shape(VALUE self, VALUE ary, size_t* shape);
28
+ void na_index_arg_to_internal_order(int argc, VALUE* argv, VALUE self);
29
+ void na_setup_shape(narray_t* na, int ndim, size_t* shape);
30
+
31
+ #define na_get_elmsz nary_element_stride
32
+ // #define na_element_stride nary_element_stride
33
+ unsigned int nary_element_stride(VALUE nary);
34
+ #define na_dtype_elmsz nary_dtype_element_stride
35
+ size_t nary_dtype_element_stride(VALUE klass);
36
+
37
+ #define na_get_pointer nary_get_pointer
38
+ char* nary_get_pointer(VALUE);
39
+ #define na_get_pointer_for_write nary_get_pointer_for_write
40
+ char* nary_get_pointer_for_write(VALUE);
41
+ #define na_get_pointer_for_read nary_get_pointer_for_read
42
+ char* nary_get_pointer_for_read(VALUE);
43
+ #define na_get_pointer_for_read_write nary_get_pointer_for_read_write
44
+ char* nary_get_pointer_for_read_write(VALUE);
45
+ #define na_get_offset nary_get_offset
46
+ size_t nary_get_offset(VALUE self);
47
+
48
+ #define na_copy_flags nary_copy_flags
49
+ void nary_copy_flags(VALUE src, VALUE dst);
50
+
51
+ #define na_check_ladder nary_check_ladder
52
+ VALUE nary_check_ladder(VALUE self, int start_dim);
53
+ #define na_check_contiguous nary_check_contiguous
54
+ VALUE nary_check_contiguous(VALUE self);
55
+
56
+ #define na_flatten_dim nary_flatten_dim
57
+ VALUE nary_flatten_dim(VALUE self, int sd);
58
+
59
+ #define na_flatten nary_flatten
60
+ VALUE nary_flatten(VALUE);
61
+
62
+ #define na_copy nary_dup
63
+ VALUE nary_dup(VALUE);
64
+
65
+ #define na_store nary_store
66
+ VALUE nary_store(VALUE self, VALUE src);
67
+
68
+ #define na_upcast numo_na_upcast
69
+ VALUE numo_na_upcast(VALUE type1, VALUE type2);
70
+
71
+ void na_release_lock(VALUE); // currently do nothing
72
+
73
+ // used in reduce methods
74
+ #define na_reduce_dimension nary_reduce_dimension
75
+ VALUE nary_reduce_dimension(int argc, VALUE* argv, int naryc, VALUE* naryv, ndfunc_t* ndf, na_iter_func_t nan_iter);
76
+
77
+ #define na_reduce_options nary_reduce_options
78
+ VALUE nary_reduce_options(VALUE axes, VALUE* opts, int naryc, VALUE* naryv, ndfunc_t* ndf);
79
+
80
+ // ndloop
81
+ VALUE na_ndloop(ndfunc_t* nf, int argc, ...);
82
+ VALUE na_ndloop2(ndfunc_t* nf, VALUE args);
83
+ VALUE na_ndloop3(ndfunc_t* nf, void* ptr, int argc, ...);
84
+ VALUE na_ndloop4(ndfunc_t* nf, void* ptr, VALUE args);
85
+
86
+ VALUE na_ndloop_cast_narray_to_rarray(ndfunc_t* nf, VALUE nary, VALUE fmt);
87
+ VALUE na_ndloop_store_rarray(ndfunc_t* nf, VALUE nary, VALUE rary);
88
+ VALUE na_ndloop_store_rarray2(ndfunc_t* nf, VALUE nary, VALUE rary, VALUE opt);
89
+ VALUE na_ndloop_inspect(VALUE nary, na_text_func_t func, VALUE opt);
90
+ VALUE na_ndloop_with_index(ndfunc_t* nf, int argc, ...);
91
+
92
+ #define na_info_str nary_info_str
93
+ VALUE nary_info_str(VALUE);
94
+
95
+ #define na_test_reduce nary_test_reduce
96
+ bool nary_test_reduce(VALUE reduce, int dim);
97
+
98
+ void nary_step_array_index(VALUE self, size_t ary_size, size_t* plen, ssize_t* pbeg, ssize_t* pstep);
99
+ void nary_step_sequence(VALUE self, size_t* plen, double* pbeg, double* pstep);
100
+ void na_parse_enumerator_step(VALUE enum_obj, VALUE* pstep);
101
+
102
+ // used in aref, aset
103
+ #define na_get_result_dimension nary_get_result_dimension
104
+ int nary_get_result_dimension(VALUE self, int argc, VALUE* argv, ssize_t stride, size_t* pos_idx);
105
+ #define na_aref_main nary_aref_main
106
+ VALUE nary_aref_main(int nidx, VALUE* idx, VALUE self, int keep_dim, int nd);
107
+
108
+ #include "ruby/version.h"
109
+
110
+ #if RUBY_API_VERSION_CODE == 20100 // 2.1.0
111
+ int rb_get_kwargs(VALUE keyword_hash, const ID* table, int required, int optional, VALUE*);
112
+ VALUE rb_extract_keywords(VALUE* orighash);
113
+ #endif
114
+
115
+ #endif /* ifndef INTERN_H */
@@ -0,0 +1,480 @@
1
+ /*
2
+ narray.h
3
+ Ruby/Numo::NArray - Numerical Array class for Ruby
4
+ Copyright (C) 1999-2020 Masahiro TANAKA
5
+ */
6
+ #ifndef NARRAY_H
7
+ #define NARRAY_H
8
+
9
+ #if defined(__cplusplus)
10
+ extern "C" {
11
+ #if 0
12
+ } /* satisfy cc-mode */
13
+ #endif
14
+ #endif
15
+
16
+ #define NARRAY_VERSION "0.9.3"
17
+ #define NARRAY_VERSION_CODE 930
18
+
19
+ #include <math.h>
20
+ #include "numo/compat.h"
21
+ #include "numo/template.h"
22
+ #include "numo/extconf.h"
23
+
24
+ #ifdef HAVE_STDBOOL_H
25
+ #include <stdbool.h>
26
+ #endif
27
+
28
+ #ifdef HAVE_STDINT_H
29
+ #include <stdint.h>
30
+ #endif
31
+
32
+ #ifdef HAVE_SYS_TYPES_H
33
+ #include <sys/types.h>
34
+ #endif
35
+
36
+ #ifndef HAVE_U_INT8_T
37
+ #ifdef HAVE_UINT8_T
38
+ typedef uint8_t u_int8_t;
39
+ #endif
40
+ #endif
41
+
42
+ #ifndef HAVE_U_INT16_T
43
+ #ifdef HAVE_UINT16_T
44
+ typedef uint16_t u_int16_t;
45
+ #endif
46
+ #endif
47
+
48
+ #ifndef HAVE_U_INT32_T
49
+ #ifdef HAVE_UINT32_T
50
+ typedef uint32_t u_int32_t;
51
+ #endif
52
+ #endif
53
+
54
+ #ifndef HAVE_U_INT64_T
55
+ #ifdef HAVE_UINT64_T
56
+ typedef uint64_t u_int64_t;
57
+ #endif
58
+ #endif
59
+
60
+ #define SZF PRI_SIZE_PREFIX // defined in ruby.h
61
+
62
+ #if SIZEOF_LONG == 8
63
+ #define NUM2INT64(x) NUM2LONG(x)
64
+ #define INT642NUM(x) LONG2NUM(x)
65
+ #define NUM2UINT64(x) NUM2ULONG(x)
66
+ #define UINT642NUM(x) ULONG2NUM(x)
67
+ #ifndef PRId64
68
+ #define PRId64 "ld"
69
+ #endif
70
+ #ifndef PRIu64
71
+ #define PRIu64 "lu"
72
+ #endif
73
+ #elif SIZEOF_LONG_LONG == 8
74
+ #define NUM2INT64(x) NUM2LL(x)
75
+ #define INT642NUM(x) LL2NUM(x)
76
+ #define NUM2UINT64(x) NUM2ULL(x)
77
+ #define UINT642NUM(x) ULL2NUM(x)
78
+ #ifndef PRId64
79
+ #define PRId64 "lld"
80
+ #endif
81
+ #ifndef PRIu64
82
+ #define PRIu64 "llu"
83
+ #endif
84
+ #else
85
+ #error---->> numo/narray requires 8-byte integer. <<----
86
+ #endif
87
+
88
+ #if SIZEOF_LONG == 4
89
+ #define NUM2INT32(x) NUM2LONG(x)
90
+ #define INT322NUM(x) LONG2NUM(x)
91
+ #define NUM2UINT32(x) NUM2ULONG(x)
92
+ #define UINT322NUM(x) ULONG2NUM(x)
93
+ #ifndef PRId32
94
+ #define PRId32 "ld"
95
+ #endif
96
+ #ifndef PRIu32
97
+ #define PRIu32 "lu"
98
+ #endif
99
+ #elif SIZEOF_INT == 4
100
+ #define NUM2INT32(x) NUM2INT(x)
101
+ #define NUM2UINT32(x) NUM2UINT(x)
102
+ #if SIZEOF_LONG > 4
103
+ #define INT322NUM(x) INT2FIX(x)
104
+ #define UINT322NUM(x) INT2FIX(x)
105
+ #else
106
+ #define INT322NUM(x) INT2NUM(x)
107
+ #define UINT322NUM(x) UINT2NUM(x)
108
+ #endif
109
+ #ifndef PRId32
110
+ #define PRId32 "d"
111
+ #endif
112
+ #ifndef PRIu32
113
+ #define PRIu32 "u"
114
+ #endif
115
+ #else
116
+ #error---->> numo/narray requires 4-byte integer. <<----
117
+ #endif
118
+
119
+ #ifndef HAVE_TYPE_BOOL
120
+ typedef int bool;
121
+ #endif
122
+ #ifndef FALSE /* in case these macros already exist */
123
+ #define FALSE 0 /* values of bool */
124
+ #endif
125
+ #ifndef TRUE
126
+ #define TRUE 1
127
+ #endif
128
+
129
+ typedef struct {
130
+ float dat[2];
131
+ } scomplex;
132
+ typedef struct {
133
+ double dat[2];
134
+ } dcomplex;
135
+ typedef int fortran_integer;
136
+
137
+ #define REAL(x) ((x).dat[0])
138
+ #define IMAG(x) ((x).dat[1])
139
+
140
+ extern int na_debug_flag;
141
+
142
+ #ifndef NARRAY_C
143
+ extern VALUE numo_cNArray;
144
+ extern VALUE rb_mNumo;
145
+ extern VALUE nary_eCastError;
146
+ extern VALUE nary_eShapeError;
147
+ extern VALUE nary_eOperationError;
148
+ extern VALUE nary_eDimensionError;
149
+ extern VALUE nary_eValueError;
150
+ extern const rb_data_type_t na_data_type;
151
+
152
+ // EXTERN const int na_sizeof[NA_NTYPES+1];
153
+ #endif
154
+
155
+ #define cNArray numo_cNArray
156
+ #define mNumo rb_mNumo
157
+ // #define na_upcast(x,y) numo_na_upcast(x,y)
158
+
159
+ /* global variables within this module */
160
+ extern VALUE numo_cBit;
161
+ extern VALUE numo_cDFloat;
162
+ extern VALUE numo_cSFloat;
163
+ extern VALUE numo_cDComplex;
164
+ extern VALUE numo_cSComplex;
165
+ extern VALUE numo_cInt64;
166
+ extern VALUE numo_cInt32;
167
+ extern VALUE numo_cInt16;
168
+ extern VALUE numo_cInt8;
169
+ extern VALUE numo_cUInt64;
170
+ extern VALUE numo_cUInt32;
171
+ extern VALUE numo_cUInt16;
172
+ extern VALUE numo_cUInt8;
173
+ extern VALUE numo_cRObject;
174
+ #ifndef HAVE_RB_CCOMPLEX
175
+ extern VALUE rb_cComplex;
176
+ #endif
177
+ #ifdef HAVE_RB_ARITHMETIC_SEQUENCE_EXTRACT
178
+ extern VALUE rb_cArithSeq;
179
+ #endif
180
+
181
+ extern VALUE sym_reduce;
182
+ extern VALUE sym_option;
183
+ extern VALUE sym_loop_opt;
184
+ extern VALUE sym_init;
185
+
186
+ #define NARRAY_DATA_T 0x1
187
+ #define NARRAY_VIEW_T 0x2
188
+ #define NARRAY_FILEMAP_T 0x3
189
+
190
+ typedef struct RNArray {
191
+ unsigned char ndim; // # of dimensions
192
+ unsigned char type;
193
+ unsigned char flag[2]; // flags
194
+ unsigned short elmsz; // element size
195
+ size_t size; // # of total elements
196
+ size_t* shape; // # of elements for each dimension
197
+ VALUE reduce;
198
+ } narray_t;
199
+
200
+ typedef struct RNArrayData {
201
+ narray_t base;
202
+ char* ptr;
203
+ bool owned;
204
+ } narray_data_t;
205
+
206
+ typedef union {
207
+ ssize_t stride;
208
+ size_t* index;
209
+ } stridx_t;
210
+
211
+ typedef struct RNArrayView {
212
+ narray_t base;
213
+ VALUE data; // data object
214
+ size_t offset; // offset of start point from data pointer
215
+ // :in units of elm.unit_bits
216
+ // address_unit pointer_unit access_unit data_unit
217
+ // elm.step_unit = elm.bit_size / elm.access_unit
218
+ // elm.step_unit = elm.size_bits / elm.unit_bits
219
+ stridx_t* stridx; // stride or indices of data pointer for each dimension
220
+ } narray_view_t;
221
+
222
+ // filemap is unimplemented
223
+ typedef struct RNArrayFileMap {
224
+ narray_t base;
225
+ char* ptr;
226
+ #ifdef WIN32
227
+ HANDLE hFile;
228
+ HANDLE hMap;
229
+ #else // POSIX mmap
230
+ int prot;
231
+ int flag;
232
+ #endif
233
+ } narray_filemap_t;
234
+
235
+ // this will be revised in future.
236
+ typedef struct {
237
+ unsigned int element_bits;
238
+ unsigned int element_bytes;
239
+ unsigned int element_stride;
240
+ } narray_type_info_t;
241
+
242
+ // from ruby/enumerator.c
243
+ struct enumerator {
244
+ VALUE obj;
245
+ ID meth;
246
+ VALUE args;
247
+ // use only above in this source
248
+ VALUE fib;
249
+ VALUE dst;
250
+ VALUE lookahead;
251
+ VALUE feedvalue;
252
+ VALUE stop_exc;
253
+ VALUE size;
254
+ // incompatible below depending on ruby version
255
+ // VALUE procs; // ruby 2.4
256
+ // rb_enumerator_size_func *size_fn; // ruby 2.1-2.4
257
+ // VALUE (*size_fn)(ANYARGS); // ruby 2.0
258
+ };
259
+
260
+ static inline narray_t* na_get_narray_t(VALUE obj) {
261
+ narray_t* na;
262
+
263
+ Check_TypedStruct(obj, &na_data_type);
264
+ na = (narray_t*)DATA_PTR(obj);
265
+ return na;
266
+ }
267
+
268
+ static inline narray_t* _na_get_narray_t(VALUE obj, unsigned char na_type) {
269
+ narray_t* na;
270
+
271
+ Check_TypedStruct(obj, &na_data_type);
272
+ na = (narray_t*)DATA_PTR(obj);
273
+ if (na->type != na_type) {
274
+ rb_bug("unknown type 0x%x (0x%x given)", na_type, na->type);
275
+ }
276
+ return na;
277
+ }
278
+
279
+ #define na_get_narray_data_t(obj) (narray_data_t*)_na_get_narray_t(obj, NARRAY_DATA_T)
280
+ #define na_get_narray_view_t(obj) (narray_view_t*)_na_get_narray_t(obj, NARRAY_VIEW_T)
281
+ #define na_get_narray_filemap_t(obj) (narray_filemap_t*)_na_get_narray_t(obj, NARRAY_FILEMAP_T)
282
+
283
+ #define GetNArray(obj, var) TypedData_Get_Struct(obj, narray_t, &na_data_type, var)
284
+ #define GetNArrayView(obj, var) TypedData_Get_Struct(obj, narray_view_t, &na_data_type, var)
285
+ #define GetNArrayData(obj, var) TypedData_Get_Struct(obj, narray_data_t, &na_data_type, var)
286
+
287
+ #define SDX_IS_STRIDE(x) ((x).stride & 0x1)
288
+ #define SDX_IS_INDEX(x) (!SDX_IS_STRIDE(x))
289
+ #define SDX_GET_STRIDE(x) ((x).stride >> 1)
290
+ #define SDX_GET_INDEX(x) ((x).index)
291
+
292
+ #define SDX_SET_STRIDE(x, s) ((x).stride = ((s) << 1) | 0x1)
293
+ #define SDX_SET_INDEX(x, idx) ((x).index = idx)
294
+
295
+ #define RNARRAY(val) ((narray_t*)DATA_PTR(val))
296
+ #define RNARRAY_DATA(val) ((narray_data_t*)DATA_PTR(val))
297
+ #define RNARRAY_VIEW(val) ((narray_view_t*)DATA_PTR(val))
298
+ #define RNARRAY_FILEMAP(val) ((narray_filemap_t*)DATA_PTR(val))
299
+
300
+ #ifdef HAVE_RTYPEDDATA_GET_DATA
301
+ #define RENUMERATOR_PTR(ptr) ((struct enumerator*)RTYPEDDATA_GET_DATA(ptr))
302
+ #else
303
+ #define RENUMERATOR_PTR(ptr) ((struct enumerator*)DATA_PTR(ptr))
304
+ #endif
305
+
306
+ #define RNARRAY_NDIM(val) (RNARRAY(val)->ndim)
307
+ #define RNARRAY_TYPE(val) (RNARRAY(val)->type)
308
+ #define RNARRAY_FLAG(val) (RNARRAY(val)->flag)
309
+ #define RNARRAY_SIZE(val) (RNARRAY(val)->size)
310
+ #define RNARRAY_SHAPE(val) (RNARRAY(val)->shape)
311
+ #define RNARRAY_REDUCE(val) (RNARRAY(val)->reduce)
312
+
313
+ #define RNARRAY_DATA_PTR(val) (RNARRAY_DATA(val)->ptr)
314
+ #define RNARRAY_VIEW_DATA(val) (RNARRAY_VIEW(val)->data)
315
+ #define RNARRAY_VIEW_OFFSET(val) (RNARRAY_VIEW(val)->offset)
316
+ #define RNARRAY_VIEW_STRIDX(val) (RNARRAY_VIEW(val)->stridx)
317
+
318
+ #define NA_NDIM(na) (((narray_t*)na)->ndim)
319
+ #define NA_TYPE(na) (((narray_t*)na)->type)
320
+ #define NA_SIZE(na) (((narray_t*)na)->size)
321
+ #define NA_SHAPE(na) (((narray_t*)na)->shape)
322
+ #define NA_REDUCE(na) (((narray_t*)na)->reduce)
323
+
324
+ #define NA_FLAG(obj) (na_get_narray_t(obj)->flag)
325
+ #define NA_FLAG0(obj) (NA_FLAG(obj)[0])
326
+ #define NA_FLAG1(obj) (NA_FLAG(obj)[1])
327
+
328
+ #define NA_DATA(na) ((narray_data_t*)(na))
329
+ #define NA_VIEW(na) ((narray_view_t*)(na))
330
+ #define NA_DATA_PTR(na) (NA_DATA(na)->ptr)
331
+ #define NA_DATA_OWNED(na) (NA_DATA(na)->owned)
332
+ #define NA_VIEW_DATA(na) (NA_VIEW(na)->data)
333
+ #define NA_VIEW_OFFSET(na) (NA_VIEW(na)->offset)
334
+ #define NA_VIEW_STRIDX(na) (NA_VIEW(na)->stridx)
335
+
336
+ #define NA_IS_INDEX_AT(na, i) (SDX_IS_INDEX(NA_VIEW_STRIDX(na)[i]))
337
+ #define NA_IS_STRIDE_AT(na, i) (SDX_IS_STRIDE(NA_VIEW_STRIDX(na)[i]))
338
+ #define NA_INDEX_AT(na, i) (SDX_GET_INDEX(NA_VIEW_STRIDX(na)[i]))
339
+ #define NA_STRIDE_AT(na, i) (SDX_GET_STRIDE(NA_VIEW_STRIDX(na)[i]))
340
+
341
+ #define NA_FILEMAP_PTR(na) (((narray_filemap_t*)na)->ptr)
342
+
343
+ #define NA_FL0_TEST(x, f) (NA_FLAG0(x) & (f))
344
+ #define NA_FL1_TEST(x, f) (NA_FLAG1(x) & (f))
345
+
346
+ #define NA_FL0_SET(x, f) \
347
+ do { \
348
+ NA_FLAG0(x) |= (f); \
349
+ } while (0)
350
+ #define NA_FL1_SET(x, f) \
351
+ do { \
352
+ NA_FLAG1(x) |= (f); \
353
+ } while (0)
354
+
355
+ #define NA_FL0_UNSET(x, f) \
356
+ do { \
357
+ NA_FLAG0(x) &= ~(f); \
358
+ } while (0)
359
+ #define NA_FL1_UNSET(x, f) \
360
+ do { \
361
+ NA_FLAG1(x) &= ~(f); \
362
+ } while (0)
363
+
364
+ #define NA_FL0_REVERSE(x, f) \
365
+ do { \
366
+ NA_FLAG0(x) ^= (f); \
367
+ } while (0)
368
+ #define NA_FL1_REVERSE(x, f) \
369
+ do { \
370
+ NA_FLAG1(x) ^= (f); \
371
+ } while (0)
372
+
373
+ /* FLAGS
374
+ - row-major / column-major
375
+ - Overwrite or not
376
+ - byteswapp
377
+ - Extensible?
378
+ - matrix or not
379
+ */
380
+
381
+ #define NA_FL0_BIG_ENDIAN (0x1 << 0)
382
+ #define NA_FL0_COLUMN_MAJOR (0x1 << 1)
383
+ #define NA_FL1_LOCK (0x1 << 0)
384
+ #define NA_FL1_INPLACE (0x1 << 1)
385
+
386
+ #define TEST_COLUMN_MAJOR(x) NA_FL0_TEST(x, NA_FL0_COLUMN_MAJOR)
387
+ #define SET_COLUMN_MAJOR(x) NA_FL0_SET(x, NA_FL0_COLUMN_MAJOR)
388
+ #define UNSET_COLUMN_MAJOR(x) NA_FL0_UNSET(x, NA_FL0_COLUMN_MAJOR)
389
+
390
+ #define TEST_ROW_MAJOR(x) (!TEST_COLUMN_MAJOR(x))
391
+ #define SET_ROW_MAJOR(x) UNSET_COLUMN_MAJOR(x)
392
+ #define UNSET_ROW_MAJOR(x) SET_COLUMN_MAJOR(x)
393
+
394
+ #define TEST_BIG_ENDIAN(x) NA_FL0_TEST(x, NA_FL0_BIG_ENDIAN)
395
+ #define SET_BIG_ENDIAN(x) NA_FL0_SET(x, NA_FL0_BIG_ENDIAN)
396
+ #define UNSET_BIG_ENDIAN(x) NA_FL0_UNSET(x, NA_FL0_BIG_ENDIAN)
397
+
398
+ #define TEST_LITTLE_ENDIAN(x) (!TEST_BIG_ENDIAN(x))
399
+ #define SET_LITTLE_ENDIAN(x) UNSET_BIG_ENDIAN(x)
400
+ #define UNSET_LITTLE_ENDIAN(x) SET_BIG_ENDIAN(x)
401
+
402
+ #define REVERSE_ENDIAN(x) NA_FL0_REVERSE((x), NA_FL0_BIG_ENDIAN)
403
+
404
+ #define TEST_LOCK(x) NA_FL1_TEST(x, NA_FL1_LOCK)
405
+ #define SET_LOCK(x) NA_FL1_SET(x, NA_FL1_LOCK)
406
+ #define UNSET_LOCK(x) NA_FL1_UNSET(x, NA_FL1_LOCK)
407
+
408
+ #define TEST_INPLACE(x) NA_FL1_TEST(x, NA_FL1_INPLACE)
409
+ #define SET_INPLACE(x) NA_FL1_SET(x, NA_FL1_INPLACE)
410
+ #define UNSET_INPLACE(x) NA_FL1_UNSET(x, NA_FL1_INPLACE)
411
+
412
+ #ifdef DYNAMIC_ENDIAN
413
+ // not supported
414
+ #else
415
+ #ifdef WORDS_BIGENDIAN
416
+ #define TEST_HOST_ORDER(x) TEST_BIG_ENDIAN(x)
417
+ #define SET_HOST_ORDER(x) SET_BIG_ENDIAN(x)
418
+ #define UNSET_HOST_ORDER(x) UNSET_BIG_ENDIAN(x)
419
+ #define TEST_BYTE_SWAPPED(x) TEST_LITTLE_ENDIAN(x)
420
+ #define SET_BYTE_SWAPPED(x) SET_LITTLE_ENDIAN(x)
421
+ #define UNSET_BYTE_SWAPPED(x) UNSET_LITTLE_ENDIAN(x)
422
+ #define NA_FL0_INIT NA_FL0_BIG_ENDIAN
423
+ #else // LITTLE ENDIAN
424
+ #define TEST_HOST_ORDER(x) TEST_LITTLE_ENDIAN(x)
425
+ #define SET_HOST_ORDER(x) SET_LITTLE_ENDIAN(x)
426
+ #define UNSET_HOST_ORDER(x) UNSET_LITTLE_ENDIAN(x)
427
+ #define TEST_BYTE_SWAPPED(x) TEST_BIG_ENDIAN(x)
428
+ #define SET_BYTE_SWAPPED(x) SET_BIG_ENDIAN(x)
429
+ #define UNSET_BYTE_SWAPPED(x) UNSET_BIG_ENDIAN(x)
430
+ #define NA_FL0_INIT 0
431
+ #endif
432
+ #endif
433
+ #define NA_FL1_INIT 0
434
+
435
+ #define IsNArray(obj) (rb_obj_is_kind_of(obj, cNArray) == Qtrue)
436
+
437
+ #define DEBUG_PRINT(v) puts(StringValueCStr(rb_funcall(v, rb_intern("inspect"), 0)))
438
+
439
+ #define NA_IsNArray(obj) (rb_obj_is_kind_of(obj, cNArray) == Qtrue)
440
+ #define NA_IsArray(obj) (TYPE(obj) == T_ARRAY || rb_obj_is_kind_of(obj, cNArray) == Qtrue)
441
+
442
+ #define NUM2REAL(v) NUM2DBL(rb_funcall((v), na_id_real, 0))
443
+ #define NUM2IMAG(v) NUM2DBL(rb_funcall((v), na_id_imag, 0))
444
+
445
+ #define NA_MAX_DIMENSION (int)(sizeof(VALUE) * 8 - 2)
446
+ #define NA_MAX_ELMSZ 65535
447
+
448
+ typedef unsigned int BIT_DIGIT;
449
+ // #define BYTE_BIT_DIGIT sizeof(BIT_DIGIT)
450
+ #define NB (sizeof(BIT_DIGIT) * 8)
451
+ #define BALL (~(BIT_DIGIT)0)
452
+ #define SLB(n) (((n) == NB) ? ~(BIT_DIGIT)0 : (~(~(BIT_DIGIT)0 << (n))))
453
+
454
+ #define ELEMENT_BIT_SIZE "ELEMENT_BIT_SIZE"
455
+ #define ELEMENT_BYTE_SIZE "ELEMENT_BYTE_SIZE"
456
+ #define CONTIGUOUS_STRIDE "CONTIGUOUS_STRIDE"
457
+
458
+ #ifdef RUBY_INTEGER_UNIFICATION
459
+ #define IS_INTEGER_CLASS(c) ((c) == rb_cInteger)
460
+ #else
461
+ #define IS_INTEGER_CLASS(c) ((c) == rb_cFixnum || (c) == rb_cBignum)
462
+ #endif
463
+
464
+ #include "numo/ndloop.h"
465
+ #include "numo/intern.h"
466
+
467
+ // for Ractor support code
468
+ #ifndef HAVE_RB_EXT_RACTOR_SAFE
469
+ #undef RUBY_TYPED_FROZEN_SHAREABLE
470
+ #define RUBY_TYPED_FROZEN_SHAREABLE 0
471
+ #endif
472
+
473
+ #if defined(__cplusplus)
474
+ #if 0
475
+ { /* satisfy cc-mode */
476
+ #endif
477
+ } /* extern "C" { */
478
+ #endif
479
+
480
+ #endif /* ifndef NARRAY_H */
@@ -0,0 +1,93 @@
1
+ /*
2
+ ndloop.h
3
+ Ruby/Numo::NArray - Numerical Array class for Ruby
4
+ Copyright (C) 1999-2020 Masahiro TANAKA
5
+ */
6
+ #ifndef NDLOOP_H
7
+ #define NDLOOP_H
8
+
9
+ typedef struct NA_LOOP_ITER {
10
+ ssize_t pos; // - required for each dimension.
11
+ ssize_t step;
12
+ size_t* idx;
13
+ } na_loop_iter_t;
14
+
15
+ typedef struct NA_LOOP_ARGS {
16
+ VALUE value;
17
+ ssize_t elmsz;
18
+ char* ptr;
19
+ // char *buf_ptr; //
20
+ int ndim; // required for each argument.
21
+ // ssize_t pos; - not required here.
22
+ size_t* shape;
23
+ na_loop_iter_t* iter; // moved from na_loop_t
24
+ } na_loop_args_t;
25
+
26
+ // pass this structure to user iterator
27
+ typedef struct NA_LOOP {
28
+ int narg;
29
+ int ndim; // n of user dimension - required for each iterator.
30
+ size_t* n; // n of elements for each dim (=shape)
31
+ na_loop_args_t* args; // for each arg
32
+ VALUE option;
33
+ void* opt_ptr;
34
+ VALUE err_type;
35
+ } na_loop_t;
36
+
37
+ // ------------------ ndfunc -------------------------------------------
38
+
39
+ #define NDF_HAS_LOOP (1 << 0) // x[i]
40
+ #define NDF_STRIDE_LOOP (1 << 1) // *(x+stride*i)
41
+ #define NDF_INDEX_LOOP (1 << 2) // *(x+idx[i])
42
+ #define NDF_KEEP_DIM (1 << 3)
43
+ #define NDF_INPLACE (1 << 4)
44
+ #define NDF_ACCEPT_BYTESWAP (1 << 5)
45
+
46
+ #define NDF_FLAT_REDUCE (1 << 6)
47
+ #define NDF_EXTRACT (1 << 7)
48
+ #define NDF_CUM (1 << 8)
49
+
50
+ #define FULL_LOOP (NDF_HAS_LOOP | NDF_STRIDE_LOOP | NDF_INDEX_LOOP | NDF_INPLACE)
51
+ #define FULL_LOOP_NIP (NDF_HAS_LOOP | NDF_STRIDE_LOOP | NDF_INDEX_LOOP)
52
+ #define STRIDE_LOOP (NDF_HAS_LOOP | NDF_STRIDE_LOOP | NDF_INPLACE)
53
+ #define STRIDE_LOOP_NIP (NDF_HAS_LOOP | NDF_STRIDE_LOOP)
54
+ #define NO_LOOP 0
55
+
56
+ #define OVERWRITE Qtrue // used for CASTABLE(t)
57
+
58
+ #define NDF_TEST(nf, fl) ((nf)->flag & (fl))
59
+ #define NDF_SET(nf, fl) \
60
+ { (nf)->flag |= (fl); }
61
+
62
+ #define NDF_ARG_READ_ONLY 1
63
+ #define NDF_ARG_WRITE_ONLY 2
64
+ #define NDF_ARG_READ_WRITE 3
65
+
66
+ // type of user function
67
+ typedef void(*na_iter_func_t) _((na_loop_t* const));
68
+ typedef VALUE(*na_text_func_t) _((char* ptr, size_t pos, VALUE opt));
69
+ // typedef void (*) void (*loop_func)(ndfunc_t*, na_md_loop_t*))
70
+
71
+ typedef struct NDF_ARG_IN {
72
+ VALUE type; // argument types
73
+ int dim; // # of dimension of argument handled by user function
74
+ // if dim==-1, reduce dimension
75
+ } ndfunc_arg_in_t;
76
+
77
+ typedef struct NDF_ARG_OUT {
78
+ VALUE type; // argument types
79
+ int dim; // # of dimension of argument handled by user function
80
+ size_t* shape;
81
+ } ndfunc_arg_out_t;
82
+
83
+ // spec of user function
84
+ typedef struct NDFUNCTION {
85
+ na_iter_func_t func; // user function
86
+ unsigned int flag; // what kind of loop user function supports
87
+ int nin; // # of arguments
88
+ int nout; // # of results
89
+ ndfunc_arg_in_t* ain; // spec of input arguments
90
+ ndfunc_arg_out_t* aout; // spec of output result
91
+ } ndfunc_t;
92
+
93
+ #endif /* NDLOOP_H */