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/ca_obj_tile.c CHANGED
@@ -16,7 +16,6 @@
16
16
  ---------------------------------------------------------------------------- */
17
17
 
18
18
  #include "carray.h"
19
- #include "carray_internal.h"
20
19
  #include "ca_composite_dispatch.h"
21
20
  #include "ca_obj_face.h" /* CA_FACE_LIFT_IF_FACE */
22
21
 
@@ -46,7 +45,7 @@ const rb_data_type_t catile_data_type = {
46
45
 
47
46
  const rb_data_type_t catile_mask_data_type = {
48
47
  .parent = &catile_data_type,
49
- .wrap_struct_name = "CATileMask",
48
+ .wrap_struct_name = "CATile::Mask",
50
49
  .function = {
51
50
  .dmark = ca_mark,
52
51
  .dfree = ca_free_nop,
@@ -56,7 +55,7 @@ const rb_data_type_t catile_mask_data_type = {
56
55
  .flags = RUBY_TYPED_FREE_IMMEDIATELY
57
56
  };
58
57
 
59
- int8_t CA_OBJ_TILE;
58
+ int16_t CA_OBJ_TILE;
60
59
 
61
60
  VALUE rb_cCATile;
62
61
  VALUE rb_cCATileMask;
@@ -422,16 +421,14 @@ static void
422
421
  ca_tile_func_allocate (void *ap)
423
422
  {
424
423
  CATile *ca = (CATile *) ap;
425
- ca_attach(ca->parent);
426
- ca->ptr = ca_alloc_cells((CArray *) ca);
424
+ ca->ptr = ca_alloc_cells_then_attach((CArray *) ca, 1, &ca->parent);
427
425
  }
428
426
 
429
427
  static void
430
428
  ca_tile_func_attach (void *ap)
431
429
  {
432
430
  CATile *ca = (CATile *) ap;
433
- ca_attach(ca->parent);
434
- ca->ptr = ca_alloc_cells((CArray *) ca);
431
+ ca->ptr = ca_alloc_cells_then_attach((CArray *) ca, 1, &ca->parent);
435
432
  ca_tile_attach_into(ca, ca->ptr);
436
433
  }
437
434
 
@@ -527,13 +524,13 @@ ca_operation_function_t ca_tile_func = {
527
524
  /* ------------------------------------------------------------------- */
528
525
 
529
526
  VALUE
530
- rb_ca_tile_new (VALUE cary, ca_size_t *reps)
527
+ rb_ca_tile_new (volatile VALUE cary, ca_size_t *reps)
531
528
  {
532
529
  volatile VALUE obj;
533
530
  CArray *parent;
534
531
  CATile *ca;
535
532
  rb_check_carray_object(cary);
536
- TypedData_Get_Struct(cary, CArray, &carray_data_type, parent);
533
+ GetCArray(cary, parent);
537
534
  ca = ca_tile_new(parent, reps);
538
535
  obj = ca_wrap_struct(ca);
539
536
  rb_ca_set_parent(obj, cary);
@@ -550,13 +547,14 @@ rb_ca_tile_new (VALUE cary, ca_size_t *reps)
550
547
  a.tile([2, 3]) # 2D, OK (Array form, single arg)
551
548
  a.tile(3) # 2D, raises ArgumentError (argc != ndim) */
552
549
  static VALUE
553
- rb_ca_tile (int argc, VALUE *argv, VALUE self)
550
+ rb_ca_tile (int argc, VALUE *argv, volatile VALUE self)
554
551
  {
555
552
  CArray *ca;
556
553
  ca_size_t reps[CA_RANK_MAX];
557
554
  int8_t i;
558
555
 
559
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
556
+ rb_reject_options(rb_pop_options(&argc, &argv));
557
+ GetCArray(self, ca);
560
558
 
561
559
  /* Single-arg Array form: a.tile([2, 3]) */
562
560
  if ( argc == 1 && rb_obj_is_kind_of(argv[0], rb_cArray) ) {
@@ -598,7 +596,7 @@ rb_ca_tile_s_allocate (VALUE klass)
598
596
  }
599
597
 
600
598
  static VALUE
601
- rb_ca_tile_initialize_copy (VALUE self, VALUE other)
599
+ rb_ca_tile_initialize_copy (volatile VALUE self, volatile VALUE other)
602
600
  {
603
601
  CATile *ca, *cs;
604
602
  TypedData_Get_Struct(self, CATile, &catile_data_type, ca);
@@ -613,12 +611,13 @@ rb_ca_tile_initialize_copy (VALUE self, VALUE other)
613
611
  /* Debug-only accessor returning the tile descriptor as a Hash.
614
612
  Naming: leading underscore signals internal use. */
615
613
  static VALUE
616
- rb_ca_tile_descriptor (VALUE self)
614
+ rb_ca_tile_descriptor (volatile VALUE self)
617
615
  {
618
- CATile *ca = (CATile *) DATA_PTR(self);
616
+ CATile *ca;
619
617
  VALUE hash, reps_ary;
620
618
  int8_t k;
621
619
  ca_size_t total = 1;
620
+ TypedData_Get_Struct(self, CATile, &catile_data_type, ca);
622
621
 
623
622
  reps_ary = rb_ary_new_capa(ca->ndim);
624
623
  for ( k = 0; k < ca->ndim; k++ ) {
@@ -637,14 +636,13 @@ void
637
636
  Init_ca_obj_tile (void)
638
637
  {
639
638
  rb_cCATile = rb_define_class("CATile", rb_cCAView);
640
- rb_cCATileMask = rb_define_class("CATileMask", rb_cCATile);
639
+ rb_cCATileMask = rb_define_class_under(rb_cCATile, "Mask", rb_cCATile);
641
640
 
642
641
  CA_OBJ_TILE = ca_install_obj_type(rb_cCATile,
643
642
  &catile_data_type,
644
643
  rb_cCATileMask,
645
644
  &catile_mask_data_type, &ca_tile_func, sizeof(ca_tile_func));
646
645
  ca_register_cell_moving_view(CA_OBJ_TILE);
647
- rb_define_const(rb_cObject, "CA_OBJ_TILE", INT2NUM(CA_OBJ_TILE));
648
646
 
649
647
  rb_define_method(rb_cCArray, "tile", rb_ca_tile, -1);
650
648
 
data/ext/ca_obj_time.c CHANGED
@@ -14,7 +14,6 @@
14
14
  --------------------------------------------------------------------------- */
15
15
 
16
16
  #include "carray.h"
17
- #include "carray_internal.h"
18
17
  #include "ca_obj_face.h"
19
18
 
20
19
  /* unit enum (base epoch is 1970-01-01) */
@@ -36,16 +35,7 @@ enum {
36
35
 
37
36
  typedef struct {
38
37
  /* === CAView prefix === */
39
- int16_t obj_type;
40
- int8_t data_type; /* surface: CA_FIXLEN (storage is the int64 parent) */
41
- int8_t ndim;
42
- int32_t flags; /* CA_FLAG_IS_FACE set */
43
- ca_size_t bytes; /* fixed to sizeof(int64_t) */
44
- ca_size_t elements;
45
- ca_size_t *dim;
46
- char *ptr;
47
- CArray *mask;
48
- char *_pool; /* framework-managed pool buffer (NULL = legacy ALLOC_N path). */
38
+ CA_ARRAY_HEADER
49
39
  CArray *parent;
50
40
  uint32_t attach;
51
41
  uint8_t nosync;
@@ -91,7 +81,7 @@ const rb_data_type_t catime_data_type = {
91
81
  .flags = RUBY_TYPED_FREE_IMMEDIATELY
92
82
  };
93
83
 
94
- static int8_t CA_OBJ_TIME;
84
+ static int16_t CA_OBJ_TIME;
95
85
  static VALUE rb_cCATime;
96
86
  static VALUE rb_cCATimeElement;
97
87
 
@@ -144,7 +134,7 @@ rb_ca_time_element_s_allocate (VALUE klass)
144
134
  }
145
135
 
146
136
  static VALUE
147
- rb_ca_time_element_initialize (VALUE self, VALUE value, VALUE unit)
137
+ rb_ca_time_element_initialize (volatile VALUE self, VALUE value, VALUE unit)
148
138
  {
149
139
  CATimeElement *s;
150
140
  TypedData_Get_Struct(self, CATimeElement,
@@ -156,7 +146,7 @@ rb_ca_time_element_initialize (VALUE self, VALUE value, VALUE unit)
156
146
 
157
147
  /* dup / clone copy the value and the unit (the allocator leaves both 0). */
158
148
  static VALUE
159
- rb_ca_time_element_initialize_copy (VALUE self, VALUE orig)
149
+ rb_ca_time_element_initialize_copy (volatile VALUE self, volatile VALUE orig)
160
150
  {
161
151
  CATimeElement *s, *o;
162
152
  if ( self == orig ) {
@@ -170,7 +160,7 @@ rb_ca_time_element_initialize_copy (VALUE self, VALUE orig)
170
160
  }
171
161
 
172
162
  static VALUE
173
- rb_ca_time_element_value (VALUE self)
163
+ rb_ca_time_element_value (volatile VALUE self)
174
164
  {
175
165
  CATimeElement *s;
176
166
  TypedData_Get_Struct(self, CATimeElement,
@@ -180,7 +170,7 @@ rb_ca_time_element_value (VALUE self)
180
170
 
181
171
  /* unit → CATime::Resolution(count, base) */
182
172
  static VALUE
183
- rb_ca_time_element_unit (VALUE self)
173
+ rb_ca_time_element_unit (volatile VALUE self)
184
174
  {
185
175
  CATimeElement *s;
186
176
  TypedData_Get_Struct(self, CATimeElement,
@@ -396,14 +386,14 @@ ca_time_extract_unit (VALUE unit, int8_t *base, int64_t *count)
396
386
  The public unit-spec normalization (Symbol / String / Resolution) is done
397
387
  on the Ruby side; this primitive takes the resolved base enum + count. */
398
388
  VALUE
399
- rb_ca_time_wrap (VALUE parent_val, int8_t unit, int64_t count)
389
+ rb_ca_time_wrap (volatile VALUE parent_val, int8_t unit, int64_t count)
400
390
  {
401
391
  volatile VALUE obj;
402
392
  CArray *parent;
403
393
  CATime *ca;
404
394
 
405
395
  rb_check_carray_object(parent_val);
406
- TypedData_Get_Struct(parent_val, CArray, &carray_data_type, parent);
396
+ GetCArray(parent_val, parent);
407
397
 
408
398
  ca = ca_time_new(parent, unit, count);
409
399
  obj = ca_wrap_struct(ca);
@@ -425,7 +415,7 @@ rb_ca_time_wrap_method (VALUE klass, VALUE raw, VALUE unit_spec)
425
415
 
426
416
  /* CATime#unit → CATime::Resolution(count, base) */
427
417
  static VALUE
428
- rb_ca_time_unit (VALUE self)
418
+ rb_ca_time_unit (volatile VALUE self)
429
419
  {
430
420
  CATime *ca;
431
421
  TypedData_Get_Struct(self, CATime, &catime_data_type, ca);
@@ -441,7 +431,7 @@ rb_ca_time_unit (VALUE self)
441
431
  String. Decode it as the parent int64 (= storage), then construct
442
432
  CATime::Element(epoch, unit). */
443
433
  static VALUE
444
- rb_ca_time_storage_to_element (VALUE self, VALUE raw)
434
+ rb_ca_time_storage_to_element (volatile VALUE self, volatile VALUE raw)
445
435
  {
446
436
  CATime *ca;
447
437
  CATimeElement *s;
@@ -482,7 +472,7 @@ rb_ca_time_s_allocate (VALUE klass)
482
472
  }
483
473
 
484
474
  static VALUE
485
- rb_ca_time_initialize_copy (VALUE self, VALUE other)
475
+ rb_ca_time_initialize_copy (volatile VALUE self, volatile VALUE other)
486
476
  {
487
477
  CATime *ca, *cs;
488
478
  TypedData_Get_Struct(self, CATime, &catime_data_type, ca);
@@ -520,7 +510,6 @@ Init_ca_obj_time (void)
520
510
  rb_cCArrayMask,
521
511
  &carray_mask_data_type,
522
512
  &ca_time_func, sizeof(ca_time_func));
523
- rb_define_const(rb_cObject, "CA_OBJ_TIME", INT2NUM(CA_OBJ_TIME));
524
513
 
525
514
  rb_define_alloc_func(rb_cCATime, rb_ca_time_s_allocate);
526
515
  rb_define_method(rb_cCATime, "initialize_copy",
@@ -529,7 +518,7 @@ Init_ca_obj_time (void)
529
518
  rb_define_private_method(rb_singleton_class(rb_cCATime), "wrap_storage",
530
519
  rb_ca_time_wrap_method, 2);
531
520
  rb_define_method(rb_cCATime, "unit", rb_ca_time_unit, 0);
532
- rb_define_method(rb_cCATime, "storage_to_element",
521
+ rb_define_private_method(rb_cCATime, "storage_to_element",
533
522
  rb_ca_time_storage_to_element, 1);
534
523
  /* Registers the C-level fast path so CA_FACE_STORAGE_TO_ELEMENT_IF_FACE can
535
524
  skip rb_funcall on the hot path. */
@@ -14,7 +14,6 @@
14
14
  --------------------------------------------------------------------------- */
15
15
 
16
16
  #include "carray.h"
17
- #include "carray_internal.h"
18
17
  #include "ca_obj_face.h"
19
18
 
20
19
  /* The unit enum is shared with CATime (= same unit system).
@@ -26,16 +25,7 @@ extern void ca_time_extract_unit (VALUE unit, int8_t *base, int64_t *count);
26
25
 
27
26
  typedef struct {
28
27
  /* === CAView prefix === */
29
- int16_t obj_type;
30
- int8_t data_type; /* surface: CA_FIXLEN (storage is the int64 parent) */
31
- int8_t ndim;
32
- int32_t flags; /* CA_FLAG_IS_FACE set */
33
- ca_size_t bytes; /* fixed to sizeof(int64_t) */
34
- ca_size_t elements;
35
- ca_size_t *dim;
36
- char *ptr;
37
- CArray *mask;
38
- char *_pool; /* framework-managed pool buffer (NULL = legacy ALLOC_N path). */
28
+ CA_ARRAY_HEADER
39
29
  CArray *parent;
40
30
  uint32_t attach;
41
31
  uint8_t nosync;
@@ -78,7 +68,7 @@ const rb_data_type_t catimedelta_data_type = {
78
68
  .flags = RUBY_TYPED_FREE_IMMEDIATELY
79
69
  };
80
70
 
81
- static int8_t CA_OBJ_TIMEDELTA;
71
+ static int16_t CA_OBJ_TIMEDELTA;
82
72
  static VALUE rb_cCATimedelta;
83
73
  static VALUE rb_cCATimedeltaElement;
84
74
 
@@ -123,7 +113,7 @@ rb_ca_timedelta_element_s_allocate (VALUE klass)
123
113
  }
124
114
 
125
115
  static VALUE
126
- rb_ca_timedelta_element_initialize (VALUE self, VALUE value, VALUE unit)
116
+ rb_ca_timedelta_element_initialize (volatile VALUE self, VALUE value, VALUE unit)
127
117
  {
128
118
  CATimedeltaElement *s;
129
119
  TypedData_Get_Struct(self, CATimedeltaElement,
@@ -135,7 +125,7 @@ rb_ca_timedelta_element_initialize (VALUE self, VALUE value, VALUE unit)
135
125
 
136
126
  /* dup / clone copy the value and the unit (the allocator leaves both 0). */
137
127
  static VALUE
138
- rb_ca_timedelta_element_initialize_copy (VALUE self, VALUE orig)
128
+ rb_ca_timedelta_element_initialize_copy (volatile VALUE self, volatile VALUE orig)
139
129
  {
140
130
  CATimedeltaElement *s, *o;
141
131
  if ( self == orig ) {
@@ -149,7 +139,7 @@ rb_ca_timedelta_element_initialize_copy (VALUE self, VALUE orig)
149
139
  }
150
140
 
151
141
  static VALUE
152
- rb_ca_timedelta_element_value (VALUE self)
142
+ rb_ca_timedelta_element_value (volatile VALUE self)
153
143
  {
154
144
  CATimedeltaElement *s;
155
145
  TypedData_Get_Struct(self, CATimedeltaElement,
@@ -159,7 +149,7 @@ rb_ca_timedelta_element_value (VALUE self)
159
149
 
160
150
  /* unit → CATime::Resolution(count, base) */
161
151
  static VALUE
162
- rb_ca_timedelta_element_unit (VALUE self)
152
+ rb_ca_timedelta_element_unit (volatile VALUE self)
163
153
  {
164
154
  CATimedeltaElement *s;
165
155
  TypedData_Get_Struct(self, CATimedeltaElement,
@@ -291,14 +281,14 @@ ca_operation_function_t ca_timedelta_func = {
291
281
  /* ------------------------------------------------------------------- */
292
282
 
293
283
  VALUE
294
- rb_ca_timedelta_wrap (VALUE parent_val, int8_t unit, int64_t count)
284
+ rb_ca_timedelta_wrap (volatile VALUE parent_val, int8_t unit, int64_t count)
295
285
  {
296
286
  volatile VALUE obj;
297
287
  CArray *parent;
298
288
  CATimedelta *ca;
299
289
 
300
290
  rb_check_carray_object(parent_val);
301
- TypedData_Get_Struct(parent_val, CArray, &carray_data_type, parent);
291
+ GetCArray(parent_val, parent);
302
292
 
303
293
  ca = ca_timedelta_new(parent, unit, count);
304
294
  obj = ca_wrap_struct(ca);
@@ -320,7 +310,7 @@ rb_ca_timedelta_wrap_method (VALUE klass, VALUE raw, VALUE unit_spec)
320
310
 
321
311
  /* CATimedelta#unit → CATime::Resolution(count, base) */
322
312
  static VALUE
323
- rb_ca_timedelta_unit (VALUE self)
313
+ rb_ca_timedelta_unit (volatile VALUE self)
324
314
  {
325
315
  CATimedelta *ca;
326
316
  TypedData_Get_Struct(self, CATimedelta, &catimedelta_data_type, ca);
@@ -336,7 +326,7 @@ rb_ca_timedelta_s_allocate (VALUE klass)
336
326
  }
337
327
 
338
328
  static VALUE
339
- rb_ca_timedelta_initialize_copy (VALUE self, VALUE other)
329
+ rb_ca_timedelta_initialize_copy (volatile VALUE self, volatile VALUE other)
340
330
  {
341
331
  CATimedelta *ca, *cs;
342
332
  TypedData_Get_Struct(self, CATimedelta, &catimedelta_data_type, ca);
@@ -355,7 +345,7 @@ rb_ca_timedelta_initialize_copy (VALUE self, VALUE other)
355
345
  or an 8-byte String (FIXLEN surface fetch); decodes the parent int64
356
346
  and constructs CATimedelta::Element(value, unit). */
357
347
  static VALUE
358
- rb_ca_timedelta_storage_to_element (VALUE self, VALUE raw)
348
+ rb_ca_timedelta_storage_to_element (volatile VALUE self, volatile VALUE raw)
359
349
  {
360
350
  CATimedelta *ca;
361
351
  CATimedeltaElement *s;
@@ -411,7 +401,6 @@ Init_ca_obj_timedelta (void)
411
401
  rb_cCArrayMask,
412
402
  &carray_mask_data_type,
413
403
  &ca_timedelta_func, sizeof(ca_timedelta_func));
414
- rb_define_const(rb_cObject, "CA_OBJ_TIMEDELTA", INT2NUM(CA_OBJ_TIMEDELTA));
415
404
 
416
405
  rb_define_alloc_func(rb_cCATimedelta, rb_ca_timedelta_s_allocate);
417
406
  rb_define_method(rb_cCATimedelta, "initialize_copy",
@@ -420,7 +409,7 @@ Init_ca_obj_timedelta (void)
420
409
  rb_define_private_method(rb_singleton_class(rb_cCATimedelta), "wrap_storage",
421
410
  rb_ca_timedelta_wrap_method, 2);
422
411
  rb_define_method(rb_cCATimedelta, "unit", rb_ca_timedelta_unit, 0);
423
- rb_define_method(rb_cCATimedelta, "storage_to_element",
412
+ rb_define_private_method(rb_cCATimedelta, "storage_to_element",
424
413
  rb_ca_timedelta_storage_to_element, 1);
425
414
  /* Registers the state-compatibility predicate so CA_FACE_STORAGE_TO_ELEMENT_IF_FACE
426
415
  can skip rb_funcall on the hot path. */
@@ -7,12 +7,11 @@
7
7
  ---------------------------------------------------------------------------- */
8
8
 
9
9
  #include "carray.h"
10
- #include "carray_internal.h" /* ca_view_inherit_mask */
11
10
  #include "ca_obj_face.h" /* CA_FACE_LIFT_IF_FACE */
12
11
 
13
12
  extern ca_operation_function_t ca_stride_func;
14
13
 
15
- static int8_t CA_OBJ_TRANSPOSE;
14
+ static int16_t CA_OBJ_TRANSPOSE;
16
15
 
17
16
  VALUE rb_cCATrans;
18
17
  VALUE rb_cCATransMask;
@@ -85,13 +84,13 @@ ca_trans_new (CArray *parent, ca_size_t *imap)
85
84
  /* ------------------------------------------------------------------- */
86
85
 
87
86
  VALUE
88
- rb_ca_trans_new (VALUE cary, ca_size_t *imap)
87
+ rb_ca_trans_new (volatile VALUE cary, ca_size_t *imap)
89
88
  {
90
89
  volatile VALUE obj;
91
90
  CArray *parent;
92
91
  CAStride *ca;
93
92
  rb_check_carray_object(cary);
94
- TypedData_Get_Struct(cary, CArray, &carray_data_type, parent);
93
+ GetCArray(cary, parent);
95
94
  ca = ca_trans_new(parent, imap);
96
95
  obj = ca_wrap_struct(ca);
97
96
  rb_ca_set_parent(obj, cary);
@@ -102,14 +101,14 @@ rb_ca_trans_new (VALUE cary, ca_size_t *imap)
102
101
  args, reverses the dimension order (= same as `.T`). With argc
103
102
  == ndim, uses imap as the per-axis permutation. */
104
103
  static VALUE
105
- rb_ca_trans (int argc, VALUE *argv, VALUE self)
104
+ rb_ca_trans (int argc, VALUE *argv, volatile VALUE self)
106
105
  {
107
106
  volatile VALUE obj, ropt;
108
107
  CArray *ca;
109
108
  ca_size_t imap[CA_RANK_MAX];
110
109
  int8_t i;
111
110
 
112
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
111
+ GetCArray(self, ca);
113
112
 
114
113
  /* The axis order is positional only. Reject a trailing Hash here so a
115
114
  keyword argument does not read as an argument-count mismatch. */
@@ -122,7 +121,7 @@ rb_ca_trans (int argc, VALUE *argv, VALUE self)
122
121
  else if (argc == ca->ndim) {
123
122
  /* Each axis read as every axis argument is: negative from the end. */
124
123
  for (i = 0; i < ca->ndim; i++) {
125
- imap[i] = ca_normalize_axis_arg(argv[i], ca->ndim, NULL);
124
+ imap[i] = rb_ca_normalize_axis_arg(argv[i], ca->ndim, NULL);
126
125
  }
127
126
  }
128
127
  else {
@@ -136,7 +135,7 @@ rb_ca_trans (int argc, VALUE *argv, VALUE self)
136
135
  }
137
136
 
138
137
  static VALUE
139
- rb_ca_trans_initialize_copy (VALUE self, VALUE other)
138
+ rb_ca_trans_initialize_copy (volatile VALUE self, volatile VALUE other)
140
139
  {
141
140
  CAStride *ca, *cs;
142
141
  TypedData_Get_Struct(self, CAStride, &castride_data_type, ca);
@@ -155,14 +154,13 @@ void
155
154
  Init_ca_obj_transpose (void)
156
155
  {
157
156
  rb_cCATrans = rb_define_class("CATranspose", rb_cCAStride);
158
- rb_cCATransMask = rb_define_class("CATransposeMask", rb_cCATrans);
157
+ rb_cCATransMask = rb_define_class_under(rb_cCATrans, "Mask", rb_cCATrans);
159
158
 
160
159
  CA_OBJ_TRANSPOSE = ca_install_obj_type(rb_cCATrans,
161
160
  &castride_data_type,
162
161
  rb_cCATransMask,
163
162
  &castride_mask_data_type,
164
163
  &ca_stride_func, sizeof(ca_stride_func));
165
- rb_define_const(rb_cObject, "CA_OBJ_TRANSPOSE", INT2NUM(CA_OBJ_TRANSPOSE));
166
164
 
167
165
  rb_define_method(rb_cCArray, "transpose", rb_ca_trans, -1);
168
166
  rb_define_alias(rb_cCArray, "T", "transpose");
data/ext/ca_obj_triop.c CHANGED
@@ -30,7 +30,6 @@
30
30
  ---------------------------------------------------------------------------- */
31
31
 
32
32
  #include "carray.h"
33
- #include "carray_internal.h" /* ca_lazy_arena_* */
34
33
  #include "ca_triop_dispatch.h"
35
34
  #include "ca_monop_dispatch.h" /* CA_MONOP_CAST_BASE */
36
35
  #include "carray_math_kernel.h" /* ca_triop_not_implement */
@@ -38,26 +37,17 @@
38
37
  extern VALUE ca_broadcast_view (VALUE src, int8_t ndim,
39
38
  ca_size_t *target_dim);
40
39
 
41
- int8_t CA_OBJ_TRIOP;
40
+ int16_t CA_OBJ_TRIOP;
42
41
  VALUE rb_cCATriOp;
43
42
 
44
- extern int8_t CA_OBJ_LAZY_MARKER;
43
+ extern int16_t CA_OBJ_LAZY_MARKER;
45
44
 
46
45
  /* ------------------------------------------------------------------- */
47
46
  /* CATriOp struct */
48
47
  /* ------------------------------------------------------------------- */
49
48
 
50
49
  typedef struct CATriOp {
51
- int16_t obj_type;
52
- int8_t data_type;
53
- int8_t ndim;
54
- int32_t flags;
55
- ca_size_t bytes;
56
- ca_size_t elements;
57
- ca_size_t *dim;
58
- char *ptr;
59
- CArray *mask;
60
- char *_pool;
50
+ CA_ARRAY_HEADER
61
51
  CArray *parent; /* = op1 */
62
52
  uint32_t attach;
63
53
  uint8_t nosync;
@@ -139,8 +129,8 @@ ca_triop_setup (CATriOp *ca, CArray *op1, CArray *op2, CArray *op3,
139
129
  ca->op2 = op2;
140
130
  ca->op3 = op3;
141
131
  ca->op_id = op_id;
142
- ca->op2_is_scalar = ( op2->elements == 1 && op1->elements > 1 ) ? 1 : 0;
143
- ca->op3_is_scalar = ( op3->elements == 1 && op1->elements > 1 ) ? 1 : 0;
132
+ ca->op2_is_scalar = ( op2->elements == 1 ) ? 1 : 0;
133
+ ca->op3_is_scalar = ( op3->elements == 1 ) ? 1 : 0;
144
134
  ca->operands[0] = op1;
145
135
  ca->operands[1] = op2;
146
136
  ca->operands[2] = op3;
@@ -447,11 +437,11 @@ ca_operation_function_t ca_triop_func = {
447
437
  /* ------------------------------------------------------------------- */
448
438
 
449
439
  static VALUE
450
- collapse_marker (VALUE cary)
440
+ collapse_marker (volatile VALUE cary)
451
441
  {
452
442
  CArray *ca;
453
443
  if ( ! rb_obj_is_carray(cary) ) return cary;
454
- TypedData_Get_Struct(cary, CArray, &carray_data_type, ca);
444
+ GetCArray(cary, ca);
455
445
  if ( ca->obj_type == CA_OBJ_LAZY_MARKER ) {
456
446
  return rb_ca_parent(cary);
457
447
  }
@@ -461,7 +451,7 @@ collapse_marker (VALUE cary)
461
451
  /* Low-level constructor. Does NOT insert cast nodes; the caller must
462
452
  have promoted all three operands to the common data_type. */
463
453
  static VALUE
464
- rb_ca_triop_new (VALUE cary1, VALUE cary2, VALUE cary3, uint16_t op_id)
454
+ rb_ca_triop_new (volatile VALUE cary1, volatile VALUE cary2, volatile VALUE cary3, uint16_t op_id)
465
455
  {
466
456
  volatile VALUE obj;
467
457
  CArray *op1, *op2, *op3;
@@ -469,9 +459,9 @@ rb_ca_triop_new (VALUE cary1, VALUE cary2, VALUE cary3, uint16_t op_id)
469
459
  rb_check_carray_object(cary1);
470
460
  rb_check_carray_object(cary2);
471
461
  rb_check_carray_object(cary3);
472
- TypedData_Get_Struct(cary1, CArray, &carray_data_type, op1);
473
- TypedData_Get_Struct(cary2, CArray, &carray_data_type, op2);
474
- TypedData_Get_Struct(cary3, CArray, &carray_data_type, op3);
462
+ GetCArray(cary1, op1);
463
+ GetCArray(cary2, op2);
464
+ GetCArray(cary3, op3);
475
465
  ca = ca_triop_new(op1, op2, op3, op_id);
476
466
  obj = ca_wrap_struct(ca);
477
467
  rb_ca_set_parent(obj, cary1);
@@ -484,15 +474,13 @@ rb_ca_triop_new (VALUE cary1, VALUE cary2, VALUE cary3, uint16_t op_id)
484
474
 
485
475
  /* Insert a cast node if operand's data_type differs from target. */
486
476
  static VALUE
487
- insert_cast (VALUE cary, int8_t target_dt, CArray **ca_out)
477
+ insert_cast (volatile VALUE cary, int8_t target_dt, CArray **ca_out)
488
478
  {
489
479
  CArray *ca;
490
- TypedData_Get_Struct(cary, CArray, &carray_data_type, ca);
480
+ GetCArray(cary, ca);
491
481
  if ( ca->data_type != target_dt ) {
492
- VALUE cast_op = INT2NUM(CA_MONOP_CAST_BASE + target_dt);
493
- cary = rb_funcall(rb_const_get(rb_cObject, rb_intern("CAMonOp")),
494
- rb_intern("__build__"), 2, cary, cast_op);
495
- TypedData_Get_Struct(cary, CArray, &carray_data_type, ca);
482
+ cary = rb_ca_monop_build(cary, CA_MONOP_CAST_BASE + target_dt);
483
+ GetCArray(cary, ca);
496
484
  }
497
485
  *ca_out = ca;
498
486
  return cary;
@@ -503,8 +491,9 @@ insert_cast (VALUE cary, int8_t target_dt, CArray **ca_out)
503
491
  data_type differs from the common data_type, then resolves
504
492
  same-ndim size-1 broadcast pairwise against op1 (the walk anchor).
505
493
 
506
- Broadcast: each of op2 / op3 is aligned against op1 via
507
- ca_broadcast_pair. After alignment, each must have either the
494
+ Broadcast: the three are aligned pairwise via ca_broadcast_pair
495
+ (op1 / op2 again last, as two of them can meet at a shape smaller than
496
+ the third's). After alignment, op2 / op3 must each have either the
508
497
  same elements as op1 or be a 1-element CScalar (walked with
509
498
  element_step = 0 by the kernel). If op1 itself is a 1-element
510
499
  CScalar and any of op2 / op3 is an array, op1 is lifted via
@@ -534,18 +523,26 @@ rb_ca_triop_build (VALUE cary1, VALUE cary2, VALUE cary3, uint16_t op_id)
534
523
  }
535
524
  /* Scalar operands (clip's Numeric bounds, a CScalar) are wrapped and
536
525
  promoted pairwise in the order the eager triop uses. */
537
- else if ( ! rb_obj_is_carray(r1) || ! rb_obj_is_carray(r2) || ! rb_obj_is_carray(r3) ||
538
- rb_obj_is_cscalar(r2) || rb_obj_is_cscalar(r3) ) {
526
+ /* a bound the type can not hold is taken as the eager clip takes it */
527
+ else if ( op_id == CA_TRIOP_CLIP ) {
528
+ ca_scalar_operand_for_op(&r2, r1, CA_SCALAR_FOR_MAX);
529
+ ca_scalar_operand_for_op(&r3, r1, CA_SCALAR_FOR_MIN);
530
+ }
531
+ else if ( op_id == CA_TRIOP_FMA || op_id == CA_TRIOP_FMS ) {
532
+ ca_triop_ring_scalar_operands(&r1, &r2, &r3);
533
+ }
534
+ if ( op_id != CA_TRIOP_SELECT &&
535
+ ( ! rb_obj_is_carray(r1) || ! rb_obj_is_carray(r2) || ! rb_obj_is_carray(r3) ||
536
+ rb_obj_is_cscalar(r1) || rb_obj_is_cscalar(r2) || rb_obj_is_cscalar(r3) ) ) {
539
537
  if ( ! rb_obj_is_carray(r1) ) {
540
538
  rb_raise(rb_eArgError,
541
539
  "CATriOp: the first operand must be a CArray");
542
540
  }
541
+ int r1_scalar = rb_obj_is_cscalar(r1);
543
542
  int r2_scalar = ! rb_obj_is_carray(r2) || rb_obj_is_cscalar(r2);
544
543
  int r3_scalar = ! rb_obj_is_carray(r3) || rb_obj_is_cscalar(r3);
545
- rb_ca_cast_self_or_other(&r1, &r2);
546
- rb_ca_cast_self_or_other(&r1, &r3);
547
- rb_ca_cast_self_or_other(&r2, &r3);
548
- rb_ca_cast_self_or_other(&r1, &r2);
544
+ rb_ca_cast_three(&r1, &r2, &r3);
545
+ r1 = ca_lazy_settle_scalar(r1, r1_scalar);
549
546
  r2 = ca_lazy_settle_scalar(r2, r2_scalar);
550
547
  r3 = ca_lazy_settle_scalar(r3, r3_scalar);
551
548
  }
@@ -553,9 +550,9 @@ rb_ca_triop_build (VALUE cary1, VALUE cary2, VALUE cary3, uint16_t op_id)
553
550
  ca_refuse_without_common_type(r1, r2);
554
551
  ca_refuse_without_common_type(r1, r3);
555
552
  ca_refuse_without_common_type(r2, r3);
556
- TypedData_Get_Struct(r1, CArray, &carray_data_type, op1);
557
- TypedData_Get_Struct(r2, CArray, &carray_data_type, op2);
558
- TypedData_Get_Struct(r3, CArray, &carray_data_type, op3);
553
+ GetCArray(r1, op1);
554
+ GetCArray(r2, op2);
555
+ GetCArray(r3, op3);
559
556
 
560
557
  /* Step 1: cast to common data_type. */
561
558
  ca_triop_kernel_input_data_types(op_id,
@@ -601,21 +598,24 @@ rb_ca_triop_build (VALUE cary1, VALUE cary2, VALUE cary3, uint16_t op_id)
601
598
  for ( k = 0; k < anchor_ndim - 1; k++ ) count[k] = 1;
602
599
  count[anchor_ndim - 1] = 0;
603
600
  r1 = rb_ca_repeat_new(r1, anchor_ndim, count);
604
- TypedData_Get_Struct(r1, CArray, &carray_data_type, op1);
601
+ GetCArray(r1, op1);
605
602
  }
606
603
  r1 = ca_broadcast_view(r1, anchor_ndim, anchor->dim);
607
- TypedData_Get_Struct(r1, CArray, &carray_data_type, op1);
604
+ GetCArray(r1, op1);
608
605
  (void) r_anchor;
609
606
  }
610
607
  }
611
608
 
612
- /* Now align op2 / op3 against op1 for size-1 broadcast. */
609
+ /* Now bring the three to one shape for size-1 broadcast. Every pair is
610
+ aligned, and the first again after the others: op1 and op2 may meet
611
+ at a shape smaller than op3's ([1,4] [1,4] [3,4]). */
613
612
  ca_broadcast_pair(&r1, &r2);
614
- TypedData_Get_Struct(r1, CArray, &carray_data_type, op1);
615
- TypedData_Get_Struct(r2, CArray, &carray_data_type, op2);
616
613
  ca_broadcast_pair(&r1, &r3);
617
- TypedData_Get_Struct(r1, CArray, &carray_data_type, op1);
618
- TypedData_Get_Struct(r3, CArray, &carray_data_type, op3);
614
+ ca_broadcast_pair(&r2, &r3);
615
+ ca_broadcast_pair(&r1, &r2);
616
+ GetCArray(r1, op1);
617
+ GetCArray(r2, op2);
618
+ GetCArray(r3, op3);
619
619
 
620
620
  /* Step 3: each of op2 / op3 must match op1's element count OR be a
621
621
  1-element CScalar (kernel walks with element_step = 0 in that case).
@@ -648,7 +648,7 @@ rb_ca_triop_s_build (VALUE klass, VALUE cary1, VALUE cary2, VALUE cary3,
648
648
  than computing them: a kernel that can raise on a cell or calls Ruby
649
649
  for it. CArray::Fusion reads it so a plan carries the same rule. */
650
650
  static VALUE
651
- rb_ca_triop_trapping (VALUE self)
651
+ rb_ca_triop_trapping (volatile VALUE self)
652
652
  {
653
653
  CATriOp *to;
654
654
  TypedData_Get_Struct(self, CATriOp, &catriop_data_type, to);
@@ -657,7 +657,7 @@ rb_ca_triop_trapping (VALUE self)
657
657
  }
658
658
 
659
659
  static VALUE
660
- rb_ca_triop_op_id (VALUE self)
660
+ rb_ca_triop_op_id (volatile VALUE self)
661
661
  {
662
662
  CATriOp *to;
663
663
  TypedData_Get_Struct(self, CATriOp, &catriop_data_type, to);
@@ -717,7 +717,7 @@ rb_ca_triop_s_allocate (VALUE klass)
717
717
  }
718
718
 
719
719
  static VALUE
720
- rb_ca_triop_initialize_copy (VALUE self, VALUE other)
720
+ rb_ca_triop_initialize_copy (volatile VALUE self, volatile VALUE other)
721
721
  {
722
722
  CATriOp *ca, *cs;
723
723
  TypedData_Get_Struct(self, CATriOp, &catriop_data_type, ca);
@@ -743,7 +743,6 @@ Init_ca_obj_triop (void)
743
743
  &catriop_data_type,
744
744
  rb_cCArrayMask,
745
745
  &carray_mask_data_type, &ca_triop_func, sizeof(ca_triop_func));
746
- rb_define_const(rb_cObject, "CA_OBJ_TRIOP", INT2NUM(CA_OBJ_TRIOP));
747
746
 
748
747
  /* op_id constants shared with lib/carray/lazy.rb. */
749
748
  rb_define_const(rb_cCATriOp, "OP_FMA", INT2NUM(CA_TRIOP_FMA));