ruby-dnn 0.1.0

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 (127) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/.travis.yml +5 -0
  4. data/CODE_OF_CONDUCT.md +74 -0
  5. data/Gemfile +6 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +42 -0
  8. data/Rakefile +10 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +8 -0
  11. data/lib/dnn.rb +14 -0
  12. data/lib/dnn/core/activations.rb +116 -0
  13. data/lib/dnn/core/error.rb +13 -0
  14. data/lib/dnn/core/initializers.rb +46 -0
  15. data/lib/dnn/core/layers.rb +366 -0
  16. data/lib/dnn/core/model.rb +158 -0
  17. data/lib/dnn/core/optimizers.rb +113 -0
  18. data/lib/dnn/core/util.rb +24 -0
  19. data/lib/dnn/core/version.rb +3 -0
  20. data/lib/dnn/ext/cifar10/Makefile +263 -0
  21. data/lib/dnn/ext/cifar10/cifar10_ext.c +52 -0
  22. data/lib/dnn/ext/cifar10/cifar10_ext.o +0 -0
  23. data/lib/dnn/ext/cifar10/cifar10_ext.so +0 -0
  24. data/lib/dnn/ext/cifar10/extconf.rb +3 -0
  25. data/lib/dnn/ext/cifar10/numo/compat.h +23 -0
  26. data/lib/dnn/ext/cifar10/numo/extconf.h +13 -0
  27. data/lib/dnn/ext/cifar10/numo/intern.h +117 -0
  28. data/lib/dnn/ext/cifar10/numo/narray.h +430 -0
  29. data/lib/dnn/ext/cifar10/numo/ndloop.h +94 -0
  30. data/lib/dnn/ext/cifar10/numo/template.h +149 -0
  31. data/lib/dnn/ext/cifar10/numo/types/bit.h +33 -0
  32. data/lib/dnn/ext/cifar10/numo/types/complex.h +409 -0
  33. data/lib/dnn/ext/cifar10/numo/types/complex_macro.h +377 -0
  34. data/lib/dnn/ext/cifar10/numo/types/dcomplex.h +44 -0
  35. data/lib/dnn/ext/cifar10/numo/types/dfloat.h +42 -0
  36. data/lib/dnn/ext/cifar10/numo/types/float_def.h +34 -0
  37. data/lib/dnn/ext/cifar10/numo/types/float_macro.h +186 -0
  38. data/lib/dnn/ext/cifar10/numo/types/int16.h +24 -0
  39. data/lib/dnn/ext/cifar10/numo/types/int32.h +24 -0
  40. data/lib/dnn/ext/cifar10/numo/types/int64.h +24 -0
  41. data/lib/dnn/ext/cifar10/numo/types/int8.h +24 -0
  42. data/lib/dnn/ext/cifar10/numo/types/int_macro.h +41 -0
  43. data/lib/dnn/ext/cifar10/numo/types/real_accum.h +486 -0
  44. data/lib/dnn/ext/cifar10/numo/types/robj_macro.h +75 -0
  45. data/lib/dnn/ext/cifar10/numo/types/robject.h +27 -0
  46. data/lib/dnn/ext/cifar10/numo/types/scomplex.h +44 -0
  47. data/lib/dnn/ext/cifar10/numo/types/sfloat.h +43 -0
  48. data/lib/dnn/ext/cifar10/numo/types/uint16.h +21 -0
  49. data/lib/dnn/ext/cifar10/numo/types/uint32.h +21 -0
  50. data/lib/dnn/ext/cifar10/numo/types/uint64.h +21 -0
  51. data/lib/dnn/ext/cifar10/numo/types/uint8.h +21 -0
  52. data/lib/dnn/ext/cifar10/numo/types/uint_macro.h +32 -0
  53. data/lib/dnn/ext/cifar10/numo/types/xint_macro.h +189 -0
  54. data/lib/dnn/ext/image_io/Makefile +263 -0
  55. data/lib/dnn/ext/image_io/extconf.rb +3 -0
  56. data/lib/dnn/ext/image_io/image_io_ext.c +89 -0
  57. data/lib/dnn/ext/image_io/image_io_ext.so +0 -0
  58. data/lib/dnn/ext/image_io/numo/compat.h +23 -0
  59. data/lib/dnn/ext/image_io/numo/extconf.h +13 -0
  60. data/lib/dnn/ext/image_io/numo/intern.h +117 -0
  61. data/lib/dnn/ext/image_io/numo/narray.h +430 -0
  62. data/lib/dnn/ext/image_io/numo/ndloop.h +94 -0
  63. data/lib/dnn/ext/image_io/numo/template.h +149 -0
  64. data/lib/dnn/ext/image_io/numo/types/bit.h +33 -0
  65. data/lib/dnn/ext/image_io/numo/types/complex.h +409 -0
  66. data/lib/dnn/ext/image_io/numo/types/complex_macro.h +377 -0
  67. data/lib/dnn/ext/image_io/numo/types/dcomplex.h +44 -0
  68. data/lib/dnn/ext/image_io/numo/types/dfloat.h +42 -0
  69. data/lib/dnn/ext/image_io/numo/types/float_def.h +34 -0
  70. data/lib/dnn/ext/image_io/numo/types/float_macro.h +186 -0
  71. data/lib/dnn/ext/image_io/numo/types/int16.h +24 -0
  72. data/lib/dnn/ext/image_io/numo/types/int32.h +24 -0
  73. data/lib/dnn/ext/image_io/numo/types/int64.h +24 -0
  74. data/lib/dnn/ext/image_io/numo/types/int8.h +24 -0
  75. data/lib/dnn/ext/image_io/numo/types/int_macro.h +41 -0
  76. data/lib/dnn/ext/image_io/numo/types/real_accum.h +486 -0
  77. data/lib/dnn/ext/image_io/numo/types/robj_macro.h +75 -0
  78. data/lib/dnn/ext/image_io/numo/types/robject.h +27 -0
  79. data/lib/dnn/ext/image_io/numo/types/scomplex.h +44 -0
  80. data/lib/dnn/ext/image_io/numo/types/sfloat.h +43 -0
  81. data/lib/dnn/ext/image_io/numo/types/uint16.h +21 -0
  82. data/lib/dnn/ext/image_io/numo/types/uint32.h +21 -0
  83. data/lib/dnn/ext/image_io/numo/types/uint64.h +21 -0
  84. data/lib/dnn/ext/image_io/numo/types/uint8.h +21 -0
  85. data/lib/dnn/ext/image_io/numo/types/uint_macro.h +32 -0
  86. data/lib/dnn/ext/image_io/numo/types/xint_macro.h +189 -0
  87. data/lib/dnn/ext/image_io/stb_image.h +7462 -0
  88. data/lib/dnn/ext/image_io/stb_image_write.h +1568 -0
  89. data/lib/dnn/ext/mnist/Makefile +263 -0
  90. data/lib/dnn/ext/mnist/extconf.rb +3 -0
  91. data/lib/dnn/ext/mnist/mnist_ext.c +49 -0
  92. data/lib/dnn/ext/mnist/mnist_ext.o +0 -0
  93. data/lib/dnn/ext/mnist/mnist_ext.so +0 -0
  94. data/lib/dnn/ext/mnist/numo/compat.h +23 -0
  95. data/lib/dnn/ext/mnist/numo/extconf.h +13 -0
  96. data/lib/dnn/ext/mnist/numo/intern.h +117 -0
  97. data/lib/dnn/ext/mnist/numo/narray.h +430 -0
  98. data/lib/dnn/ext/mnist/numo/ndloop.h +94 -0
  99. data/lib/dnn/ext/mnist/numo/template.h +149 -0
  100. data/lib/dnn/ext/mnist/numo/types/bit.h +33 -0
  101. data/lib/dnn/ext/mnist/numo/types/complex.h +409 -0
  102. data/lib/dnn/ext/mnist/numo/types/complex_macro.h +377 -0
  103. data/lib/dnn/ext/mnist/numo/types/dcomplex.h +44 -0
  104. data/lib/dnn/ext/mnist/numo/types/dfloat.h +42 -0
  105. data/lib/dnn/ext/mnist/numo/types/float_def.h +34 -0
  106. data/lib/dnn/ext/mnist/numo/types/float_macro.h +186 -0
  107. data/lib/dnn/ext/mnist/numo/types/int16.h +24 -0
  108. data/lib/dnn/ext/mnist/numo/types/int32.h +24 -0
  109. data/lib/dnn/ext/mnist/numo/types/int64.h +24 -0
  110. data/lib/dnn/ext/mnist/numo/types/int8.h +24 -0
  111. data/lib/dnn/ext/mnist/numo/types/int_macro.h +41 -0
  112. data/lib/dnn/ext/mnist/numo/types/real_accum.h +486 -0
  113. data/lib/dnn/ext/mnist/numo/types/robj_macro.h +75 -0
  114. data/lib/dnn/ext/mnist/numo/types/robject.h +27 -0
  115. data/lib/dnn/ext/mnist/numo/types/scomplex.h +44 -0
  116. data/lib/dnn/ext/mnist/numo/types/sfloat.h +43 -0
  117. data/lib/dnn/ext/mnist/numo/types/uint16.h +21 -0
  118. data/lib/dnn/ext/mnist/numo/types/uint32.h +21 -0
  119. data/lib/dnn/ext/mnist/numo/types/uint64.h +21 -0
  120. data/lib/dnn/ext/mnist/numo/types/uint8.h +21 -0
  121. data/lib/dnn/ext/mnist/numo/types/uint_macro.h +32 -0
  122. data/lib/dnn/ext/mnist/numo/types/xint_macro.h +189 -0
  123. data/lib/dnn/lib/cifar10.rb +26 -0
  124. data/lib/dnn/lib/image_io.rb +33 -0
  125. data/lib/dnn/lib/mnist.rb +61 -0
  126. data/ruby-dnn.gemspec +41 -0
  127. metadata +225 -0
@@ -0,0 +1,430 @@
1
+ /*
2
+ narray.h
3
+ Numerical Array Extension for Ruby
4
+ (C) Copyright 1999-2017 by 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.1.1"
17
+ #define NARRAY_VERSION_CODE 911
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
+ #endif
85
+
86
+ #if SIZEOF_LONG==4
87
+ # define NUM2INT32(x) NUM2LONG(x)
88
+ # define INT322NUM(x) LONG2NUM(x)
89
+ # define NUM2UINT32(x) NUM2ULONG(x)
90
+ # define UINT322NUM(x) ULONG2NUM(x)
91
+ # ifndef PRId32
92
+ # define PRId32 "ld"
93
+ # endif
94
+ # ifndef PRIu32
95
+ # define PRIu32 "lu"
96
+ # endif
97
+ #elif SIZEOF_INT==4
98
+ # define NUM2INT32(x) NUM2INT(x)
99
+ # define INT322NUM(x) INT2NUM(x)
100
+ # define NUM2UINT32(x) NUM2UINT(x)
101
+ # define UINT322NUM(x) UINT2NUM(x)
102
+ # ifndef PRId32
103
+ # define PRId32 "d"
104
+ # endif
105
+ # ifndef PRIu32
106
+ # define PRIu32 "u"
107
+ # endif
108
+ #endif
109
+
110
+ #ifndef HAVE_TYPE_BOOL
111
+ typedef int bool;
112
+ #endif
113
+ #ifndef FALSE /* in case these macros already exist */
114
+ # define FALSE 0 /* values of bool */
115
+ #endif
116
+ #ifndef TRUE
117
+ # define TRUE 1
118
+ #endif
119
+
120
+ typedef struct { float dat[2]; } scomplex;
121
+ typedef struct { double dat[2]; } dcomplex;
122
+ typedef int fortran_integer;
123
+
124
+ #define REAL(x) ((x).dat[0])
125
+ #define IMAG(x) ((x).dat[1])
126
+
127
+ extern int na_debug_flag;
128
+
129
+ #ifndef NARRAY_C
130
+ extern VALUE numo_cNArray;
131
+ extern VALUE rb_mNumo;
132
+ extern VALUE nary_eCastError;
133
+ extern VALUE nary_eShapeError;
134
+ extern VALUE nary_eOperationError;
135
+ extern VALUE nary_eDimensionError;
136
+ extern VALUE nary_eValueError;
137
+ extern const rb_data_type_t na_data_type;
138
+
139
+ //EXTERN const int na_sizeof[NA_NTYPES+1];
140
+ #endif
141
+
142
+ #define cNArray numo_cNArray
143
+ #define mNumo rb_mNumo
144
+ //#define na_upcast(x,y) numo_na_upcast(x,y)
145
+
146
+ /* global variables within this module */
147
+ extern VALUE numo_cBit;
148
+ extern VALUE numo_cDFloat;
149
+ extern VALUE numo_cSFloat;
150
+ extern VALUE numo_cDComplex;
151
+ extern VALUE numo_cSComplex;
152
+ extern VALUE numo_cInt64;
153
+ extern VALUE numo_cInt32;
154
+ extern VALUE numo_cInt16;
155
+ extern VALUE numo_cInt8;
156
+ extern VALUE numo_cUInt64;
157
+ extern VALUE numo_cUInt32;
158
+ extern VALUE numo_cUInt16;
159
+ extern VALUE numo_cUInt8;
160
+ extern VALUE numo_cRObject;
161
+ extern VALUE na_cStep;
162
+ #ifndef HAVE_RB_CCOMPLEX
163
+ extern VALUE rb_cComplex;
164
+ #endif
165
+
166
+ extern VALUE sym_reduce;
167
+ extern VALUE sym_option;
168
+ extern VALUE sym_loop_opt;
169
+ extern VALUE sym_init;
170
+
171
+ #define NARRAY_DATA_T 0x1
172
+ #define NARRAY_VIEW_T 0x2
173
+ #define NARRAY_FILEMAP_T 0x3
174
+
175
+ typedef struct RNArray {
176
+ unsigned char ndim; // # of dimensions
177
+ unsigned char type;
178
+ unsigned char flag[2]; // flags
179
+ unsigned short elmsz; // element size
180
+ size_t size; // # of total elements
181
+ size_t *shape; // # of elements for each dimension
182
+ VALUE reduce;
183
+ } narray_t;
184
+
185
+
186
+ typedef struct RNArrayData {
187
+ narray_t base;
188
+ char *ptr;
189
+ } narray_data_t;
190
+
191
+
192
+ typedef union {
193
+ ssize_t stride;
194
+ size_t *index;
195
+ } stridx_t;
196
+
197
+ typedef struct RNArrayView {
198
+ narray_t base;
199
+ VALUE data; // data object
200
+ size_t offset; // offset of start point from data pointer
201
+ // :in units of elm.unit_bits
202
+ // address_unit pointer_unit access_unit data_unit
203
+ // elm.step_unit = elm.bit_size / elm.access_unit
204
+ // elm.step_unit = elm.size_bits / elm.unit_bits
205
+ stridx_t *stridx; // stride or indices of data pointer for each dimension
206
+ } narray_view_t;
207
+
208
+
209
+ // filemap is unimplemented
210
+ typedef struct RNArrayFileMap {
211
+ narray_t base;
212
+ char *ptr;
213
+ #ifdef WIN32
214
+ HANDLE hFile;
215
+ HANDLE hMap;
216
+ #else // POSIX mmap
217
+ int prot;
218
+ int flag;
219
+ #endif
220
+ } narray_filemap_t;
221
+
222
+
223
+ // this will be revised in future.
224
+ typedef struct {
225
+ unsigned int element_bits;
226
+ unsigned int element_bytes;
227
+ unsigned int element_stride;
228
+ } narray_type_info_t;
229
+
230
+
231
+ static inline narray_t *
232
+ na_get_narray_t(VALUE obj)
233
+ {
234
+ narray_t *na;
235
+
236
+ Check_TypedStruct(obj,&na_data_type);
237
+ na = (narray_t*)DATA_PTR(obj);
238
+ return na;
239
+ }
240
+
241
+ static inline narray_t *
242
+ _na_get_narray_t(VALUE obj, unsigned char na_type)
243
+ {
244
+ narray_t *na;
245
+
246
+ Check_TypedStruct(obj,&na_data_type);
247
+ na = (narray_t*)DATA_PTR(obj);
248
+ if (na->type != na_type) {
249
+ rb_bug("unknown type 0x%x (0x%x given)", na_type, na->type);
250
+ }
251
+ return na;
252
+ }
253
+
254
+ #define na_get_narray_data_t(obj) (narray_data_t*)_na_get_narray_t(obj,NARRAY_DATA_T)
255
+ #define na_get_narray_view_t(obj) (narray_view_t*)_na_get_narray_t(obj,NARRAY_VIEW_T)
256
+ #define na_get_narray_filemap_t(obj) (narray_filemap_t*)_na_get_narray_t(obj,NARRAY_FILEMAP_T)
257
+
258
+ #define GetNArray(obj,var) TypedData_Get_Struct(obj, narray_t, &na_data_type, var)
259
+ #define GetNArrayView(obj,var) TypedData_Get_Struct(obj, narray_view_t, &na_data_type, var)
260
+ #define GetNArrayData(obj,var) TypedData_Get_Struct(obj, narray_data_t, &na_data_type, var)
261
+
262
+ #define SDX_IS_STRIDE(x) ((x).stride&0x1)
263
+ #define SDX_IS_INDEX(x) (!SDX_IS_STRIDE(x))
264
+ #define SDX_GET_STRIDE(x) ((x).stride>>1)
265
+ #define SDX_GET_INDEX(x) ((x).index)
266
+
267
+ #define SDX_SET_STRIDE(x,s) ((x).stride=((s)<<1)|0x1)
268
+ #define SDX_SET_INDEX(x,idx) ((x).index=idx)
269
+
270
+ #define RNARRAY(val) ((narray_t*)DATA_PTR(val))
271
+ #define RNARRAY_DATA(val) ((narray_data_t*)DATA_PTR(val))
272
+ #define RNARRAY_VIEW(val) ((narray_view_t*)DATA_PTR(val))
273
+ #define RNARRAY_FILEMAP(val) ((narray_filemap_t*)DATA_PTR(val))
274
+
275
+ #define RNARRAY_NDIM(val) (RNARRAY(val)->ndim)
276
+ #define RNARRAY_TYPE(val) (RNARRAY(val)->type)
277
+ #define RNARRAY_FLAG(val) (RNARRAY(val)->flag)
278
+ #define RNARRAY_SIZE(val) (RNARRAY(val)->size)
279
+ #define RNARRAY_SHAPE(val) (RNARRAY(val)->shape)
280
+ #define RNARRAY_REDUCE(val) (RNARRAY(val)->reduce)
281
+
282
+ #define RNARRAY_DATA_PTR(val) (RNARRAY_DATA(val)->ptr)
283
+ #define RNARRAY_VIEW_DATA(val) (RNARRAY_VIEW(val)->data)
284
+ #define RNARRAY_VIEW_OFFSET(val) (RNARRAY_VIEW(val)->offset)
285
+ #define RNARRAY_VIEW_STRIDX(val) (RNARRAY_VIEW(val)->stridx)
286
+
287
+ #define NA_NDIM(na) (((narray_t*)na)->ndim)
288
+ #define NA_TYPE(na) (((narray_t*)na)->type)
289
+ #define NA_SIZE(na) (((narray_t*)na)->size)
290
+ #define NA_SHAPE(na) (((narray_t*)na)->shape)
291
+ #define NA_REDUCE(na) (((narray_t*)na)->reduce)
292
+
293
+ #define NA_FLAG(obj) (na_get_narray_t(obj)->flag)
294
+ #define NA_FLAG0(obj) (NA_FLAG(obj)[0])
295
+ #define NA_FLAG1(obj) (NA_FLAG(obj)[1])
296
+
297
+ #define NA_DATA(na) ((narray_data_t*)(na))
298
+ #define NA_VIEW(na) ((narray_view_t*)(na))
299
+ #define NA_DATA_PTR(na) (NA_DATA(na)->ptr)
300
+ #define NA_VIEW_DATA(na) (NA_VIEW(na)->data)
301
+ #define NA_VIEW_OFFSET(na) (NA_VIEW(na)->offset)
302
+ #define NA_VIEW_STRIDX(na) (NA_VIEW(na)->stridx)
303
+
304
+ #define NA_IS_INDEX_AT(na,i) (SDX_IS_INDEX(NA_VIEW_STRIDX(na)[i]))
305
+ #define NA_IS_STRIDE_AT(na,i) (SDX_IS_STRIDE(NA_VIEW_STRIDX(na)[i]))
306
+ #define NA_INDEX_AT(na,i) (SDX_GET_INDEX(NA_VIEW_STRIDX(na)[i]))
307
+ #define NA_STRIDE_AT(na,i) (SDX_GET_STRIDE(NA_VIEW_STRIDX(na)[i]))
308
+
309
+ #define NA_FILEMAP_PTR(na) (((narray_filemap_t*)na)->ptr)
310
+
311
+
312
+ #define NA_FL0_TEST(x,f) (NA_FLAG0(x)&(f))
313
+ #define NA_FL1_TEST(x,f) (NA_FLAG1(x)&(f))
314
+
315
+ #define NA_FL0_SET(x,f) do {NA_FLAG0(x) |= (f);} while(0)
316
+ #define NA_FL1_SET(x,f) do {NA_FLAG1(x) |= (f);} while(0)
317
+
318
+ #define NA_FL0_UNSET(x,f) do {NA_FLAG0(x) &= ~(f);} while(0)
319
+ #define NA_FL1_UNSET(x,f) do {NA_FLAG1(x) &= ~(f);} while(0)
320
+
321
+ #define NA_FL0_REVERSE(x,f) do {NA_FLAG0(x) ^= (f);} while(0)
322
+ #define NA_FL1_REVERSE(x,f) do {NA_FLAG1(x) ^= (f);} while(0)
323
+
324
+
325
+ /* FLAGS
326
+ - row-major / column-major
327
+ - Overwrite or not
328
+ - byteswapp
329
+ - Extensible?
330
+ - matrix or not
331
+ */
332
+
333
+ #define NA_FL0_BIG_ENDIAN (0x1<<0)
334
+ #define NA_FL0_COLUMN_MAJOR (0x1<<1)
335
+ #define NA_FL1_LOCK (0x1<<0)
336
+ #define NA_FL1_INPLACE (0x1<<1)
337
+
338
+ #define TEST_COLUMN_MAJOR(x) NA_FL0_TEST(x,NA_FL0_COLUMN_MAJOR)
339
+ #define SET_COLUMN_MAJOR(x) NA_FL0_SET(x,NA_FL0_COLUMN_MAJOR)
340
+ #define UNSET_COLUMN_MAJOR(x) NA_FL0_UNSET(x,NA_FL0_COLUMN_MAJOR)
341
+
342
+ #define TEST_ROW_MAJOR(x) (!TEST_COLUMN_MAJOR(x))
343
+ #define SET_ROW_MAJOR(x) UNSET_COLUMN_MAJOR(x)
344
+ #define UNSET_ROW_MAJOR(x) SET_COLUMN_MAJOR(x)
345
+
346
+ #define TEST_BIG_ENDIAN(x) NA_FL0_TEST(x,NA_FL0_BIG_ENDIAN)
347
+ #define SET_BIG_ENDIAN(x) NA_FL0_SET(x,NA_FL0_BIG_ENDIAN)
348
+ #define UNSET_BIG_ENDIAN(x) NA_FL0_UNSET(x,NA_FL0_BIG_ENDIAN)
349
+
350
+ #define TEST_LITTLE_ENDIAN(x) (!TEST_BIG_ENDIAN(x))
351
+ #define SET_LITTLE_ENDIAN(x) UNSET_BIG_ENDIAN(x)
352
+ #define UNSET_LITTLE_ENDIAN(x) SET_BIG_ENDIAN(x)
353
+
354
+ #define REVERSE_ENDIAN(x) NA_FL0_REVERSE((x),NA_FL0_BIG_ENDIAN)
355
+
356
+ #define TEST_LOCK(x) NA_FL1_TEST(x,NA_FL1_LOCK)
357
+ #define SET_LOCK(x) NA_FL1_SET(x,NA_FL1_LOCK)
358
+ #define UNSET_LOCK(x) NA_FL1_UNSET(x,NA_FL1_LOCK)
359
+
360
+ #define TEST_INPLACE(x) NA_FL1_TEST(x,NA_FL1_INPLACE)
361
+ #define SET_INPLACE(x) NA_FL1_SET(x,NA_FL1_INPLACE)
362
+ #define UNSET_INPLACE(x) NA_FL1_UNSET(x,NA_FL1_INPLACE)
363
+
364
+ #ifdef DYNAMIC_ENDIAN
365
+ // not supported
366
+ #else
367
+ #ifdef WORDS_BIGENDIAN
368
+ #define TEST_HOST_ORDER(x) TEST_BIG_ENDIAN(x)
369
+ #define SET_HOST_ORDER(x) SET_BIG_ENDIAN(x)
370
+ #define UNSET_HOST_ORDER(x) UNSET_BIG_ENDIAN(x)
371
+ #define TEST_BYTE_SWAPPED(x) TEST_LITTLE_ENDIAN(x)
372
+ #define SET_BYTE_SWAPPED(x) SET_LITTLE_ENDIAN(x)
373
+ #define UNSET_BYTE_SWAPPED(x) UNSET_LITTLE_ENDIAN(x)
374
+ #define NA_FL0_INIT NA_FL0_BIG_ENDIAN
375
+ #else // LITTLE ENDIAN
376
+ #define TEST_HOST_ORDER(x) TEST_LITTLE_ENDIAN(x)
377
+ #define SET_HOST_ORDER(x) SET_LITTLE_ENDIAN(x)
378
+ #define UNSET_HOST_ORDER(x) UNSET_LITTLE_ENDIAN(x)
379
+ #define TEST_BYTE_SWAPPED(x) TEST_BIG_ENDIAN(x)
380
+ #define SET_BYTE_SWAPPED(x) SET_BIG_ENDIAN(x)
381
+ #define UNSET_BYTE_SWAPPED(x) UNSET_BIG_ENDIAN(x)
382
+ #define NA_FL0_INIT 0
383
+ #endif
384
+ #endif
385
+ #define NA_FL1_INIT 0
386
+
387
+
388
+ #define IsNArray(obj) (rb_obj_is_kind_of(obj,cNArray)==Qtrue)
389
+
390
+ #define DEBUG_PRINT(v) puts(StringValueCStr(rb_funcall(v,rb_intern("inspect"),0)))
391
+
392
+ #define NA_IsNArray(obj) \
393
+ (rb_obj_is_kind_of(obj,cNArray)==Qtrue)
394
+ #define NA_IsArray(obj) \
395
+ (TYPE(obj)==T_ARRAY || rb_obj_is_kind_of(obj,cNArray)==Qtrue)
396
+
397
+ #define NUM2REAL(v) NUM2DBL( rb_funcall((v),na_id_real,0) )
398
+ #define NUM2IMAG(v) NUM2DBL( rb_funcall((v),na_id_imag,0) )
399
+
400
+ #define NA_MAX_DIMENSION (int)(sizeof(VALUE)*8-2)
401
+ #define NA_MAX_ELMSZ 65535
402
+
403
+ typedef unsigned int BIT_DIGIT;
404
+ //#define BYTE_BIT_DIGIT sizeof(BIT_DIGIT)
405
+ #define NB (sizeof(BIT_DIGIT)*8)
406
+ #define BALL (~(BIT_DIGIT)0)
407
+ #define SLB(n) (((n)==NB)?~(BIT_DIGIT)0:(~(~(BIT_DIGIT)0<<(n))))
408
+
409
+ #define ELEMENT_BIT_SIZE "ELEMENT_BIT_SIZE"
410
+ #define ELEMENT_BYTE_SIZE "ELEMENT_BYTE_SIZE"
411
+ #define CONTIGUOUS_STRIDE "CONTIGUOUS_STRIDE"
412
+
413
+
414
+ #ifdef RUBY_INTEGER_UNIFICATION
415
+ #define IS_INTEGER_CLASS(c) ((c)==rb_cInteger)
416
+ #else
417
+ #define IS_INTEGER_CLASS(c) ((c)==rb_cFixnum||(c)==rb_cBignum)
418
+ #endif
419
+
420
+ #include "numo/ndloop.h"
421
+ #include "numo/intern.h"
422
+
423
+ #if defined(__cplusplus)
424
+ #if 0
425
+ { /* satisfy cc-mode */
426
+ #endif
427
+ } /* extern "C" { */
428
+ #endif
429
+
430
+ #endif /* ifndef NARRAY_H */
@@ -0,0 +1,94 @@
1
+ /*
2
+ ndloop.h
3
+ Numerical Array Extension for Ruby
4
+ (C) Copyright 1999-2017 by 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 dimention - 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
+
38
+ // ------------------ ndfunc -------------------------------------------
39
+
40
+ #define NDF_HAS_LOOP (1<<0) // x[i]
41
+ #define NDF_STRIDE_LOOP (1<<1) // *(x+stride*i)
42
+ #define NDF_INDEX_LOOP (1<<2) // *(x+idx[i])
43
+ #define NDF_KEEP_DIM (1<<3)
44
+ #define NDF_INPLACE (1<<4)
45
+ #define NDF_ACCEPT_BYTESWAP (1<<5)
46
+
47
+ #define NDF_FLAT_REDUCE (1<<6)
48
+ #define NDF_EXTRACT (1<<7)
49
+ #define NDF_CUM (1<<8)
50
+
51
+ #define FULL_LOOP (NDF_HAS_LOOP|NDF_STRIDE_LOOP|NDF_INDEX_LOOP|NDF_INPLACE)
52
+ #define FULL_LOOP_NIP (NDF_HAS_LOOP|NDF_STRIDE_LOOP|NDF_INDEX_LOOP)
53
+ #define STRIDE_LOOP (NDF_HAS_LOOP|NDF_STRIDE_LOOP|NDF_INPLACE)
54
+ #define STRIDE_LOOP_NIP (NDF_HAS_LOOP|NDF_STRIDE_LOOP)
55
+ #define NO_LOOP 0
56
+
57
+ #define OVERWRITE Qtrue // used for CASTABLE(t)
58
+
59
+ #define NDF_TEST(nf,fl) ((nf)->flag & (fl))
60
+ #define NDF_SET(nf,fl) {(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
+
72
+ typedef struct NDF_ARG_IN {
73
+ VALUE type; // argument types
74
+ int dim; // # of dimension of argument handled by user function
75
+ // if dim==-1, reduce dimension
76
+ } ndfunc_arg_in_t;
77
+
78
+ typedef struct NDF_ARG_OUT {
79
+ VALUE type; // argument types
80
+ int dim; // # of dimension of argument handled by user function
81
+ size_t *shape;
82
+ } ndfunc_arg_out_t;
83
+
84
+ // spec of user function
85
+ typedef struct NDFUNCTION {
86
+ na_iter_func_t func; // user function
87
+ unsigned int flag; // what kind of loop user function supports
88
+ int nin; // # of arguments
89
+ int nout; // # of results
90
+ ndfunc_arg_in_t *ain; // spec of input arguments
91
+ ndfunc_arg_out_t *aout; // spec of output result
92
+ } ndfunc_t;
93
+
94
+ #endif /* NDLOOP_H */