carray 3.0.6 → 3.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 (277) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -1
  3. data/CHANGELOG.md +7 -1830
  4. data/README.md +62 -35
  5. data/carray.gemspec +16 -4
  6. data/docs/changelogs/CHANGELOG-3.0.md +2464 -0
  7. data/ext/ca_array_pool.c +4 -5
  8. data/ext/ca_axis_descriptor.h +45 -30
  9. data/ext/ca_axis_dispatch.c +7 -4
  10. data/ext/ca_axis_group.c +12 -15
  11. data/ext/ca_bincmp_dispatch.h +16 -38
  12. data/ext/ca_binop_dispatch.c +35 -0
  13. data/ext/ca_binop_dispatch.h +22 -46
  14. data/ext/ca_categorical_iterator.c +36 -34
  15. data/ext/ca_compare.c +0 -1
  16. data/ext/ca_compare.h +8 -1
  17. data/ext/ca_composite_dispatch.h +6 -0
  18. data/ext/ca_for_buffer.h +6 -1
  19. data/ext/ca_group_iter.c +17 -18
  20. data/ext/ca_iter_state_fields.h +196 -0
  21. data/ext/ca_iter_substrate.h +84 -103
  22. data/ext/ca_kernel_iterator.c +125 -89
  23. data/ext/ca_kernel_iterator.h +229 -1504
  24. data/ext/ca_kernel_iterator_internal.h +1167 -0
  25. data/ext/ca_moncmp_dispatch.h +17 -24
  26. data/ext/ca_monop_dispatch.h +25 -74
  27. data/ext/ca_obj_array.c +92 -165
  28. data/ext/ca_obj_bincmp.c +65 -48
  29. data/ext/ca_obj_binop.c +74 -49
  30. data/ext/ca_obj_bitarray.c +7 -21
  31. data/ext/ca_obj_bitfield.c +93 -26
  32. data/ext/ca_obj_block.c +11 -12
  33. data/ext/ca_obj_byte_swap.c +34 -34
  34. data/ext/ca_obj_const_string.c +464 -304
  35. data/ext/ca_obj_face.c +167 -121
  36. data/ext/ca_obj_face.h +32 -170
  37. data/ext/ca_obj_fake.c +127 -34
  38. data/ext/ca_obj_farray.c +13 -9
  39. data/ext/ca_obj_field.c +17 -13
  40. data/ext/ca_obj_fixlen_string.c +57 -34
  41. data/ext/ca_obj_grid.c +149 -113
  42. data/ext/ca_obj_meld.c +28 -26
  43. data/ext/ca_obj_moncmp.c +10 -21
  44. data/ext/ca_obj_monop.c +88 -73
  45. data/ext/ca_obj_object.c +60 -46
  46. data/ext/ca_obj_record.c +74 -22
  47. data/ext/ca_obj_reduce.c +10 -14
  48. data/ext/ca_obj_refer.c +146 -39
  49. data/ext/ca_obj_remap.c +11 -29
  50. data/ext/ca_obj_repeat.c +41 -23
  51. data/ext/ca_obj_roll.c +20 -28
  52. data/ext/ca_obj_select.c +17 -27
  53. data/ext/ca_obj_select_axis.c +35 -42
  54. data/ext/ca_obj_shift.c +22 -42
  55. data/ext/ca_obj_source.c +1 -5
  56. data/ext/ca_obj_stack.c +33 -29
  57. data/ext/ca_obj_stride.c +99 -62
  58. data/ext/ca_obj_string.c +8 -20
  59. data/ext/ca_obj_tile.c +14 -16
  60. data/ext/ca_obj_time.c +12 -23
  61. data/ext/ca_obj_timedelta.c +12 -23
  62. data/ext/ca_obj_transpose.c +8 -10
  63. data/ext/ca_obj_triop.c +48 -49
  64. data/ext/ca_obj_window.c +87 -99
  65. data/ext/ca_op_cmplx64.h +4 -0
  66. data/ext/ca_op_ipower.c +97 -27
  67. data/ext/ca_op_powi.h +9 -28
  68. data/ext/ca_pow5_table.h +2 -1
  69. data/ext/ca_rng_normal.h +4 -0
  70. data/ext/ca_rng_xoshiro256pp.h +3 -0
  71. data/ext/ca_sort_kernels.h +23 -26
  72. data/ext/ca_sweep_engine.c +92 -26
  73. data/ext/ca_sweep_engine.h +22 -15
  74. data/ext/ca_transform_common.c +3 -8
  75. data/ext/ca_triop_dispatch.h +20 -32
  76. data/ext/caframe_csv_reader.c +12 -12
  77. data/ext/carray.h +300 -1009
  78. data/ext/carray_access.c +320 -207
  79. data/ext/carray_address_basis.c +128 -59
  80. data/ext/carray_attribute.c +81 -104
  81. data/ext/carray_bincount.c +12 -14
  82. data/ext/carray_broadcast.c +31 -27
  83. data/ext/carray_call_cfunc.c +174 -285
  84. data/ext/carray_call_cfunc.h +36 -28
  85. data/ext/carray_cast.c +1191 -351
  86. data/ext/carray_cast_func.rb +68 -8
  87. data/ext/carray_class.c +6 -11
  88. data/ext/carray_conversion.c +98 -39
  89. data/ext/carray_copy.c +52 -26
  90. data/ext/carray_core.c +286 -132
  91. data/ext/carray_count.c +53 -22
  92. data/ext/carray_element.c +49 -28
  93. data/ext/carray_factorize.c +64 -63
  94. data/ext/carray_generate.c +26 -19
  95. data/ext/carray_histogram.c +9 -11
  96. data/ext/carray_hold.c +7 -9
  97. data/ext/carray_index_classifier.c +127 -230
  98. data/ext/carray_index_classifier.h +11 -10
  99. data/ext/carray_integer_total.c +523 -0
  100. data/ext/carray_internal.h +1364 -156
  101. data/ext/carray_key_windows.c +3 -3
  102. data/ext/carray_lazy.c +37 -37
  103. data/ext/carray_loop.c +27 -23
  104. data/ext/carray_mask.c +235 -150
  105. data/ext/carray_math_kernel.h +23 -47
  106. data/ext/carray_mathfunc.c +10 -3
  107. data/ext/carray_median_percentile.c +106 -100
  108. data/ext/carray_memory_view.c +83 -69
  109. data/ext/carray_operator.c +490 -128
  110. data/ext/carray_order.c +163 -84
  111. data/ext/carray_partition.c +14 -16
  112. data/ext/carray_random.c +48 -42
  113. data/ext/carray_round.c +33 -5
  114. data/ext/carray_scatter.c +57 -39
  115. data/ext/carray_segment.c +42 -46
  116. data/ext/carray_slab.c +77 -51
  117. data/ext/carray_slab.h +44 -33
  118. data/ext/carray_sort.c +37 -31
  119. data/ext/carray_sort_kernel.c +36 -10
  120. data/ext/carray_struct.c +44 -34
  121. data/ext/carray_test.c +131 -81
  122. data/ext/carray_time_text.c +30 -31
  123. data/ext/carray_undef.c +50 -7
  124. data/ext/carray_utils.c +373 -139
  125. data/ext/extconf.rb +24 -17
  126. data/ext/mk_call_cfunc.rb +25 -31
  127. data/ext/mk_pow5_table.rb +2 -1
  128. data/ext/mkkernel.rb +819 -488
  129. data/ext/ruby_carray.c +47 -42
  130. data/ext/version.h +7 -5
  131. data/lib/carray/attribute.rb +19 -18
  132. data/lib/carray/autoload_carray.rb +20 -15
  133. data/lib/carray/autoload_method_extension.rb +64 -57
  134. data/lib/carray/axis_group.rb +272 -185
  135. data/lib/carray/basics.rb +298 -56
  136. data/lib/carray/bincount_nd.rb +41 -18
  137. data/lib/carray/block_iterator.rb +167 -119
  138. data/lib/carray/boolean_reduce.rb +7 -4
  139. data/lib/carray/categorical.rb +84 -59
  140. data/lib/carray/categorical_iterator.rb +90 -67
  141. data/lib/carray/complex.rb +61 -41
  142. data/lib/carray/conditional.rb +98 -23
  143. data/lib/carray/const_string.rb +112 -46
  144. data/lib/carray/core_extensions.rb +20 -69
  145. data/lib/carray/distance_limit.rb +7 -5
  146. data/lib/carray/factories.rb +269 -0
  147. data/lib/carray/fixlen_string.rb +95 -12
  148. data/lib/carray/frame/column_verbs.rb +76 -9
  149. data/lib/carray/frame/concat.rb +51 -7
  150. data/lib/carray/frame/convert.rb +2 -1
  151. data/lib/carray/frame/csv_parser.rb +20 -5
  152. data/lib/carray/frame/frame.rb +73 -31
  153. data/lib/carray/frame/group.rb +247 -187
  154. data/lib/carray/frame/io.rb +36 -12
  155. data/lib/carray/frame/join.rb +56 -17
  156. data/lib/carray/frame/records.rb +7 -5
  157. data/lib/carray/frame/reshape.rb +27 -7
  158. data/lib/carray/frame/sort.rb +10 -8
  159. data/lib/carray/frame/verbs.rb +217 -134
  160. data/lib/carray/frame/windows.rb +254 -19
  161. data/lib/carray/frame.rb +1 -2
  162. data/lib/carray/fuse_source.rb +28 -23
  163. data/lib/carray/fusion.rb +30 -30
  164. data/lib/carray/histogram.rb +35 -16
  165. data/lib/carray/inspect.rb +7 -7
  166. data/lib/carray/iterator.rb +104 -23
  167. data/lib/carray/key_windows.rb +93 -61
  168. data/lib/carray/lazy.rb +208 -111
  169. data/lib/carray/limits.rb +80 -0
  170. data/lib/carray/mask_gap_fill.rb +21 -16
  171. data/lib/carray/math.rb +87 -50
  172. data/lib/carray/meld_reduce.rb +30 -8
  173. data/lib/carray/methods/align_addr.rb +29 -17
  174. data/lib/carray/methods/bin.rb +8 -1
  175. data/lib/carray/methods/bincount.rb +4 -2
  176. data/lib/carray/methods/bit_string.rb +19 -31
  177. data/lib/carray/methods/broadcast.rb +8 -3
  178. data/lib/carray/methods/choose.rb +5 -5
  179. data/lib/carray/methods/composition.rb +73 -72
  180. data/lib/carray/methods/{index.rb → coordinate.rb} +13 -13
  181. data/lib/carray/methods/discovery_along.rb +3 -3
  182. data/lib/carray/methods/factorize.rb +2 -0
  183. data/lib/carray/methods/gather_nd.rb +106 -103
  184. data/lib/carray/methods/insert_block.rb +2 -2
  185. data/lib/carray/methods/is_between.rb +6 -4
  186. data/lib/carray/methods/is_in.rb +183 -143
  187. data/lib/carray/methods/join.rb +2 -1
  188. data/lib/carray/methods/lag.rb +6 -2
  189. data/lib/carray/methods/locate_addr.rb +157 -94
  190. data/lib/carray/methods/mask_duplicates.rb +1 -1
  191. data/lib/carray/methods/meshgrid.rb +28 -16
  192. data/lib/carray/methods/mode.rb +67 -67
  193. data/lib/carray/methods/nunique.rb +9 -7
  194. data/lib/carray/methods/repeat.rb +23 -13
  195. data/lib/carray/methods/resize.rb +1 -1
  196. data/lib/carray/methods/snap.rb +24 -20
  197. data/lib/carray/methods/string_format.rb +2 -2
  198. data/lib/carray/methods/unique.rb +4 -2
  199. data/lib/carray/methods/value_counts.rb +1 -1
  200. data/lib/carray/mkmf.rb +25 -28
  201. data/lib/carray/percentile_arguments.rb +82 -0
  202. data/lib/carray/record_order.rb +50 -20
  203. data/lib/carray/rng.rb +1 -0
  204. data/lib/carray/runtime.rb +195 -17
  205. data/lib/carray/segment_iterator.rb +262 -137
  206. data/lib/carray/serialize.rb +635 -566
  207. data/lib/carray/slab_iterator.rb +114 -114
  208. data/lib/carray/stack.rb +14 -15
  209. data/lib/carray/string.rb +16 -22
  210. data/lib/carray/string_operation_extension.rb +97 -52
  211. data/lib/carray/struct.rb +122 -164
  212. data/lib/carray/struct_builder.rb +247 -73
  213. data/lib/carray/time.rb +663 -365
  214. data/lib/carray/window_iterator.rb +232 -155
  215. data/lib/carray.rb +6 -11
  216. data/yard-stubs/ca_obj_array.rb +20 -135
  217. data/yard-stubs/ca_obj_bitarray.rb +1 -8
  218. data/yard-stubs/ca_obj_bitfield.rb +6 -2
  219. data/yard-stubs/ca_obj_byte_swap.rb +21 -7
  220. data/yard-stubs/ca_obj_fake.rb +3 -1
  221. data/yard-stubs/ca_obj_farray.rb +1 -1
  222. data/yard-stubs/ca_obj_field.rb +8 -6
  223. data/yard-stubs/ca_obj_refer.rb +10 -5
  224. data/yard-stubs/ca_obj_roll.rb +7 -6
  225. data/yard-stubs/ca_obj_shift.rb +2 -3
  226. data/yard-stubs/ca_obj_stride.rb +5 -7
  227. data/yard-stubs/ca_obj_tile.rb +1 -1
  228. data/yard-stubs/ca_obj_time.rb +43 -0
  229. data/yard-stubs/ca_obj_transpose.rb +1 -1
  230. data/yard-stubs/ca_obj_window.rb +28 -16
  231. data/yard-stubs/carray_access.rb +55 -6
  232. data/yard-stubs/carray_attribute.rb +26 -38
  233. data/yard-stubs/carray_cast.rb +121 -162
  234. data/yard-stubs/carray_class.rb +3 -4
  235. data/yard-stubs/carray_conversion.rb +6 -7
  236. data/yard-stubs/carray_copy.rb +2 -1
  237. data/yard-stubs/carray_count.rb +10 -5
  238. data/yard-stubs/carray_element.rb +4 -3
  239. data/yard-stubs/carray_limits.rb +100 -0
  240. data/yard-stubs/carray_loop.rb +0 -20
  241. data/yard-stubs/carray_mask.rb +59 -41
  242. data/yard-stubs/carray_math.rb +162 -100
  243. data/yard-stubs/carray_mathfunc.rb +415 -3
  244. data/yard-stubs/carray_median_percentile.rb +29 -11
  245. data/yard-stubs/carray_memory_view.rb +2 -2
  246. data/yard-stubs/carray_order.rb +72 -33
  247. data/yard-stubs/carray_random.rb +6 -6
  248. data/yard-stubs/carray_reduce.rb +353 -0
  249. data/yard-stubs/carray_rng.rb +92 -0
  250. data/yard-stubs/carray_scatter.rb +12 -3
  251. data/yard-stubs/carray_slab.rb +7 -4
  252. data/yard-stubs/carray_sort.rb +7 -6
  253. data/yard-stubs/carray_test.rb +18 -5
  254. data/yard-stubs/carray_undef.rb +44 -5
  255. data/yard-stubs/ruby_carray.rb +75 -40
  256. metadata +63 -25
  257. data/CHANGELOG.v1.md +0 -126
  258. data/ext/carray_build_flags.h +0 -3
  259. data/ext/carray_config.h +0 -28
  260. data/ext/carray_data_type.c +0 -90
  261. data/ext/carray_kernels_bincmp.c +0 -6457
  262. data/ext/carray_kernels_binop.c +0 -31534
  263. data/ext/carray_kernels_init.c +0 -1734
  264. data/ext/carray_kernels_moncmp.c +0 -2104
  265. data/ext/carray_kernels_monop.c +0 -25387
  266. data/ext/carray_kernels_reduce_aggregate.c +0 -27545
  267. data/ext/carray_kernels_reduce_boolean.c +0 -1924
  268. data/ext/carray_kernels_reduce_cumulative.c +0 -11646
  269. data/ext/carray_kernels_reduce_extreme.c +0 -21631
  270. data/ext/carray_kernels_reduce_variance.c +0 -5981
  271. data/ext/carray_kernels_scan.c +0 -4302
  272. data/ext/carray_kernels_search.c +0 -37776
  273. data/ext/carray_kernels_sort.c +0 -10814
  274. data/ext/carray_kernels_triop.c +0 -1864
  275. data/lib/carray/construct.rb +0 -360
  276. data/lib/carray/data_type_extension.rb +0 -261
  277. data/lib/carray/data_type_limits.rb +0 -91
data/ext/carray_utils.c CHANGED
@@ -11,20 +11,17 @@
11
11
  ---------------------------------------------------------------------------- */
12
12
 
13
13
  #include <stdarg.h>
14
+ #include <time.h>
15
+ #include <math.h>
14
16
  #include "ruby.h"
15
17
  #include "carray.h"
16
- #include "carray_internal.h" /* ca_calling_method_name */
17
18
 
18
19
  #include "ruby/st.h"
19
20
  #include "ruby/thread_native.h"
21
+ #include "ruby/thread.h" /* rb_thread_call_without_gvl */
20
22
  #include "ruby/ractor.h"
21
23
  #include "ruby/debug.h" /* rb_profile_frames: the method the user called */
22
24
 
23
- static ID id_begin, id_end, id_excl_end;
24
- #define RANGE_BEG(r) (rb_funcall(r, id_begin, 0))
25
- #define RANGE_END(r) (rb_funcall(r, id_end, 0))
26
- #define RANGE_EXCL(r) (rb_funcall(r, id_excl_end, 0))
27
-
28
25
  /* ------------------------------------------------------------------- */
29
26
 
30
27
  void
@@ -103,6 +100,14 @@ ca_get_loop_count (int n, ...)
103
100
  return elements;
104
101
  }
105
102
 
103
+ /* A bound of a range that is not a key of `[]`: a window or a bit range says
104
+ how many cells, so a Float is refused rather than cut down. */
105
+ static ca_size_t
106
+ ca_range_bound (VALUE v)
107
+ {
108
+ return ca_integer_arg(v, "a range bound", NULL);
109
+ }
110
+
106
111
  /*
107
112
  ca_parse_range and ca_parse_range_without_check parse the following
108
113
  types of range specifications
@@ -138,7 +143,7 @@ ca_parse_range (VALUE arg, ca_size_t size,
138
143
  }
139
144
  else if ( rb_obj_is_kind_of(arg, rb_cInteger) ) {
140
145
  /* i */
141
- start = NUM2SIZE(arg);
146
+ start = ca_range_bound(arg);
142
147
  CA_CHECK_INDEX(start, size);
143
148
  *poffset = start;
144
149
  *pcount = 1;
@@ -146,9 +151,14 @@ ca_parse_range (VALUE arg, ca_size_t size,
146
151
  }
147
152
  else if ( rb_obj_is_kind_of(arg, rb_cRange) ) {
148
153
  /* i..j */
149
- start = NUM2SIZE(RANGE_BEG(arg));
150
- last = NUM2SIZE(RANGE_END(arg));
151
- excl = RTEST(RANGE_EXCL(arg));
154
+ {
155
+ VALUE beg, end;
156
+ int exclusive;
157
+ rb_range_values(arg, &beg, &end, &exclusive);
158
+ start = ca_range_bound(beg);
159
+ last = ca_range_bound(end);
160
+ excl = exclusive;
161
+ }
152
162
  CA_CHECK_INDEX(start, size);
153
163
  if ( last < 0 ) {
154
164
  last += size;
@@ -168,12 +178,13 @@ ca_parse_range (VALUE arg, ca_size_t size,
168
178
  rb_arithmetic_sequence_components_t x;
169
179
  rb_arithmetic_sequence_extract(arg, &x);
170
180
 
171
- start = NUM2SIZE(x.begin);
172
- last = NUM2SIZE(x.end);
181
+ start = ca_range_bound(x.begin);
182
+ last = ca_range_bound(x.end);
173
183
  excl = RTEST(x.exclude_end);
174
- step = NUM2SIZE(x.step);
184
+ step = ca_range_bound(x.step);
175
185
  if ( step == 0 ) {
176
- rb_raise(rb_eArgError, "step should not be 0");
186
+ rb_raise(rb_eArgError, "%s: step should not be 0",
187
+ ca_calling_method_name());
177
188
  }
178
189
  CA_CHECK_INDEX(start, size);
179
190
  if ( last < 0 ) {
@@ -209,9 +220,10 @@ ca_parse_range (VALUE arg, ca_size_t size,
209
220
  VALUE arg0 = rb_ary_entry(arg, 0);
210
221
  VALUE arg1 = rb_ary_entry(arg, 1);
211
222
  if ( NIL_P(arg0) ) { /* [nil,k] */
212
- step = NUM2SIZE(arg1);
223
+ step = ca_range_bound(arg1);
213
224
  if ( step == 0 ) {
214
- rb_raise(rb_eArgError, "step should not be 0");
225
+ rb_raise(rb_eArgError, "%s: step should not be 0",
226
+ ca_calling_method_name());
215
227
  }
216
228
  start = 0;
217
229
  count = (size-1)/llabs(step) + 1;
@@ -224,12 +236,18 @@ ca_parse_range (VALUE arg, ca_size_t size,
224
236
  *pstep = step;
225
237
  }
226
238
  else if ( rb_obj_is_kind_of(arg0, rb_cRange) ) { /* [i..j,k] */
227
- start = NUM2SIZE(RANGE_BEG(arg0));
228
- last = NUM2SIZE(RANGE_END(arg0));
229
- excl = RTEST(RANGE_EXCL(arg0));
230
- step = NUM2SIZE(arg1);
239
+ {
240
+ VALUE beg, end;
241
+ int exclusive;
242
+ rb_range_values(arg0, &beg, &end, &exclusive);
243
+ start = ca_range_bound(beg);
244
+ last = ca_range_bound(end);
245
+ excl = exclusive;
246
+ }
247
+ step = ca_range_bound(arg1);
231
248
  if ( step == 0 ) {
232
- rb_raise(rb_eArgError, "step should not be 0");
249
+ rb_raise(rb_eArgError, "%s: step should not be 0",
250
+ ca_calling_method_name());
233
251
  }
234
252
  CA_CHECK_INDEX(start, size);
235
253
  if ( last < 0 ) {
@@ -260,12 +278,13 @@ ca_parse_range (VALUE arg, ca_size_t size,
260
278
  rb_arithmetic_sequence_components_t x;
261
279
  rb_arithmetic_sequence_extract(arg0, &x);
262
280
 
263
- start = NUM2SIZE(x.begin);
264
- last = NUM2SIZE(x.end);
281
+ start = ca_range_bound(x.begin);
282
+ last = ca_range_bound(x.end);
265
283
  excl = RTEST(x.exclude_end);
266
- step = NUM2SIZE(x.step);
284
+ step = ca_range_bound(x.step);
267
285
  if ( step == 0 ) {
268
- rb_raise(rb_eArgError, "step should not be 0");
286
+ rb_raise(rb_eArgError, "%s: step should not be 0",
287
+ ca_calling_method_name());
269
288
  }
270
289
  CA_CHECK_INDEX(start, size);
271
290
  if ( last < 0 ) {
@@ -293,8 +312,8 @@ ca_parse_range (VALUE arg, ca_size_t size,
293
312
  }
294
313
  #endif
295
314
  else { /* [i,n] */
296
- start = NUM2SIZE(arg0);
297
- count = NUM2SIZE(arg1);
315
+ start = ca_range_bound(arg0);
316
+ count = ca_range_bound(arg1);
298
317
  CA_CHECK_INDEX(start, size);
299
318
  bound = start + (count - 1);
300
319
  if ( count > 0 && ( bound < 0 || bound >= size ) ) {
@@ -306,11 +325,12 @@ ca_parse_range (VALUE arg, ca_size_t size,
306
325
  }
307
326
  }
308
327
  else if ( RARRAY_LEN(arg) == 3 ) { /* [i,n,k] */
309
- start = NUM2SIZE(rb_ary_entry(arg, 0));
310
- count = NUM2SIZE(rb_ary_entry(arg, 1));
311
- step = NUM2SIZE(rb_ary_entry(arg, 2));
328
+ start = ca_range_bound(rb_ary_entry(arg, 0));
329
+ count = ca_range_bound(rb_ary_entry(arg, 1));
330
+ step = ca_range_bound(rb_ary_entry(arg, 2));
312
331
  if ( step == 0 ) {
313
- rb_raise(rb_eArgError, "step should not be 0");
332
+ rb_raise(rb_eArgError, "%s: step should not be 0",
333
+ ca_calling_method_name());
314
334
  }
315
335
  CA_CHECK_INDEX(start, size);
316
336
  bound = start + (count - 1)*step;
@@ -322,11 +342,13 @@ ca_parse_range (VALUE arg, ca_size_t size,
322
342
  *pstep = step;
323
343
  }
324
344
  else {
325
- rb_raise(rb_eArgError, "unknown range specification");
345
+ rb_raise(rb_eArgError, "%s: unknown range specification",
346
+ ca_calling_method_name());
326
347
  }
327
348
  }
328
349
  else {
329
- rb_raise(rb_eArgError, "unknown range specification");
350
+ rb_raise(rb_eArgError, "%s: unknown range specification",
351
+ ca_calling_method_name());
330
352
  }
331
353
  }
332
354
 
@@ -345,18 +367,23 @@ ca_parse_range_without_check (VALUE arg, ca_size_t size,
345
367
  }
346
368
  else if ( rb_obj_is_kind_of(arg, rb_cInteger) ) {
347
369
  /* i */
348
- start = NUM2SIZE(arg);
370
+ start = ca_range_bound(arg);
349
371
  *poffset = start;
350
372
  *pcount = 1;
351
373
  *pstep = 1;
352
374
  }
353
375
  else if ( rb_obj_is_kind_of(arg, rb_cRange) ) {
354
376
  /* i..j */
355
- start = NUM2SIZE(RANGE_BEG(arg));
356
- last = NUM2SIZE(RANGE_END(arg));
357
- excl = RTEST(RANGE_EXCL(arg));
377
+ {
378
+ VALUE beg, end;
379
+ int exclusive;
380
+ rb_range_values(arg, &beg, &end, &exclusive);
381
+ start = ca_range_bound(beg);
382
+ last = ca_range_bound(end);
383
+ excl = exclusive;
384
+ }
358
385
  if ( excl ) {
359
- last += ( (last>=start) ? -1 : 1 );
386
+ last -= 1;
360
387
  }
361
388
  *poffset = start;
362
389
  *pcount = last - start + 1;
@@ -367,16 +394,16 @@ ca_parse_range_without_check (VALUE arg, ca_size_t size,
367
394
  rb_arithmetic_sequence_components_t x;
368
395
  rb_arithmetic_sequence_extract(arg, &x);
369
396
 
370
- start = NUM2SIZE(x.begin);
371
- last = NUM2SIZE(x.end);
397
+ start = ca_range_bound(x.begin);
398
+ last = ca_range_bound(x.end);
372
399
  excl = RTEST(x.exclude_end);
373
- step = NUM2SIZE(x.step);
400
+ step = ca_range_bound(x.step);
374
401
  if ( step == 0 ) {
375
402
  rb_raise(rb_eArgError, "%s: step should not be 0",
376
403
  ca_calling_method_name());
377
404
  }
378
405
  if ( excl ) {
379
- last += ( (last>=start) ? -1 : 1 );
406
+ last += ( (step>0) ? -1 : 1 );
380
407
  }
381
408
  count = (last - start)/llabs(step) + 1;
382
409
  *poffset = start;
@@ -394,7 +421,7 @@ ca_parse_range_without_check (VALUE arg, ca_size_t size,
394
421
  VALUE arg1 = rb_ary_entry(arg, 1);
395
422
  if ( NIL_P(arg0) ) { /* [nil,k] */
396
423
  start = 0;
397
- step = NUM2SIZE(arg1);
424
+ step = ca_range_bound(arg1);
398
425
  if ( step == 0 ) {
399
426
  rb_raise(rb_eArgError, "%s: step should not be 0",
400
427
  ca_calling_method_name());
@@ -405,10 +432,15 @@ ca_parse_range_without_check (VALUE arg, ca_size_t size,
405
432
  *pstep = step;
406
433
  }
407
434
  else if ( rb_obj_is_kind_of(arg0, rb_cRange) ) { /* [i..j,k] */
408
- start = NUM2SIZE(RANGE_BEG(arg0));
409
- last = NUM2SIZE(RANGE_END(arg0));
410
- excl = RTEST(RANGE_EXCL(arg0));
411
- step = NUM2SIZE(arg1);
435
+ {
436
+ VALUE beg, end;
437
+ int exclusive;
438
+ rb_range_values(arg0, &beg, &end, &exclusive);
439
+ start = ca_range_bound(beg);
440
+ last = ca_range_bound(end);
441
+ excl = exclusive;
442
+ }
443
+ step = ca_range_bound(arg1);
412
444
  if ( step == 0 ) {
413
445
  rb_raise(rb_eArgError, "%s: step should not be 0",
414
446
  ca_calling_method_name());
@@ -426,10 +458,10 @@ ca_parse_range_without_check (VALUE arg, ca_size_t size,
426
458
  rb_arithmetic_sequence_components_t x;
427
459
  rb_arithmetic_sequence_extract(arg0, &x);
428
460
 
429
- start = NUM2SIZE(x.begin);
430
- last = NUM2SIZE(x.end);
461
+ start = ca_range_bound(x.begin);
462
+ last = ca_range_bound(x.end);
431
463
  excl = RTEST(x.exclude_end);
432
- step = NUM2SIZE(x.step);
464
+ step = ca_range_bound(x.step);
433
465
  if ( step == 0 ) {
434
466
  rb_raise(rb_eArgError, "%s: step should not be 0",
435
467
  ca_calling_method_name());
@@ -444,27 +476,29 @@ ca_parse_range_without_check (VALUE arg, ca_size_t size,
444
476
  }
445
477
  #endif
446
478
  else { /* [i,n] */
447
- start = NUM2SIZE(arg0);
448
- count = NUM2SIZE(arg1);
479
+ start = ca_range_bound(arg0);
480
+ count = ca_range_bound(arg1);
449
481
  *poffset = start;
450
482
  *pcount = count;
451
483
  *pstep = 1;
452
484
  }
453
485
  }
454
486
  else if ( RARRAY_LEN(arg) == 3 ) { /* [i,n,k] */
455
- start = NUM2SIZE(rb_ary_entry(arg, 0));
456
- count = NUM2SIZE(rb_ary_entry(arg, 1));
457
- step = NUM2SIZE(rb_ary_entry(arg, 2));
487
+ start = ca_range_bound(rb_ary_entry(arg, 0));
488
+ count = ca_range_bound(rb_ary_entry(arg, 1));
489
+ step = ca_range_bound(rb_ary_entry(arg, 2));
458
490
  *poffset = start;
459
491
  *pcount = count;
460
492
  *pstep = step;
461
493
  }
462
494
  else {
463
- rb_raise(rb_eArgError, "unknown range specification");
495
+ rb_raise(rb_eArgError, "%s: unknown range specification",
496
+ ca_calling_method_name());
464
497
  }
465
498
  }
466
499
  else {
467
- rb_raise(rb_eArgError, "unknown range specification");
500
+ rb_raise(rb_eArgError, "%s: unknown range specification",
501
+ ca_calling_method_name());
468
502
  }
469
503
  }
470
504
 
@@ -505,8 +539,8 @@ ca_bounds_normalize_index (int8_t bounds, ca_size_t size0, ca_size_t k)
505
539
  k = k % (2*size0);
506
540
  return ( k < size0 ) ? k : 2*size0-1-k;
507
541
  default:
508
- rb_raise(rb_eArgError,
509
- "unknown window boundary specified (%i)", bounds);
542
+ rb_raise(rb_eRuntimeError,
543
+ "[BUG] ca_bounds_normalize_index: unknown boundary (%i)", bounds);
510
544
  }
511
545
  }
512
546
 
@@ -528,7 +562,7 @@ ca_stable_id_name (ID id)
528
562
  {
529
563
  st_data_t found;
530
564
  const char *name = NULL;
531
- rb_nativethread_lock_lock(&ca_id_names_lock);
565
+ ca_native_lock_without_gvl(&ca_id_names_lock);
532
566
  if ( st_lookup(ca_id_names, (st_data_t) id, &found) ) {
533
567
  name = (const char *) found;
534
568
  }
@@ -537,18 +571,19 @@ ca_stable_id_name (ID id)
537
571
  return name;
538
572
  }
539
573
  {
540
- VALUE str = rb_id2str(id);
574
+ volatile VALUE str = rb_id2str(id);
541
575
  long len;
542
576
  char *copy;
543
577
  if ( NIL_P(str) ) {
544
578
  return NULL;
545
579
  }
546
580
  len = RSTRING_LEN(str);
581
+ /* buffer: kept by the cache; freed here only when another thread stored it first */
547
582
  copy = ALLOC_N(char, len + 1);
548
583
  memcpy(copy, RSTRING_PTR(str), len);
549
584
  copy[len] = '\0';
550
- RB_GC_GUARD(str);
551
- rb_nativethread_lock_lock(&ca_id_names_lock);
585
+ /* lock: st_insert can run a GC, which does not take this lock */
586
+ ca_native_lock_without_gvl(&ca_id_names_lock);
552
587
  if ( st_lookup(ca_id_names, (st_data_t) id, &found) ) {
553
588
  xfree(copy);
554
589
  name = (const char *) found;
@@ -627,7 +662,7 @@ static long ca_library_root_len = 0;
627
662
  /* CArray.library_root = dir (private): where the frames of the library's
628
663
  own Ruby methods are. The first call wins. */
629
664
  static VALUE
630
- rb_ca_s_set_library_root (VALUE klass, VALUE dir)
665
+ rb_ca_s_set_library_root (VALUE klass, volatile VALUE dir)
631
666
  {
632
667
  if ( ca_library_root == NULL ) {
633
668
  long len = RSTRING_LEN(StringValue(dir));
@@ -644,9 +679,9 @@ rb_ca_s_set_library_root (VALUE klass, VALUE dir)
644
679
  library's: a method of a CA* or CScalar class, or a CA_* conversion
645
680
  function. A method of a core class (Array#map, Integer#times) is not. */
646
681
  static int
647
- ca_frame_is_library_cfunc (VALUE frame, VALUE name)
682
+ ca_frame_is_library_cfunc (VALUE frame, volatile VALUE name)
648
683
  {
649
- VALUE path = rb_profile_frame_classpath(frame);
684
+ volatile VALUE path = rb_profile_frame_classpath(frame);
650
685
  if ( ! NIL_P(path) ) {
651
686
  const char *p = RSTRING_PTR(path);
652
687
  long n = RSTRING_LEN(path);
@@ -679,8 +714,9 @@ ca_user_called_method_id (void)
679
714
  }
680
715
  n = rb_profile_frames(0, CA_NAMING_FRAMES, frames, lines);
681
716
  for (i = 0; i < n; i++) {
682
- VALUE file = rb_profile_frame_absolute_path(frames[i]);
683
- VALUE name, label;
717
+ volatile VALUE file = rb_profile_frame_absolute_path(frames[i]);
718
+ VALUE name;
719
+ volatile VALUE label;
684
720
  if ( NIL_P(file) ) {
685
721
  /* code evaluated with a file name (class_eval in the library) and the
686
722
  core written in Ruby ("<internal:numeric>") have a path but no
@@ -716,7 +752,7 @@ ca_user_called_method_id (void)
716
752
  innermost C method is named, as before the walk. */
717
753
  label = rb_profile_frame_label(frames[i]);
718
754
  if ( ! NIL_P(label) && strncmp(StringValueCStr(label), "block ", 6) == 0 ) {
719
- VALUE base = rb_profile_frame_base_label(frames[i]);
755
+ volatile VALUE base = rb_profile_frame_base_label(frames[i]);
720
756
  if ( ! NIL_P(base) && RSTRING_LEN(base) > 0 && RSTRING_PTR(base)[0] == '<' ) {
721
757
  return 0;
722
758
  }
@@ -730,6 +766,17 @@ ca_user_called_method_id (void)
730
766
  return found;
731
767
  }
732
768
 
769
+ double
770
+ ca_tolerance_arg (VALUE tol, const char *name)
771
+ {
772
+ double t = NUM2DBL(tol);
773
+ if ( ! ( t >= 0.0 ) ) {
774
+ rb_raise(rb_eArgError, "%s: tol must be zero or positive (got %"PRIsVALUE")",
775
+ name, rb_inspect(tol));
776
+ }
777
+ return t;
778
+ }
779
+
733
780
  const char *
734
781
  ca_calling_method_name (void)
735
782
  {
@@ -776,29 +823,33 @@ ca_shape_elements (int8_t ndim, const ca_size_t *dim, ca_size_t bytes)
776
823
  }
777
824
  for ( i = 0; i < ndim; i++ ) {
778
825
  if ( __builtin_mul_overflow(elements, dim[i], &elements) ) {
779
- rb_raise(rb_eArgError, "too large byte length");
826
+ rb_raise(rb_eArgError, "%s: too large byte length",
827
+ ca_calling_method_name());
780
828
  }
781
829
  }
782
830
  if ( __builtin_mul_overflow(elements, (bytes > 0 ? bytes : 1), &total) ) {
783
- rb_raise(rb_eArgError, "too large byte length");
831
+ rb_raise(rb_eArgError, "%s: too large byte length",
832
+ ca_calling_method_name());
784
833
  }
785
834
  return elements;
786
835
  }
787
836
 
788
837
  /* An index array is read as ca_size_t. A float or complex one would be
789
- cut to an integer on the way, so it is refused, as the indexer refuses
790
- it. A uint64 one holding 2**63 or more would turn negative and count
791
- from the end of the axis, so it is refused as the out-of-range index it
792
- is. */
838
+ cut to an integer on the way, as an object one would, and a boolean one
839
+ selects cells rather than naming addresses, so these are refused. A
840
+ uint64 one holding 2**63 or more would turn negative and count from the
841
+ end of the axis, so it is refused as the out-of-range index it is. */
793
842
  void
794
- ca_check_index_array (VALUE v)
843
+ ca_check_index_array (volatile VALUE v)
795
844
  {
796
845
  CArray *ca;
797
846
  if ( ! rb_obj_is_carray(v) ) return;
798
847
  GetCArray(v, ca);
799
- if ( ca_is_float_type(ca) || ca_is_complex_type(ca) ) {
848
+ if ( ca_is_float_type(ca) || ca_is_complex_type(ca)
849
+ || ca->data_type == CA_OBJECT || ca->data_type == CA_BOOLEAN ) {
800
850
  rb_raise(rb_eIndexError,
801
- "data_type %s is invalid for an index (should be integer)",
851
+ "%s: data_type %s is invalid for an index (should be integer)",
852
+ ca_calling_method_name(),
802
853
  ca_type_name[ca->data_type]);
803
854
  }
804
855
  if ( ca->data_type != CA_UINT64 || ca->elements == 0 ) return;
@@ -806,11 +857,26 @@ ca_check_index_array (VALUE v)
806
857
  VALUE max = rb_funcall(v, rb_intern("max"), 0);
807
858
  if ( rb_obj_is_kind_of(max, rb_cInteger) &&
808
859
  RTEST(rb_funcall(max, rb_intern(">"), 1, LL2NUM(CA_LENGTH_MAX))) ) {
809
- rb_raise(rb_eIndexError, "index %"PRIsVALUE" is out of range", max);
860
+ rb_raise(rb_eIndexError, "%s: index %"PRIsVALUE" is out of range",
861
+ ca_calling_method_name(), max);
810
862
  }
811
863
  }
812
864
  }
813
865
 
866
+ int
867
+ ca_bool_arg (VALUE v, const char *arg)
868
+ {
869
+ if ( v == Qtrue ) {
870
+ return 1;
871
+ }
872
+ if ( v == Qfalse || NIL_P(v) ) {
873
+ return 0;
874
+ }
875
+ rb_raise(rb_eArgError, "%s: %s: must be true or false (got %"PRIsVALUE")",
876
+ ca_calling_method_name(), arg, rb_inspect(v));
877
+ return 0;
878
+ }
879
+
814
880
  long
815
881
  ca_integer_arg (VALUE v, const char *arg, const char *name)
816
882
  {
@@ -818,6 +884,17 @@ ca_integer_arg (VALUE v, const char *arg, const char *name)
818
884
  rb_raise(rb_eTypeError, "%s: %s must be an Integer (got %"PRIsVALUE")",
819
885
  name ? name : ca_calling_method_name(), arg, rb_obj_class(v));
820
886
  }
887
+ if ( RB_TYPE_P(v, T_BIGNUM) ) {
888
+ long out;
889
+ int sign = rb_integer_pack(v, &out, 1, sizeof(out), 0,
890
+ INTEGER_PACK_NATIVE_BYTE_ORDER |
891
+ INTEGER_PACK_2COMP);
892
+ if ( sign == 2 || sign == -2 ) {
893
+ rb_raise(rb_eRangeError, "%s: %s %"PRIsVALUE" is out of range",
894
+ name ? name : ca_calling_method_name(), arg, v);
895
+ }
896
+ return out;
897
+ }
821
898
  return NUM2LONG(v);
822
899
  }
823
900
 
@@ -825,8 +902,8 @@ void
825
902
  ca_index_out_of_range_at (ca_size_t idx, ca_size_t n, int axis)
826
903
  {
827
904
  rb_raise(rb_eIndexError,
828
- "%s: index %lld is out of range for length %lld on axis %d",
829
- ca_calling_method_name(), (long long) idx, (long long) n, axis);
905
+ "%s: index %"PRId64" is out of range for length %"PRId64" on axis %d",
906
+ ca_calling_method_name(), (int64_t) idx, (int64_t) n, axis);
830
907
  }
831
908
 
832
909
  void
@@ -844,8 +921,8 @@ ca_obj_compare_refused (VALUE a, VALUE b)
844
921
  void
845
922
  ca_index_out_of_range (ca_size_t idx, ca_size_t n, const char *what)
846
923
  {
847
- rb_raise(rb_eIndexError, "%s: %s %lld is out of range for length %lld",
848
- ca_calling_method_name(), what, (long long) idx, (long long) n);
924
+ rb_raise(rb_eIndexError, "%s: %s %"PRId64" is out of range for length %"PRId64,
925
+ ca_calling_method_name(), what, (int64_t) idx, (int64_t) n);
849
926
  }
850
927
 
851
928
  /* The min_count: keyword of a reduction: nil (not given) is -1, any other
@@ -915,26 +992,62 @@ ca_fill_as (VALUE fill, int8_t data_type)
915
992
  return rb_ca_fetch_addr(cell, 0);
916
993
  }
917
994
 
918
- /* The `fill_value:` of a reduction, applied to its result: the masked
919
- cells of a CArray result are filled as a store would fill them, each
920
- member of a two-array result (minmax) alike. `whole` says the result
921
- was computed with every axis kept for a full reduction; its one cell
922
- is then returned as the scalar the caller asked for. A caller passes
923
- no fill for `fill_value: UNDEF`, which leaves the cells undefined. */
924
- VALUE
925
- ca_reduce_fill (VALUE result, VALUE fill, int whole)
995
+ /* A fill_value for an integer answer: an Integer, or a Float with an
996
+ integer value (2.0), taken as that Integer. A value with a fraction, NaN
997
+ or an infinity is refused, as a store would otherwise cut it to an integer
998
+ without a word. Any other value is left to the store. */
999
+ static VALUE
1000
+ ca_integer_fill (VALUE fill, int8_t data_type, const char *name)
1001
+ {
1002
+ if ( RB_FLOAT_TYPE_P(fill) ) {
1003
+ double x = RFLOAT_VALUE(fill);
1004
+ if ( ! isfinite(x) || x != floor(x) ) {
1005
+ rb_raise(rb_eArgError,
1006
+ "%s: fill_value %"PRIsVALUE" is not an integer, and the answer is %s",
1007
+ name, rb_inspect(fill), ca_type_name[data_type]);
1008
+ }
1009
+ return rb_dbl2big(x);
1010
+ }
1011
+ if ( rb_obj_is_kind_of(fill, rb_cRational) ) {
1012
+ if ( rb_rational_den(fill) != INT2FIX(1) ) {
1013
+ rb_raise(rb_eArgError,
1014
+ "%s: fill_value %"PRIsVALUE" is not an integer, and the answer is %s",
1015
+ name, rb_inspect(fill), ca_type_name[data_type]);
1016
+ }
1017
+ return rb_rational_num(fill);
1018
+ }
1019
+ return fill;
1020
+ }
1021
+
1022
+ /* recursion: one level per dimension of a reduction's answer */
1023
+ static VALUE
1024
+ ca_reduce_fill_in (volatile VALUE result, VALUE fill, int whole, int widen, const char *name)
926
1025
  {
927
1026
  if ( RB_TYPE_P(result, T_ARRAY) ) {
928
1027
  long i, n = RARRAY_LEN(result);
929
1028
  VALUE out = rb_ary_new_capa(n);
930
1029
  for ( i = 0; i < n; i++ ) {
931
- rb_ary_push(out, ca_reduce_fill(RARRAY_AREF(result, i), fill, whole));
1030
+ rb_ary_push(out, ca_reduce_fill_in(RARRAY_AREF(result, i), fill, whole, widen, name));
932
1031
  }
933
1032
  return out;
934
1033
  }
935
1034
  if ( rb_obj_is_kind_of(result, rb_cCArray) ) {
936
1035
  CArray *cr;
937
1036
  GetCArray(result, cr);
1037
+ if ( ca_is_integer_type(cr) && ! ca_is_face(cr) ) {
1038
+ fill = ca_integer_fill(fill, cr->data_type, name);
1039
+ /* A sum or a product answers in float64 where its total is out of the
1040
+ integer range; a fill out of that range is such a total, so the
1041
+ answer becomes float64 when a cell takes it. */
1042
+ if ( widen && ca_integer_range_side(fill, cr->data_type) != 0 ) {
1043
+ if ( ca_has_mask(cr) && ca_is_any_masked(cr) ) {
1044
+ result = rb_ca_mask_fill_copy(rb_ca_wrap_readonly(result,
1045
+ INT2NUM(CA_FLOAT64)),
1046
+ rb_Float(fill));
1047
+ }
1048
+ return whole ? rb_ca_fetch_addr(result, 0) : result;
1049
+ }
1050
+ }
938
1051
  if ( ca_has_mask(cr) ) {
939
1052
  result = rb_ca_mask_fill_copy(result, fill);
940
1053
  }
@@ -942,9 +1055,8 @@ ca_reduce_fill (VALUE result, VALUE fill, int whole)
942
1055
  /* No cell needed the fill, but a fill the result cannot hold is
943
1056
  refused all the same, not only when some cell happens to be
944
1057
  undefined: it goes into a one-cell array of the result's kind. */
945
- volatile VALUE one = rb_funcall(rb_funcall(result, rb_intern("flatten"), 0),
946
- rb_intern("[]"), 1,
947
- rb_range_new(INT2FIX(0), INT2FIX(0), 0));
1058
+ volatile VALUE one = rb_ca_fetch(rb_ca_flatten(result),
1059
+ rb_range_new(INT2FIX(0), INT2FIX(0), 0));
948
1060
  rb_ca_store_addr(rb_ca_template(one), 0, fill);
949
1061
  }
950
1062
  return whole ? rb_ca_fetch_addr(result, 0) : result;
@@ -952,16 +1064,50 @@ ca_reduce_fill (VALUE result, VALUE fill, int whole)
952
1064
  return ( result == CA_UNDEF ) ? fill : result;
953
1065
  }
954
1066
 
955
- /* CArray.__fill_undefined_result_cells__(result, fill_value): ca_reduce_fill for a
956
- reduction written in Ruby. A fill_value of nil or UNDEF leaves the
957
- undefined cells as they are. */
1067
+ /* The `fill_value:` of a reduction, applied to its result: the masked
1068
+ cells of a CArray result are filled as a store would fill them, each
1069
+ member of a two-array result (minmax) alike. `whole` says the result
1070
+ was computed with every axis kept for a full reduction; its one cell
1071
+ is then returned as the scalar the caller asked for. A caller passes
1072
+ no fill for `fill_value: UNDEF`, which leaves the cells undefined. An
1073
+ integer result refuses a fill with a fraction (ca_integer_fill). */
1074
+ VALUE
1075
+ ca_reduce_fill (VALUE result, VALUE fill, int whole)
1076
+ {
1077
+ return ca_reduce_fill_in(result, fill, whole, 0, ca_calling_method_name());
1078
+ }
1079
+
1080
+ /* ca_reduce_fill for a sum or a product: an integer answer takes a fill out
1081
+ of its range by becoming float64, where a cell takes the fill. */
1082
+ VALUE
1083
+ ca_reduce_fill_widening (VALUE result, VALUE fill, int whole, const char *name)
1084
+ {
1085
+ return ca_reduce_fill_in(result, fill, whole, 1, name);
1086
+ }
1087
+
1088
+ /* CArray.__fill_undefined_result_cells__(result, fill_value, op = nil):
1089
+ ca_reduce_fill for a reduction written in Ruby. A fill_value of nil or
1090
+ UNDEF leaves the undefined cells as they are. `op` names the reduction
1091
+ in a refusal, and :sum or :prod lets an integer answer widen to float64
1092
+ for a fill out of its range (ca_reduce_fill_widening). */
958
1093
  static VALUE
959
- rb_ca_s_reduce_fill (VALUE klass, VALUE result, VALUE fill)
1094
+ rb_ca_s_reduce_fill (int argc, VALUE *argv, VALUE klass)
960
1095
  {
1096
+ VALUE result, fill, op;
1097
+ char name[64];
1098
+ rb_scan_args(argc, argv, "21", &result, &fill, &op);
961
1099
  if ( NIL_P(fill) || fill == CA_UNDEF ) {
962
1100
  return result;
963
1101
  }
964
- return ca_reduce_fill(result, fill, 0);
1102
+ if ( NIL_P(op) ) {
1103
+ return ca_reduce_fill(result, fill, 0);
1104
+ }
1105
+ Check_Type(op, T_SYMBOL);
1106
+ /* name: copied out at once, as the fill runs Ruby before a refusal reads it */
1107
+ snprintf(name, sizeof(name), "%s", rb_id2name(SYM2ID(op)));
1108
+ return ca_reduce_fill_in(result, fill, 0,
1109
+ op == ID2SYM(rb_intern("sum")) || op == ID2SYM(rb_intern("prod")),
1110
+ name);
965
1111
  }
966
1112
 
967
1113
  /* ca_integer_arg for an `axis` argument. */
@@ -1002,7 +1148,7 @@ void
1002
1148
  ca_check_rank_fits (long ndim, const char *name)
1003
1149
  {
1004
1150
  if ( ndim > CA_RANK_MAX ) {
1005
- rb_raise(rb_eArgError, "%s: ndim %ld exceeds CA_RANK_MAX %d",
1151
+ rb_raise(rb_eArgError, "%s: ndim %ld exceeds CArray::RANK_MAX %d",
1006
1152
  name ? name : ca_calling_method_name(), ndim, CA_RANK_MAX);
1007
1153
  }
1008
1154
  }
@@ -1014,7 +1160,7 @@ ca_check_rank_fits (long ndim, const char *name)
1014
1160
  too, since no ndim reaches it. `name` and the insertion-position use of
1015
1161
  `ndim` are as for rb_ca_normalize_axis_for_ndim. */
1016
1162
  int
1017
- ca_normalize_axis_arg (VALUE raxis, int ndim, const char *name)
1163
+ rb_ca_normalize_axis_arg (VALUE raxis, int ndim, const char *name)
1018
1164
  {
1019
1165
  ca_check_rank_fits(ndim, name); /* no array has the axes to name */
1020
1166
  if ( RB_INTEGER_TYPE_P(raxis) && ! RB_FIXNUM_P(raxis) ) {
@@ -1029,11 +1175,11 @@ ca_normalize_axis_arg (VALUE raxis, int ndim, const char *name)
1029
1175
  for backward compatibility with the existing instance method
1030
1176
  `CArray#normalize_axis`. */
1031
1177
  int
1032
- rb_ca_normalize_axis_value (VALUE self, VALUE raxis, const char *name)
1178
+ rb_ca_normalize_axis_value (volatile VALUE self, VALUE raxis, const char *name)
1033
1179
  {
1034
1180
  CArray *ca;
1035
1181
  GetCArray(self, ca);
1036
- return ca_normalize_axis_arg(raxis, (int) ca->ndim, name);
1182
+ return rb_ca_normalize_axis_arg(raxis, (int) ca->ndim, name);
1037
1183
  }
1038
1184
 
1039
1185
  /* Ruby-facing `CArray#normalize_axis(axis, name=nil)` — returns the
@@ -1044,10 +1190,12 @@ rb_ca_normalize_axis_value (VALUE self, VALUE raxis, const char *name)
1044
1190
  static VALUE
1045
1191
  rb_ca_normalize_axis (int argc, VALUE *argv, VALUE self)
1046
1192
  {
1047
- VALUE raxis, rname;
1193
+ VALUE raxis, rname_arg;
1194
+ volatile VALUE rname;
1048
1195
  const char *name;
1049
1196
  int k;
1050
- rb_scan_args(argc, argv, "11", &raxis, &rname);
1197
+ rb_scan_args(argc, argv, "11", &raxis, &rname_arg);
1198
+ rname = rname_arg;
1051
1199
  name = NIL_P(rname) ? NULL : StringValueCStr(rname);
1052
1200
  k = rb_ca_normalize_axis_value(self, raxis, name);
1053
1201
  return INT2NUM(k);
@@ -1063,12 +1211,14 @@ rb_ca_normalize_axis (int argc, VALUE *argv, VALUE self)
1063
1211
  static VALUE
1064
1212
  rb_ca_s_normalize_axis (int argc, VALUE *argv, VALUE klass)
1065
1213
  {
1066
- VALUE raxis, rndim, rname;
1214
+ VALUE raxis, rndim, rname_arg;
1215
+ volatile VALUE rname;
1067
1216
  const char *name;
1068
1217
  int k;
1069
- rb_scan_args(argc, argv, "21", &raxis, &rndim, &rname);
1218
+ rb_scan_args(argc, argv, "21", &raxis, &rndim, &rname_arg);
1219
+ rname = rname_arg;
1070
1220
  name = NIL_P(rname) ? NULL : StringValueCStr(rname);
1071
- k = ca_normalize_axis_arg(raxis, NUM2INT(rndim), name);
1221
+ k = rb_ca_normalize_axis_arg(raxis, NUM2INT(rndim), name);
1072
1222
  return INT2NUM(k);
1073
1223
  }
1074
1224
 
@@ -1079,15 +1229,17 @@ rb_ca_s_normalize_axis (int argc, VALUE *argv, VALUE klass)
1079
1229
  * normalization), or an Array<Integer> (each element normalized).
1080
1230
  * Raises on out-of-range or duplicate axes. */
1081
1231
  static VALUE
1082
- rb_ca_normalize_axes (int argc, VALUE *argv, VALUE self)
1232
+ rb_ca_normalize_axes (int argc, VALUE *argv, volatile VALUE self)
1083
1233
  {
1084
- VALUE raxes, rname, out;
1234
+ VALUE raxes, rname_arg, out;
1235
+ volatile VALUE rname;
1085
1236
  CArray *ca;
1086
1237
  int8_t axes_buf[CA_RANK_MAX];
1087
1238
  int8_t naxes, i;
1088
1239
  const char *name;
1089
1240
 
1090
- rb_scan_args(argc, argv, "11", &raxes, &rname);
1241
+ rb_scan_args(argc, argv, "11", &raxes, &rname_arg);
1242
+ rname = rname_arg;
1091
1243
  name = NIL_P(rname) ? NULL : StringValueCStr(rname); /* NULL: looked up on error */
1092
1244
  GetCArray(self, ca);
1093
1245
 
@@ -1128,22 +1280,16 @@ static const struct {
1128
1280
  { "cmplx128", CA_CMPLX128 },
1129
1281
  { "cmplx256", CA_CMPLX256 }, /* reserved (retired in 3.0); ca_valid[15]=0 */
1130
1282
  { "object", CA_OBJECT },
1131
- { "byte", CA_UINT8 },
1132
- { "short", CA_INT16 },
1133
- { "int", CA_INT32 },
1134
- { "float", CA_FLOAT32 },
1135
- { "double", CA_FLOAT64 },
1136
- { "complex", CA_CMPLX64 },
1137
- { "dcomplex", CA_CMPLX128 },
1138
1283
  { NULL, CA_NONE },
1139
1284
  };
1140
1285
 
1141
1286
 
1142
1287
 
1288
+ /* recursion: re-enters once, with a Symbol as a String */
1143
1289
  int8_t
1144
- rb_ca_guess_type (VALUE obj)
1290
+ rb_ca_guess_type (volatile VALUE obj)
1145
1291
  {
1146
- VALUE inspect;
1292
+ volatile VALUE inspect;
1147
1293
 
1148
1294
  if ( TYPE(obj) == T_FIXNUM ) {
1149
1295
  /* Check before narrowing to int8_t, so the message names the id given. */
@@ -1156,12 +1302,12 @@ rb_ca_guess_type (VALUE obj)
1156
1302
  }
1157
1303
  else if ( TYPE(obj) == T_STRING ) {
1158
1304
  const char *name0;
1159
- char *name = StringValuePtr(obj);
1305
+ const char *name = StringValueCStr(obj);
1160
1306
  int i;
1161
1307
  i = 0;
1162
1308
  while ( ( name0 = ca_name_to_type[i].name ) ) {
1163
1309
  name0 = ca_name_to_type[i].name;
1164
- if ( ! strncmp(name, name0, strlen(name0)) ) {
1310
+ if ( ! strcmp(name, name0) ) {
1165
1311
  return ca_name_to_type[i].data_type;
1166
1312
  }
1167
1313
  i++;
@@ -1181,6 +1327,28 @@ rb_ca_guess_type (VALUE obj)
1181
1327
  ca_calling_method_name(), StringValuePtr(inspect));
1182
1328
  }
1183
1329
 
1330
+ /* A data_type given from Ruby is named (a Symbol, a String or a data
1331
+ class); the number behind it is the C enum, which C code passes and Ruby
1332
+ does not. Called by the methods that take a data_type from Ruby, not by
1333
+ rb_ca_guess_type, which C callers reach with a number. */
1334
+ void
1335
+ ca_refuse_type_number (VALUE rtype)
1336
+ {
1337
+ if ( ! RB_INTEGER_TYPE_P(rtype) ) {
1338
+ return;
1339
+ }
1340
+ if ( FIXNUM_P(rtype) && FIX2LONG(rtype) > CA_NONE &&
1341
+ FIX2LONG(rtype) < CA_NTYPE && ca_valid[FIX2LONG(rtype)] ) {
1342
+ rb_raise(rb_eArgError,
1343
+ "%s: data_type %"PRIsVALUE" is a type number; name the type (%"PRIsVALUE")",
1344
+ ca_calling_method_name(), rtype,
1345
+ rb_inspect(ID2SYM(ca_data_type_sym[FIX2LONG(rtype)])));
1346
+ }
1347
+ rb_raise(rb_eArgError,
1348
+ "%s: data_type %"PRIsVALUE" is a number; name the type (a Symbol such as :int32)",
1349
+ ca_calling_method_name(), rtype);
1350
+ }
1351
+
1184
1352
  void
1185
1353
  rb_ca_guess_type_and_bytes (VALUE rtype, VALUE rbytes,
1186
1354
  int8_t *data_type, ca_size_t *bytes)
@@ -1196,13 +1364,24 @@ rb_ca_guess_type_and_bytes (VALUE rtype, VALUE rbytes,
1196
1364
  *bytes = 0;
1197
1365
  }
1198
1366
  else {
1199
- *bytes = NUM2SIZE(rbytes);
1367
+ *bytes = ca_integer_arg(rbytes, "bytes", NULL);
1200
1368
  }
1201
1369
  }
1202
1370
  }
1203
1371
  else {
1204
- CA_CHECK_DATA_TYPE_GIVEN(*data_type);
1372
+ CA_CHECK_DATA_TYPE(*data_type);
1205
1373
  *bytes = ca_sizeof[*data_type];
1374
+ /* A width is chosen only for :fixlen; another type has its own. 0
1375
+ (the internal callers' "none") and the type's own width are no
1376
+ choice and are taken. */
1377
+ if ( ! NIL_P(rbytes) ) {
1378
+ ca_size_t given = ca_integer_arg(rbytes, "bytes", NULL);
1379
+ if ( given != 0 && given != *bytes ) {
1380
+ rb_raise(rb_eArgError, "%s: bytes: is for :fixlen, not %"PRIsVALUE,
1381
+ ca_calling_method_name(),
1382
+ rb_inspect(ID2SYM(ca_data_type_sym[*data_type])));
1383
+ }
1384
+ }
1206
1385
  }
1207
1386
  }
1208
1387
 
@@ -1217,9 +1396,9 @@ rb_ca_s_guess_type_and_bytes (int argc, VALUE *argv, VALUE klass)
1217
1396
  VALUE rtype, rbytes;
1218
1397
  int8_t data_type;
1219
1398
  ca_size_t bytes;
1220
- rb_scan_args(argc, argv, "11", (VALUE *) &rtype, (VALUE *) &rbytes);
1399
+ rb_scan_args(argc, argv, "11", &rtype, &rbytes);
1221
1400
  rb_ca_guess_type_and_bytes(rtype, rbytes, &data_type, &bytes);
1222
- return rb_assoc_new(INT2NUM(data_type), SIZE2NUM(bytes));
1401
+ return rb_ary_new_from_args(2, INT2NUM(data_type), SIZE2NUM(bytes));
1223
1402
  }
1224
1403
 
1225
1404
  /* Takes the trailing keyword Hash off argv when the caller passed keywords.
@@ -1315,7 +1494,7 @@ rb_scan_options (VALUE ropt, const char *spec_in, ...)
1315
1494
  has_option = 1;
1316
1495
  }
1317
1496
  else if ( ! NIL_P(ropt) ) {
1318
- VALUE inspect = rb_inspect(ropt);
1497
+ volatile VALUE inspect = rb_inspect(ropt);
1319
1498
  rb_raise(rb_eArgError,
1320
1499
  "<%s> is invalid option specifier",
1321
1500
  StringValuePtr(inspect));
@@ -1345,13 +1524,16 @@ rb_scan_options (VALUE ropt, const char *spec_in, ...)
1345
1524
  long i, n = RARRAY_LEN(keys);
1346
1525
  for ( i = 0; i < n; i++ ) {
1347
1526
  VALUE key = rb_ary_entry(keys, i);
1348
- VALUE kname = SYMBOL_P(key) ? rb_sym2str(key) : rb_obj_as_string(key);
1349
- const char *kn = StringValueCStr(kname);
1350
- long kn_len = RSTRING_LEN(kname);
1351
1527
  int found = 0;
1352
- sp = spec_in;
1353
- while ( ( tok = option_key(&sp, &len) ) != NULL ) {
1354
- if ( len == kn_len && memcmp(tok, kn, len) == 0 ) { found = 1; break; }
1528
+ /* A value is read by Symbol above, so only a Symbol can name a key. */
1529
+ if ( SYMBOL_P(key) ) {
1530
+ volatile VALUE kname = rb_sym2str(key);
1531
+ const char *kn = StringValueCStr(kname);
1532
+ long kn_len = RSTRING_LEN(kname);
1533
+ sp = spec_in;
1534
+ while ( ( tok = option_key(&sp, &len) ) != NULL ) {
1535
+ if ( len == kn_len && memcmp(tok, kn, len) == 0 ) { found = 1; break; }
1536
+ }
1355
1537
  }
1356
1538
  if ( ! found ) {
1357
1539
  rb_raise(rb_eArgError, "unknown keyword: %"PRIsVALUE, rb_inspect(key));
@@ -1394,7 +1576,7 @@ rb_set_options (VALUE ropt, const char *spec_in, ...)
1394
1576
  has_option = 1;
1395
1577
  }
1396
1578
  else if ( ! NIL_P(ropt) ) {
1397
- VALUE inspect = rb_inspect(ropt);
1579
+ volatile VALUE inspect = rb_inspect(ropt);
1398
1580
  rb_raise(rb_eArgError,
1399
1581
  "<%s> is invalid option specifier",
1400
1582
  StringValuePtr(inspect));
@@ -1414,6 +1596,57 @@ rb_set_options (VALUE ropt, const char *spec_in, ...)
1414
1596
  }
1415
1597
 
1416
1598
 
1599
+ /* RubyVM::InstructionSequence#to_a, one caller at a time across Ractors.
1600
+ The first to_a on an instruction sequence builds a decoded copy of its
1601
+ code and publishes the buffer before filling it, so a second Ractor
1602
+ that reads the same sequence at that moment decodes raw addresses and
1603
+ crashes the process (Ruby 3.4 and 4.0; fixed for 4.1). A block written
1604
+ inside Ractor.new is one sequence shared by every Ractor started from
1605
+ it. The wait for the lock is made without the GVL, so a Ractor that
1606
+ holds the lock and starts a GC is not left waiting on one that wants
1607
+ it. */
1608
+ static rb_nativethread_lock_t ca_iseq_to_a_lock;
1609
+
1610
+ static VALUE
1611
+ ca_iseq_to_a_call (VALUE iseq)
1612
+ {
1613
+ return rb_funcall(iseq, rb_intern("to_a"), 0);
1614
+ }
1615
+
1616
+ static VALUE
1617
+ rb_ca_s_instruction_sequence_to_a (VALUE self, VALUE iseq)
1618
+ {
1619
+ VALUE result;
1620
+ int state = 0;
1621
+ /* lock: Ruby runs under it, to serialise to_a; every waiter waits without the GVL */
1622
+ ca_native_lock_without_gvl(&ca_iseq_to_a_lock);
1623
+ /* protect: what is held is the lock, not an array or a buffer */
1624
+ result = rb_protect(ca_iseq_to_a_call, iseq, &state);
1625
+ rb_nativethread_lock_unlock(&ca_iseq_to_a_lock);
1626
+ if ( state ) {
1627
+ rb_jump_tag(state);
1628
+ }
1629
+ return result;
1630
+ }
1631
+
1632
+ /* A short pause between tries for a lock, made without the GVL. */
1633
+ static void *
1634
+ ca_native_lock_pause (void *arg)
1635
+ {
1636
+ struct timespec t = { 0, 20000 }; /* 20 us */
1637
+ nanosleep(&t, NULL);
1638
+ return NULL;
1639
+ }
1640
+
1641
+ void
1642
+ ca_native_lock_without_gvl (rb_nativethread_lock_t *lock)
1643
+ {
1644
+ /* lock: the pause below runs while the lock is not held */
1645
+ while ( rb_native_mutex_trylock(lock) != 0 ) {
1646
+ rb_thread_call_without_gvl(ca_native_lock_pause, NULL, RUBY_UBF_IO, NULL);
1647
+ }
1648
+ }
1649
+
1417
1650
  /* Whether this runs in the main Ractor: Init does, and leaves a value in
1418
1651
  its Ractor-local storage that no other Ractor has. */
1419
1652
  static rb_ractor_local_key_t ca_main_ractor_key;
@@ -1432,11 +1665,12 @@ Init_carray_utils (void)
1432
1665
  rb_ractor_local_storage_value_set(ca_main_ractor_key, Qtrue);
1433
1666
  ca_id_names = st_init_numtable();
1434
1667
  rb_nativethread_lock_initialize(&ca_id_names_lock);
1668
+ rb_nativethread_lock_initialize(&ca_iseq_to_a_lock);
1435
1669
  id_answering = rb_intern("__carray_answering_method__");
1436
- id_begin = rb_intern("begin");
1437
- id_end = rb_intern("end");
1438
- id_excl_end = rb_intern("exclude_end?");
1439
1670
 
1671
+ rb_define_private_method(rb_singleton_class(rb_cCArray),
1672
+ "instruction_sequence_to_a",
1673
+ rb_ca_s_instruction_sequence_to_a, 1);
1440
1674
  rb_define_singleton_method(rb_cCArray, "guess_type_and_bytes",
1441
1675
  rb_ca_s_guess_type_and_bytes, -1);
1442
1676
 
@@ -1447,7 +1681,7 @@ Init_carray_utils (void)
1447
1681
  rb_define_singleton_method(rb_cCArray, "__present_cells_needed_per_result_cell__",
1448
1682
  rb_ca_s_min_count_need, 3);
1449
1683
  rb_define_singleton_method(rb_cCArray, "__fill_undefined_result_cells__",
1450
- rb_ca_s_reduce_fill, 2);
1684
+ rb_ca_s_reduce_fill, -1);
1451
1685
  rb_define_private_method(rb_singleton_class(rb_cCArray), "library_root=",
1452
1686
  rb_ca_s_set_library_root, 1);
1453
1687
  rb_define_singleton_method(rb_cCArray, "__name_errors_after_method__",