carray 3.0.7 → 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 (275) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -1
  3. data/CHANGELOG.md +7 -1940
  4. data/README.md +38 -32
  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 +6 -9
  11. data/ext/ca_bincmp_dispatch.h +16 -38
  12. data/ext/ca_binop_dispatch.c +27 -0
  13. data/ext/ca_binop_dispatch.h +22 -47
  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 -1506
  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 +90 -163
  28. data/ext/ca_obj_bincmp.c +47 -46
  29. data/ext/ca_obj_binop.c +38 -38
  30. data/ext/ca_obj_bitarray.c +5 -17
  31. data/ext/ca_obj_bitfield.c +15 -30
  32. data/ext/ca_obj_block.c +11 -12
  33. data/ext/ca_obj_byte_swap.c +32 -30
  34. data/ext/ca_obj_const_string.c +57 -62
  35. data/ext/ca_obj_face.c +99 -91
  36. data/ext/ca_obj_face.h +32 -170
  37. data/ext/ca_obj_fake.c +15 -21
  38. data/ext/ca_obj_farray.c +7 -9
  39. data/ext/ca_obj_field.c +17 -13
  40. data/ext/ca_obj_fixlen_string.c +8 -19
  41. data/ext/ca_obj_grid.c +137 -106
  42. data/ext/ca_obj_meld.c +23 -21
  43. data/ext/ca_obj_moncmp.c +10 -21
  44. data/ext/ca_obj_monop.c +24 -35
  45. data/ext/ca_obj_object.c +51 -37
  46. data/ext/ca_obj_record.c +74 -22
  47. data/ext/ca_obj_reduce.c +8 -10
  48. data/ext/ca_obj_refer.c +42 -29
  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 +18 -24
  52. data/ext/ca_obj_select.c +10 -22
  53. data/ext/ca_obj_select_axis.c +27 -36
  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 +28 -24
  57. data/ext/ca_obj_stride.c +53 -54
  58. data/ext/ca_obj_string.c +6 -18
  59. data/ext/ca_obj_tile.c +12 -12
  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 +35 -39
  64. data/ext/ca_obj_window.c +67 -86
  65. data/ext/ca_op_cmplx64.h +4 -0
  66. data/ext/ca_op_ipower.c +34 -12
  67. data/ext/ca_op_powi.h +4 -0
  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 +20 -26
  72. data/ext/ca_sweep_engine.c +2 -3
  73. data/ext/ca_sweep_engine.h +17 -9
  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 +11 -11
  77. data/ext/carray.h +300 -1011
  78. data/ext/carray_access.c +234 -180
  79. data/ext/carray_address_basis.c +33 -38
  80. data/ext/carray_attribute.c +81 -104
  81. data/ext/carray_bincount.c +12 -14
  82. data/ext/carray_broadcast.c +30 -26
  83. data/ext/carray_call_cfunc.c +169 -277
  84. data/ext/carray_call_cfunc.h +36 -28
  85. data/ext/carray_cast.c +912 -346
  86. data/ext/carray_cast_func.rb +31 -3
  87. data/ext/carray_class.c +6 -11
  88. data/ext/carray_conversion.c +43 -38
  89. data/ext/carray_copy.c +28 -26
  90. data/ext/carray_core.c +217 -126
  91. data/ext/carray_count.c +27 -15
  92. data/ext/carray_element.c +40 -25
  93. data/ext/carray_factorize.c +53 -52
  94. data/ext/carray_generate.c +26 -19
  95. data/ext/carray_histogram.c +8 -10
  96. data/ext/carray_hold.c +7 -9
  97. data/ext/carray_index_classifier.c +123 -228
  98. data/ext/carray_index_classifier.h +11 -10
  99. data/ext/carray_integer_total.c +523 -0
  100. data/ext/carray_internal.h +1274 -160
  101. data/ext/carray_key_windows.c +3 -3
  102. data/ext/carray_lazy.c +21 -36
  103. data/ext/carray_loop.c +19 -22
  104. data/ext/carray_mask.c +123 -145
  105. data/ext/carray_math_kernel.h +23 -47
  106. data/ext/carray_mathfunc.c +10 -3
  107. data/ext/carray_median_percentile.c +95 -94
  108. data/ext/carray_memory_view.c +42 -47
  109. data/ext/carray_operator.c +423 -122
  110. data/ext/carray_order.c +121 -85
  111. data/ext/carray_partition.c +13 -15
  112. data/ext/carray_random.c +47 -41
  113. data/ext/carray_round.c +9 -4
  114. data/ext/carray_scatter.c +54 -40
  115. data/ext/carray_segment.c +41 -45
  116. data/ext/carray_slab.c +64 -47
  117. data/ext/carray_slab.h +44 -33
  118. data/ext/carray_sort.c +32 -29
  119. data/ext/carray_sort_kernel.c +12 -10
  120. data/ext/carray_struct.c +38 -43
  121. data/ext/carray_test.c +47 -43
  122. data/ext/carray_time_text.c +24 -31
  123. data/ext/carray_undef.c +50 -7
  124. data/ext/carray_utils.c +258 -94
  125. data/ext/extconf.rb +24 -17
  126. data/ext/mk_call_cfunc.rb +19 -20
  127. data/ext/mk_pow5_table.rb +2 -1
  128. data/ext/mkkernel.rb +782 -461
  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 +264 -180
  135. data/lib/carray/basics.rb +272 -48
  136. data/lib/carray/bincount_nd.rb +33 -16
  137. data/lib/carray/block_iterator.rb +123 -102
  138. data/lib/carray/boolean_reduce.rb +7 -4
  139. data/lib/carray/categorical.rb +63 -51
  140. data/lib/carray/categorical_iterator.rb +89 -67
  141. data/lib/carray/complex.rb +61 -41
  142. data/lib/carray/conditional.rb +49 -22
  143. data/lib/carray/const_string.rb +54 -62
  144. data/lib/carray/core_extensions.rb +1 -1
  145. data/lib/carray/distance_limit.rb +7 -5
  146. data/lib/carray/factories.rb +269 -0
  147. data/lib/carray/fixlen_string.rb +25 -24
  148. data/lib/carray/frame/column_verbs.rb +20 -8
  149. data/lib/carray/frame/concat.rb +3 -3
  150. data/lib/carray/frame/convert.rb +2 -1
  151. data/lib/carray/frame/csv_parser.rb +7 -1
  152. data/lib/carray/frame/frame.rb +35 -25
  153. data/lib/carray/frame/group.rb +183 -150
  154. data/lib/carray/frame/io.rb +26 -7
  155. data/lib/carray/frame/join.rb +37 -12
  156. data/lib/carray/frame/records.rb +1 -1
  157. data/lib/carray/frame/reshape.rb +3 -3
  158. data/lib/carray/frame/sort.rb +10 -8
  159. data/lib/carray/frame/verbs.rb +148 -118
  160. data/lib/carray/frame/windows.rb +232 -21
  161. data/lib/carray/frame.rb +1 -2
  162. data/lib/carray/fuse_source.rb +24 -21
  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 +97 -24
  167. data/lib/carray/key_windows.rb +83 -57
  168. data/lib/carray/lazy.rb +196 -94
  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 +5 -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 +72 -71
  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/gather_nd.rb +106 -106
  183. data/lib/carray/methods/insert_block.rb +2 -2
  184. data/lib/carray/methods/is_between.rb +6 -4
  185. data/lib/carray/methods/is_in.rb +165 -145
  186. data/lib/carray/methods/join.rb +2 -1
  187. data/lib/carray/methods/lag.rb +6 -2
  188. data/lib/carray/methods/locate_addr.rb +150 -138
  189. data/lib/carray/methods/mask_duplicates.rb +1 -1
  190. data/lib/carray/methods/meshgrid.rb +28 -16
  191. data/lib/carray/methods/mode.rb +67 -67
  192. data/lib/carray/methods/nunique.rb +9 -7
  193. data/lib/carray/methods/repeat.rb +12 -12
  194. data/lib/carray/methods/resize.rb +1 -1
  195. data/lib/carray/methods/snap.rb +24 -20
  196. data/lib/carray/methods/string_format.rb +2 -2
  197. data/lib/carray/methods/unique.rb +4 -2
  198. data/lib/carray/methods/value_counts.rb +1 -1
  199. data/lib/carray/mkmf.rb +25 -28
  200. data/lib/carray/percentile_arguments.rb +82 -0
  201. data/lib/carray/record_order.rb +18 -17
  202. data/lib/carray/rng.rb +1 -0
  203. data/lib/carray/runtime.rb +190 -22
  204. data/lib/carray/segment_iterator.rb +251 -129
  205. data/lib/carray/serialize.rb +618 -571
  206. data/lib/carray/slab_iterator.rb +114 -114
  207. data/lib/carray/stack.rb +11 -12
  208. data/lib/carray/string.rb +16 -22
  209. data/lib/carray/string_operation_extension.rb +76 -52
  210. data/lib/carray/struct.rb +122 -164
  211. data/lib/carray/struct_builder.rb +243 -72
  212. data/lib/carray/time.rb +496 -342
  213. data/lib/carray/window_iterator.rb +191 -150
  214. data/lib/carray.rb +6 -11
  215. data/yard-stubs/ca_obj_array.rb +20 -135
  216. data/yard-stubs/ca_obj_bitarray.rb +1 -8
  217. data/yard-stubs/ca_obj_bitfield.rb +4 -3
  218. data/yard-stubs/ca_obj_byte_swap.rb +21 -7
  219. data/yard-stubs/ca_obj_fake.rb +3 -1
  220. data/yard-stubs/ca_obj_farray.rb +1 -1
  221. data/yard-stubs/ca_obj_field.rb +8 -6
  222. data/yard-stubs/ca_obj_refer.rb +7 -5
  223. data/yard-stubs/ca_obj_roll.rb +7 -6
  224. data/yard-stubs/ca_obj_shift.rb +2 -3
  225. data/yard-stubs/ca_obj_stride.rb +5 -7
  226. data/yard-stubs/ca_obj_tile.rb +1 -1
  227. data/yard-stubs/ca_obj_time.rb +43 -0
  228. data/yard-stubs/ca_obj_transpose.rb +1 -1
  229. data/yard-stubs/ca_obj_window.rb +26 -15
  230. data/yard-stubs/carray_access.rb +51 -4
  231. data/yard-stubs/carray_attribute.rb +26 -38
  232. data/yard-stubs/carray_cast.rb +118 -165
  233. data/yard-stubs/carray_class.rb +3 -4
  234. data/yard-stubs/carray_conversion.rb +6 -7
  235. data/yard-stubs/carray_copy.rb +2 -1
  236. data/yard-stubs/carray_count.rb +5 -3
  237. data/yard-stubs/carray_element.rb +4 -3
  238. data/yard-stubs/carray_limits.rb +100 -0
  239. data/yard-stubs/carray_loop.rb +0 -20
  240. data/yard-stubs/carray_mask.rb +59 -41
  241. data/yard-stubs/carray_math.rb +157 -100
  242. data/yard-stubs/carray_mathfunc.rb +415 -3
  243. data/yard-stubs/carray_median_percentile.rb +29 -11
  244. data/yard-stubs/carray_memory_view.rb +2 -2
  245. data/yard-stubs/carray_order.rb +67 -34
  246. data/yard-stubs/carray_random.rb +6 -6
  247. data/yard-stubs/carray_reduce.rb +353 -0
  248. data/yard-stubs/carray_rng.rb +92 -0
  249. data/yard-stubs/carray_scatter.rb +10 -2
  250. data/yard-stubs/carray_slab.rb +7 -4
  251. data/yard-stubs/carray_sort.rb +7 -6
  252. data/yard-stubs/carray_undef.rb +44 -5
  253. data/yard-stubs/ruby_carray.rb +75 -40
  254. metadata +62 -24
  255. data/CHANGELOG.v1.md +0 -126
  256. data/ext/carray_build_flags.h +0 -3
  257. data/ext/carray_config.h +0 -28
  258. data/ext/carray_data_type.c +0 -90
  259. data/ext/carray_kernels_bincmp.c +0 -6457
  260. data/ext/carray_kernels_binop.c +0 -31534
  261. data/ext/carray_kernels_init.c +0 -1734
  262. data/ext/carray_kernels_moncmp.c +0 -2104
  263. data/ext/carray_kernels_monop.c +0 -25337
  264. data/ext/carray_kernels_reduce_aggregate.c +0 -27545
  265. data/ext/carray_kernels_reduce_boolean.c +0 -1924
  266. data/ext/carray_kernels_reduce_cumulative.c +0 -11646
  267. data/ext/carray_kernels_reduce_extreme.c +0 -21631
  268. data/ext/carray_kernels_reduce_variance.c +0 -5981
  269. data/ext/carray_kernels_scan.c +0 -4302
  270. data/ext/carray_kernels_search.c +0 -37776
  271. data/ext/carray_kernels_sort.c +0 -10814
  272. data/ext/carray_kernels_triop.c +0 -1864
  273. data/lib/carray/construct.rb +0 -360
  274. data/lib/carray/data_type_extension.rb +0 -262
  275. data/lib/carray/data_type_limits.rb +0 -91
@@ -11,9 +11,9 @@
11
11
  #include <stdarg.h>
12
12
 
13
13
  #include "carray.h"
14
- #include "carray_internal.h" /* ca_lazy_arena_* */
15
14
  #include "ca_binop_dispatch.h" /* ca_binop_op_id_of_table */
16
15
  #include "ca_monop_dispatch.h" /* ca_monop_op_id_of_table */
16
+ #include "ca_triop_dispatch.h" /* ca_triop_fma, ca_triop_fms */
17
17
 
18
18
  /* Operand scratch for a chunked / gathered kernel run.
19
19
 
@@ -201,7 +201,7 @@ ca_chunked_run_acquire (ca_chunked_run_t *run, int k)
201
201
  {
202
202
  CArray *ca = run->ca[k];
203
203
  if ( ca_attach_is_alias(ca) ) {
204
- ca_attach(ca); /* window: released by ca_chunked_run on the way out */
204
+ ca_attach(ca); /* attach: released by ca_chunked_run on the way out */
205
205
  run->attached[k] = 1;
206
206
  run->src[k] = (char *) ca->ptr;
207
207
  }
@@ -729,12 +729,12 @@ ca_mask_overlay_attached (CArray *out, int n,
729
729
  is the output (a new entity, attach legit). fast = ca_attach_is_alias(ca1)
730
730
  → 1-shot; slow = ALLOCV + ca_xfer_all without ca_func[X].attach. */
731
731
  VALUE
732
- rb_ca_call_monop (VALUE self, ca_monop_func_t func[])
732
+ rb_ca_call_monop (volatile VALUE self, ca_monop_func_t func[])
733
733
  {
734
734
  volatile VALUE out;
735
735
  CArray *ca1, *ca2; /* ca2 = ca1.op */
736
736
 
737
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca1);
737
+ GetCArray(self, ca1);
738
738
 
739
739
  /* Boolean-as-numeric promotion: an arithmetic monop (-@ etc.) has no
740
740
  boolean kernel (func[CA_BOOLEAN] == ca_monop_not_implement), so coerce
@@ -742,7 +742,7 @@ rb_ca_call_monop (VALUE self, ca_monop_func_t func[])
742
742
  storage. Monops that DO define a boolean kernel are left as bool. */
743
743
  if ( ca1->data_type == CA_BOOLEAN && func[CA_BOOLEAN] == ca_monop_not_implement ) {
744
744
  self = rb_ca_wrap_readonly(self, INT2NUM(CA_INT64));
745
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca1);
745
+ GetCArray(self, ca1);
746
746
  }
747
747
  if ( func[ca1->data_type] == ca_monop_not_implement ) {
748
748
  ca_op_refuse_unimplemented(self, ca1->data_type, rb_eCADataTypeError);
@@ -763,7 +763,7 @@ rb_ca_call_monop (VALUE self, ca_monop_func_t func[])
763
763
  k.n = ca1->elements;
764
764
  k.m = ( ca2->mask ) ? (boolean8_t *)ca2->mask->ptr : NULL;
765
765
  k.p2 = ca2->ptr; k.i2 = 1;
766
- ca_attach(ca1); /* window: the kernel runs through ca_op_guarded when it can raise */
766
+ ca_attach(ca1); /* attach: the kernel runs through ca_op_guarded when it can raise */
767
767
  k.p1 = ca1->ptr; k.i1 = 1;
768
768
  if ( ca_monop_may_raise(func, ca1->data_type) ) {
769
769
  ca_op_guarded(ca_monop_call, &k, 1, &ca1, "r", NULL);
@@ -883,23 +883,22 @@ rb_ca_call_monop (VALUE self, ca_monop_func_t func[])
883
883
  }
884
884
 
885
885
  /* the INT64 view of a boolean operand is reached only through ca1 */
886
- RB_GC_GUARD(self);
887
886
  return out;
888
887
  }
889
888
 
890
889
  VALUE
891
- rb_ca_call_monop_bang (VALUE self, ca_monop_func_t func[])
890
+ rb_ca_call_monop_bang (volatile VALUE self, ca_monop_func_t func[])
892
891
  {
893
892
  CArray *ca1; /* ca1.op! */
894
893
 
895
894
  rb_ca_modify(self);
896
895
 
897
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca1);
896
+ GetCArray(self, ca1);
898
897
  if ( func[ca1->data_type] == ca_monop_not_implement ) {
899
898
  ca_op_refuse_unimplemented(self, ca1->data_type, rb_eCADataTypeError);
900
899
  }
901
900
 
902
- ca_attach(ca1); /* window: the kernel runs through ca_op_guarded when it can raise */
901
+ ca_attach(ca1); /* attach: the kernel runs through ca_op_guarded when it can raise */
903
902
  {
904
903
  ca_monop_call_t k;
905
904
  k.f = func[ca1->data_type];
@@ -925,22 +924,24 @@ rb_ca_call_monop_bang (VALUE self, ca_monop_func_t func[])
925
924
  the output array has different cell size from input. No bang form
926
925
  (= data_type change in-place is ill-defined; bang must preserve data_type). */
927
926
  VALUE
928
- rb_ca_call_monop_typed (VALUE self, ca_monop_func_t func[],
927
+ rb_ca_call_monop_typed (volatile VALUE self, ca_monop_func_t func[],
929
928
  int8_t out_data_types[])
930
929
  {
931
930
  volatile VALUE out;
932
931
  CArray *ca1, *ca2;
933
932
  int8_t out_dt;
934
933
 
935
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca1);
934
+ GetCArray(self, ca1);
936
935
 
937
936
  out_dt = out_data_types[ca1->data_type];
938
937
  if ( out_dt < 0 ) {
939
938
  ca_op_refuse_unimplemented(self, ca1->data_type, rb_eCADataTypeError);
940
939
  }
941
940
 
942
- /* Allocate output with the per-op output data_type. Same shape as input. */
943
- ca2 = carray_new(out_dt, ca1->ndim, ca1->dim, 0, NULL);
941
+ /* Allocate output with the per-op output data_type. Same shape as input,
942
+ and a scalar for a scalar, as rb_ca_call_monop's template gives. */
943
+ ca2 = ca_is_scalar(ca1) ? (CArray *) cscalar_new(out_dt, 0, NULL)
944
+ : carray_new(out_dt, ca1->ndim, ca1->dim, 0, NULL);
944
945
  out = ca_wrap_struct(ca2);
945
946
 
946
947
  /* Same fast/slow pattern as rb_ca_call_monop. ca1 input-only, ca2 output. */
@@ -956,7 +957,7 @@ rb_ca_call_monop_typed (VALUE self, ca_monop_func_t func[],
956
957
  k.n = ca1->elements;
957
958
  k.m = ( ca2->mask ) ? (boolean8_t *)ca2->mask->ptr : NULL;
958
959
  k.p2 = ca2->ptr; k.i2 = 1;
959
- ca_attach(ca1); /* window: the kernel runs through ca_op_guarded when it can raise */
960
+ ca_attach(ca1); /* attach: the kernel runs through ca_op_guarded when it can raise */
960
961
  k.p1 = ca1->ptr; k.i1 = 1;
961
962
  if ( ca_monop_may_raise(func, ca1->data_type) ) {
962
963
  ca_op_guarded(ca_monop_call, &k, 1, &ca1, "r", NULL);
@@ -1092,6 +1093,10 @@ rb_ca_binop_pass_to_other (VALUE self, VALUE other, ID method)
1092
1093
  {
1093
1094
  volatile VALUE pair;
1094
1095
  pair = rb_funcall(other, rb_intern("coerce"), 1, self);
1096
+ if ( ! RB_TYPE_P(pair, T_ARRAY) || RARRAY_LEN(pair) != 2 ) {
1097
+ rb_raise(rb_eTypeError, "%s: coerce must return [x, y]",
1098
+ ca_calling_method_name());
1099
+ }
1095
1100
  self = rb_ary_entry(pair, 0);
1096
1101
  other = rb_ary_entry(pair, 1);
1097
1102
  return rb_funcall(self, method, 1, other);
@@ -1137,14 +1142,14 @@ kleene_gather_bool (CArray *ca, boolean8_t *vbuf, boolean8_t *mbuf, ca_size_t n)
1137
1142
  untouched, integer bitwise stays blind). `out` is a fresh entity, so
1138
1143
  out->ptr / out->mask->ptr are writable without attach. */
1139
1144
  VALUE
1140
- ca_kleene_bool_fixup (VALUE vout, VALUE vself, VALUE vother, int is_or)
1145
+ ca_kleene_bool_fixup (volatile VALUE vout, VALUE vself, VALUE vother, int is_or)
1141
1146
  {
1142
1147
  CArray *out, *a, *b;
1143
1148
  volatile VALUE va, vb, holder;
1144
1149
  boolean8_t *ov, *om, *av, *am, *bv, *bm;
1145
1150
  ca_size_t n, i;
1146
1151
 
1147
- TypedData_Get_Struct(vout, CArray, &carray_data_type, out);
1152
+ GetCArray(vout, out);
1148
1153
  if ( out->data_type != CA_BOOLEAN ) {
1149
1154
  return vout; /* integer bitwise: blind, unchanged */
1150
1155
  }
@@ -1156,9 +1161,10 @@ ca_kleene_bool_fixup (VALUE vout, VALUE vself, VALUE vother, int is_or)
1156
1161
  /* Re-normalise the operands the same way rb_ca_call_binop did, so scalar /
1157
1162
  CScalar / view operands all present as boolean CArrays. */
1158
1163
  va = vself; vb = vother;
1164
+ ca_binop_read_boolean_as_truth(1, &va, &vb);
1159
1165
  rb_ca_cast_self_or_other(&va, &vb);
1160
- TypedData_Get_Struct(va, CArray, &carray_data_type, a);
1161
- TypedData_Get_Struct(vb, CArray, &carray_data_type, b);
1166
+ GetCArray(va, a);
1167
+ GetCArray(vb, b);
1162
1168
 
1163
1169
  /* One temporary of Ruby's for all four: reading an operand can raise. */
1164
1170
  n = out->elements;
@@ -1226,33 +1232,105 @@ ca_binop_chunk_kernel (ca_chunked_run_t *run, ca_size_t off, ca_size_t n,
1226
1232
  /* A boolean array as an object array of true / false. The ordinary cast
1227
1233
  gives 1 / 0, which a logical operator on an object array reads as two
1228
1234
  true values (0 is true in Ruby). Masked cells stay masked. */
1235
+ /* The cells of an object array beside a boolean array, read as a boolean
1236
+ array by the rule a scalar operand follows: true / false, and the
1237
+ integers 1 / 0 as true / false. A masked cell stays masked. Any other
1238
+ value has no truth value for the operator, which refuses it. */
1229
1239
  static VALUE
1230
- ca_boolean_as_truth_objects (VALUE vb)
1240
+ ca_object_as_boolean_operand (volatile VALUE vo)
1231
1241
  {
1232
- CArray *cb;
1242
+ CArray *co;
1233
1243
  ca_size_t i;
1234
- VALUE out;
1235
- TypedData_Get_Struct(vb, CArray, &carray_data_type, cb);
1236
- out = rb_carray_new(CA_OBJECT, cb->ndim, cb->dim, 0, NULL);
1237
- for ( i = 0; i < cb->elements; i++ ) {
1238
- rb_ca_store_addr(out, i, rb_ca_fetch_addr(vb, i));
1244
+ VALUE out, x;
1245
+ GetCArray(vo, co);
1246
+ out = rb_carray_new(CA_BOOLEAN, co->ndim, co->dim, 0, NULL);
1247
+ for ( i = 0; i < co->elements; i++ ) {
1248
+ x = rb_ca_fetch_addr(vo, i);
1249
+ if ( x == INT2FIX(0) ) {
1250
+ x = Qfalse;
1251
+ }
1252
+ else if ( x == INT2FIX(1) ) {
1253
+ x = Qtrue;
1254
+ }
1255
+ else if ( x != Qtrue && x != Qfalse && x != CA_UNDEF ) {
1256
+ rb_raise(rb_eCADataTypeError,
1257
+ "%s: a boolean array takes true / false / 1 / 0 as the other "
1258
+ "operand, got %" PRIsVALUE " in an object array",
1259
+ ca_calling_method_name(), rb_inspect(x));
1260
+ }
1261
+ rb_ca_store_addr(out, i, x);
1239
1262
  }
1240
1263
  return out;
1241
1264
  }
1242
1265
 
1243
1266
  static int
1244
- ca_value_is_type (VALUE v, int8_t data_type)
1267
+ ca_value_is_type (volatile VALUE v, int8_t data_type)
1245
1268
  {
1246
1269
  CArray *ca;
1247
1270
  if ( ! rb_obj_is_carray(v) ) return 0;
1248
- TypedData_Get_Struct(v, CArray, &carray_data_type, ca);
1271
+ GetCArray(v, ca);
1249
1272
  return ca->data_type == data_type;
1250
1273
  }
1251
1274
 
1252
- /* A logical operator (`logical`: one with a boolean kernel) between a
1253
- boolean array and an object array reads the boolean cells as true /
1254
- false. The eager binop and the lazy builder both call this before
1255
- anything else decides the operands' types, so both read them alike. */
1275
+ /* The other operand of a logical operator beside a boolean array. A
1276
+ boolean array takes true / false, and the integers 1 / 0 as true /
1277
+ false, as it does in a store; a scalar Integer or an integer CScalar
1278
+ holding 1 / 0 is read as true / false here. Any other number, and an
1279
+ array of numbers, has no truth value for the operator, which refuses
1280
+ it. Any other value is left to the operator. */
1281
+ static VALUE
1282
+ ca_boolean_logical_operand (volatile VALUE v)
1283
+ {
1284
+ CArray *ca;
1285
+ VALUE x;
1286
+ ca_size_t idx0 = 0;
1287
+
1288
+ if ( RB_INTEGER_TYPE_P(v) ) {
1289
+ x = v;
1290
+ }
1291
+ else if ( rb_obj_is_carray(v) ) {
1292
+ GetCArray(v, ca);
1293
+ if ( ca->data_type == CA_BOOLEAN ) {
1294
+ return v;
1295
+ }
1296
+ if ( ! RTEST(rb_obj_is_cscalar(v)) || ca_is_face(ca) ||
1297
+ ! ca_is_integer_type(ca) ) {
1298
+ rb_raise(rb_eCADataTypeError,
1299
+ "%s: the other operand of a boolean array is an array of %s; "
1300
+ "compare it (ne(0)) for its truth, or cast the boolean "
1301
+ "array (to_type) for integer bits",
1302
+ ca_calling_method_name(), ca_type_name[ca->data_type]);
1303
+ }
1304
+ x = rb_ca_fetch_index(v, &idx0);
1305
+ if ( x == CA_UNDEF ) {
1306
+ return x;
1307
+ }
1308
+ }
1309
+ else if ( rb_obj_is_kind_of(v, rb_cNumeric) ) {
1310
+ x = v; /* a Float or other number has no truth value here */
1311
+ }
1312
+ else {
1313
+ return v;
1314
+ }
1315
+ if ( x == INT2FIX(0) ) {
1316
+ return Qfalse;
1317
+ }
1318
+ if ( x == INT2FIX(1) ) {
1319
+ return Qtrue;
1320
+ }
1321
+ rb_raise(rb_eCADataTypeError,
1322
+ "%s: a boolean array takes true / false / 1 / 0 as the other "
1323
+ "operand, got %" PRIsVALUE,
1324
+ ca_calling_method_name(), x);
1325
+ return Qnil;
1326
+ }
1327
+
1328
+ /* A logical operator (`logical`: one with a boolean kernel) beside a
1329
+ boolean array reads the other operand by its truth: true / false, and
1330
+ 1 / 0 as true / false, for a scalar and for each cell of an object
1331
+ array alike. The eager
1332
+ binop and the lazy builder both call this before anything else decides
1333
+ the operands' types, so both read them alike. */
1256
1334
  void
1257
1335
  ca_binop_read_boolean_as_truth (int logical, volatile VALUE *pa,
1258
1336
  volatile VALUE *pb)
@@ -1261,10 +1339,40 @@ ca_binop_read_boolean_as_truth (int logical, volatile VALUE *pa,
1261
1339
  return;
1262
1340
  }
1263
1341
  if ( ca_value_is_type(*pa, CA_BOOLEAN) && ca_value_is_type(*pb, CA_OBJECT) ) {
1264
- *pa = ca_boolean_as_truth_objects(*pa);
1342
+ *pb = ca_object_as_boolean_operand(*pb);
1265
1343
  }
1266
1344
  else if ( ca_value_is_type(*pb, CA_BOOLEAN) && ca_value_is_type(*pa, CA_OBJECT) ) {
1267
- *pb = ca_boolean_as_truth_objects(*pb);
1345
+ *pa = ca_object_as_boolean_operand(*pa);
1346
+ }
1347
+ else if ( ca_value_is_type(*pa, CA_BOOLEAN) ) {
1348
+ *pb = ca_boolean_logical_operand(*pb);
1349
+ }
1350
+ else if ( ca_value_is_type(*pb, CA_BOOLEAN) ) {
1351
+ *pa = ca_boolean_logical_operand(*pa);
1352
+ }
1353
+ }
1354
+
1355
+ /* An integer array an operator has no kernel for says so before its
1356
+ scalar operand is looked at: an Integer beside it keeps the array's
1357
+ type, so the range of the Integer is not what stops the operation. */
1358
+ static void
1359
+ ca_binop_refuse_unimplemented_integer (volatile VALUE array, VALUE scalar,
1360
+ ca_binop_func_t func[])
1361
+ {
1362
+ CArray *ca;
1363
+
1364
+ if ( ! rb_obj_is_carray(array) || RTEST(rb_obj_is_cscalar(array)) ) {
1365
+ return;
1366
+ }
1367
+ if ( ! RB_INTEGER_TYPE_P(scalar) &&
1368
+ ! ( rb_obj_is_carray(scalar) && RTEST(rb_obj_is_cscalar(scalar)) &&
1369
+ RTEST(rb_ca_is_integer_type(scalar)) ) ) {
1370
+ return;
1371
+ }
1372
+ GetCArray(array, ca);
1373
+ if ( ca_is_integer_type(ca) && ! ca_is_face(ca) &&
1374
+ func[ca->data_type] == ca_binop_not_implement ) {
1375
+ ca_op_refuse_unimplemented(array, ca->data_type, rb_eCADataTypeError);
1268
1376
  }
1269
1377
  }
1270
1378
 
@@ -1284,6 +1392,12 @@ rb_ca_call_binop (volatile VALUE self, volatile VALUE other,
1284
1392
 
1285
1393
  receiver = rb_obj_is_carray(other) ? Qnil : self;
1286
1394
 
1395
+ ca_binop_refuse_unimplemented_integer(self, other, func);
1396
+ ca_binop_refuse_unimplemented_integer(other, self, func);
1397
+
1398
+ ca_scalar_operands_for_op(&self, &other,
1399
+ ca_binop_scalar_operand_mode(ca_binop_op_id_of_table(func)));
1400
+
1287
1401
  /* pmax and its kin pick by order, so a value the array can not hold is
1288
1402
  refused as an ordering refuses it. */
1289
1403
  if ( ! rb_obj_is_carray(other) && rb_obj_is_carray(self) &&
@@ -1304,15 +1418,15 @@ rb_ca_call_binop (volatile VALUE self, volatile VALUE other,
1304
1418
  /* do implicit casting */
1305
1419
  rb_ca_cast_self_or_other(&self, &other);
1306
1420
 
1307
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca1);
1308
- TypedData_Get_Struct(other, CArray, &carray_data_type, ca2);
1421
+ GetCArray(self, ca1);
1422
+ GetCArray(other, ca2);
1309
1423
 
1310
1424
  /* A Face whose cells are not Ruby values does not go down to an object
1311
1425
  array to meet a single Ruby object: an operator it does not define for
1312
1426
  an array of its own stays undefined. */
1313
1427
  if ( ca1->data_type == CA_OBJECT && rb_obj_is_carray(receiver) ) {
1314
1428
  CArray *ca0;
1315
- TypedData_Get_Struct(receiver, CArray, &carray_data_type, ca0);
1429
+ GetCArray(receiver, ca0);
1316
1430
  if ( ca_is_face(ca0) && ca0->data_type == CA_FIXLEN ) {
1317
1431
  ca_op_refuse_unimplemented(receiver, ca0->data_type,
1318
1432
  rb_eCADataTypeError);
@@ -1329,8 +1443,8 @@ rb_ca_call_binop (volatile VALUE self, volatile VALUE other,
1329
1443
  if ( ca1->data_type == CA_BOOLEAN && func[CA_BOOLEAN] == ca_binop_not_implement ) {
1330
1444
  self = rb_ca_wrap_readonly(self, INT2NUM(CA_INT64));
1331
1445
  other = rb_ca_wrap_readonly(other, INT2NUM(CA_INT64));
1332
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca1);
1333
- TypedData_Get_Struct(other, CArray, &carray_data_type, ca2);
1446
+ GetCArray(self, ca1);
1447
+ GetCArray(other, ca2);
1334
1448
  }
1335
1449
  if ( func[ca1->data_type] == ca_binop_not_implement ) {
1336
1450
  ca_op_refuse_unimplemented(ca_is_face(ca1) ? self : other, ca1->data_type,
@@ -1393,7 +1507,7 @@ rb_ca_call_binop (volatile VALUE self, volatile VALUE other,
1393
1507
  k.n = n_kernel;
1394
1508
  k.m = ( ca3->mask ) ? (boolean8_t *) ca3->mask->ptr : NULL;
1395
1509
  k.p3 = ca3->ptr; k.i3 = i3;
1396
- ca_attach_n(2, ca1, ca2); /* window: the kernel runs through ca_op_guarded when it can raise */
1510
+ ca_attach_n(2, ca1, ca2); /* attach: the kernel runs through ca_op_guarded when it can raise */
1397
1511
  k.p1 = ca1->ptr; k.i1 = i1;
1398
1512
  k.p2 = ca2->ptr; k.i2 = i2;
1399
1513
  if ( ca_binop_may_raise(func, ca1->data_type) ) {
@@ -1610,10 +1724,10 @@ rb_ca_call_binop (volatile VALUE self, volatile VALUE other,
1610
1724
  ca_xfer_all(ca2, p2, CA_XFER_GET);
1611
1725
  }
1612
1726
  if ( ca_attach_is_alias(ca1) ) {
1613
- ca_attach(ca1); p1 = (char *) ca1->ptr; a1 = 1; /* window: an alias attach, cannot raise */
1727
+ ca_attach(ca1); p1 = (char *) ca1->ptr; a1 = 1; /* attach: an alias attach, cannot raise */
1614
1728
  }
1615
1729
  if ( ca_attach_is_alias(ca2) ) {
1616
- ca_attach(ca2); p2 = (char *) ca2->ptr; a2 = 1; /* window: as above */
1730
+ ca_attach(ca2); p2 = (char *) ca2->ptr; a2 = 1; /* attach: as above */
1617
1731
  }
1618
1732
 
1619
1733
  {
@@ -1689,7 +1803,7 @@ ca_bang_input_may_overlap (CArray *target, CArray *input)
1689
1803
  `other` only.
1690
1804
  self always goes through ca_attach + ca_sync (= write-back to root). */
1691
1805
  VALUE
1692
- rb_ca_call_binop_bang (VALUE self, VALUE other, ca_binop_func_t func[])
1806
+ rb_ca_call_binop_bang (volatile VALUE self, volatile VALUE other, ca_binop_func_t func[])
1693
1807
  {
1694
1808
  CArray *ca1, *ca2; /* ca1.op!(ca2) */
1695
1809
  int self_is_scalar, other_is_scalar;
@@ -1697,11 +1811,24 @@ rb_ca_call_binop_bang (VALUE self, VALUE other, ca_binop_func_t func[])
1697
1811
 
1698
1812
  rb_ca_modify(self);
1699
1813
 
1814
+ /* a boolean receiver reads the other operand by its truth, as the
1815
+ operator does */
1816
+ if ( func[CA_BOOLEAN] != ca_binop_not_implement &&
1817
+ ca_value_is_type(self, CA_BOOLEAN) ) {
1818
+ other = ca_boolean_logical_operand(other);
1819
+ }
1820
+
1821
+ ca_binop_refuse_unimplemented_integer(self, other, func);
1822
+
1823
+ /* the receiver is the write target and stays as it is */
1824
+ ca_scalar_operand_for_op((volatile VALUE *) &other, self,
1825
+ ca_binop_scalar_operand_mode(ca_binop_op_id_of_table(func)));
1826
+
1700
1827
  /* do implicit casting */
1701
1828
  rb_ca_cast_other(&self, &other);
1702
1829
 
1703
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca1);
1704
- TypedData_Get_Struct(other, CArray, &carray_data_type, ca2);
1830
+ GetCArray(self, ca1);
1831
+ GetCArray(other, ca2);
1705
1832
  if ( func[ca1->data_type] == ca_binop_not_implement ) {
1706
1833
  ca_op_refuse_unimplemented(self, ca1->data_type, rb_eCADataTypeError);
1707
1834
  }
@@ -1714,7 +1841,7 @@ rb_ca_call_binop_bang (VALUE self, VALUE other, ca_binop_func_t func[])
1714
1841
  the symmetric one a binary operation is held to. */
1715
1842
  if ( !other_is_scalar ) {
1716
1843
  ca_broadcast_to_destination(self, &other);
1717
- TypedData_Get_Struct(other, CArray, &carray_data_type, ca2);
1844
+ GetCArray(other, ca2);
1718
1845
  other_is_scalar = RTEST(rb_obj_is_cscalar(other));
1719
1846
  }
1720
1847
 
@@ -1743,12 +1870,12 @@ rb_ca_call_binop_bang (VALUE self, VALUE other, ca_binop_func_t func[])
1743
1870
  ca_xfer_all(ca2, p2, CA_XFER_GET);
1744
1871
  }
1745
1872
 
1746
- ca_attach(ca1); /* window: the overlay detaches it if it raises; the
1873
+ ca_attach(ca1); /* attach: the overlay detaches it if it raises; the
1747
1874
  kernel runs through ca_op_guarded when it can */
1748
1875
  ca_mask_overlay_attached(ca1, 2, ca1, ca2, NULL);
1749
1876
 
1750
1877
  if ( in_place2 ) {
1751
- ca_attach(ca2); /* window: an alias attach, which cannot raise */
1878
+ ca_attach(ca2); /* attach: an alias attach, which cannot raise */
1752
1879
  p2 = ca2->ptr;
1753
1880
  a2 = 1;
1754
1881
  }
@@ -1774,7 +1901,6 @@ rb_ca_call_binop_bang (VALUE self, VALUE other, ca_binop_func_t func[])
1774
1901
  ca_sync_detach(ca1);
1775
1902
 
1776
1903
  /* a cast or broadcast view of other is reached only through ca2 */
1777
- RB_GC_GUARD(other);
1778
1904
  return self;
1779
1905
  }
1780
1906
 
@@ -1826,7 +1952,7 @@ rb_ca_triop_select_template (VALUE self, VALUE other2, VALUE other3,
1826
1952
 
1827
1953
  #define EAGER_ATTACH_INPUT(ca_, p_, attached_) do { \
1828
1954
  if ( ca_attach_is_alias(ca_) ) { \
1829
- ca_attach(ca_); /* window: an alias attach, cannot raise */ \
1955
+ ca_attach(ca_); /* attach: an alias attach, cannot raise */ \
1830
1956
  (p_) = (char *)(ca_)->ptr; \
1831
1957
  (attached_) = 1; \
1832
1958
  } \
@@ -1877,19 +2003,82 @@ ca_triop_chunk_kernel (ca_chunked_run_t *run, ca_size_t off, ca_size_t n,
1877
2003
  }
1878
2004
  }
1879
2005
 
2006
+ /* Whether v leaves the answer in ca's type: an Integer, an integer
2007
+ CScalar, or an array of ca's type. A Float or an array of another type
2008
+ decides another type of answer. */
2009
+ static int
2010
+ ca_triop_operand_keeps_type (volatile VALUE v, CArray *ca)
2011
+ {
2012
+ CArray *cv;
2013
+ if ( RB_INTEGER_TYPE_P(v) ) {
2014
+ return 1;
2015
+ }
2016
+ if ( ! rb_obj_is_carray(v) ) {
2017
+ return 0;
2018
+ }
2019
+ GetCArray(v, cv);
2020
+ if ( ca_is_face(cv) ) {
2021
+ return 0;
2022
+ }
2023
+ if ( RTEST(rb_obj_is_cscalar(v)) ) {
2024
+ return ca_is_integer_type(cv);
2025
+ }
2026
+ return cv->data_type == ca->data_type;
2027
+ }
2028
+
2029
+ /* fma and fms are + and *, so a scalar the receiver's type can not hold
2030
+ is taken as + and * take it (modulo 2 ** bits), when no operand of
2031
+ another type decides the type of the answer; otherwise the three
2032
+ operands are left to the promotion. */
2033
+ void
2034
+ ca_triop_ring_scalar_operands (volatile VALUE *self, volatile VALUE *other2,
2035
+ volatile VALUE *other3)
2036
+ {
2037
+ CArray *ca;
2038
+ if ( ! rb_obj_is_carray(*self) ) {
2039
+ return;
2040
+ }
2041
+ GetCArray(*self, ca);
2042
+ if ( ! ca_triop_operand_keeps_type(*other2, ca) ||
2043
+ ! ca_triop_operand_keeps_type(*other3, ca) ) {
2044
+ return;
2045
+ }
2046
+ ca_scalar_operand_for_op(other2, *self, CA_SCALAR_FOR_RING);
2047
+ ca_scalar_operand_for_op(other3, *self, CA_SCALAR_FOR_RING);
2048
+ }
2049
+
2050
+ /* clip is max with its lower bound and min with its upper one, so a bound
2051
+ the receiver's type can not hold is taken as those take it
2052
+ (ca_scalar_operand_for_op). */
2053
+ static void
2054
+ ca_triop_scalar_operands (volatile VALUE *self, volatile VALUE *other2,
2055
+ volatile VALUE *other3, ca_triop_func_t func[])
2056
+ {
2057
+ if ( func == ca_triop_clip ) {
2058
+ ca_scalar_operand_for_op(other2, *self, CA_SCALAR_FOR_MAX);
2059
+ ca_scalar_operand_for_op(other3, *self, CA_SCALAR_FOR_MIN);
2060
+ }
2061
+ else if ( func == ca_triop_fma || func == ca_triop_fms ) {
2062
+ ca_triop_ring_scalar_operands(self, other2, other3);
2063
+ }
2064
+ }
2065
+
1880
2066
  VALUE
1881
- rb_ca_call_triop (VALUE self, VALUE other2, VALUE other3,
2067
+ rb_ca_call_triop (volatile VALUE self, volatile VALUE other2, volatile VALUE other3,
1882
2068
  ca_triop_func_t func[])
1883
2069
  {
1884
2070
  volatile VALUE out;
1885
2071
  CArray *ca1, *ca2, *ca3, *cao;
1886
2072
 
2073
+ ca_triop_scalar_operands((volatile VALUE *) &self, (volatile VALUE *) &other2,
2074
+ (volatile VALUE *) &other3, func);
2075
+
1887
2076
  /* After this, all three CArrays share the same data_type. */
1888
2077
  rb_ca_cast_three(&self, &other2, &other3);
1889
2078
 
1890
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca1);
1891
- TypedData_Get_Struct(other2, CArray, &carray_data_type, ca2);
1892
- TypedData_Get_Struct(other3, CArray, &carray_data_type, ca3);
2079
+ GetCArray(self, ca1);
2080
+ GetCArray(other2, ca2);
2081
+ GetCArray(other3, ca3);
1893
2082
 
1894
2083
  /* Boolean-as-numeric promotion (same rule as rb_ca_call_binop): an
1895
2084
  arithmetic triop (fma / fms) has no boolean kernel, so its
@@ -1903,9 +2092,9 @@ rb_ca_call_triop (VALUE self, VALUE other2, VALUE other3,
1903
2092
  self = rb_ca_wrap_readonly(self, INT2NUM(CA_INT64));
1904
2093
  other2 = rb_ca_wrap_readonly(other2, INT2NUM(CA_INT64));
1905
2094
  other3 = rb_ca_wrap_readonly(other3, INT2NUM(CA_INT64));
1906
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca1);
1907
- TypedData_Get_Struct(other2, CArray, &carray_data_type, ca2);
1908
- TypedData_Get_Struct(other3, CArray, &carray_data_type, ca3);
2095
+ GetCArray(self, ca1);
2096
+ GetCArray(other2, ca2);
2097
+ GetCArray(other3, ca3);
1909
2098
  }
1910
2099
  if ( func[ca1->data_type] == ca_triop_not_implement ) {
1911
2100
  ca_op_refuse_unimplemented(ca_is_face(ca1) ? self
@@ -1934,7 +2123,7 @@ rb_ca_call_triop (VALUE self, VALUE other2, VALUE other3,
1934
2123
  }
1935
2124
 
1936
2125
  out = rb_ca_triop_select_template(self, other2, other3, ca1, ca2, ca3);
1937
- TypedData_Get_Struct(out, CArray, &carray_data_type, cao);
2126
+ GetCArray(out, cao);
1938
2127
 
1939
2128
  ca_mask_overlay_safe(cao, 3, ca1, ca2, ca3);
1940
2129
 
@@ -2034,9 +2223,6 @@ rb_ca_call_triop (VALUE self, VALUE other2, VALUE other3,
2034
2223
  }
2035
2224
 
2036
2225
  /* the INT64 views of a boolean triop are reached only through ca1..ca3 */
2037
- RB_GC_GUARD(self);
2038
- RB_GC_GUARD(other2);
2039
- RB_GC_GUARD(other3);
2040
2226
  return out;
2041
2227
  }
2042
2228
 
@@ -2051,19 +2237,22 @@ ca_triop_bang_chunked (VALUE arg)
2051
2237
  }
2052
2238
 
2053
2239
  VALUE
2054
- rb_ca_call_triop_bang (VALUE self, VALUE other2, VALUE other3,
2240
+ rb_ca_call_triop_bang (volatile VALUE self, volatile VALUE other2, volatile VALUE other3,
2055
2241
  ca_triop_func_t func[])
2056
2242
  {
2057
2243
  CArray *ca1, *ca2, *ca3;
2058
2244
 
2059
2245
  rb_ca_modify(self);
2060
2246
 
2247
+ ca_triop_scalar_operands((volatile VALUE *) &self, (volatile VALUE *) &other2,
2248
+ (volatile VALUE *) &other3, func);
2249
+
2061
2250
  rb_ca_cast_other(&self, &other2);
2062
2251
  rb_ca_cast_other(&self, &other3);
2063
2252
 
2064
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca1);
2065
- TypedData_Get_Struct(other2, CArray, &carray_data_type, ca2);
2066
- TypedData_Get_Struct(other3, CArray, &carray_data_type, ca3);
2253
+ GetCArray(self, ca1);
2254
+ GetCArray(other2, ca2);
2255
+ GetCArray(other3, ca3);
2067
2256
  if ( func[ca1->data_type] == ca_triop_not_implement ) {
2068
2257
  ca_op_refuse_unimplemented(self, ca1->data_type, rb_eCADataTypeError);
2069
2258
  }
@@ -2072,11 +2261,11 @@ rb_ca_call_triop_bang (VALUE self, VALUE other2, VALUE other3,
2072
2261
  input operand in turn (the same one assignment uses). */
2073
2262
  if ( ! rb_obj_is_cscalar(other2) ) {
2074
2263
  ca_broadcast_to_destination(self, &other2);
2075
- TypedData_Get_Struct(other2, CArray, &carray_data_type, ca2);
2264
+ GetCArray(other2, ca2);
2076
2265
  }
2077
2266
  if ( ! rb_obj_is_cscalar(other3) ) {
2078
2267
  ca_broadcast_to_destination(self, &other3);
2079
- TypedData_Get_Struct(other3, CArray, &carray_data_type, ca3);
2268
+ GetCArray(other3, ca3);
2080
2269
  }
2081
2270
 
2082
2271
  {
@@ -2120,18 +2309,18 @@ rb_ca_call_triop_bang (VALUE self, VALUE other2, VALUE other3,
2120
2309
 
2121
2310
  /* self IS the output (= write target; attach legit per refined
2122
2311
  invariant) */
2123
- ca_attach(ca1); /* window: the overlay detaches it if it raises; the
2312
+ ca_attach(ca1); /* attach: the overlay detaches it if it raises; the
2124
2313
  kernel runs through ca_op_guarded */
2125
2314
  ca_mask_overlay_attached(ca1, 3, ca1, ca2, ca3);
2126
2315
 
2127
2316
  attached2 = attached3 = 0;
2128
2317
  if ( in_place2 ) {
2129
- ca_attach(ca2); /* window: an alias attach, cannot raise */
2318
+ ca_attach(ca2); /* attach: an alias attach, cannot raise */
2130
2319
  p2 = (char *) ca2->ptr;
2131
2320
  attached2 = 1;
2132
2321
  }
2133
2322
  if ( in_place3 ) {
2134
- ca_attach(ca3); /* window: as above */
2323
+ ca_attach(ca3); /* attach: as above */
2135
2324
  p3 = (char *) ca3->ptr;
2136
2325
  attached3 = 1;
2137
2326
  }
@@ -2163,7 +2352,7 @@ rb_ca_call_triop_bang (VALUE self, VALUE other2, VALUE other3,
2163
2352
  ca1->ptr + off*bytes in chunks; sync at end. The run gathers
2164
2353
  per chunk, and any gather can raise, so the run is guarded
2165
2354
  whatever the kernel. */
2166
- ca_attach(ca1); /* window: the overlay detaches it if it raises; the
2355
+ ca_attach(ca1); /* attach: the overlay detaches it if it raises; the
2167
2356
  kernel runs through ca_op_guarded */
2168
2357
  ca_mask_overlay_attached(ca1, 3, ca1, ca2, ca3);
2169
2358
  {
@@ -2202,20 +2391,18 @@ rb_ca_call_triop_bang (VALUE self, VALUE other2, VALUE other3,
2202
2391
 
2203
2392
  /* cast or broadcast views of the operands are reached only through
2204
2393
  ca2 and ca3 */
2205
- RB_GC_GUARD(other2);
2206
- RB_GC_GUARD(other3);
2207
2394
  return self;
2208
2395
  }
2209
2396
 
2210
2397
  /* moncmp driver. ca1 input-only (EAGER_ACQUIRE/RELEASE fast/slow), ca2 =
2211
2398
  new boolean entity output (attach legit). */
2212
2399
  VALUE
2213
- rb_ca_call_moncmp (VALUE self, ca_moncmp_func_t func[])
2400
+ rb_ca_call_moncmp (volatile VALUE self, ca_moncmp_func_t func[])
2214
2401
  {
2215
2402
  volatile VALUE out;
2216
2403
  CArray *ca1, *ca2; /* ca2 = ca1.op */
2217
2404
 
2218
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca1);
2405
+ GetCArray(self, ca1);
2219
2406
  if ( func[ca1->data_type] == ca_moncmp_not_implement ) {
2220
2407
  ca_op_refuse_unimplemented(self, ca1->data_type, rb_eCADataTypeError);
2221
2408
  }
@@ -2227,7 +2414,7 @@ rb_ca_call_moncmp (VALUE self, ca_moncmp_func_t func[])
2227
2414
  out = rb_carray_new(CA_BOOLEAN, ca1->ndim, ca1->dim, 0, NULL);
2228
2415
  }
2229
2416
 
2230
- TypedData_Get_Struct(out, CArray, &carray_data_type, ca2);
2417
+ GetCArray(out, ca2);
2231
2418
 
2232
2419
  ca_mask_overlay_safe(ca2, 1, ca1);
2233
2420
 
@@ -2339,16 +2526,31 @@ ca_bincmp_answer_when_negative (int op_id, int signed_side)
2339
2526
  }
2340
2527
 
2341
2528
  static int
2342
- ca_bincmp_plain_integer_array (VALUE v, int8_t *dt)
2529
+ ca_bincmp_plain_integer_array (volatile VALUE v, int8_t *dt)
2343
2530
  {
2344
2531
  CArray *ca;
2345
2532
  if ( ! rb_obj_is_carray(v) ) return 0;
2346
- TypedData_Get_Struct(v, CArray, &carray_data_type, ca);
2533
+ GetCArray(v, ca);
2347
2534
  if ( ca_is_face(ca) ) return 0;
2348
2535
  *dt = ca->data_type;
2349
2536
  return ca_is_unsigned_integer_dt(*dt) || ca_is_signed_integer_dt(*dt);
2350
2537
  }
2351
2538
 
2539
+ /* An array an Integer is compared with by value: a plain integer array in
2540
+ its own type, and a boolean array as its 0 / 1 int64 numbers, so an
2541
+ Integer beyond int64 answers as it does beside an int64 array. */
2542
+ static int
2543
+ ca_bincmp_integer_scalar_reference (volatile VALUE v, int8_t *dt)
2544
+ {
2545
+ CArray *ca;
2546
+ if ( ca_bincmp_plain_integer_array(v, dt) ) return 1;
2547
+ if ( ! rb_obj_is_carray(v) ) return 0;
2548
+ GetCArray(v, ca);
2549
+ if ( ca_is_face(ca) || ca->data_type != CA_BOOLEAN ) return 0;
2550
+ *dt = CA_INT64;
2551
+ return 1;
2552
+ }
2553
+
2352
2554
  /* Prepares the operands of comparison op_id (CA_BINCMP_*). Returns
2353
2555
  0 nothing to do,
2354
2556
  1 every cell answers true, 2 every cell answers false
@@ -2365,15 +2567,18 @@ ca_bincmp_integer_prepare (volatile VALUE *self, volatile VALUE *other,
2365
2567
 
2366
2568
  if ( op_id < CA_BINCMP_LT || op_id > CA_BINCMP_NE ) return 0;
2367
2569
 
2570
+ /* an integer CScalar compares by its value, as an Integer does */
2571
+ ca_scalar_operands_for_op(self, other, CA_SCALAR_FOR_COMPARE);
2572
+
2368
2573
  if ( RB_INTEGER_TYPE_P(*other) &&
2369
- ca_bincmp_plain_integer_array(*self, &dt1) ) {
2574
+ ca_bincmp_integer_scalar_reference(*self, &dt1) ) {
2370
2575
  side = ca_integer_range_side(*other, dt1);
2371
2576
  if ( side == 0 ) return 0;
2372
2577
  *other = *self;
2373
2578
  return ca_bincmp_ordered_answer(op_id, side > 0) ? 1 : 2;
2374
2579
  }
2375
2580
  if ( RB_INTEGER_TYPE_P(*self) &&
2376
- ca_bincmp_plain_integer_array(*other, &dt2) ) {
2581
+ ca_bincmp_integer_scalar_reference(*other, &dt2) ) {
2377
2582
  side = ca_integer_range_side(*self, dt2);
2378
2583
  if ( side == 0 ) return 0;
2379
2584
  *self = *other;
@@ -2394,6 +2599,58 @@ ca_bincmp_integer_prepare (volatile VALUE *self, volatile VALUE *other,
2394
2599
  return 3;
2395
2600
  }
2396
2601
 
2602
+ /* is_close / is_equiv between integers. The kernel takes the difference
2603
+ of two cells of one integer type exactly; this finds the operands no
2604
+ common integer type holds. Returns
2605
+ 0 the kernel answers,
2606
+ 1 the kernel answers once the arrays are taken to int64 (an Integer
2607
+ beyond a narrower array's type, or a signed array beside an
2608
+ unsigned one of 4 bytes or fewer),
2609
+ 2 the answer is computed by value (is_close_by_value /
2610
+ is_equiv_by_value): an Integer beyond int64 or beside a uint64
2611
+ array, or a signed array beside a uint64 array.
2612
+ A boolean array beside an Integer is read as its 0 / 1 int64 numbers. */
2613
+ int
2614
+ ca_bincmp_tolerance_integer_prepare (VALUE self, VALUE other)
2615
+ {
2616
+ int8_t dt1, dt2, dtu;
2617
+
2618
+ if ( RB_INTEGER_TYPE_P(self) ) {
2619
+ VALUE tmp = self; self = other; other = tmp;
2620
+ }
2621
+ if ( RB_INTEGER_TYPE_P(other) ) {
2622
+ if ( ! ca_bincmp_integer_scalar_reference(self, &dt1) ) return 0;
2623
+ if ( ca_integer_range_side(other, dt1) == 0 ) return 0;
2624
+ return ( dt1 != CA_UINT64 && ca_integer_range_side(other, CA_INT64) == 0 )
2625
+ ? 1 : 2;
2626
+ }
2627
+ if ( ! ca_bincmp_plain_integer_array(self, &dt1) ||
2628
+ ! ca_bincmp_plain_integer_array(other, &dt2) ) {
2629
+ return 0;
2630
+ }
2631
+ if ( ca_is_unsigned_integer_dt(dt1) == ca_is_unsigned_integer_dt(dt2) ) {
2632
+ return 0;
2633
+ }
2634
+ if ( ! ca_is_unsigned_integer_dt(ca_promote_type(dt1, dt2)) ) {
2635
+ return 0;
2636
+ }
2637
+ dtu = ca_is_unsigned_integer_dt(dt1) ? dt1 : dt2;
2638
+ return ( dtu == CA_UINT64 ) ? 2 : 1;
2639
+ }
2640
+
2641
+ /* The answer of is_close (is_equiv = 0) or is_equiv computed by value. */
2642
+ VALUE
2643
+ ca_bincmp_tolerance_by_value (VALUE self, VALUE other, int is_equiv,
2644
+ double tol)
2645
+ {
2646
+ if ( ! rb_obj_is_carray(self) ) {
2647
+ VALUE tmp = self; self = other; other = tmp;
2648
+ }
2649
+ return rb_funcall(self, rb_intern(is_equiv ? "is_equiv_by_value"
2650
+ : "is_close_by_value"),
2651
+ 2, other, rb_float_new(tol));
2652
+ }
2653
+
2397
2654
  static int
2398
2655
  ca_bincmp_op_id_of (ca_bincmp_func_t *func)
2399
2656
  {
@@ -2420,21 +2677,20 @@ ca_bincmp_mixed_sign (VALUE self, VALUE other, ca_bincmp_func_t func[],
2420
2677
  ca_size_t i, step;
2421
2678
  int answer = ca_bincmp_answer_when_negative(op_id, signed_side);
2422
2679
 
2423
- l = rb_funcall(self, rb_intern("to_type"), 1,
2424
- INT2NUM(( signed_side == 1 ) ? CA_INT64 : CA_UINT64));
2425
- r = rb_funcall(other, rb_intern("to_type"), 1,
2426
- INT2NUM(( signed_side == 2 ) ? CA_INT64 : CA_UINT64));
2680
+ l = rb_ca_wrap_readonly(self,
2681
+ INT2NUM(( signed_side == 1 ) ? CA_INT64 : CA_UINT64));
2682
+ r = rb_ca_wrap_readonly(other,
2683
+ INT2NUM(( signed_side == 2 ) ? CA_INT64 : CA_UINT64));
2427
2684
  ca_broadcast_pair(&l, &r);
2428
2685
 
2429
2686
  /* A fresh uint64 entity holding the signed operand modulo 2**64; read
2430
2687
  as int64 it still says which cells are negative. */
2431
- su = rb_funcall(( signed_side == 1 ) ? l : r, rb_intern("to_type"), 1,
2432
- INT2NUM(CA_UINT64));
2688
+ su = rb_ca_to_type(( signed_side == 1 ) ? l : r, INT2NUM(CA_UINT64), Qnil);
2433
2689
  out = ( signed_side == 1 ) ? rb_ca_call_bincmp(su, r, func, 0.0)
2434
2690
  : rb_ca_call_bincmp(l, su, func, 0.0);
2435
2691
 
2436
- TypedData_Get_Struct(su, CArray, &carray_data_type, cs);
2437
- TypedData_Get_Struct(out, CArray, &carray_data_type, co);
2692
+ GetCArray(su, cs);
2693
+ GetCArray(out, co);
2438
2694
  sp = (int64_t *) cs->ptr;
2439
2695
  op = (boolean8_t *) co->ptr;
2440
2696
  m = co->mask ? (boolean8_t *) co->mask->ptr : NULL;
@@ -2444,15 +2700,13 @@ ca_bincmp_mixed_sign (VALUE self, VALUE other, ca_bincmp_func_t func[],
2444
2700
  op[i] = (boolean8_t) answer;
2445
2701
  }
2446
2702
  }
2447
- RB_GC_GUARD(l);
2448
- RB_GC_GUARD(r);
2449
2703
  return out;
2450
2704
  }
2451
2705
 
2452
2706
  /* The answer of eq (ne = 0) or ne (ne = 1) when no cell can match: false
2453
2707
  (true for ne) in every cell, masked where `self` is masked. */
2454
2708
  VALUE
2455
- ca_bincmp_no_match (VALUE self, int ne)
2709
+ ca_bincmp_no_match (volatile VALUE self, int ne)
2456
2710
  {
2457
2711
  volatile VALUE out;
2458
2712
  CArray *ca, *co;
@@ -2460,12 +2714,13 @@ ca_bincmp_no_match (VALUE self, int ne)
2460
2714
  out = RTEST(rb_obj_is_cscalar(self))
2461
2715
  ? rb_cscalar_new(CA_BOOLEAN, 0, NULL)
2462
2716
  : rb_carray_new(CA_BOOLEAN, ca->ndim, ca->dim, 0, NULL);
2463
- TypedData_Get_Struct(out, CArray, &carray_data_type, co);
2717
+ GetCArray(out, co);
2464
2718
  ca_mask_overlay_safe(co, 1, ca);
2465
2719
  memset(co->ptr, ne ? 1 : 0, co->elements * co->bytes);
2466
2720
  return out;
2467
2721
  }
2468
2722
 
2723
+ /* recursion: re-enters once, for self against self */
2469
2724
  VALUE
2470
2725
  rb_ca_call_bincmp (volatile VALUE self, volatile VALUE other,
2471
2726
  ca_bincmp_func_t func[],
@@ -2483,7 +2738,8 @@ rb_ca_call_bincmp (volatile VALUE self, volatile VALUE other,
2483
2738
  return rb_ca_is_not_masked(self);
2484
2739
  }
2485
2740
  else {
2486
- rb_raise(rb_eArgError, "array can not be compared with UNDEF");
2741
+ rb_raise(rb_eArgError, "%s: UNDEF is compared only with eq and ne",
2742
+ ca_calling_method_name());
2487
2743
  }
2488
2744
  }
2489
2745
 
@@ -2491,8 +2747,8 @@ rb_ca_call_bincmp (volatile VALUE self, volatile VALUE other,
2491
2747
  matches. */
2492
2748
  if ( (ca_bincmp_func_t) func == (ca_bincmp_func_t) ca_bincmp_eq
2493
2749
  || (ca_bincmp_func_t) func == (ca_bincmp_func_t) ca_bincmp_ne ) {
2494
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca1);
2495
- if ( ca_query_matches_nothing(ca1, other) ) {
2750
+ GetCArray(self, ca1);
2751
+ if ( ca_query_matches_nothing(self, ca1, other) ) {
2496
2752
  return ca_bincmp_no_match(self,
2497
2753
  (ca_bincmp_func_t) func == (ca_bincmp_func_t) ca_bincmp_ne);
2498
2754
  }
@@ -2502,7 +2758,7 @@ rb_ca_call_bincmp (volatile VALUE self, volatile VALUE other,
2502
2758
  refuses it. An array that has no ordering at all (complex) keeps its
2503
2759
  own refusal below. */
2504
2760
  if ( ! rb_obj_is_carray(other) && ca_bincmp_op_id_of(func) <= CA_BINCMP_GE ) {
2505
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca1);
2761
+ GetCArray(self, ca1);
2506
2762
  if ( func[ca1->data_type] != ca_bincmp_not_implement ) {
2507
2763
  ca_refuse_foreign_order(other, self);
2508
2764
  }
@@ -2514,6 +2770,22 @@ rb_ca_call_bincmp (volatile VALUE self, volatile VALUE other,
2514
2770
  for fixlen-storage Faces (memcmp is already correct there). */
2515
2771
  ca_face_reconcile_comparison(&self, &other);
2516
2772
 
2773
+ if ( func == ca_bincmp_is_close || func == ca_bincmp_is_equiv ) {
2774
+ switch ( ca_bincmp_tolerance_integer_prepare(self, other) ) {
2775
+ case 1:
2776
+ if ( rb_obj_is_carray(self) ) {
2777
+ self = rb_ca_wrap_readonly(self, INT2NUM(CA_INT64));
2778
+ }
2779
+ if ( rb_obj_is_carray(other) ) {
2780
+ other = rb_ca_wrap_readonly(other, INT2NUM(CA_INT64));
2781
+ }
2782
+ break;
2783
+ case 2:
2784
+ return ca_bincmp_tolerance_by_value(self, other,
2785
+ func == ca_bincmp_is_equiv, tol);
2786
+ }
2787
+ }
2788
+
2517
2789
  {
2518
2790
  int op_id = ca_bincmp_op_id_of(func);
2519
2791
  int signed_side = 0;
@@ -2527,8 +2799,8 @@ rb_ca_call_bincmp (volatile VALUE self, volatile VALUE other,
2527
2799
  /* do implicit casting */
2528
2800
  rb_ca_cast_self_or_other(&self, &other);
2529
2801
 
2530
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca1);
2531
- TypedData_Get_Struct(other, CArray, &carray_data_type, ca2);
2802
+ GetCArray(self, ca1);
2803
+ GetCArray(other, ca2);
2532
2804
  if ( func[ca1->data_type] == ca_bincmp_not_implement ) {
2533
2805
  ca_op_refuse_unimplemented(ca_is_face(ca1) ? self : other, ca1->data_type,
2534
2806
  rb_eTypeError);
@@ -2562,7 +2834,7 @@ rb_ca_call_bincmp (volatile VALUE self, volatile VALUE other,
2562
2834
  out = rb_carray_new(CA_BOOLEAN, ca1->ndim, ca1->dim, 0, NULL);
2563
2835
  n_kernel = ca1->elements; i1 = 1; i2 = 1; i3 = 1;
2564
2836
  }
2565
- TypedData_Get_Struct(out, CArray, &carray_data_type, ca3);
2837
+ GetCArray(out, ca3);
2566
2838
 
2567
2839
  ca_mask_overlay_safe(ca3, 2, ca1, ca2);
2568
2840
 
@@ -2730,34 +3002,36 @@ ca_bincmp_not_implement (ca_size_t n, boolean8_t *m,
2730
3002
  rb_raise(rb_eTypeError, "invalid data_type for bincmp (not implemented)");
2731
3003
  }
2732
3004
 
3005
+ /* CAMath.<f>(x): x.<f> for an array. A number is wrapped in a CScalar, so it
3006
+ gets the answer an array of it gets, and the one cell comes back as a
3007
+ Ruby value: an Integer in the int64 range as int64, a Float as float64, a
3008
+ Complex as cmplx128, any other number (a larger Integer, a Rational) as
3009
+ an object cell. Anything else is refused, as an operator refuses it. */
2733
3010
  VALUE
2734
3011
  ca_math_call (VALUE mod, VALUE arg, ID id)
2735
3012
  {
3013
+ volatile VALUE obj, out;
3014
+ (void) mod;
2736
3015
  if ( rb_obj_is_carray(arg) ) {
2737
3016
  return rb_funcall(arg, id, 0);
2738
3017
  }
2739
- #ifdef HAVE_COMPLEX_H
2740
- else if ( RB_TYPE_P(arg, T_COMPLEX) ) {
2741
- if ( rb_respond_to(arg, id) ) {
2742
- return rb_funcall(arg, id, 0);
2743
- }
2744
- else if ( rb_respond_to(rb_mMath, id) ) {
2745
- return rb_funcall(rb_mMath, id, 1, arg);
2746
- }
2747
- else {
2748
- rb_raise(rb_eTypeError, "unknown method for Math");
2749
- }
3018
+ ca_refuse_ruby_array(arg, "an operand");
3019
+ if ( ! rb_obj_is_kind_of(arg, rb_cNumeric) ) {
3020
+ rb_raise(rb_eTypeError, "%s: %"PRIsVALUE" is not a number or a CArray",
3021
+ ca_calling_method_name(), rb_obj_class(arg));
3022
+ }
3023
+ if ( RB_FIXNUM_P(arg) || RB_FLOAT_TYPE_P(arg) || RB_TYPE_P(arg, T_COMPLEX) ||
3024
+ ( RB_TYPE_P(arg, T_BIGNUM) && rb_absint_numwords(arg, 1, NULL) <= 63 ) ) {
3025
+ obj = rb_ca_cast(arg);
2750
3026
  }
2751
- #endif
2752
3027
  else {
2753
- /* rb_funcall calls even for Math's private method -> infinite loop */
2754
- if ( rb_respond_to(rb_mMath, id) ) {
2755
- return rb_funcall(rb_mMath, id, 1, arg);
2756
- }
2757
- else {
2758
- rb_raise(rb_eTypeError, "unknown method for Math");
2759
- }
3028
+ obj = rb_cscalar_new_with_value(CA_OBJECT, 0, arg);
3029
+ }
3030
+ out = rb_funcall(obj, id, 0);
3031
+ if ( rb_obj_is_cscalar(out) ) {
3032
+ return rb_ca_fetch_addr(out, 0);
2760
3033
  }
3034
+ return out;
2761
3035
  }
2762
3036
 
2763
3037
  /* @overload coerce (other)
@@ -2765,19 +3039,46 @@ ca_math_call (VALUE mod, VALUE arg, ID id)
2765
3039
  [TBD]
2766
3040
  */
2767
3041
 
3042
+ /* recursion: re-enters once, with other converted */
2768
3043
  static VALUE
2769
- rb_ca_coerce (VALUE self, VALUE other)
3044
+ rb_ca_coerce (volatile VALUE self, VALUE other)
2770
3045
  {
2771
3046
  if ( rb_obj_is_carray(other) ) {
2772
3047
  return Qnil;
2773
3048
  }
2774
3049
  else if ( rb_respond_to(other, rb_intern("to_ca")) ) {
2775
- return rb_ca_coerce(self, rb_funcall(other,rb_intern("to_ca"),0));
3050
+ VALUE converted = rb_funcall(other, rb_intern("to_ca"), 0);
3051
+ if ( ! rb_obj_is_carray(converted) ) {
3052
+ rb_raise(rb_eTypeError,
3053
+ "%s: %"PRIsVALUE"#to_ca returned %"PRIsVALUE", not a CArray",
3054
+ ca_calling_method_name(), rb_obj_class(other),
3055
+ rb_obj_class(converted));
3056
+ }
3057
+ return rb_ca_coerce(self, converted);
2776
3058
  }
2777
3059
  else {
2778
- /* do implicit casting */
3060
+ /* A boolean array beside an Integer on its left keeps its type, so the
3061
+ operator decides: 1 / 0 come as true / false, and any other Integer
3062
+ as an int64 the operator promotes the array to (+ - * ...) or
3063
+ refuses (& | ^). */
3064
+ if ( RB_INTEGER_TYPE_P(other) ) {
3065
+ CArray *cs;
3066
+ GetCArray(self, cs);
3067
+ if ( cs->data_type == CA_BOOLEAN && ! ca_is_face(cs) ) {
3068
+ if ( other == INT2FIX(0) || other == INT2FIX(1) ) {
3069
+ other = rb_ca_wrap_readonly(other == INT2FIX(1) ? Qtrue : Qfalse,
3070
+ INT2NUM(CA_BOOLEAN));
3071
+ }
3072
+ else {
3073
+ other = rb_ca_wrap_readonly(other, INT2NUM(CA_INT64));
3074
+ }
3075
+ return rb_ary_new_from_args(2, other, self);
3076
+ }
3077
+ }
3078
+ /* do implicit casting; an Integer the array's type can not hold
3079
+ raises, and the coerce in lib/carray/lazy.rb takes it from there */
2779
3080
  rb_ca_cast_self_or_other(&self, &other);
2780
- return rb_assoc_new(other, self);
3081
+ return rb_ary_new_from_args(2, other, self);
2781
3082
  }
2782
3083
  }
2783
3084