carray 1.1.4 → 1.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (231) hide show
  1. checksums.yaml +4 -4
  2. data/COPYING +56 -0
  3. data/GPL +340 -0
  4. data/Gemfile +9 -0
  5. data/Gemfile.lock +33 -0
  6. data/LEGAL +50 -0
  7. data/NOTE +73 -0
  8. data/Rakefile +20 -0
  9. data/TODO +5 -0
  10. data/ca_iter_block.c +242 -0
  11. data/ca_iter_dimension.c +287 -0
  12. data/ca_iter_window.c +202 -0
  13. data/ca_obj_array.c +1189 -0
  14. data/ca_obj_bitarray.c +523 -0
  15. data/ca_obj_bitfield.c +636 -0
  16. data/ca_obj_block.c +885 -0
  17. data/ca_obj_fake.c +405 -0
  18. data/ca_obj_farray.c +482 -0
  19. data/ca_obj_field.c +625 -0
  20. data/ca_obj_grid.c +738 -0
  21. data/ca_obj_mapping.c +614 -0
  22. data/ca_obj_object.c +777 -0
  23. data/ca_obj_reduce.c +299 -0
  24. data/ca_obj_refer.c +627 -0
  25. data/ca_obj_repeat.c +640 -0
  26. data/ca_obj_select.c +558 -0
  27. data/ca_obj_shift.c +952 -0
  28. data/ca_obj_transpose.c +582 -0
  29. data/ca_obj_unbound_repeat.c +557 -0
  30. data/ca_obj_window.c +1023 -0
  31. data/carray.h +1381 -0
  32. data/carray_access.c +1798 -0
  33. data/carray_attribute.c +903 -0
  34. data/carray_call_cfunc.c +1107 -0
  35. data/carray_cast.c +1155 -0
  36. data/carray_cast_func.rb +498 -0
  37. data/carray_class.c +132 -0
  38. data/carray_conversion.c +518 -0
  39. data/carray_copy.c +453 -0
  40. data/carray_core.c +1307 -0
  41. data/carray_element.c +572 -0
  42. data/carray_generate.c +681 -0
  43. data/carray_iterator.c +630 -0
  44. data/carray_loop.c +462 -0
  45. data/carray_mask.c +1174 -0
  46. data/carray_math.rb +834 -0
  47. data/carray_numeric.c +257 -0
  48. data/carray_operator.c +582 -0
  49. data/carray_order.c +1040 -0
  50. data/carray_random.c +529 -0
  51. data/carray_sort_addr.c +261 -0
  52. data/carray_stat.c +2102 -0
  53. data/carray_stat_proc.rb +1990 -0
  54. data/carray_test.c +602 -0
  55. data/carray_undef.c +69 -0
  56. data/carray_utils.c +740 -0
  57. data/ext/calculus/carray_calculus.c +792 -0
  58. data/ext/calculus/carray_interp.c +355 -0
  59. data/ext/calculus/extconf.rb +12 -0
  60. data/ext/calculus/lib/autoload/autoload_math_calculus.rb +2 -0
  61. data/ext/calculus/lib/math/calculus.rb +119 -0
  62. data/ext/calculus/lib/math/interp/adapter_interp1d.rb +31 -0
  63. data/ext/dataframe/API.txt +11 -0
  64. data/ext/dataframe/extconf.rb +3 -0
  65. data/ext/dataframe/lib/carray/autoload/autoload_dataframe_dataframe.rb +14 -0
  66. data/ext/dataframe/lib/carray/dataframe/dataframe.rb +1104 -0
  67. data/ext/dataframe/sample/test_uniq_sort.rb +5 -0
  68. data/ext/fortio/extconf.rb +3 -0
  69. data/ext/fortio/lib/carray/autoload/autoload_fortran_format.rb +5 -0
  70. data/ext/fortio/lib/carray/io/fortran_format.rb +43 -0
  71. data/ext/fortio/lib/fortio.rb +3 -0
  72. data/ext/fortio/lib/fortio/fortran_format.rb +603 -0
  73. data/ext/fortio/lib/fortio/fortran_format.tab.rb +536 -0
  74. data/ext/fortio/lib/fortio/fortran_format.y +215 -0
  75. data/ext/fortio/lib/fortio/fortran_namelist.rb +151 -0
  76. data/ext/fortio/lib/fortio/fortran_namelist.tab.rb +470 -0
  77. data/ext/fortio/lib/fortio/fortran_namelist.y +213 -0
  78. data/ext/fortio/lib/fortio/fortran_sequential.rb +345 -0
  79. data/ext/fortio/ruby_fortio.c +182 -0
  80. data/ext/fortio/test/test_H.rb +5 -0
  81. data/ext/fortio/test/test_T.rb +7 -0
  82. data/ext/fortio/test/test_fortran_format.rb +86 -0
  83. data/ext/fortio/test/test_namelist.rb +25 -0
  84. data/ext/fortio/test/test_sequential.rb +13 -0
  85. data/ext/fortio/test/test_sequential2.rb +13 -0
  86. data/ext/fortio/work/test.rb +10 -0
  87. data/ext/fortio/work/test_e.rb +19 -0
  88. data/ext/fortio/work/test_ep.rb +10 -0
  89. data/ext/fortio/work/test_parse.rb +12 -0
  90. data/ext/imagemap/carray_imagemap.c +495 -0
  91. data/ext/imagemap/doc/call_graph.dot +28 -0
  92. data/ext/imagemap/draw.c +567 -0
  93. data/ext/imagemap/extconf.rb +13 -0
  94. data/ext/imagemap/lib/autoload/autoload_graphics_imagemap.rb +1 -0
  95. data/ext/imagemap/lib/graphics/imagemap.rb +273 -0
  96. data/ext/imagemap/lib/image_map.rb +4 -0
  97. data/ext/imagemap/test/swath_index.rb +83 -0
  98. data/ext/imagemap/test/swath_warp.rb +99 -0
  99. data/ext/imagemap/test/test.rb +23 -0
  100. data/ext/imagemap/test/test_image.rb +42 -0
  101. data/ext/imagemap/test/test_line.rb +14 -0
  102. data/ext/imagemap/test/test_rotate.rb +17 -0
  103. data/ext/imagemap/test/test_triangle.rb +20 -0
  104. data/ext/imagemap/test/test_warp.rb +26 -0
  105. data/ext/mathfunc/carray_mathfunc.c +321 -0
  106. data/ext/mathfunc/extconf.rb +18 -0
  107. data/ext/mathfunc/lib/autoload/autoload_math_mathfunc.rb +1 -0
  108. data/ext/mathfunc/lib/math/mathfunc.rb +15 -0
  109. data/ext/mathfunc/test/test_hypot.rb +5 -0
  110. data/ext/mathfunc/test/test_j0.rb +22 -0
  111. data/ext/mathfunc/test/test_jn.rb +8 -0
  112. data/ext/mathfunc/test/test_sph.rb +9 -0
  113. data/ext/narray/README +22 -0
  114. data/ext/narray/ca_wrap_narray.c +491 -0
  115. data/ext/narray/carray_narray.c +21 -0
  116. data/ext/narray/extconf.rb +57 -0
  117. data/ext/narray/lib/autoload/autoload_math_narray.rb +1 -0
  118. data/ext/narray/lib/autoload/autoload_math_narray_miss.rb +11 -0
  119. data/ext/narray/lib/math/narray.rb +17 -0
  120. data/ext/narray/lib/math/narray_miss.rb +45 -0
  121. data/extconf.rb +3 -25
  122. data/lib/carray.rb +28 -0
  123. data/lib/carray/autoload/autoload_base.rb +23 -0
  124. data/lib/carray/autoload/autoload_graphics_gnuplot.rb +2 -0
  125. data/lib/carray/autoload/autoload_io_csv.rb +14 -0
  126. data/lib/carray/autoload/autoload_io_excel.rb +5 -0
  127. data/lib/carray/autoload/autoload_io_imagemagick.rb +6 -0
  128. data/lib/carray/autoload/autoload_io_pg.rb +6 -0
  129. data/lib/carray/autoload/autoload_io_sqlite3.rb +12 -0
  130. data/lib/carray/autoload/autoload_io_table.rb +1 -0
  131. data/lib/carray/autoload/autoload_math_histogram.rb +5 -0
  132. data/lib/carray/autoload/autoload_math_interp.rb +4 -0
  133. data/lib/carray/autoload/autoload_math_recurrence.rb +6 -0
  134. data/lib/carray/autoload/autoload_object_iterator.rb +1 -0
  135. data/lib/carray/autoload/autoload_object_link.rb +1 -0
  136. data/lib/carray/autoload/autoload_object_pack.rb +2 -0
  137. data/lib/carray/base/autoload.rb +94 -0
  138. data/lib/carray/base/basic.rb +1051 -0
  139. data/lib/carray/base/inspect.rb +252 -0
  140. data/lib/carray/base/iterator.rb +367 -0
  141. data/lib/carray/base/math.rb +403 -0
  142. data/lib/carray/base/obsolete.rb +93 -0
  143. data/lib/carray/base/serialize.rb +260 -0
  144. data/lib/carray/base/struct.rb +634 -0
  145. data/lib/carray/graphics/gnuplot.rb +2116 -0
  146. data/lib/carray/info.rb +112 -0
  147. data/lib/carray/io/csv.rb +560 -0
  148. data/lib/carray/io/excel.rb +26 -0
  149. data/lib/carray/io/imagemagick.rb +231 -0
  150. data/lib/carray/io/pg.rb +101 -0
  151. data/lib/carray/io/sqlite3.rb +202 -0
  152. data/lib/carray/io/table.rb +77 -0
  153. data/lib/carray/math/histogram.rb +179 -0
  154. data/lib/carray/math/interp.rb +57 -0
  155. data/lib/carray/math/interp/adapter_gsl_spline.rb +47 -0
  156. data/lib/carray/math/recurrence.rb +95 -0
  157. data/lib/carray/mkmf.rb +145 -0
  158. data/lib/carray/object/ca_obj_iterator.rb +52 -0
  159. data/lib/carray/object/ca_obj_link.rb +52 -0
  160. data/lib/carray/object/ca_obj_pack.rb +101 -0
  161. data/mkmath.rb +731 -0
  162. data/mt19937ar.c +182 -0
  163. data/mt19937ar.h +86 -0
  164. data/rdoc_main.rb +27 -0
  165. data/rdoc_math.rb +5 -0
  166. data/rdoc_stat.rb +31 -0
  167. data/ruby_carray.c +242 -0
  168. data/ruby_ccomplex.c +497 -0
  169. data/ruby_float_func.c +83 -0
  170. data/spec/CABlockIterator/CABlockIterator_spec.rb +113 -0
  171. data/spec/CArray/bug/store_spec.rb +27 -0
  172. data/spec/CArray/index/repeat_spec.rb +10 -0
  173. data/spec/CArray/method/eq_spec.rb +80 -0
  174. data/spec/CArray/method/is_nan_spec.rb +12 -0
  175. data/spec/CArray/method/ne_spec.rb +18 -0
  176. data/spec/CArray/method/round_spec.rb +11 -0
  177. data/spec/CArray/object/_attribute_spec.rb +32 -0
  178. data/spec/CArray/object/s_new_spec.rb +31 -0
  179. data/spec/CArray/serialize/Serialization_spec.rb +89 -0
  180. data/spec/spec_all.rb +11 -0
  181. data/test/test_ALL.rb +50 -0
  182. data/test/test_CABitfield.rb +59 -0
  183. data/test/test_CABlock.rb +208 -0
  184. data/test/test_CAField.rb +40 -0
  185. data/test/test_CAGrid.rb +76 -0
  186. data/test/test_CAMapping.rb +106 -0
  187. data/test/test_CAMmap.rb +11 -0
  188. data/test/test_CARefer.rb +94 -0
  189. data/test/test_CARepeat.rb +66 -0
  190. data/test/test_CASelect.rb +23 -0
  191. data/test/test_CAShift.rb +17 -0
  192. data/test/test_CATranspose.rb +61 -0
  193. data/test/test_CAVirtual.rb +214 -0
  194. data/test/test_CAWindow.rb +55 -0
  195. data/test/test_CAWrap.rb +9 -0
  196. data/test/test_CArray.rb +228 -0
  197. data/test/test_CComplex.rb +83 -0
  198. data/test/test_CScalar.rb +91 -0
  199. data/test/test_attribute.rb +281 -0
  200. data/test/test_block_iterator.rb +17 -0
  201. data/test/test_boolean.rb +99 -0
  202. data/test/test_cast.rb +33 -0
  203. data/test/test_class.rb +85 -0
  204. data/test/test_complex.rb +43 -0
  205. data/test/test_composite.rb +125 -0
  206. data/test/test_convert.rb +79 -0
  207. data/test/test_copy.rb +141 -0
  208. data/test/test_creation.rb +85 -0
  209. data/test/test_element.rb +146 -0
  210. data/test/test_extream.rb +55 -0
  211. data/test/test_generate.rb +75 -0
  212. data/test/test_index.rb +71 -0
  213. data/test/test_mask.rb +578 -0
  214. data/test/test_math.rb +98 -0
  215. data/test/test_narray.rb +64 -0
  216. data/test/test_order.rb +147 -0
  217. data/test/test_random.rb +15 -0
  218. data/test/test_ref_store.rb +211 -0
  219. data/test/test_stat.rb +414 -0
  220. data/test/test_struct.rb +72 -0
  221. data/test/test_virtual.rb +49 -0
  222. data/utils/ca_ase.rb +21 -0
  223. data/utils/ca_methods.rb +15 -0
  224. data/utils/cast_checker.rb +30 -0
  225. data/utils/create_rdoc.sh +9 -0
  226. data/utils/diff_method.rb +52 -0
  227. data/utils/extract_rdoc.rb +27 -0
  228. data/utils/make_tgz.sh +3 -0
  229. data/utils/remove_resource_fork.sh +5 -0
  230. data/version.h +3 -3
  231. metadata +266 -1
@@ -0,0 +1,636 @@
1
+ /* ---------------------------------------------------------------------------
2
+
3
+ ca_obj_bitfield.c
4
+
5
+ This file is part of Ruby/CArray extension library.
6
+ You can redistribute it and/or modify it under the terms of
7
+ the Ruby Licence.
8
+
9
+ Copyright (C) 2005 Hiroki Motoyoshi
10
+
11
+ ---------------------------------------------------------------------------- */
12
+
13
+ #include "carray.h"
14
+
15
+ typedef struct {
16
+ int16_t obj_type;
17
+ int8_t data_type;
18
+ int8_t rank;
19
+ int32_t flags;
20
+ int32_t bytes;
21
+ int32_t elements;
22
+ int32_t *dim;
23
+ char *ptr;
24
+ CArray *mask;
25
+ CArray *parent;
26
+ uint32_t attach;
27
+ uint8_t nosync;
28
+ /* -------------*/
29
+ int32_t byte_offset;
30
+ int32_t bit_offset;
31
+ uint32_t bit_mask;
32
+ } CABitfield;
33
+
34
+ static int8_t CA_OBJ_BITFIELD;
35
+
36
+ static VALUE rb_cCABitfield;
37
+
38
+ /* rdoc:
39
+ class CABitField < CAVirtual # :nodoc:
40
+ end
41
+ */
42
+
43
+ static int
44
+ bitfield_bitlen (uint32_t bit_mask, int32_t bytes)
45
+ {
46
+ int bitsize = bytes * 8;
47
+ int count = 0;
48
+ int i;
49
+ for (i=0; i<bitsize; i++) {
50
+ if ( ( bit_mask >> i ) & 1 ) {
51
+ count++;
52
+ }
53
+ }
54
+ return count;
55
+ }
56
+
57
+ static void
58
+ bitfield_fetch(char *dst, int dbytes,
59
+ char *src, int sbytes,
60
+ int32_t byte_offset, int32_t bit_offset, uint32_t bit_mask,
61
+ int32_t elements)
62
+ {
63
+ int k;
64
+ switch ( dbytes ) {
65
+ case 1: {
66
+ char *p;
67
+ uint8_t *q, *r;
68
+ uint8_t mask = (uint8_t) bit_mask;
69
+ p = src + byte_offset;
70
+ q = (uint8_t *) dst;
71
+ #ifdef _OPENMP
72
+ #pragma omp parallel for private(r)
73
+ #endif
74
+ for (k=0; k<elements; k++) {
75
+ r = (uint8_t *) (p+k*sbytes);
76
+ *(q+k) = (( *r & mask ) >> bit_offset);
77
+ }
78
+ break;
79
+ }
80
+ case 2: {
81
+ char *p;
82
+ uint16_t *q, *r;
83
+ uint16_t mask = (uint16_t) bit_mask;
84
+ p = src + byte_offset;
85
+ q = (uint16_t *) dst;
86
+ #ifdef _OPENMP
87
+ #pragma omp parallel for private(r)
88
+ #endif
89
+ for (k=0; k<elements; k++) {
90
+ r = (uint16_t *) (p+k*sbytes);
91
+ *(q+k) = (( *r & mask ) >> bit_offset);
92
+ }
93
+ break;
94
+ }
95
+ case 4: {
96
+ char *p;
97
+ uint32_t *q, *r;
98
+ uint32_t mask = (uint32_t) bit_mask;
99
+ p = src + byte_offset;
100
+ q = (uint32_t *) dst;
101
+ #ifdef _OPENMP
102
+ #pragma omp parallel for private(r)
103
+ #endif
104
+ for (k=0; k<elements; k++) {
105
+ r = (uint32_t *) (p+k*sbytes);
106
+ *(q+k) = (( *r & mask ) >> bit_offset);
107
+ }
108
+ break;
109
+ }
110
+ case 8: {
111
+ char *p;
112
+ uint64_t *q, *r;
113
+ uint64_t mask = (uint64_t) bit_mask;
114
+ p = src + byte_offset;
115
+ q = (uint64_t *) dst;
116
+ #ifdef _OPENMP
117
+ #pragma omp parallel for private(r)
118
+ #endif
119
+ for (k=0; k<elements; k++) {
120
+ r = (uint64_t *) (p+k*sbytes);
121
+ *(q+k) = (( *r & mask ) >> bit_offset);
122
+ }
123
+ break;
124
+ }
125
+ default:
126
+ rb_raise(rb_eRuntimeError, "[BUG]");
127
+ }
128
+ }
129
+
130
+ static void
131
+ bitfield_store(char *src, int sbytes,
132
+ char *dst, int dbytes,
133
+ int32_t byte_offset, int32_t bit_offset, uint32_t bit_mask,
134
+ int32_t elements)
135
+ {
136
+ int k;
137
+ switch ( dbytes ) {
138
+ case 1: {
139
+ char *q;
140
+ uint8_t *p, *r;
141
+ uint8_t mask = (uint8_t) bit_mask;
142
+ p = (uint8_t *) src;
143
+ q = dst + byte_offset;
144
+ #ifdef _OPENMP
145
+ #pragma omp parallel for private(r)
146
+ #endif
147
+ for (k=0; k<elements; k++) {
148
+ r = p + k;
149
+ *(uint8_t*)(q+k*dbytes) =
150
+ ( ((*(uint8_t*)(q+k*dbytes)) & ~mask) | ((*r << bit_offset) & mask) );
151
+ }
152
+ break;
153
+ }
154
+ case 2: {
155
+ char *q;
156
+ uint16_t *p, *r;
157
+ uint16_t mask = (uint16_t) bit_mask;
158
+ p = (uint16_t *) src;
159
+ q = dst + byte_offset;
160
+ #ifdef _OPENMP
161
+ #pragma omp parallel for private(r)
162
+ #endif
163
+ for (k=0; k<elements; k++) {
164
+ r = p + k;
165
+ *(uint16_t*)(q+k*dbytes) =
166
+ ( ((*(uint16_t*)(q+k*dbytes)) & ~mask) | ((*r << bit_offset) & mask) );
167
+ }
168
+ break;
169
+ }
170
+ case 4: {
171
+ char *q;
172
+ uint32_t *p, *r;
173
+ uint32_t mask = (uint32_t) bit_mask;
174
+ p = (uint32_t *) src;
175
+ q = dst + byte_offset;
176
+ #ifdef _OPENMP
177
+ #pragma omp parallel for private(r)
178
+ #endif
179
+ for (k=0; k<elements; k++) {
180
+ r = p + k;
181
+ *(uint32_t*)(q+k*dbytes) =
182
+ ( ((*(uint32_t*)(q+k*dbytes)) & ~mask) | ((*r << bit_offset) & mask) );
183
+ }
184
+ break;
185
+ }
186
+ case 8: {
187
+ char *q;
188
+ uint64_t *p, *r;
189
+ uint64_t mask = (uint64_t) bit_mask;
190
+ p = (uint64_t *) src;
191
+ q = dst + byte_offset;
192
+ #ifdef _OPENMP
193
+ #pragma omp parallel for private(r)
194
+ #endif
195
+ for (k=0; k<elements; k++) {
196
+ r = p + k;
197
+ *(uint64_t*)(q+k*dbytes) =
198
+ ( ((*(uint64_t*)(q+k*dbytes)) & ~mask) | ((*r << bit_offset) & mask) );
199
+ }
200
+ break;
201
+ }
202
+ default:
203
+ rb_raise(rb_eRuntimeError, "[BUG]");
204
+ }
205
+ }
206
+
207
+
208
+ /* ------------------------------------------------------------------- */
209
+
210
+ int
211
+ ca_bitfield_setup (CABitfield *ca, CArray *parent,
212
+ int offset, int bitlen)
213
+ {
214
+ int8_t rank;
215
+ int8_t data_type;
216
+ int32_t bytes = 0, elements;
217
+ int bitsize;
218
+ int32_t byte_offset;
219
+ int32_t bit_offset;
220
+ uint32_t bit_mask;
221
+ int i;
222
+
223
+ /* check arguments */
224
+
225
+ /*
226
+ if ( ! ca_is_integer_type(parent) && ! ca_is_float_type(ca) ) {
227
+ rb_raise(rb_eCADataTypeError, "invalid data_type for bitfield");
228
+ }
229
+ */
230
+
231
+ rank = parent->rank;
232
+ bitsize = parent->bytes * 8;
233
+ elements = parent->elements;
234
+
235
+ if ( bitlen <= 0 || bitlen > 64 ) {
236
+ rb_raise(rb_eIndexError, "invalid bit length specified for bit field");
237
+ }
238
+
239
+ if ( offset + bitlen -1 >= bitsize ) {
240
+ rb_raise(rb_eIndexError, "invalid offset for bit field");
241
+ }
242
+
243
+ if ( bitlen == 1 ) {
244
+ data_type = CA_BOOLEAN;
245
+ }
246
+ else if ( bitlen <= 8 ) {
247
+ data_type = CA_UINT8;
248
+ }
249
+ else if ( bitlen <= 16 ) {
250
+ data_type = CA_UINT16;
251
+ }
252
+ else if ( bitlen <= 32 ) {
253
+ data_type = CA_UINT32;
254
+ }
255
+ else {
256
+ data_type = CA_UINT64;
257
+ }
258
+
259
+ CA_CHECK_BYTES(data_type, bytes);
260
+
261
+ if ( bitlen > bytes * 8 ) {
262
+ rb_raise(rb_eArgError, "invalid bit length for specified data_type");
263
+ }
264
+
265
+ if ( ( data_type == CA_BOOLEAN ) && ( bitlen > 1 ) ) {
266
+ rb_raise(rb_eArgError, "invalid bit length for specified data_type");
267
+ }
268
+
269
+ if ( ca_endian == CA_BIG_ENDIAN ) {
270
+ byte_offset = parent->bytes - offset/8 - bytes;
271
+ bit_offset = offset % 8;
272
+ bit_mask = 0;
273
+ for (i=0; i<bitlen; i++) {
274
+ bit_mask += 1 << ( bit_offset + i );
275
+ }
276
+ if ( byte_offset < 0 ) {
277
+ for (i=0; i<-byte_offset; i++) {
278
+ bit_mask = bit_mask << 8;
279
+ bit_offset += 8;
280
+ }
281
+ byte_offset = 0;
282
+ }
283
+ }
284
+ else {
285
+ byte_offset = offset / 8;
286
+ bit_offset = offset % 8;
287
+ bit_mask = 0;
288
+ for (i=0; i<bitlen; i++) {
289
+ bit_mask += 1 << ( bit_offset + i );
290
+ }
291
+ }
292
+
293
+ ca->obj_type = CA_OBJ_BITFIELD;
294
+ ca->data_type = data_type;
295
+ ca->flags = 0;
296
+ ca->rank = rank;
297
+ ca->bytes = bytes;
298
+ ca->elements = elements;
299
+ ca->ptr = NULL;
300
+ ca->mask = NULL;
301
+ ca->dim = ALLOC_N(int32_t, rank);
302
+
303
+ ca->parent = parent;
304
+ ca->attach = 0;
305
+ ca->nosync = 0;
306
+
307
+ ca->byte_offset = byte_offset;
308
+ ca->bit_offset = bit_offset;
309
+ ca->bit_mask = bit_mask;
310
+
311
+ memcpy(ca->dim, parent->dim, rank * sizeof(int32_t));
312
+
313
+ if ( ca_has_mask(parent) ) {
314
+ ca_create_mask(ca);
315
+ }
316
+
317
+ return 0;
318
+ }
319
+
320
+ CABitfield *
321
+ ca_bitfield_new (CArray *parent, int offset, int bitlen)
322
+ {
323
+ CABitfield *ca = ALLOC(CABitfield);
324
+ ca_bitfield_setup(ca, parent, offset, bitlen);
325
+ return ca;
326
+ }
327
+
328
+ static void
329
+ free_ca_bitfield (void *ap)
330
+ {
331
+ CABitfield *ca = (CABitfield *) ap;
332
+ if ( ca != NULL ) {
333
+ ca_free(ca->mask);
334
+ xfree(ca->dim);
335
+ xfree(ca);
336
+ }
337
+ }
338
+
339
+ static void ca_bitfield_attach (CABitfield *ca);
340
+ static void ca_bitfield_sync (CABitfield *ca);
341
+ static void ca_bitfield_fill (CABitfield *ca, char *ptr);
342
+
343
+ /* ------------------------------------------------------------------- */
344
+
345
+ static void *
346
+ ca_bitfield_func_clone (void *ap)
347
+ {
348
+ CABitfield *ca = (CABitfield *) ap;
349
+ return ca_bitfield_new(ca->parent,
350
+ 8 * ca->byte_offset + ca->bit_offset,
351
+ bitfield_bitlen(ca->bit_mask, ca->bytes));
352
+ }
353
+
354
+ static char *
355
+ ca_bitfield_func_ptr_at_addr (void *ap, int32_t addr)
356
+ {
357
+ CABitfield *ca = (CABitfield *) ap;
358
+ if ( ! ca->ptr ) {
359
+ rb_raise(rb_eRuntimeError, "[BUG]");
360
+ return NULL;
361
+ }
362
+ else {
363
+ return ca->ptr + ca->bytes * addr;
364
+ }
365
+ }
366
+
367
+ static char *
368
+ ca_bitfield_func_ptr_at_index (void *ap, int32_t *idx)
369
+ {
370
+ CABitfield *ca = (CABitfield *) ap;
371
+ if ( ! ca->ptr ) {
372
+ rb_raise(rb_eRuntimeError, "[BUG]");
373
+ return NULL;
374
+ }
375
+ else {
376
+ return ca_func[CA_OBJ_ARRAY].ptr_at_index(ca, idx);
377
+ }
378
+ }
379
+
380
+ static void
381
+ ca_bitfield_func_fetch_index (void *ap, int32_t *idx, void *ptr)
382
+ {
383
+ CABitfield *ca = (CABitfield *) ap;
384
+ char *v = xmalloc(ca->parent->bytes);
385
+ ca_fetch_index(ca->parent, idx, v);
386
+ memset(ptr, 0, ca->bytes);
387
+ bitfield_fetch(ptr, ca->bytes, v, ca->parent->bytes,
388
+ ca->byte_offset, ca->bit_offset, ca->bit_mask, 1);
389
+ xfree(v);
390
+ }
391
+
392
+ static void
393
+ ca_bitfield_func_store_index (void *ap, int32_t *idx, void *ptr)
394
+ {
395
+ CABitfield *ca = (CABitfield *) ap;
396
+ char *v = xmalloc(ca->parent->bytes);
397
+ ca_fetch_index(ca->parent, idx, v);
398
+ bitfield_store(ptr, ca->bytes, v, ca->parent->bytes,
399
+ ca->byte_offset, ca->bit_offset, ca->bit_mask, 1);
400
+ ca_store_index(ca->parent, idx, v);
401
+ xfree(v);
402
+ }
403
+
404
+ static void
405
+ ca_bitfield_func_allocate (void *ap)
406
+ {
407
+ CABitfield *ca = (CABitfield *) ap;
408
+ ca_attach(ca->parent);
409
+ /* ca->ptr = ALLOC_N(char, ca_length(ca)); */
410
+ ca->ptr = malloc_with_check(ca_length(ca));
411
+ }
412
+
413
+ static void
414
+ ca_bitfield_func_attach (void *ap)
415
+ {
416
+ void ca_bitfield_attach (CABitfield *cb);
417
+ CABitfield *ca = (CABitfield *) ap;
418
+ ca_attach(ca->parent);
419
+ /* ca->ptr = ALLOC_N(char, ca_length(ca)); */
420
+ ca->ptr = malloc_with_check(ca_length(ca));
421
+ ca_bitfield_attach(ca);
422
+ }
423
+
424
+ static void
425
+ ca_bitfield_func_sync (void *ap)
426
+ {
427
+ CABitfield *ca = (CABitfield *) ap;
428
+ ca_bitfield_sync(ca);
429
+ ca_sync(ca->parent);
430
+ }
431
+
432
+ static void
433
+ ca_bitfield_func_detach (void *ap)
434
+ {
435
+ CABitfield *ca = (CABitfield *) ap;
436
+ free(ca->ptr);
437
+ ca->ptr = NULL;
438
+ ca_detach(ca->parent);
439
+ }
440
+
441
+ static void
442
+ ca_bitfield_func_copy_data (void *ap, void *ptr)
443
+ {
444
+ CABitfield *ca = (CABitfield *) ap;
445
+ char *ptr0 = ca->ptr;
446
+ ca_attach(ca->parent);
447
+ ca->ptr = ptr;
448
+ ca_bitfield_attach(ca);
449
+ ca->ptr = ptr0;
450
+ ca_detach(ca->parent);
451
+ }
452
+
453
+ static void
454
+ ca_bitfield_func_sync_data (void *ap, void *ptr)
455
+ {
456
+ CABitfield *ca = (CABitfield *) ap;
457
+ char *ptr0 = ca->ptr;
458
+ ca_attach(ca->parent);
459
+ ca->ptr = ptr;
460
+ ca_bitfield_sync(ca);
461
+ ca->ptr = ptr0;
462
+ ca_sync(ca->parent);
463
+ ca_detach(ca->parent);
464
+ }
465
+
466
+ static void
467
+ ca_bitfield_func_fill_data (void *ap, void *ptr)
468
+ {
469
+ CABitfield *ca = (CABitfield *) ap;
470
+ ca_attach(ca->parent);
471
+ ca_bitfield_fill(ca, ptr);
472
+ ca_sync(ca->parent);
473
+ ca_detach(ca->parent);
474
+ }
475
+
476
+ static void
477
+ ca_bitfield_func_create_mask (void *ap)
478
+ {
479
+ CABitfield *ca = (CABitfield *) ap;
480
+
481
+ ca_update_mask(ca->parent);
482
+ if ( ! ca->parent->mask ) {
483
+ ca_create_mask(ca->parent);
484
+ }
485
+
486
+ ca->mask = (CArray *) ca_refer_new(ca->parent->mask,
487
+ CA_BOOLEAN, ca->rank, ca->dim, 0, 0);
488
+ }
489
+
490
+ ca_operation_function_t ca_bitfield_func = {
491
+ -1, /* CA_OBJ_BITFIELD */
492
+ CA_VIRTUAL_ARRAY,
493
+ free_ca_bitfield,
494
+ ca_bitfield_func_clone,
495
+ ca_bitfield_func_ptr_at_addr,
496
+ ca_bitfield_func_ptr_at_index,
497
+ NULL,
498
+ ca_bitfield_func_fetch_index,
499
+ NULL,
500
+ ca_bitfield_func_store_index,
501
+ ca_bitfield_func_allocate,
502
+ ca_bitfield_func_attach,
503
+ ca_bitfield_func_sync,
504
+ ca_bitfield_func_detach,
505
+ ca_bitfield_func_copy_data,
506
+ ca_bitfield_func_sync_data,
507
+ ca_bitfield_func_fill_data,
508
+ ca_bitfield_func_create_mask,
509
+ };
510
+
511
+ /* ------------------------------------------------------------------- */
512
+
513
+ static void
514
+ ca_bitfield_attach (CABitfield *ca)
515
+ {
516
+ memset(ca->ptr, 0, ca_length(ca));
517
+ bitfield_fetch(ca->ptr, ca->bytes, ca->parent->ptr, ca->parent->bytes,
518
+ ca->byte_offset, ca->bit_offset, ca->bit_mask, ca->elements);
519
+ }
520
+
521
+ static void
522
+ ca_bitfield_sync (CABitfield *ca)
523
+ {
524
+ bitfield_store(ca->ptr, ca->bytes, ca->parent->ptr, ca->parent->bytes,
525
+ ca->byte_offset, ca->bit_offset, ca->bit_mask, ca->elements);
526
+ }
527
+
528
+ static void
529
+ ca_bitfield_fill (CABitfield *ca, char *ptr)
530
+ {
531
+ char *q = ca->parent->ptr;
532
+ int32_t bytesp = ca->bytes;
533
+ int32_t bytesq = ca->parent->bytes;
534
+ int32_t byte_offset = ca->byte_offset;
535
+ int32_t bit_offset = ca->bit_offset;
536
+ uint32_t bit_mask = ca->bit_mask;
537
+ int32_t i;
538
+
539
+ for (i=0; i<ca->elements; i++) {
540
+ bitfield_store(ptr, bytesp, q, bytesq,
541
+ byte_offset, bit_offset, bit_mask, 1);
542
+ q += bytesq;
543
+ }
544
+ }
545
+
546
+ /* ------------------------------------------------------------------- */
547
+
548
+ VALUE
549
+ rb_ca_bitfield_new (VALUE cary, int offset, int bitlen)
550
+ {
551
+ volatile VALUE obj;
552
+ CArray *parent;
553
+ CABitfield *ca;
554
+ rb_check_carray_object(cary);
555
+ Data_Get_Struct(cary, CArray, parent);
556
+ ca = ca_bitfield_new(parent, offset, bitlen);
557
+ obj = ca_wrap_struct(ca);
558
+ rb_ca_set_parent(obj, cary);
559
+ return obj;
560
+ }
561
+
562
+ /* rdoc:
563
+ class CArray
564
+ def bitfield (range, type)
565
+ end
566
+ end
567
+ */
568
+
569
+ VALUE
570
+ rb_ca_bitfield (int argc, VALUE *argv, VALUE self)
571
+ {
572
+ volatile VALUE rrange, rtype;
573
+ CArray *ca;
574
+ int offset, bitlen, step, data_type = CA_NONE;
575
+ int bitsize;
576
+
577
+ rb_scan_args(argc, argv, "11", &rrange, &rtype);
578
+
579
+ Data_Get_Struct(self, CArray, ca);
580
+
581
+ if ( TYPE(rrange) == T_FIXNUM ) {
582
+ offset = NUM2INT(rrange);
583
+ bitlen = 1;
584
+ }
585
+ else {
586
+ bitsize = ca->bytes * 8;
587
+ ca_parse_range(rrange, bitsize, &offset, &bitlen, &step);
588
+ if ( step != 1 ) {
589
+ rb_raise(rb_eIndexError, "invalid bit range specified for bit field");
590
+ }
591
+ }
592
+
593
+ if ( ! NIL_P(rtype) ) {
594
+ data_type = rb_ca_guess_type(rtype);
595
+ }
596
+
597
+ return rb_ca_bitfield_new(self, offset, bitlen);
598
+ }
599
+
600
+ static VALUE
601
+ rb_ca_bitfield_s_allocate (VALUE klass)
602
+ {
603
+ CABitfield *ca;
604
+ return Data_Make_Struct(klass, CABitfield, ca_mark, ca_free, ca);
605
+ }
606
+
607
+ static VALUE
608
+ rb_ca_bitfield_initialize_copy (VALUE self, VALUE other)
609
+ {
610
+ CABitfield *ca, *cs;
611
+
612
+ Data_Get_Struct(self, CABitfield, ca);
613
+ Data_Get_Struct(other, CABitfield, cs);
614
+
615
+ ca_bitfield_setup(ca, cs->parent,
616
+ 8 * cs->byte_offset + cs->bit_offset,
617
+ bitfield_bitlen(cs->bit_mask, cs->bytes));
618
+
619
+ return self;
620
+ }
621
+
622
+ void
623
+ Init_ca_obj_bitfield ()
624
+ {
625
+ rb_cCABitfield = rb_define_class("CABitfield", rb_cCAVirtual);
626
+
627
+ CA_OBJ_BITFIELD = ca_install_obj_type(rb_cCABitfield, ca_bitfield_func);
628
+ rb_define_const(rb_cObject, "CA_OBJ_BITFIELD", INT2NUM(CA_OBJ_BITFIELD));
629
+
630
+ rb_define_method(rb_cCArray, "bitfield", rb_ca_bitfield, -1);
631
+
632
+ rb_define_alloc_func(rb_cCABitfield, rb_ca_bitfield_s_allocate);
633
+ rb_define_method(rb_cCABitfield, "initialize_copy",
634
+ rb_ca_bitfield_initialize_copy, 1);
635
+ }
636
+