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
@@ -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
 
@@ -1272,7 +1380,7 @@ VALUE
1272
1380
  rb_ca_call_binop (volatile VALUE self, volatile VALUE other,
1273
1381
  ca_binop_func_t func[])
1274
1382
  {
1275
- volatile VALUE out;
1383
+ volatile VALUE out, receiver;
1276
1384
  CArray *ca1, *ca2, *ca3; /* ca3 = ca1.op(ca2) */
1277
1385
  int self_is_scalar, other_is_scalar;
1278
1386
  ca_size_t n_kernel;
@@ -1282,11 +1390,48 @@ rb_ca_call_binop (volatile VALUE self, volatile VALUE other,
1282
1390
  ca_binop_read_boolean_as_truth(func[CA_BOOLEAN] != ca_binop_not_implement,
1283
1391
  &self, &other);
1284
1392
 
1393
+ receiver = rb_obj_is_carray(other) ? Qnil : self;
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
+
1401
+ /* pmax and its kin pick by order, so a value the array can not hold is
1402
+ refused as an ordering refuses it. */
1403
+ if ( ! rb_obj_is_carray(other) && rb_obj_is_carray(self) &&
1404
+ ca_binop_picks_by_order(ca_binop_op_id_of_table(func)) ) {
1405
+ CArray *cs;
1406
+ GetCArray(self, cs);
1407
+ /* a boolean array picks as its 0 / 1 int64 numbers */
1408
+ if ( func[cs->data_type == CA_BOOLEAN ? CA_INT64 : cs->data_type]
1409
+ != ca_binop_not_implement ) {
1410
+ ca_refuse_foreign_order(other, self);
1411
+ }
1412
+ else {
1413
+ /* an array the op is not defined for says so, whatever the operand */
1414
+ ca_op_refuse_unimplemented(self, cs->data_type, rb_eCADataTypeError);
1415
+ }
1416
+ }
1417
+
1285
1418
  /* do implicit casting */
1286
1419
  rb_ca_cast_self_or_other(&self, &other);
1287
1420
 
1288
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca1);
1289
- TypedData_Get_Struct(other, CArray, &carray_data_type, ca2);
1421
+ GetCArray(self, ca1);
1422
+ GetCArray(other, ca2);
1423
+
1424
+ /* A Face whose cells are not Ruby values does not go down to an object
1425
+ array to meet a single Ruby object: an operator it does not define for
1426
+ an array of its own stays undefined. */
1427
+ if ( ca1->data_type == CA_OBJECT && rb_obj_is_carray(receiver) ) {
1428
+ CArray *ca0;
1429
+ GetCArray(receiver, ca0);
1430
+ if ( ca_is_face(ca0) && ca0->data_type == CA_FIXLEN ) {
1431
+ ca_op_refuse_unimplemented(receiver, ca0->data_type,
1432
+ rb_eCADataTypeError);
1433
+ }
1434
+ }
1290
1435
 
1291
1436
  /* Boolean-as-numeric promotion: an arithmetic op (+ - * / % ** ...) has
1292
1437
  no boolean kernel, so its func[CA_BOOLEAN] slot is ca_binop_not_implement.
@@ -1298,8 +1443,8 @@ rb_ca_call_binop (volatile VALUE self, volatile VALUE other,
1298
1443
  if ( ca1->data_type == CA_BOOLEAN && func[CA_BOOLEAN] == ca_binop_not_implement ) {
1299
1444
  self = rb_ca_wrap_readonly(self, INT2NUM(CA_INT64));
1300
1445
  other = rb_ca_wrap_readonly(other, INT2NUM(CA_INT64));
1301
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca1);
1302
- TypedData_Get_Struct(other, CArray, &carray_data_type, ca2);
1446
+ GetCArray(self, ca1);
1447
+ GetCArray(other, ca2);
1303
1448
  }
1304
1449
  if ( func[ca1->data_type] == ca_binop_not_implement ) {
1305
1450
  ca_op_refuse_unimplemented(ca_is_face(ca1) ? self : other, ca1->data_type,
@@ -1362,7 +1507,7 @@ rb_ca_call_binop (volatile VALUE self, volatile VALUE other,
1362
1507
  k.n = n_kernel;
1363
1508
  k.m = ( ca3->mask ) ? (boolean8_t *) ca3->mask->ptr : NULL;
1364
1509
  k.p3 = ca3->ptr; k.i3 = i3;
1365
- 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 */
1366
1511
  k.p1 = ca1->ptr; k.i1 = i1;
1367
1512
  k.p2 = ca2->ptr; k.i2 = i2;
1368
1513
  if ( ca_binop_may_raise(func, ca1->data_type) ) {
@@ -1579,10 +1724,10 @@ rb_ca_call_binop (volatile VALUE self, volatile VALUE other,
1579
1724
  ca_xfer_all(ca2, p2, CA_XFER_GET);
1580
1725
  }
1581
1726
  if ( ca_attach_is_alias(ca1) ) {
1582
- 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 */
1583
1728
  }
1584
1729
  if ( ca_attach_is_alias(ca2) ) {
1585
- 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 */
1586
1731
  }
1587
1732
 
1588
1733
  {
@@ -1658,7 +1803,7 @@ ca_bang_input_may_overlap (CArray *target, CArray *input)
1658
1803
  `other` only.
1659
1804
  self always goes through ca_attach + ca_sync (= write-back to root). */
1660
1805
  VALUE
1661
- 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[])
1662
1807
  {
1663
1808
  CArray *ca1, *ca2; /* ca1.op!(ca2) */
1664
1809
  int self_is_scalar, other_is_scalar;
@@ -1666,11 +1811,24 @@ rb_ca_call_binop_bang (VALUE self, VALUE other, ca_binop_func_t func[])
1666
1811
 
1667
1812
  rb_ca_modify(self);
1668
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
+
1669
1827
  /* do implicit casting */
1670
1828
  rb_ca_cast_other(&self, &other);
1671
1829
 
1672
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca1);
1673
- TypedData_Get_Struct(other, CArray, &carray_data_type, ca2);
1830
+ GetCArray(self, ca1);
1831
+ GetCArray(other, ca2);
1674
1832
  if ( func[ca1->data_type] == ca_binop_not_implement ) {
1675
1833
  ca_op_refuse_unimplemented(self, ca1->data_type, rb_eCADataTypeError);
1676
1834
  }
@@ -1683,7 +1841,7 @@ rb_ca_call_binop_bang (VALUE self, VALUE other, ca_binop_func_t func[])
1683
1841
  the symmetric one a binary operation is held to. */
1684
1842
  if ( !other_is_scalar ) {
1685
1843
  ca_broadcast_to_destination(self, &other);
1686
- TypedData_Get_Struct(other, CArray, &carray_data_type, ca2);
1844
+ GetCArray(other, ca2);
1687
1845
  other_is_scalar = RTEST(rb_obj_is_cscalar(other));
1688
1846
  }
1689
1847
 
@@ -1712,12 +1870,12 @@ rb_ca_call_binop_bang (VALUE self, VALUE other, ca_binop_func_t func[])
1712
1870
  ca_xfer_all(ca2, p2, CA_XFER_GET);
1713
1871
  }
1714
1872
 
1715
- 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
1716
1874
  kernel runs through ca_op_guarded when it can */
1717
1875
  ca_mask_overlay_attached(ca1, 2, ca1, ca2, NULL);
1718
1876
 
1719
1877
  if ( in_place2 ) {
1720
- ca_attach(ca2); /* window: an alias attach, which cannot raise */
1878
+ ca_attach(ca2); /* attach: an alias attach, which cannot raise */
1721
1879
  p2 = ca2->ptr;
1722
1880
  a2 = 1;
1723
1881
  }
@@ -1743,7 +1901,6 @@ rb_ca_call_binop_bang (VALUE self, VALUE other, ca_binop_func_t func[])
1743
1901
  ca_sync_detach(ca1);
1744
1902
 
1745
1903
  /* a cast or broadcast view of other is reached only through ca2 */
1746
- RB_GC_GUARD(other);
1747
1904
  return self;
1748
1905
  }
1749
1906
 
@@ -1795,7 +1952,7 @@ rb_ca_triop_select_template (VALUE self, VALUE other2, VALUE other3,
1795
1952
 
1796
1953
  #define EAGER_ATTACH_INPUT(ca_, p_, attached_) do { \
1797
1954
  if ( ca_attach_is_alias(ca_) ) { \
1798
- ca_attach(ca_); /* window: an alias attach, cannot raise */ \
1955
+ ca_attach(ca_); /* attach: an alias attach, cannot raise */ \
1799
1956
  (p_) = (char *)(ca_)->ptr; \
1800
1957
  (attached_) = 1; \
1801
1958
  } \
@@ -1846,27 +2003,82 @@ ca_triop_chunk_kernel (ca_chunked_run_t *run, ca_size_t off, ca_size_t n,
1846
2003
  }
1847
2004
  }
1848
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
+
1849
2066
  VALUE
1850
- rb_ca_call_triop (VALUE self, VALUE other2, VALUE other3,
2067
+ rb_ca_call_triop (volatile VALUE self, volatile VALUE other2, volatile VALUE other3,
1851
2068
  ca_triop_func_t func[])
1852
2069
  {
1853
2070
  volatile VALUE out;
1854
2071
  CArray *ca1, *ca2, *ca3, *cao;
1855
2072
 
1856
- /* Pairwise data_type promotion: ((self, other2) -> common), then
1857
- ((self', other3) -> common). Mirrors how the binop driver normalises
1858
- two operands; for triop we apply it twice. After this, all three
1859
- CArrays share the same data_type (and unbound-repeats are resolved). */
1860
- rb_ca_cast_self_or_other(&self, &other2);
1861
- rb_ca_cast_self_or_other(&self, &other3);
1862
- rb_ca_cast_self_or_other(&other2, &other3);
1863
- /* one more pass to re-normalise self vs other2 in case the
1864
- other2/other3 cast widened other2 above self's data_type */
1865
- rb_ca_cast_self_or_other(&self, &other2);
1866
-
1867
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca1);
1868
- TypedData_Get_Struct(other2, CArray, &carray_data_type, ca2);
1869
- TypedData_Get_Struct(other3, CArray, &carray_data_type, ca3);
2073
+ ca_triop_scalar_operands((volatile VALUE *) &self, (volatile VALUE *) &other2,
2074
+ (volatile VALUE *) &other3, func);
2075
+
2076
+ /* After this, all three CArrays share the same data_type. */
2077
+ rb_ca_cast_three(&self, &other2, &other3);
2078
+
2079
+ GetCArray(self, ca1);
2080
+ GetCArray(other2, ca2);
2081
+ GetCArray(other3, ca3);
1870
2082
 
1871
2083
  /* Boolean-as-numeric promotion (same rule as rb_ca_call_binop): an
1872
2084
  arithmetic triop (fma / fms) has no boolean kernel, so its
@@ -1880,9 +2092,9 @@ rb_ca_call_triop (VALUE self, VALUE other2, VALUE other3,
1880
2092
  self = rb_ca_wrap_readonly(self, INT2NUM(CA_INT64));
1881
2093
  other2 = rb_ca_wrap_readonly(other2, INT2NUM(CA_INT64));
1882
2094
  other3 = rb_ca_wrap_readonly(other3, INT2NUM(CA_INT64));
1883
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca1);
1884
- TypedData_Get_Struct(other2, CArray, &carray_data_type, ca2);
1885
- TypedData_Get_Struct(other3, CArray, &carray_data_type, ca3);
2095
+ GetCArray(self, ca1);
2096
+ GetCArray(other2, ca2);
2097
+ GetCArray(other3, ca3);
1886
2098
  }
1887
2099
  if ( func[ca1->data_type] == ca_triop_not_implement ) {
1888
2100
  ca_op_refuse_unimplemented(ca_is_face(ca1) ? self
@@ -1911,7 +2123,7 @@ rb_ca_call_triop (VALUE self, VALUE other2, VALUE other3,
1911
2123
  }
1912
2124
 
1913
2125
  out = rb_ca_triop_select_template(self, other2, other3, ca1, ca2, ca3);
1914
- TypedData_Get_Struct(out, CArray, &carray_data_type, cao);
2126
+ GetCArray(out, cao);
1915
2127
 
1916
2128
  ca_mask_overlay_safe(cao, 3, ca1, ca2, ca3);
1917
2129
 
@@ -2011,9 +2223,6 @@ rb_ca_call_triop (VALUE self, VALUE other2, VALUE other3,
2011
2223
  }
2012
2224
 
2013
2225
  /* the INT64 views of a boolean triop are reached only through ca1..ca3 */
2014
- RB_GC_GUARD(self);
2015
- RB_GC_GUARD(other2);
2016
- RB_GC_GUARD(other3);
2017
2226
  return out;
2018
2227
  }
2019
2228
 
@@ -2028,19 +2237,22 @@ ca_triop_bang_chunked (VALUE arg)
2028
2237
  }
2029
2238
 
2030
2239
  VALUE
2031
- 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,
2032
2241
  ca_triop_func_t func[])
2033
2242
  {
2034
2243
  CArray *ca1, *ca2, *ca3;
2035
2244
 
2036
2245
  rb_ca_modify(self);
2037
2246
 
2247
+ ca_triop_scalar_operands((volatile VALUE *) &self, (volatile VALUE *) &other2,
2248
+ (volatile VALUE *) &other3, func);
2249
+
2038
2250
  rb_ca_cast_other(&self, &other2);
2039
2251
  rb_ca_cast_other(&self, &other3);
2040
2252
 
2041
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca1);
2042
- TypedData_Get_Struct(other2, CArray, &carray_data_type, ca2);
2043
- TypedData_Get_Struct(other3, CArray, &carray_data_type, ca3);
2253
+ GetCArray(self, ca1);
2254
+ GetCArray(other2, ca2);
2255
+ GetCArray(other3, ca3);
2044
2256
  if ( func[ca1->data_type] == ca_triop_not_implement ) {
2045
2257
  ca_op_refuse_unimplemented(self, ca1->data_type, rb_eCADataTypeError);
2046
2258
  }
@@ -2049,11 +2261,11 @@ rb_ca_call_triop_bang (VALUE self, VALUE other2, VALUE other3,
2049
2261
  input operand in turn (the same one assignment uses). */
2050
2262
  if ( ! rb_obj_is_cscalar(other2) ) {
2051
2263
  ca_broadcast_to_destination(self, &other2);
2052
- TypedData_Get_Struct(other2, CArray, &carray_data_type, ca2);
2264
+ GetCArray(other2, ca2);
2053
2265
  }
2054
2266
  if ( ! rb_obj_is_cscalar(other3) ) {
2055
2267
  ca_broadcast_to_destination(self, &other3);
2056
- TypedData_Get_Struct(other3, CArray, &carray_data_type, ca3);
2268
+ GetCArray(other3, ca3);
2057
2269
  }
2058
2270
 
2059
2271
  {
@@ -2097,18 +2309,18 @@ rb_ca_call_triop_bang (VALUE self, VALUE other2, VALUE other3,
2097
2309
 
2098
2310
  /* self IS the output (= write target; attach legit per refined
2099
2311
  invariant) */
2100
- 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
2101
2313
  kernel runs through ca_op_guarded */
2102
2314
  ca_mask_overlay_attached(ca1, 3, ca1, ca2, ca3);
2103
2315
 
2104
2316
  attached2 = attached3 = 0;
2105
2317
  if ( in_place2 ) {
2106
- ca_attach(ca2); /* window: an alias attach, cannot raise */
2318
+ ca_attach(ca2); /* attach: an alias attach, cannot raise */
2107
2319
  p2 = (char *) ca2->ptr;
2108
2320
  attached2 = 1;
2109
2321
  }
2110
2322
  if ( in_place3 ) {
2111
- ca_attach(ca3); /* window: as above */
2323
+ ca_attach(ca3); /* attach: as above */
2112
2324
  p3 = (char *) ca3->ptr;
2113
2325
  attached3 = 1;
2114
2326
  }
@@ -2140,7 +2352,7 @@ rb_ca_call_triop_bang (VALUE self, VALUE other2, VALUE other3,
2140
2352
  ca1->ptr + off*bytes in chunks; sync at end. The run gathers
2141
2353
  per chunk, and any gather can raise, so the run is guarded
2142
2354
  whatever the kernel. */
2143
- 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
2144
2356
  kernel runs through ca_op_guarded */
2145
2357
  ca_mask_overlay_attached(ca1, 3, ca1, ca2, ca3);
2146
2358
  {
@@ -2179,20 +2391,18 @@ rb_ca_call_triop_bang (VALUE self, VALUE other2, VALUE other3,
2179
2391
 
2180
2392
  /* cast or broadcast views of the operands are reached only through
2181
2393
  ca2 and ca3 */
2182
- RB_GC_GUARD(other2);
2183
- RB_GC_GUARD(other3);
2184
2394
  return self;
2185
2395
  }
2186
2396
 
2187
2397
  /* moncmp driver. ca1 input-only (EAGER_ACQUIRE/RELEASE fast/slow), ca2 =
2188
2398
  new boolean entity output (attach legit). */
2189
2399
  VALUE
2190
- rb_ca_call_moncmp (VALUE self, ca_moncmp_func_t func[])
2400
+ rb_ca_call_moncmp (volatile VALUE self, ca_moncmp_func_t func[])
2191
2401
  {
2192
2402
  volatile VALUE out;
2193
2403
  CArray *ca1, *ca2; /* ca2 = ca1.op */
2194
2404
 
2195
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca1);
2405
+ GetCArray(self, ca1);
2196
2406
  if ( func[ca1->data_type] == ca_moncmp_not_implement ) {
2197
2407
  ca_op_refuse_unimplemented(self, ca1->data_type, rb_eCADataTypeError);
2198
2408
  }
@@ -2204,7 +2414,7 @@ rb_ca_call_moncmp (VALUE self, ca_moncmp_func_t func[])
2204
2414
  out = rb_carray_new(CA_BOOLEAN, ca1->ndim, ca1->dim, 0, NULL);
2205
2415
  }
2206
2416
 
2207
- TypedData_Get_Struct(out, CArray, &carray_data_type, ca2);
2417
+ GetCArray(out, ca2);
2208
2418
 
2209
2419
  ca_mask_overlay_safe(ca2, 1, ca1);
2210
2420
 
@@ -2316,16 +2526,31 @@ ca_bincmp_answer_when_negative (int op_id, int signed_side)
2316
2526
  }
2317
2527
 
2318
2528
  static int
2319
- ca_bincmp_plain_integer_array (VALUE v, int8_t *dt)
2529
+ ca_bincmp_plain_integer_array (volatile VALUE v, int8_t *dt)
2320
2530
  {
2321
2531
  CArray *ca;
2322
2532
  if ( ! rb_obj_is_carray(v) ) return 0;
2323
- TypedData_Get_Struct(v, CArray, &carray_data_type, ca);
2533
+ GetCArray(v, ca);
2324
2534
  if ( ca_is_face(ca) ) return 0;
2325
2535
  *dt = ca->data_type;
2326
2536
  return ca_is_unsigned_integer_dt(*dt) || ca_is_signed_integer_dt(*dt);
2327
2537
  }
2328
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
+
2329
2554
  /* Prepares the operands of comparison op_id (CA_BINCMP_*). Returns
2330
2555
  0 nothing to do,
2331
2556
  1 every cell answers true, 2 every cell answers false
@@ -2342,15 +2567,18 @@ ca_bincmp_integer_prepare (volatile VALUE *self, volatile VALUE *other,
2342
2567
 
2343
2568
  if ( op_id < CA_BINCMP_LT || op_id > CA_BINCMP_NE ) return 0;
2344
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
+
2345
2573
  if ( RB_INTEGER_TYPE_P(*other) &&
2346
- ca_bincmp_plain_integer_array(*self, &dt1) ) {
2574
+ ca_bincmp_integer_scalar_reference(*self, &dt1) ) {
2347
2575
  side = ca_integer_range_side(*other, dt1);
2348
2576
  if ( side == 0 ) return 0;
2349
2577
  *other = *self;
2350
2578
  return ca_bincmp_ordered_answer(op_id, side > 0) ? 1 : 2;
2351
2579
  }
2352
2580
  if ( RB_INTEGER_TYPE_P(*self) &&
2353
- ca_bincmp_plain_integer_array(*other, &dt2) ) {
2581
+ ca_bincmp_integer_scalar_reference(*other, &dt2) ) {
2354
2582
  side = ca_integer_range_side(*self, dt2);
2355
2583
  if ( side == 0 ) return 0;
2356
2584
  *self = *other;
@@ -2371,6 +2599,58 @@ ca_bincmp_integer_prepare (volatile VALUE *self, volatile VALUE *other,
2371
2599
  return 3;
2372
2600
  }
2373
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
+
2374
2654
  static int
2375
2655
  ca_bincmp_op_id_of (ca_bincmp_func_t *func)
2376
2656
  {
@@ -2397,21 +2677,20 @@ ca_bincmp_mixed_sign (VALUE self, VALUE other, ca_bincmp_func_t func[],
2397
2677
  ca_size_t i, step;
2398
2678
  int answer = ca_bincmp_answer_when_negative(op_id, signed_side);
2399
2679
 
2400
- l = rb_funcall(self, rb_intern("to_type"), 1,
2401
- INT2NUM(( signed_side == 1 ) ? CA_INT64 : CA_UINT64));
2402
- r = rb_funcall(other, rb_intern("to_type"), 1,
2403
- 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));
2404
2684
  ca_broadcast_pair(&l, &r);
2405
2685
 
2406
2686
  /* A fresh uint64 entity holding the signed operand modulo 2**64; read
2407
2687
  as int64 it still says which cells are negative. */
2408
- su = rb_funcall(( signed_side == 1 ) ? l : r, rb_intern("to_type"), 1,
2409
- INT2NUM(CA_UINT64));
2688
+ su = rb_ca_to_type(( signed_side == 1 ) ? l : r, INT2NUM(CA_UINT64), Qnil);
2410
2689
  out = ( signed_side == 1 ) ? rb_ca_call_bincmp(su, r, func, 0.0)
2411
2690
  : rb_ca_call_bincmp(l, su, func, 0.0);
2412
2691
 
2413
- TypedData_Get_Struct(su, CArray, &carray_data_type, cs);
2414
- TypedData_Get_Struct(out, CArray, &carray_data_type, co);
2692
+ GetCArray(su, cs);
2693
+ GetCArray(out, co);
2415
2694
  sp = (int64_t *) cs->ptr;
2416
2695
  op = (boolean8_t *) co->ptr;
2417
2696
  m = co->mask ? (boolean8_t *) co->mask->ptr : NULL;
@@ -2421,11 +2700,27 @@ ca_bincmp_mixed_sign (VALUE self, VALUE other, ca_bincmp_func_t func[],
2421
2700
  op[i] = (boolean8_t) answer;
2422
2701
  }
2423
2702
  }
2424
- RB_GC_GUARD(l);
2425
- RB_GC_GUARD(r);
2426
2703
  return out;
2427
2704
  }
2428
2705
 
2706
+ /* The answer of eq (ne = 0) or ne (ne = 1) when no cell can match: false
2707
+ (true for ne) in every cell, masked where `self` is masked. */
2708
+ VALUE
2709
+ ca_bincmp_no_match (volatile VALUE self, int ne)
2710
+ {
2711
+ volatile VALUE out;
2712
+ CArray *ca, *co;
2713
+ GetCArray(self, ca);
2714
+ out = RTEST(rb_obj_is_cscalar(self))
2715
+ ? rb_cscalar_new(CA_BOOLEAN, 0, NULL)
2716
+ : rb_carray_new(CA_BOOLEAN, ca->ndim, ca->dim, 0, NULL);
2717
+ GetCArray(out, co);
2718
+ ca_mask_overlay_safe(co, 1, ca);
2719
+ memset(co->ptr, ne ? 1 : 0, co->elements * co->bytes);
2720
+ return out;
2721
+ }
2722
+
2723
+ /* recursion: re-enters once, for self against self */
2429
2724
  VALUE
2430
2725
  rb_ca_call_bincmp (volatile VALUE self, volatile VALUE other,
2431
2726
  ca_bincmp_func_t func[],
@@ -2443,7 +2738,29 @@ rb_ca_call_bincmp (volatile VALUE self, volatile VALUE other,
2443
2738
  return rb_ca_is_not_masked(self);
2444
2739
  }
2445
2740
  else {
2446
- 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());
2743
+ }
2744
+ }
2745
+
2746
+ /* eq and ne with a value the array can not hold (nil, a String): no cell
2747
+ matches. */
2748
+ if ( (ca_bincmp_func_t) func == (ca_bincmp_func_t) ca_bincmp_eq
2749
+ || (ca_bincmp_func_t) func == (ca_bincmp_func_t) ca_bincmp_ne ) {
2750
+ GetCArray(self, ca1);
2751
+ if ( ca_query_matches_nothing(self, ca1, other) ) {
2752
+ return ca_bincmp_no_match(self,
2753
+ (ca_bincmp_func_t) func == (ca_bincmp_func_t) ca_bincmp_ne);
2754
+ }
2755
+ }
2756
+
2757
+ /* An ordering with a value the array can not hold is refused as Float#<
2758
+ refuses it. An array that has no ordering at all (complex) keeps its
2759
+ own refusal below. */
2760
+ if ( ! rb_obj_is_carray(other) && ca_bincmp_op_id_of(func) <= CA_BINCMP_GE ) {
2761
+ GetCArray(self, ca1);
2762
+ if ( func[ca1->data_type] != ca_bincmp_not_implement ) {
2763
+ ca_refuse_foreign_order(other, self);
2447
2764
  }
2448
2765
  }
2449
2766
 
@@ -2453,6 +2770,22 @@ rb_ca_call_bincmp (volatile VALUE self, volatile VALUE other,
2453
2770
  for fixlen-storage Faces (memcmp is already correct there). */
2454
2771
  ca_face_reconcile_comparison(&self, &other);
2455
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
+
2456
2789
  {
2457
2790
  int op_id = ca_bincmp_op_id_of(func);
2458
2791
  int signed_side = 0;
@@ -2466,8 +2799,8 @@ rb_ca_call_bincmp (volatile VALUE self, volatile VALUE other,
2466
2799
  /* do implicit casting */
2467
2800
  rb_ca_cast_self_or_other(&self, &other);
2468
2801
 
2469
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca1);
2470
- TypedData_Get_Struct(other, CArray, &carray_data_type, ca2);
2802
+ GetCArray(self, ca1);
2803
+ GetCArray(other, ca2);
2471
2804
  if ( func[ca1->data_type] == ca_bincmp_not_implement ) {
2472
2805
  ca_op_refuse_unimplemented(ca_is_face(ca1) ? self : other, ca1->data_type,
2473
2806
  rb_eTypeError);
@@ -2501,7 +2834,7 @@ rb_ca_call_bincmp (volatile VALUE self, volatile VALUE other,
2501
2834
  out = rb_carray_new(CA_BOOLEAN, ca1->ndim, ca1->dim, 0, NULL);
2502
2835
  n_kernel = ca1->elements; i1 = 1; i2 = 1; i3 = 1;
2503
2836
  }
2504
- TypedData_Get_Struct(out, CArray, &carray_data_type, ca3);
2837
+ GetCArray(out, ca3);
2505
2838
 
2506
2839
  ca_mask_overlay_safe(ca3, 2, ca1, ca2);
2507
2840
 
@@ -2669,34 +3002,36 @@ ca_bincmp_not_implement (ca_size_t n, boolean8_t *m,
2669
3002
  rb_raise(rb_eTypeError, "invalid data_type for bincmp (not implemented)");
2670
3003
  }
2671
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. */
2672
3010
  VALUE
2673
3011
  ca_math_call (VALUE mod, VALUE arg, ID id)
2674
3012
  {
3013
+ volatile VALUE obj, out;
3014
+ (void) mod;
2675
3015
  if ( rb_obj_is_carray(arg) ) {
2676
3016
  return rb_funcall(arg, id, 0);
2677
3017
  }
2678
- #ifdef HAVE_COMPLEX_H
2679
- else if ( RB_TYPE_P(arg, T_COMPLEX) ) {
2680
- if ( rb_respond_to(arg, id) ) {
2681
- return rb_funcall(arg, id, 0);
2682
- }
2683
- else if ( rb_respond_to(rb_mMath, id) ) {
2684
- return rb_funcall(rb_mMath, id, 1, arg);
2685
- }
2686
- else {
2687
- rb_raise(rb_eTypeError, "unknown method for Math");
2688
- }
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);
2689
3026
  }
2690
- #endif
2691
3027
  else {
2692
- /* rb_funcall calls even for Math's private method -> infinite loop */
2693
- if ( rb_respond_to(rb_mMath, id) ) {
2694
- return rb_funcall(rb_mMath, id, 1, arg);
2695
- }
2696
- else {
2697
- rb_raise(rb_eTypeError, "unknown method for Math");
2698
- }
3028
+ obj = rb_cscalar_new_with_value(CA_OBJECT, 0, arg);
2699
3029
  }
3030
+ out = rb_funcall(obj, id, 0);
3031
+ if ( rb_obj_is_cscalar(out) ) {
3032
+ return rb_ca_fetch_addr(out, 0);
3033
+ }
3034
+ return out;
2700
3035
  }
2701
3036
 
2702
3037
  /* @overload coerce (other)
@@ -2704,19 +3039,46 @@ ca_math_call (VALUE mod, VALUE arg, ID id)
2704
3039
  [TBD]
2705
3040
  */
2706
3041
 
3042
+ /* recursion: re-enters once, with other converted */
2707
3043
  static VALUE
2708
- rb_ca_coerce (VALUE self, VALUE other)
3044
+ rb_ca_coerce (volatile VALUE self, VALUE other)
2709
3045
  {
2710
3046
  if ( rb_obj_is_carray(other) ) {
2711
3047
  return Qnil;
2712
3048
  }
2713
3049
  else if ( rb_respond_to(other, rb_intern("to_ca")) ) {
2714
- 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);
2715
3058
  }
2716
3059
  else {
2717
- /* 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 */
2718
3080
  rb_ca_cast_self_or_other(&self, &other);
2719
- return rb_assoc_new(other, self);
3081
+ return rb_ary_new_from_args(2, other, self);
2720
3082
  }
2721
3083
  }
2722
3084