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
@@ -1,32 +1,864 @@
1
1
  /* ---------------------------------------------------------------------------
2
2
 
3
- carray_internal.h -- declarations that are only ever called by CArray's
4
- own translation units, kept out of the public surface.
5
-
6
- carray.h is what a downstream gem gets from `#include "carray.h"`, so
7
- everything it declares is a contract. Symbols with no external caller
8
- live here instead, and are moved in one audited cluster at a time (see
9
- devel/PROPOSAL_CARRAY_H_REORG.md for the layering and the backlog).
10
-
11
- This header is NOT installed (see ext/extconf.rb $INSTALLFILES) and must
12
- NOT be included by carray.h -- including it there would put every symbol
13
- below straight back into the public closure. Wiring is per-move: a .c
14
- gains `#include "carray_internal.h"` when it consumes a symbol that has
15
- landed here.
3
+ carray_internal.h -- carray's own declarations, for carray's own
4
+ translation units only.
5
+
6
+ carray.h is what an extension gets from `#include "carray.h"`, so
7
+ everything it declares is a contract. The declarations carray's own
8
+ sources use besides those -- the structs, tags and classes of its views,
9
+ the bodies of its Ruby methods, the parts its operation slots are built
10
+ from -- are here instead.
11
+
12
+ carray.h includes this header at its end, after carray_math_kernel.h
13
+ and ca_kernel_iterator.h, when CARRAY_BUILD is defined, which it is for
14
+ every source of carray, so those sources get these declarations from
15
+ `#include "carray.h"`. This header relies on everything carray.h
16
+ declares, ca_iter_state included, and does not include carray.h
17
+ itself. An extension is built without CARRAY_BUILD and never reads it.
18
+
19
+ It includes carray's other internal headers for the kernel iterator:
20
+ ca_axis_descriptor.h (the per-axis descriptors; ca_kernel_iterator.h
21
+ has already included it for the engine's fields of ca_iter_state),
22
+ ca_iter_substrate.h and ca_kernel_iterator_internal.h. Its Face section
23
+ includes ca_obj_face.h for the hook types that section uses.
24
+
25
+ This header carries no contract: anything in it may change or go away
26
+ in any release, 3.x patch releases included. It is not installed (see
27
+ ext/extconf.rb $INSTALLFILES). It ships in the gem's ext/ directory only
28
+ because carray is built from it there; an extension that puts that
29
+ directory on its include path must still not include it.
16
30
 
17
31
  Some internal clusters keep their own non-installed header rather than
18
32
  folding in (ca_sort_kernels.h, ca_op_powi.h, ca_composite_dispatch.h);
19
- what matters is only that they stay out of carray.h and out of the
20
- install list.
33
+ what matters is only that they stay out of the installed headers.
21
34
 
22
35
  ---------------------------------------------------------------------------- */
23
36
 
24
37
  #ifndef CARRAY_INTERNAL_H
25
38
  #define CARRAY_INTERNAL_H
26
39
 
27
- #include "carray.h"
40
+ #include "ruby/thread_native.h" /* rb_nativethread_lock_t */
28
41
  #include <math.h> /* isnan, for CA_OBJ_ISNAN */
29
42
 
43
+ /* ---- Cell types, limits, flag bits and enums ------------------------- */
44
+
45
+ /* Enumerator::ArithmeticSequence, cached for carray's index parsing. An
46
+ extension looks the class up with rb_const_get. */
47
+ #ifdef HAVE_RB_ARITHMETIC_SEQUENCE_EXTRACT
48
+ extern VALUE rb_cArithSeq;
49
+ #endif
50
+
51
+ /* The unaligned cell types carray's own kernels use, one for each cell
52
+ type. An extension declares its own with CA_UNALIGNED. */
53
+ typedef boolean8_t ca_u_boolean8_t;
54
+ typedef int8_t ca_u_int8_t;
55
+ typedef uint8_t ca_u_uint8_t;
56
+ typedef int16_t ca_u_int16_t CA_UNALIGNED;
57
+ typedef uint16_t ca_u_uint16_t CA_UNALIGNED;
58
+ typedef int32_t ca_u_int32_t CA_UNALIGNED;
59
+ typedef uint32_t ca_u_uint32_t CA_UNALIGNED;
60
+ typedef int64_t ca_u_int64_t CA_UNALIGNED;
61
+ typedef uint64_t ca_u_uint64_t CA_UNALIGNED;
62
+ typedef float32_t ca_u_float32_t CA_UNALIGNED;
63
+ typedef float64_t ca_u_float64_t CA_UNALIGNED;
64
+ typedef cmplx64_t ca_u_cmplx64_t CA_UNALIGNED;
65
+ typedef cmplx128_t ca_u_cmplx128_t CA_UNALIGNED;
66
+ typedef VALUE ca_u_VALUE CA_UNALIGNED;
67
+
68
+ typedef ca_u_float32_t ca_u_float;
69
+ typedef ca_u_float64_t ca_u_double;
70
+
71
+ /* CA_U(float64_t) is ca_u_float64_t (and CA_U(double) ca_u_double): for a
72
+ macro that takes the cell type. */
73
+ #define CA_U(type) ca_u_##type
74
+
75
+ /* The limit on a view's attach count, and an optimizer hint, both for
76
+ carray's own sources. */
77
+ #define CA_ATTACH_MAX 0x80000000
78
+
79
+ /* Assert an invariant to the optimizer (zero runtime cost). Used to teach
80
+ GCC's value-range propagation facts it cannot derive itself -- chiefly
81
+ `ndim <= CA_RANK_MAX`, which bounds loops and allocations over fixed
82
+ [CA_RANK_MAX] stack arrays. Without this, GCC (11.x) emits false-positive
83
+ -Wstringop-overflow / -Wmaybe-uninitialized / -Walloc-size-larger-than on
84
+ those loops because `int8_t ndim` ranges up to 127 as far as it can see.
85
+ The asserted conditions are hard invariants (CA_CHECK_RANK enforces them at
86
+ array creation); a violation is genuinely unreachable. */
87
+ #if defined(__GNUC__) || defined(__clang__)
88
+ # define CA_ASSUME(cond) do { if ( !(cond) ) __builtin_unreachable(); } while (0)
89
+ #else
90
+ # define CA_ASSUME(cond) ((void)0)
91
+ #endif
92
+
93
+ /* CALazyMarker. A marker layers "read this as the leaf of a lazy chain"
94
+ over storage that is identical to its parent's — the same premise as
95
+ CA_FLAG_IS_FACE — so the two are asked about together wherever a
96
+ storage-identical wrapper has to be seen through. Kept as its own bit
97
+ rather than folded into CA_FLAG_IS_FACE: ca_is_face is read in ~100
98
+ places that mean the Face mechanism specifically. The marker is carray's
99
+ own; an extension does not test this bit. */
100
+ #define CA_FLAG_IS_LAZY_MARKER 1024
101
+
102
+ /* The values of ca_endian, the host's byte order, for carray's own sources.
103
+ An extension that needs the byte order tests it itself. */
104
+ enum {
105
+ CA_LITTLE_ENDIAN = 0,
106
+ CA_BIG_ENDIAN = 1
107
+ };
108
+
109
+ /* The boundary rules of CAWindow and the axis descriptors, for carray's own
110
+ views. */
111
+ enum {
112
+ CA_BOUNDS_RUBY = 1,
113
+ CA_BOUNDS_STRICT,
114
+ CA_BOUNDS_NEAREST,
115
+ CA_BOUNDS_PERIODIC,
116
+ CA_BOUNDS_REFLECT,
117
+ CA_BOUNDS_FILL,
118
+ CA_BOUNDS_MASK,
119
+ }; /* CA_BOUNDS_TYPE for CAWindow */
120
+
121
+ /* Per-axis descriptor framework: types, producer interface, and common
122
+ attach engine. ca_kernel_iterator.h has already included it, for the
123
+ engine's fields of ca_iter_state. */
124
+ #include "ca_axis_descriptor.h"
125
+
126
+ /* ---- The entity's operation slots and the structs of carray's views ---- */
127
+
128
+ /* The entity's operation slots. An extension starts its own table from a
129
+ copy of the entity's, ca_operation_table_copy(CA_OBJ_ARRAY, ...), and
130
+ replaces slots in it; it does not call these slots by name. */
131
+ void * ca_array_func_clone (void *ap);
132
+ void ca_array_func_allocate (void *ap);
133
+ void ca_array_func_attach (void *ap);
134
+ void ca_array_func_sync (void *ap);
135
+ void ca_array_func_detach (void *ap);
136
+ void ca_array_func_xfer_index (void *ap, ca_size_t *idx, void *data, int dir);
137
+ void ca_array_func_xfer_addrs (void *ap, ca_size_t n, ca_size_t *addrs,
138
+ void *data, int dir);
139
+ void ca_array_func_xfer_all (void *ap, void *data, int dir);
140
+ void ca_array_func_fill_data (void *ap, void *val);
141
+ void ca_array_func_create_mask (void *ap);
142
+
143
+ /* The struct of CScalar; an extension makes one with rb_cscalar_new and
144
+ reads it as a CArray. */
145
+ typedef struct {
146
+ CA_ARRAY_HEADER
147
+ ca_size_t _dim;
148
+ } CScalar;
149
+
150
+ /* CABlock: CAStride prefix + native-spec tail.
151
+ The tail (offset, start, step, count, size0) describes the block in the
152
+ parent's terms and serves:
153
+ - the Ruby accessors #offset, #start, #step and #size0
154
+ - #idx2addr0 / #addr2addr0
155
+ Code that changes `start[]` must recompute base_offset
156
+ (ca_block_sync_base_offset) to keep the CAStride prefix consistent:
157
+ the transfer slots, taken from CAStride, read only the prefix. */
158
+ typedef struct {
159
+ CA_ARRAY_HEADER
160
+ CArray *parent;
161
+ uint32_t attach;
162
+ uint8_t nosync;
163
+ /* --- CAStride prefix continues --- */
164
+ ca_size_t *strides;
165
+ ca_size_t base_offset;
166
+ /* --- CABlock tail (native spec) --- */
167
+ ca_size_t offset;
168
+ ca_size_t *start;
169
+ ca_size_t *step;
170
+ ca_size_t *count;
171
+ ca_size_t *size0;
172
+ } CABlock;
173
+
174
+ /* The structs of the views carray itself defines. An extension does not
175
+ define a view of these kinds or read their tails, so only sources built
176
+ with carray see them. */
177
+ typedef struct {
178
+ CA_ARRAY_HEADER
179
+ CArray *parent;
180
+ uint32_t attach;
181
+ uint8_t nosync;
182
+ /* ---------- */
183
+ uint8_t *bounds; /* [ndim] per-axis policy. Per-axis so that
184
+ CAShift (per-axis roll[]) can be expressed as
185
+ a CAWindow specialisation. */
186
+ ca_size_t *start;
187
+ ca_size_t *count;
188
+ ca_size_t *size0;
189
+ char *fill;
190
+ /* Embed model: the window as one alias rectangle inside the parent plus
191
+ its fill complement. Computed by ca_window_recompute_embed when the
192
+ window is set up. Attach and sync use it to copy the rectangle and
193
+ fill the rest with no per-element bound check. */
194
+ ca_size_t *embed_parent_start; /* [ndim] parent-side alias rectangle start */
195
+ ca_size_t *embed_count; /* [ndim] alias rectangle size per axis */
196
+ ca_size_t *embed_output_offset; /* [ndim] output-side alias rectangle start */
197
+ uint8_t embed_is_empty; /* 1 = fully out of parent, alias is empty */
198
+ uint8_t embed_covers_all; /* 1 = interior-only, fill is unused */
199
+ uint8_t embed_eligible; /* 1 = all bounds in {FILL, MASK}, attach
200
+ takes the embed fast path. Other
201
+ policies (PERIODIC/REFLECT/...) fall
202
+ back to the ca_axis_dispatch_* engine. */
203
+ uint8_t embed_alias_eligible; /* 1 = direct ca_attach(window) can alias
204
+ to parent->ptr without copy.
205
+ Condition: embed_eligible
206
+ + embed_covers_all + inner axes full
207
+ (count[k] == parent->dim[k] && start[k]
208
+ == 0 for k >= 1). Embedded region
209
+ must be contig in parent storage. */
210
+ /* A window over a parent that has no mask: the all-false mask its own
211
+ mask reads instead, so that masking the out-of-bounds cells gives the
212
+ parent no mask. Owned by this window; NULL otherwise. */
213
+ CArray *mask_standin;
214
+ /* On the mask of a window: the window whose mask it is, which holds the
215
+ stand-in and the parent whose mask replaces it. NULL otherwise. */
216
+ CArray *mask_owner;
217
+ } CAWindow; /* 56 + 16*(ndim) + 1*(bytes) + 1*ndim + 24*ndim (bytes) */
218
+
219
+ /* CAShift is a pure typedef of CAWindow. CAShift construction maps (shift[], roll[], fill,
220
+ fill_mask) onto CAWindow's (start = -shift, count = parent->dim,
221
+ bounds = roll ? PERIODIC : (fill_mask ? MASK : FILL), fill). The
222
+ two views share the operation table (ca_window_func); only obj_type
223
+ differs (CA_OBJ_SHIFT vs CA_OBJ_WINDOW) to preserve user-facing class
224
+ distinction. */
225
+ typedef CAWindow CAShift;
226
+
227
+ /* CATile: N-region expansion view (tiled repetition of the parent).
228
+ Output shape = parent.dim * reps per axis; total tiles = product(reps).
229
+ Each tile is a full-parent alias at output offset
230
+ (i_0 * parent.dim[0], i_1 * parent.dim[1], ...). Attach iterates
231
+ total_tiles regions, calling ca_composite_region_gather for each.
232
+
233
+ CARoll is a typedef of CATile, as CAShift is of CAWindow: the C struct
234
+ layout is shared, and CARoll reads `reps[]` as its per-axis shift. Its
235
+ operation table is a copy of CATile's with the slots that depend on
236
+ the shift overridden (ext/ca_obj_roll.c). */
237
+ typedef struct {
238
+ CA_ARRAY_HEADER
239
+ CArray *parent;
240
+ uint32_t attach;
241
+ uint8_t nosync;
242
+ /* ---------- */
243
+ ca_size_t *reps; /* [ndim] number of tiles per axis.
244
+ Total tiles = product(reps).
245
+ dim[k] = parent->dim[k] * reps[k]. */
246
+ } CATile; /* 56 + 8*(ndim) (bytes) */
247
+
248
+ typedef CATile CARoll; /* same layout as CATile, its own obj_type
249
+ CA_OBJ_ROLL (ext/ca_obj_roll.c) */
250
+
251
+ /* CAStack: stack view of K uniform-shape parents. The K axis sits at
252
+ k_axis in the view's shape (0 by default: shape = (K, *parent_shape));
253
+ the other axes are the parents' own.
254
+ - parents stored as Ruby Array @parents on the wrapper object (= GC
255
+ anchor); C-side tail `parents[]` is alias pointer array.
256
+ - CAView.parent points to parents[0] (= legacy compat for paths that
257
+ deref ca->parent without CAStack awareness).
258
+ - dim[k_axis] = n_parents; the other dims are parents[0]->dim in order. */
259
+ typedef struct {
260
+ CA_ARRAY_HEADER
261
+ CArray *parent; /* = parents[0] (legacy CAView field) */
262
+ uint32_t attach;
263
+ uint8_t nosync;
264
+ /* ---- CAStack tail ---- */
265
+ int32_t n_parents; /* K = axis-0 size */
266
+ CArray **parents; /* ALLOC_N(CArray*, n_parents); aliases, not owned */
267
+ int8_t k_axis; /* K axis insertion position in view shape, [0, parent_ndim].
268
+ 0 = outer-axis stack (the default).
269
+ parent_ndim = K innermost.
270
+ Set by ca_stack_setup_with_axis / preserved on clone. */
271
+ CArray **mask_standins; /* NULL, or ALLOC_N(CArray*, n_parents) once the mask is
272
+ built: the all-false masks standing in for parents that
273
+ have none (owned, freed with the view) */
274
+ CArray *mask_owner; /* on the mask of a CAStack: that CAStack, whose stand-ins
275
+ this mask reads until they give way to the parents'
276
+ own masks (ca_multi_parent_mask_refresh); else NULL */
277
+ } CAStack;
278
+
279
+ /* CAMeld (ragged concatenate view along an existing axis). Unlike CAStack
280
+ which introduces a new K axis, CAMeld welds K parents along one of their
281
+ existing axes and may have uneven segment lengths there.
282
+ shape[a] = parents[0]->dim[a] for a != meld_axis
283
+ shape[meld_axis] = sum_k parents[k]->dim[meld_axis]
284
+ All other axes must match across parents (uniform check at constructor).
285
+
286
+ meld_axis may be any axis. */
287
+ typedef struct {
288
+ CA_ARRAY_HEADER
289
+ CArray *parent; /* = parents[0] (legacy CAView field) */
290
+ uint32_t attach;
291
+ uint8_t nosync;
292
+ /* ---- CAMeld tail (n_parents / parents kept adjacent to header for
293
+ CAMultiParent layout convention) ---- */
294
+ int32_t n_parents; /* K = number of segments */
295
+ CArray **parents; /* ALLOC_N(CArray*, K); aliases, not owned */
296
+ int8_t meld_axis; /* existing axis being welded, [0, parent_ndim) */
297
+ ca_size_t *seg_offset; /* ALLOC_N(ca_size_t, K+1); prefix sum along meld_axis.
298
+ seg_offset[0]=0, seg_offset[k+1]=seg_offset[k]+parents[k]->dim[meld_axis],
299
+ seg_offset[K]=dim[meld_axis]. */
300
+ CArray **mask_standins; /* as CAStack's: stand-in masks this view owns, or NULL */
301
+ CArray *mask_owner; /* as CAStack's */
302
+ } CAMeld;
303
+
304
+ /* Multi-parent view layout convention (CA_FLAG_MULTI_PARENTS). A view that
305
+ fans out to K parents must place `n_parents` + `parents[]` immediately after
306
+ the CAView header (exactly as CAStack does) so generic routines that would
307
+ otherwise walk a single ->parent can fold over all parents without knowing
308
+ the concrete view type. Cast `(CAMultiParent *) ca` only when the flag is
309
+ set. CAStack is the first conforming type (layout-checked in
310
+ Init_ca_obj_stack via offsetof). */
311
+ typedef struct {
312
+ CAView header; /* common view prefix (obj_type .. nosync) */
313
+ int32_t n_parents;
314
+ CArray **parents;
315
+ } CAMultiParent;
316
+
317
+ typedef struct {
318
+ CA_ARRAY_HEADER
319
+ CArray *parent;
320
+ uint32_t attach;
321
+ uint8_t nosync;
322
+ /* -------------*/
323
+ CArray *data;
324
+ VALUE self;
325
+ } CAObject;
326
+
327
+ /*
328
+ CAObjectMask is an internal class used only as the mask array of a
329
+ CAObject. Its bits live in the CAObject's mask hooks (mask_copy_data,
330
+ mask_fetch_addr, ...) when it has them, and in `cache` -- the mask
331
+ buffer of the CAObject's internal data array -- always. Like the mask
332
+ of any view it is cold at rest: ptr is NULL, and every read and write
333
+ goes through its slots to the hooks. An attach publishes the cache in
334
+ ptr, and `holds` counts the attaches not yet detached.
335
+ */
336
+
337
+ typedef struct {
338
+ CA_ARRAY_HEADER
339
+ /* -------------*/
340
+ VALUE array;
341
+ char *cache;
342
+ long holds;
343
+ } CAObjectMask;
344
+
345
+ /* CARepeat: structurally identical to CAStride.
346
+ The "repeat" axes are encoded as strides[k] == 0. */
347
+ typedef CAStride CARepeat;
348
+
349
+ /*
350
+ CAReduce is an internal class
351
+ used only in ca_obj_refer.c.
352
+ */
353
+
354
+ typedef struct {
355
+ CA_ARRAY_HEADER
356
+ CArray *parent;
357
+ uint32_t attach;
358
+ uint8_t nosync;
359
+ /* ---- */
360
+ ca_size_t count;
361
+ ca_size_t offset;
362
+ } CAReduce;
363
+
364
+ /* ---- Tables, TypedData tags and classes ------------------------------ */
365
+
366
+ /* The host's byte order and the promotion tables behind ca_promote_type,
367
+ for carray's own sources. An extension asks ca_promote_type for the
368
+ common data type of two. */
369
+ extern const int ca_endian;
370
+ extern const int ca_cast_table[CA_NTYPE][CA_NTYPE];
371
+ extern const int ca_cast_table2[CA_NTYPE][CA_NTYPE];
372
+
373
+ /* Symbol cache for data_type code → Symbol lookup. Initialized in
374
+ Init_carray_class. Indexed by data_type code (0 = CA_FIXLEN ...
375
+ CA_NTYPE-1 = CA_OBJECT). An extension converts with
376
+ rb_ca_data_type_to_sym. */
377
+ extern ID ca_data_type_sym[CA_NTYPE];
378
+
379
+ /* ca_func[] is indexed with sizeof(ca_operation_function_t) as its
380
+ stride, which grows when a slot is added, so only sources built with
381
+ carray see it. An extension installs its table with
382
+ ca_install_obj_type and keeps its own copy. */
383
+ extern ca_operation_function_t ca_func[CA_OBJ_TYPE_MAX];
384
+ /* The count of installed obj_types; an extension keeps the number
385
+ ca_install_obj_type returns. */
386
+ extern int ca_obj_num;
387
+
388
+ /* The tags of the views carray itself defines. An extension takes any
389
+ array through carray_data_type (GetCArray) and derives its own tags
390
+ from the ones carray.h declares, so these are seen only by sources built with
391
+ carray. */
392
+ extern const rb_data_type_t cawrap_data_type;
393
+ extern const rb_data_type_t cscalar_data_type;
394
+ extern const rb_data_type_t cabitarray_data_type;
395
+ extern const rb_data_type_t cabitfield_data_type;
396
+ extern const rb_data_type_t cablock_data_type;
397
+ extern const rb_data_type_t cafake_data_type;
398
+ extern const rb_data_type_t cabyteswap_data_type;
399
+ extern const rb_data_type_t cagrid_data_type;
400
+ extern const rb_data_type_t caobject_data_type;
401
+ extern const rb_data_type_t careduce_data_type;
402
+ extern const rb_data_type_t carefer_data_type;
403
+ extern const rb_data_type_t caselect_data_type;
404
+ extern const rb_data_type_t cashift_data_type;
405
+ extern const rb_data_type_t cawindow_data_type;
406
+
407
+ extern const rb_data_type_t cablock_mask_data_type;
408
+ extern const rb_data_type_t cagrid_mask_data_type;
409
+ extern const rb_data_type_t careduce_mask_data_type;
410
+ extern const rb_data_type_t carefer_mask_data_type;
411
+ extern const rb_data_type_t caselect_mask_data_type;
412
+ extern const rb_data_type_t cashift_mask_data_type;
413
+ extern const rb_data_type_t cawindow_mask_data_type;
414
+
415
+ /* The classes of the views and iterators carray itself defines. An
416
+ extension tests an array with rb_obj_is_kind_of against the classes
417
+ carray.h declares and does not need these. */
418
+ extern VALUE rb_cCScalar;
419
+ extern VALUE rb_cCAWrap;
420
+ extern VALUE rb_cCARefer;
421
+ extern VALUE rb_cCABlock;
422
+ extern VALUE rb_cCAField;
423
+ extern VALUE rb_cCAByteSwap;
424
+ extern VALUE rb_cCAStrideMask;
425
+ extern VALUE rb_cCASelect;
426
+ extern VALUE rb_cCAObject;
427
+ extern VALUE rb_cCARepeat;
428
+ extern VALUE rb_cCAIterator;
429
+
430
+ extern VALUE rb_cCAReferMask;
431
+ extern VALUE rb_cCABlockMask;
432
+ extern VALUE rb_cCAFieldMask;
433
+ extern VALUE rb_cCASelectMask;
434
+ extern VALUE rb_cCAObjectMask;
435
+ extern VALUE rb_cCARepeatMask;
436
+
437
+ /* The CAMath module, for carray's own sources. An extension looks it up
438
+ with rb_const_get(rb_cObject, rb_intern("CAMath")). */
439
+ extern VALUE rb_mCAMath;
440
+
441
+ /* ---- The cast loop's parsers and index parsing ----------------------- */
442
+
443
+ /* Mask-aware object->numeric parsers: return 1 with *out set on success, or
444
+ 0 (no value; caller masks the cell) on parse failure. Used by the
445
+ object->int/float cast loop so an unparseable cell becomes UNDEF instead
446
+ of a silent 0.0 (float) or a raise (int). An Integer wider than the C
447
+ type gives its low bytes (two's complement), as a narrowing C cast does;
448
+ these parsers do not raise. See ext/carray_cast.c. They are parts of
449
+ carray's cast loop; an extension converts an object array with
450
+ rb_ca_to_type. */
451
+ int ca_obj2dbl_ok (VALUE v, double *out);
452
+ int ca_obj2cmplx_ok (VALUE v, double complex *out);
453
+ int rb_obj2long_ok (VALUE v, long *out);
454
+ int rb_obj2ulong_ok (VALUE v, unsigned long *out);
455
+ int rb_obj2ll_ok (VALUE v, long long *out);
456
+ int rb_obj2ull_ok (VALUE v, unsigned long long *out);
457
+
458
+ /* index parsing (carray's own; not part of the extension surface) */
459
+
460
+
461
+ enum {
462
+ CA_REG_NONE,
463
+ CA_REG_ALL,
464
+ CA_REG_ADDRESS,
465
+ CA_REG_FLATTEN,
466
+ CA_REG_ADDRESS_COMPLEX,
467
+ CA_REG_POINT,
468
+ CA_REG_BLOCK,
469
+ CA_REG_SELECT,
470
+ CA_REG_ITERATOR,
471
+ CA_REG_REPEAT,
472
+ CA_REG_GRID,
473
+ CA_REG_ADDRESS_ARRAY,
474
+ CA_REG_METHOD_CALL,
475
+ CA_REG_MEMBER,
476
+ CA_REG_ATTRIBUTE,
477
+ }; /* CA_REGION_TYPE */
478
+
479
+ enum {
480
+ CA_IDX_SCALAR,
481
+ CA_IDX_ALL,
482
+ CA_IDX_BLOCK,
483
+ CA_IDX_SYMBOL
484
+ }; /* CA_INDEX_TYPE */
485
+
486
+ typedef union {
487
+ ca_size_t scalar;
488
+ struct {
489
+ ca_size_t start;
490
+ ca_size_t step;
491
+ ca_size_t count;
492
+ } block;
493
+ struct {
494
+ ID id;
495
+ VALUE spec;
496
+ } symbol;
497
+ } CAIndex;
498
+
499
+ typedef struct {
500
+ int16_t type;
501
+ int16_t ndim;
502
+ int32_t index_type[CA_RANK_MAX];
503
+ CAIndex index[CA_RANK_MAX];
504
+ CArray *select;
505
+ VALUE block;
506
+ VALUE symbol;
507
+ int8_t range_check;
508
+ } CAIndexInfo;
509
+
510
+ /* ---- Constructors, and the bodies of the view-making methods --------- */
511
+
512
+ /* The setup and the free_object slot of the entity and of CAWrap, for
513
+ carray's own sources. An extension makes an entity with carray_new or
514
+ rb_carray_new, and frees an array through ca_free. */
515
+ int carray_setup (CArray *ca,
516
+ int8_t data_type, int8_t ndim, ca_size_t *dim,
517
+ ca_size_t bytes, CArray *mask);
518
+
519
+ int carray_safe_setup (CArray *ca,
520
+ int8_t data_type, int8_t ndim, ca_size_t *dim,
521
+ ca_size_t bytes, CArray *mask);
522
+
523
+ void free_carray (void *ap);
524
+ void free_ca_wrap (void *ap);
525
+
526
+ /* rb_ca_new_reduced's shape computation, with the output byte width
527
+ passed explicitly so a runtime-width output data_type (CA_FIXLEN) can
528
+ be allocated. Only carray's own reductions make such an output; an
529
+ extension allocates its reduction output with rb_ca_new_reduced. */
530
+ VALUE rb_ca_new_reduced_bytes (VALUE self, int8_t *slab_axes, int8_t naxes,
531
+ int32_t data_type, ca_size_t bytes, int keep_axis);
532
+
533
+ /* The other CScalar constructors, for carray's own sources. An extension
534
+ makes a CScalar with rb_cscalar_new. */
535
+ CScalar *cscalar_new (int8_t data_type, ca_size_t bytes, CArray *ma);
536
+ CScalar *cscalar_new2 (int8_t data_type, ca_size_t bytes, char *val);
537
+ VALUE rb_cscalar_new_with_value (int8_t data_type, ca_size_t bytes, VALUE rval);
538
+
539
+ /* A Ruby value as a CArray (CArray.cast): an Integer as int64, a Float as
540
+ float64, true / false as boolean, a Complex as cmplx128, an Array or a
541
+ Range through to_ca, anything else as an object cell. A CArray is
542
+ returned as it is. */
543
+ VALUE rb_ca_cast (VALUE self);
544
+
545
+ /* The Ruby-level builder of a CARefer, used by carray's indexing. An
546
+ extension makes a reinterpreting view with ca_refer_new, or a
547
+ strided view with ca_stride_new / rb_ca_stride_new. */
548
+ VALUE rb_ca_refer_new (VALUE self,
549
+ int8_t data_type, int8_t ndim, ca_size_t *dim,
550
+ ca_size_t bytes, ca_size_t offset);
551
+
552
+ /* --- ca_obj_farray.c --- */
553
+
554
+ /* The body of CArray#farray; an extension calls the method with
555
+ rb_funcall. */
556
+ VALUE rb_ca_farray (VALUE self);
557
+
558
+ /* one stride-composition hop: express leaf (in parent's byte space) into
559
+ parent->parent's byte space. Non-static so carray's sometimes-fold views
560
+ (e.g. CAWindow::fold_stride) can compose through a synthetic stride
561
+ layer; an extension composes a whole chain with ca_stride_compose_to_root. */
562
+ int ca_stride_compose_through (CAStride *leaf, CAStride *parent,
563
+ ca_size_t *out_strides, ca_size_t *out_base);
564
+
565
+ /* The bodies of the view-making methods below (CArray#grid, #field,
566
+ #fake, #repeat and the indexing behind them). An extension makes these
567
+ views by calling the methods with rb_funcall. */
568
+
569
+ /* --- ca_obj_grid.c --- */
570
+
571
+ VALUE rb_ca_grid (int argc, VALUE *argv, VALUE self);
572
+ int rb_ca_select_axis_eligible_p (int argc, VALUE *argv, VALUE self);
573
+ VALUE rb_ca_select_axis (int argc, VALUE *argv, VALUE self);
574
+
575
+ /* --- ca_obj_mapping.c --- */
576
+
577
+ VALUE rb_ca_mapping (int argc, VALUE *argv, VALUE self);
578
+
579
+ /* --- ca_obj_field.c --- */
580
+
581
+ VALUE rb_ca_field (int argc, VALUE *argv, VALUE self);
582
+
583
+ /* --- ca_obj_byte_swap.c / ca_obj_bitfield.c --- */
584
+
585
+ VALUE rb_ca_endian (VALUE self, VALUE byte_order);
586
+ VALUE rb_ca_bitfield (int argc, VALUE *argv, VALUE self);
587
+
588
+ /* --- ca_obj_fake.c --- */
589
+
590
+ VALUE rb_ca_fake_type (VALUE self, VALUE rtype, VALUE rbytes);
591
+
592
+ /* --- ca_obj_repeat.c --- */
593
+
594
+ VALUE rb_ca_repeat (int argc, VALUE *argv, VALUE self);
595
+
596
+ /* --- carray_broadcast.c --- */
597
+
598
+ /* The shape matching of the binary operators and the lazy builders, for
599
+ carray's own sources. An extension combines two arrays by calling the
600
+ operator with rb_funcall. */
601
+ VALUE ca_broadcast_view (VALUE src, int8_t ndim, ca_size_t *target_dim);
602
+ void ca_broadcast_pair (volatile VALUE *self, volatile VALUE *other);
603
+
604
+ /* ---- Queries, checks, copying, transfer and fill --------------------- */
605
+
606
+ /* True for a CALazyMarker. */
607
+ #define ca_is_lazy_marker(ca) ( ca_test_flag((ca), CA_FLAG_IS_LAZY_MARKER) )
608
+
609
+ /* The checks at the entry of carray's own view constructors (initialize /
610
+ initialize_copy) and of its data_class arguments. */
611
+ void ca_check_uninitialized (void *ap); /* initialize / initialize_copy entry */
612
+ void ca_check_data_class (VALUE rtype);
613
+
614
+ /* The clone slot's dispatcher, for carray's own sources; an extension
615
+ clones an array with rb_obj_clone(). */
616
+ void *ca_clone (void *ap);
617
+
618
+ /* ca_fill_data with the read-only check done twice, for carray's own
619
+ sources. An extension calls ca_fill_data. */
620
+ void ca_fill (void *ap, void *ptr);
621
+
622
+ /* The shortcuts carray's own xfer_addrs slots take, for carray's own
623
+ sources. An extension's slot hands its addresses on with ca_xfer_addrs. */
624
+ /* Shared predicate: detect if addrs[] is a sequential run
625
+ (addrs[i] == addrs[0]+i for all i). Used by ca_xfer_addrs_dispatch and
626
+ by view xfer_addrs slots for whole-view / sub-region run detection ->
627
+ opportunistic engine dispatch. */
628
+ int ca_xfer_addrs_is_sequential_run (ca_size_t n, ca_size_t *addrs,
629
+ ca_size_t *base_out);
630
+ /* Resolve cand through identity CAStride compose-fold to find an
631
+ attached root. Returns cand if cand->ptr is set, cand is not CAStride
632
+ family, or no identity compose-fold path exists (the cells differ from
633
+ the root's in data type, width or position); otherwise returns the
634
+ resolved root (which has ptr). Used by view xfer_addrs slots to lift
635
+ parent->ptr gate through view CAStride layers (= chain pattern). */
636
+ CArray *ca_resolve_attached_root_via_identity (CArray *cand);
637
+
638
+ /* The parts carray's own fill_stride and fill_addrs slots are built from,
639
+ for carray's own sources. An extension that has no faster way to fill a
640
+ region leaves the slot NULL and gets the per-cell default. */
641
+ /* The per-cell walk, to stand in for regions this view cannot pass on. */
642
+ void ca_fill_stride_default (void *ap, ca_size_t base, int8_t ndim,
643
+ ca_size_t *counts, ca_size_t *steps, void *ptr);
644
+ void ca_fill_addrs_default (void *ap, ca_size_t n, ca_size_t *addrs, void *ptr);
645
+
646
+ /* For a view whose fill is a read-modify-write of its parent and so has no
647
+ region to pass down: walk the region and feed its addresses to the batched
648
+ address slot in windows, rather than descending the chain per cell. */
649
+ void ca_fill_stride_via_addrs (void *ap, ca_size_t base, int8_t ndim,
650
+ ca_size_t *counts, ca_size_t *steps,
651
+ void *ptr);
652
+
653
+ /* The test for the one region a view can always pass on: all of itself. */
654
+ int ca_fill_stride_is_whole (void *ap, ca_size_t base, int8_t ndim,
655
+ ca_size_t *counts, ca_size_t *steps);
656
+
657
+ /* Fill the whole of `ca` by handing its own extent to ca_fill_stride: the
658
+ region every caller means when it says "all of me". Views use it to route
659
+ a full-coverage fill_data into the region path. */
660
+ void ca_fill_stride_whole (void *ap, void *ptr);
661
+
662
+ /* ---- Casts, helpers, options and the bodies of Ruby methods ---------- */
663
+
664
+ /* The cast table and the cast between a cell and a Ruby object without
665
+ the Face's conversion, for carray's own sources. An extension casts
666
+ with ca_cast_block and converts a cell with rb_ca_ptr2obj and
667
+ rb_ca_obj2ptr. */
668
+ typedef void (*ca_cast_func_t)(ca_size_t, CArray *, void *, CArray *, void *, boolean8_t *);
669
+ extern ca_cast_func_t ca_cast_func_table[CA_NTYPE][CA_NTYPE];
670
+ VALUE ca_ptr2obj (void *ap, void *ptr);
671
+ void ca_obj2ptr (void *ap, VALUE obj, void *ptr);
672
+
673
+ /* Helpers of carray's own operators, views and comparisons, for carray's
674
+ own sources. An extension parses a Range with ca_parse_range and
675
+ compares arrays with rb_funcall (==, eql?). */
676
+ ca_size_t ca_get_loop_count (int n, ...);
677
+ ca_size_t ca_set_iterator (int n, ...);
678
+ void ca_swap_bytes (char *p, ca_size_t bytes, ca_size_t elements);
679
+ void ca_parse_range_without_check (VALUE arg, ca_size_t size,
680
+ ca_size_t *offset, ca_size_t *count, ca_size_t *step);
681
+ int ca_equal (void *ap, void *bp);
682
+ int ca_eql_storage (void *ap, void *bp);
683
+ ca_size_t ca_bounds_normalize_index (int8_t bounds, ca_size_t size0, ca_size_t k);
684
+
685
+ /* rb_ca_parse_reduce_axes_kw with the method name for its error messages
686
+ given by the caller. An extension uses rb_ca_parse_reduce_axes_kw,
687
+ whose messages name the Ruby method that called it. */
688
+ int8_t rb_ca_parse_reduce_axes_kw_ctx (VALUE axis_val, CArray *ca,
689
+ int8_t *out_axes, const char *ctx);
690
+
691
+ /* carray's own parsing of a type and byte-width argument pair. An
692
+ extension reads the type with rb_ca_guess_type and compares data_type
693
+ codes. */
694
+ void rb_ca_guess_type_and_bytes (VALUE rtype, VALUE rbytes,
695
+ int8_t *data_type, ca_size_t *bytes);
696
+ int rb_ca_is_type (VALUE arg, int type);
697
+
698
+ /* scan index. info->range_check is read, not written: set it (1 to check
699
+ each index against ca_dim) before the call. */
700
+ void rb_ca_scan_index (int ca_ndim, ca_size_t *ca_dim, ca_size_t elements,
701
+ long argc, VALUE *argv, CAIndexInfo *info);
702
+
703
+ /* The operand handling of carray's binary operators. An extension brings
704
+ two operands to one type with rb_ca_cast_self_or_other. */
705
+ int rb_ca_test_castable (VALUE other);
706
+ VALUE rb_ca_binop_pass_to_other (VALUE self, VALUE other, ID method);
707
+ void rb_ca_cast_other (volatile VALUE *self, volatile VALUE *other);
708
+
709
+ /* data class access (struct), for carray's own sources */
710
+ VALUE rb_obj_is_data_class (VALUE rtype);
711
+
712
+ /* The bodies of the Ruby methods that freeze an array, report its
713
+ attributes and its type, and read its data class. An extension reads
714
+ the attributes from the struct (ca->obj_type, ca->data_type, ca->ndim,
715
+ ca->bytes, ca->flags, ca->elements, ca->dim), tests the data type with
716
+ the ca_is_*_type functions and the kind of array with ca_is_entity,
717
+ ca_is_view, ca_is_attached, ca_is_empty, ca_is_readonly,
718
+ ca_is_mask_array, ca_is_value_array and ca_is_scalar, and calls the
719
+ other methods (freeze, set_read_only_flag, data_class, members,
720
+ fields, ...) with rb_funcall. */
721
+ VALUE rb_ca_freeze (VALUE self);
722
+ VALUE rb_ca_set_read_only_flag (VALUE self);
723
+
724
+ VALUE rb_ca_obj_type (VALUE self);
725
+ VALUE rb_ca_data_type (VALUE self);
726
+ VALUE rb_ca_ndim (VALUE self);
727
+ VALUE rb_ca_bytes (VALUE self);
728
+ VALUE rb_ca_flags (VALUE self);
729
+ VALUE rb_ca_elements (VALUE self);
730
+ VALUE rb_ca_dim (VALUE self);
731
+ VALUE rb_ca_data_type_name (VALUE self);
732
+
733
+ VALUE rb_ca_is_fixlen_type (VALUE self);
734
+ VALUE rb_ca_is_boolean_type (VALUE self);
735
+ VALUE rb_ca_is_integer_type (VALUE self);
736
+ VALUE rb_ca_is_float_type (VALUE self);
737
+ VALUE rb_ca_is_complex_type (VALUE self);
738
+ VALUE rb_ca_is_object_type (VALUE self);
739
+
740
+ VALUE rb_ca_is_entity (VALUE self);
741
+ VALUE rb_ca_is_virtual (VALUE self);
742
+ VALUE rb_ca_is_attached (VALUE self);
743
+ VALUE rb_ca_is_empty (VALUE self);
744
+ VALUE rb_ca_is_read_only (VALUE self);
745
+ VALUE rb_ca_is_mask_array (VALUE self);
746
+ VALUE rb_ca_is_value_array (VALUE self);
747
+
748
+ VALUE rb_ca_is_scalar (VALUE self);
749
+
750
+ VALUE rb_ca_has_data_class (VALUE self);
751
+ VALUE rb_ca_data_class (VALUE self);
752
+ /* CArray#data_class= raises ArgumentError and points to CARecord.new /
753
+ CARecord.wrap; its function (rb_ca_set_data_class) is defined in
754
+ carray_attribute.c and not declared here. */
755
+ VALUE rb_ca_data_class_decode (VALUE self, VALUE str);
756
+ VALUE rb_ca_data_class_encode (VALUE self, VALUE obj);
757
+ VALUE rb_ca_members (VALUE self);
758
+ VALUE rb_ca_face_field (VALUE self, VALUE sym);
759
+ VALUE rb_ca_fields_at (int argc, VALUE *argv, VALUE self);
760
+ VALUE rb_ca_fields (VALUE self);
761
+
762
+ /* The bodies of has_mask?, any_masked?, all_masked?, inherit_mask and
763
+ inherit_mask_replace. An extension tests the mask with ca_has_mask,
764
+ ca_is_any_masked and ca_is_all_masked, and gives an output the mask of
765
+ its operands with ca_copy_mask_overlay or ca_mask_overlay_safe. */
766
+ VALUE rb_ca_has_mask (VALUE self);
767
+ VALUE rb_ca_is_any_masked (VALUE self);
768
+ VALUE rb_ca_is_all_masked (VALUE self);
769
+ VALUE rb_ca_inherit_mask_replace_n (VALUE self, int argc, VALUE *argv);
770
+ VALUE rb_ca_inherit_mask_replace (VALUE self, int n, ...);
771
+ VALUE rb_ca_inherit_mask_n (VALUE self, int argc, VALUE *argv);
772
+ VALUE rb_ca_inherit_mask (VALUE self, int n, ...);
773
+
774
+ /* The bodies of the Ruby methods to_ca, addr2index, the elem_* family,
775
+ the conversions named by a data type (int8, float64, object, ...),
776
+ to_a, seq, seq!, where and swap_bytes, with the helpers behind them.
777
+ An extension calls these methods with rb_funcall, or uses the C
778
+ functions for the same work: ca_addr2index, ca_fetch_index /
779
+ ca_store_index or rb_ca_fetch_index / rb_ca_store_index for single
780
+ cells, rb_ca_ptr2obj and rb_ca_obj2ptr for a cell's bytes, ca_ptr2ptr
781
+ and ca_cast_block to convert cells, and rb_ca_to_type to convert an
782
+ array.
783
+
784
+ rb_ca_store_all is the body behind self[] = val that carray's own
785
+ sources call; an extension stores val in every cell with
786
+ rb_ca_store2(self, 0, NULL, val). */
787
+ VALUE rb_ca_store_all (VALUE self, VALUE val);
788
+
789
+ VALUE rb_ca_to_ca (int argc, VALUE *argv, VALUE self);
790
+
791
+ /* address calculation */
792
+ VALUE rb_ca_addr2index (VALUE self, VALUE raddr);
793
+
794
+ #define rb_ca_fetch_ptr(self, ptr) rb_ca_ptr2obj(self, ptr)
795
+ #define rb_ca_store_ptr(self, ptr, val) rb_ca_obj2ptr(self, val, ptr)
796
+
797
+ /* elemental operations */
798
+ VALUE rb_ca_elem_swap (VALUE self, VALUE vidx1, VALUE vidx2);
799
+ VALUE rb_ca_elem_copy (VALUE self, VALUE vidx1, VALUE vidx2);
800
+ VALUE rb_ca_elem_store (VALUE self, VALUE vidx, VALUE obj);
801
+ VALUE rb_ca_elem_fetch (VALUE self, VALUE vidx);
802
+ VALUE rb_ca_elem_incr (VALUE self, VALUE vidx1);
803
+ VALUE rb_ca_elem_decr (VALUE self, VALUE vidx1);
804
+ VALUE rb_ca_elem_test_masked (VALUE self, VALUE vidx1);
805
+
806
+ /* data type conversion */
807
+ VALUE rb_ca_ptr2ptr (VALUE ra1, void *ptr1, VALUE ra2, void *ptr2);
808
+ VALUE rb_ca_cast_block (ca_size_t n, VALUE ra1, void *ptr1,
809
+ VALUE ra2, void *ptr2);
810
+
811
+ VALUE rb_ca_to_boolean (VALUE self);
812
+ VALUE rb_ca_to_int8 (VALUE self);
813
+ VALUE rb_ca_to_uint8 (VALUE self);
814
+ VALUE rb_ca_to_int16 (VALUE self);
815
+ VALUE rb_ca_to_uint16 (VALUE self);
816
+ VALUE rb_ca_to_int32 (VALUE self);
817
+ VALUE rb_ca_to_uint32 (VALUE self);
818
+ VALUE rb_ca_to_int64 (VALUE self);
819
+ VALUE rb_ca_to_uint64 (VALUE self);
820
+ VALUE rb_ca_to_float32 (VALUE self);
821
+ VALUE rb_ca_to_float64 (VALUE self);
822
+ VALUE rb_ca_to_cmplx64 (VALUE self);
823
+ VALUE rb_ca_to_cmplx128 (VALUE self);
824
+ VALUE rb_ca_to_VALUE (VALUE self);
825
+ #define rb_ca_to_object(self) rb_ca_to_VALUE(self)
826
+
827
+ /* to ruby's array */
828
+ VALUE rb_ca_to_a (VALUE self);
829
+
830
+ /* generation */
831
+ VALUE rb_ca_seq_bang (VALUE self, VALUE offset, VALUE step);
832
+ VALUE rb_ca_seq_bang2 (VALUE self, int n, VALUE *args);
833
+ VALUE rb_ca_seq (VALUE self, VALUE offset, VALUE step);
834
+ VALUE rb_ca_seq2 (VALUE self, int n, VALUE *args);
835
+ VALUE rb_ca_where (VALUE self);
836
+
837
+ /* elemental byte swap */
838
+ VALUE rb_ca_swap_bytes_bang (VALUE self);
839
+ VALUE rb_ca_swap_bytes (VALUE self);
840
+
841
+ /* ---- The index walk and the debug hook ------------------------------- */
842
+
843
+ /* carray_loop.c — unified multi-dimensional index walk shared by
844
+ each_index / each_with_index / map_index! / map_with_index! and by
845
+ the construction-block sugar in rb_ca_initialize. */
846
+ #define CA_LOOP_WITH_VALUE 1
847
+ #define CA_LOOP_STORE 2
848
+
849
+ VALUE rb_ca_index_walk (VALUE self, CArray *ca, int8_t level,
850
+ ca_size_t *idx, VALUE ridx, int mode);
851
+
852
+ /* An empty function, for carray's own sources. */
853
+ void ca_debug ();
854
+
855
+ /* The kernel iterator's internal parts: the substrate the engine shares
856
+ with the descriptor framework and the CAStride family, and the
857
+ iterator's own declarations. The substrate's inline helpers use
858
+ ca_bounds_normalize_index, declared above. */
859
+ #include "ca_iter_substrate.h"
860
+ #include "ca_kernel_iterator_internal.h"
861
+
30
862
  /* ---- properties a copy or a folded view must carry (ca_obj_refer.c) -------
31
863
 
32
864
  Copies VALUE_ARRAY (dropping any mask), READ_ONLY and MASK_ARRAY from
@@ -85,6 +917,7 @@ void ca_lazy_arena_enter (void);
85
917
  void ca_lazy_arena_exit (void);
86
918
  void *ca_lazy_arena_acquire (ca_size_t bytes);
87
919
  void *ca_lazy_arena_acquire_object (ca_size_t n_elements);
920
+ void ca_lazy_arena_untag_objects (void *ptr);
88
921
  void ca_lazy_arena_release (void *ptr);
89
922
  VALUE ca_lazy_arena_protect (VALUE (*body)(VALUE),
90
923
  VALUE (*cleanup)(VALUE), VALUE arg);
@@ -145,6 +978,18 @@ ca_boolean_normalize_bytes (void *ptr, ca_size_t n)
145
978
  }
146
979
  }
147
980
 
981
+ /* A buffer for n cells of data_type, each bytes long: object cells start
982
+ as nil. */
983
+ static inline char *
984
+ ca_alloc_cells_of (int8_t data_type, ca_size_t elements, ca_size_t bytes)
985
+ {
986
+ char *p = (char *) xmalloc(elements > 0 ? elements * bytes : 1);
987
+ if ( data_type == CA_OBJECT ) {
988
+ ca_object_cells_clear(p, elements);
989
+ }
990
+ return p;
991
+ }
992
+
148
993
  static inline char *
149
994
  ca_alloc_cells (const CArray *ca)
150
995
  {
@@ -168,6 +1013,11 @@ void rb_ca_set_lifted_from (VALUE lifted, VALUE face);
168
1013
  (carray_cast.c): _to answers 1 / 0 (UNDEF), _store raises. */
169
1014
  int ca_decimal_string_to (VALUE str, int8_t data_type, void *out);
170
1015
  void ca_decimal_string_store (VALUE str, int8_t data_type, void *out);
1016
+ /* A boolean cell read from an object: a String by its words (1 / 0 /
1017
+ true / false), else true / false / 0 / 1; _to answers 0 for a cell that
1018
+ becomes UNDEF, _store raises (carray_cast.c). */
1019
+ int ca_boolean_value_to (VALUE v, void *out);
1020
+ void ca_boolean_value_store (VALUE v, void *out);
171
1021
 
172
1022
  /* Give `ca` a mask holding `m` (one byte per cell) when any cell is set,
173
1023
  and leave it without one otherwise. */
@@ -193,26 +1043,23 @@ ca_mask_from_bytes (CArray *ca, const boolean8_t *m)
193
1043
  raises so that the others still receive what was written, and raises
194
1044
  the first exception once all have been tried.
195
1045
 
196
- ca_attach_window opens list[0..n-1] ('r' attach, 'w' attach + sync,
197
- 'a' allocate + sync), runs body(arg), and closes every array however body
198
- leaves. Any window inside which Ruby runs or something raises goes
199
- through it; see the definition for what a raise does to 'w' and 'a'. */
1046
+ ca_attach_window is declared in carray.h. */
200
1047
 
201
1048
  void ca_attach_all (CArray **list, int32_t n);
202
1049
  void ca_sync_all (CArray **list, int32_t n);
203
1050
  void ca_sync_carry_on (CArray *ca, volatile VALUE *error);
204
- VALUE ca_attach_window (int32_t n, CArray **list, const char *modes,
205
- VALUE (*body)(VALUE), VALUE arg);
206
1051
 
207
- /* A window that yields to a block can be left open for good: Ruby does not
1052
+ /* Arrays attached for a block can be left attached for good: Ruby does not
208
1053
  run the ensure of a suspended Fiber that is collected (an Enumerator
209
1054
  stopped with #next and dropped). ca_window_guard_arm makes a guard that
210
- runs cleanup(arg) from a finalizer when that happens; the window disarms
211
- it when it closes normally. arg is heap memory the guard frees, and
212
- mark marks the VALUEs it holds. ca_attach_window_yielding is
1055
+ runs cleanup(arg) from a finalizer when that happens; the code that
1056
+ attached disarms it when it detaches normally (on Ruby 3.x only in the
1057
+ main Ractor, whose finalizer table is safe to change; the sentinel is
1058
+ then nil). arg is heap memory the guard frees, and mark marks the
1059
+ VALUEs it holds. ca_attach_window_yielding is
213
1060
  ca_attach_window with such a guard; owners keeps the arrays alive. */
214
1061
  VALUE ca_window_guard_arm (void (*cleanup)(void *), void (*mark)(void *),
215
- void *arg, VALUE *sentinel);
1062
+ void *arg, volatile VALUE *sentinel);
216
1063
  void ca_window_guard_disarm (VALUE guard, VALUE sentinel);
217
1064
  VALUE ca_attach_window_yielding (VALUE owners, int32_t n, CArray **list,
218
1065
  const char *modes, VALUE (*body)(VALUE),
@@ -237,6 +1084,17 @@ void ca_fill_or_free (CArray *co, VALUE (*fill)(VALUE), VALUE arg);
237
1084
  void ca_fill_or_xfree (void *buf, VALUE (*fill)(VALUE), VALUE arg);
238
1085
  void ca_fill_or_detach (int n, CArray **list, VALUE (*fill)(VALUE), VALUE arg);
239
1086
 
1087
+ /* The buffer of a view's own cells, for an attach or allocate slot that
1088
+ attaches parents[0..n-1] and then works in a buffer of its own. The
1089
+ buffer is allocated before the parents are attached, and freed if
1090
+ attaching them raises: a slot that raises before it publishes ca->ptr
1091
+ gives ca_attach no detach slot to call, and a parent left attached keeps
1092
+ reading and writing the copy it materialised instead of its root. The
1093
+ caller publishes the result as ca->ptr. utils/check_attach_then_alloc.rb
1094
+ finds a slot that allocates after attaching. */
1095
+
1096
+ char *ca_alloc_cells_then_attach (CArray *ca, int32_t n, CArray **parents);
1097
+
240
1098
  /* A view takes its parent's mask by asking the parent whether it has one,
241
1099
  and a parent that is a CAObject answers in Ruby and can raise. A view's
242
1100
  setup therefore does not ask: its constructor calls this under
@@ -259,7 +1117,7 @@ VALUE ca_window_create_bounds_mask (VALUE view);
259
1117
  /* ca_stride_setup without asking the parent for its mask; see
260
1118
  ca_view_inherit_mask. The in-tree subclasses use this. */
261
1119
 
262
- int ca_stride_setup_view (CAStride *ca, int8_t obj_type, CArray *parent,
1120
+ int ca_stride_setup_view (CAStride *ca, int16_t obj_type, CArray *parent,
263
1121
  int8_t data_type, ca_size_t bytes,
264
1122
  int8_t ndim, ca_size_t *dim,
265
1123
  ca_size_t *strides, ca_size_t base_offset);
@@ -360,6 +1218,7 @@ VALUE ca_lazy_settle_scalar (VALUE operand, int was_scalar);
360
1218
  of one view all take the same branch. */
361
1219
 
362
1220
  int ca_parent_lends_no_memory (void *ap);
1221
+ void rb_ca_cast_three (volatile VALUE *v1, volatile VALUE *v2, volatile VALUE *v3);
363
1222
 
364
1223
  /* Transfers of the descriptor views (CAGrid, CASelect, CASelectAxis,
365
1224
  CAWindow) whose parent holds no ptr (ca_axis_dispatch.c).
@@ -389,7 +1248,8 @@ void ca_axis_view_sync_owned (void *ap);
389
1248
  The constructors that DO belong to the ext-author surface stay in
390
1249
  carray.h and are not repeated here: ca_wrap_new / rb_ca_wrap_new (adopt
391
1250
  external memory), ca_stride_setup / ca_stride_new / rb_ca_stride_new
392
- (author a strided view -- see devel/CAStride.md), and ca_refer_new /
1251
+ (author a strided view -- see
1252
+ guides/devel/06_view_algebra_and_castride.md), and ca_refer_new /
393
1253
  rb_ca_refer_new (reinterpret / re-mask, which a bridge gem does use). */
394
1254
 
395
1255
  /* ca_obj_block.c */
@@ -433,35 +1293,109 @@ CAReduce *ca_reduce_new (CArray *carray, ca_size_t count, ca_size_t offset);
433
1293
  void ca_broadcast_to_destination (VALUE dst, volatile VALUE *src);
434
1294
  VALUE rb_ary_flatten_for_elements (VALUE ary, ca_size_t elements, void *ap);
435
1295
 
436
- /* carray_utils.c */
437
- const char *ca_calling_method_name (void);
1296
+ /* carray_access.c */
1297
+ void ca_refuse_complex_into_real (CArray *ca, CArray *cv);
1298
+
1299
+ /* carray_cast.c */
1300
+ void ca_cast_block_skip_masked (ca_size_t n, void *ap1, void *ptr1,
1301
+ void *ap2, void *ptr2, boolean8_t *m);
1302
+
1303
+ /* The tolerance of is_close / is_equiv: a number, zero or positive. A
1304
+ negative or NaN tolerance is refused (ArgumentError) under the name
1305
+ `name` (carray_utils.c). */
1306
+ double ca_tolerance_arg (VALUE tol, const char *name);
1307
+
1308
+ /* Refuse a Ruby Array read as one value of a cell; kind is "an integer",
1309
+ "a float" or "a boolean" (carray_cast.c). */
1310
+ NORETURN(void ca_refuse_array_value (const char *kind));
1311
+
1312
+ /* Refuse nil read as one value of a cell (carray_cast.c). */
1313
+ NORETURN(void ca_refuse_nil_value (const char *kind));
1314
+ void ca_refuse_type_number (VALUE rtype);
1315
+ VALUE ca_record_view_of (VALUE obj, VALUE data_class, const char *method);
1316
+ void ca_refuse_data_class (VALUE rtype, const char *method);
1317
+
1318
+ /* Refuse v when it is a Ruby Array given where an array with a data type
1319
+ is expected; role names what it would have been (carray_cast.c). */
1320
+ void ca_refuse_ruby_array (VALUE v, const char *role);
438
1321
  VALUE ca_answer_as_caller (VALUE (*body)(VALUE), VALUE arg);
439
1322
  long ca_integer_arg (VALUE v, const char *arg, const char *name);
1323
+ /* A Ruby value for a number cell is a Numeric or UNDEF; a String or another
1324
+ object (a Time) is refused (TypeError). The second form checks the
1325
+ elements of a Ruby Array. */
1326
+ void ca_refuse_non_number_value (int8_t data_type, VALUE v);
1327
+ void ca_refuse_non_number_elements (int8_t data_type, VALUE ary);
1328
+ /* A true / false keyword: true, false or nil (nil reads as false), else
1329
+ ArgumentError "<method>: <arg>: must be true or false (got v)". */
1330
+ int ca_bool_arg (VALUE v, const char *arg);
440
1331
  /* 1 with the position of an index key (an Integer, or a Float with no
441
1332
  fraction while exact), 2 for an Integer past int64, 0 otherwise. */
442
1333
  int ca_index_key_position (VALUE v, ca_size_t *out);
1334
+ ca_size_t ca_index_key_value (VALUE v);
443
1335
  void ca_refuse_without_common_type (VALUE self, VALUE other);
444
1336
  ca_size_t ca_shape_elements (int8_t ndim, const ca_size_t *dim, ca_size_t bytes);
445
1337
  void ca_check_index_array (VALUE v);
446
1338
  VALUE ca_order_key (VALUE v, const char *name);
447
1339
  void ca_check_integer_fits (VALUE v, int8_t data_type);
448
- VALUE ca_obj2integer_fitting (VALUE v, int8_t data_type);
1340
+ /* A Ruby value stored into an integer cell of `dst`: refused when it does
1341
+ not fit (rb_ca_obj2integer_fitting), except into the cells of a bit field
1342
+ view (ca_bitfield_beneath), which keep it modulo 2 ** bits
1343
+ (ca_integer_wrap_bits). */
1344
+ VALUE ca_obj2integer_store (CArray *dst, VALUE v, int8_t data_type);
1345
+ CArray *ca_bitfield_beneath (CArray *ca);
1346
+ VALUE ca_bitfield_wrap_integer (CArray *ca, VALUE v);
1347
+ /* v as an Integer modulo 2 ** bits; with sign_extend, in
1348
+ -2 ** (bits - 1) ... 2 ** (bits - 1) - 1. A String is read as a
1349
+ decimal integer, any other value through Integer() (1.5 is 1). */
1350
+ VALUE ca_integer_wrap_bits (VALUE v, int bits, int sign_extend);
449
1351
  int ca_integer_range_side (VALUE v, int8_t data_type);
1352
+ VALUE ca_integer_wrap_to_type (VALUE v, int8_t data_type);
1353
+ enum {
1354
+ CA_SCALAR_FOR_RING,
1355
+ CA_SCALAR_FOR_MIN,
1356
+ CA_SCALAR_FOR_MAX,
1357
+ CA_SCALAR_FOR_ORDER,
1358
+ CA_SCALAR_FOR_COMPARE
1359
+ };
1360
+ void ca_scalar_operand_for_op (volatile VALUE *scalar, VALUE array,
1361
+ int how);
1362
+ void ca_scalar_operands_for_op (volatile VALUE *self,
1363
+ volatile VALUE *other, int how);
1364
+
1365
+ /* fma / fms operands beside the receiver taken as + and * take a scalar
1366
+ (carray_operator.c). */
1367
+ void ca_triop_ring_scalar_operands (volatile VALUE *self,
1368
+ volatile VALUE *other2,
1369
+ volatile VALUE *other3);
1370
+ int ca_obj_number_fits_integer (VALUE v, int8_t data_type);
450
1371
  int ca_bincmp_integer_prepare (volatile VALUE *self, volatile VALUE *other,
451
1372
  int op_id, int *signed_side);
452
1373
  int ca_bincmp_answer_when_negative (int op_id, int signed_side);
1374
+ int ca_bincmp_tolerance_integer_prepare (VALUE self, VALUE other);
1375
+ VALUE ca_bincmp_tolerance_by_value (VALUE self, VALUE other,
1376
+ int is_equiv, double tol);
453
1377
  long ca_axis_integer (VALUE raxis, const char *name);
454
1378
  /* Refuse a write into a read-only array that is not frozen (carray_test.c);
455
1379
  every guard calls this rather than raising its own. */
456
1380
  NORETURN(void ca_refuse_read_only (void));
1381
+ /* A view reaches its parent's slots through the dispatchers (ca_attach,
1382
+ ca_xfer_all, ca_xfer_stride, ...), so a chain of views is walked by C
1383
+ recursion, one level of the dispatcher per view. Each dispatcher calls
1384
+ this on entry, before it takes anything, so a chain deeper than the
1385
+ machine stack raises SystemStackError instead of overflowing it. The
1386
+ VM's own detection turns an overflow into SystemStackError only on the
1387
+ main thread's stack; on a Fiber's stack it is a fatal error. */
1388
+ static inline void
1389
+ ca_check_stack_room (void)
1390
+ {
1391
+ if ( ruby_stack_check() ) {
1392
+ rb_raise(rb_eSysStackError, "stack level too deep (a chain of views)");
1393
+ }
1394
+ }
457
1395
  /* Refuse an operator with no kernel for `data_type`, naming the method and
458
1396
  the type (carray_operator.c); the eager operators and the lazy builders. */
459
1397
  NORETURN(void ca_op_refuse_unimplemented (VALUE operand, int8_t data_type,
460
1398
  VALUE eclass));
461
- /* Refuse a rank above CA_RANK_MAX before any dim is written (carray_utils.c). */
462
- void ca_check_rank_fits (long ndim, const char *name);
463
- /* The checks of carray.h inside the library, each naming the method the
464
- user called; the public ones stay as they are for extensions. */
465
1399
  /* A wrap of a caller's buffer lent to a CAObject hook (CA_FLAG_LENT), and
466
1400
  the mark that a view was taken of it while it was lent
467
1401
  (CA_FLAG_LENT_VIEWED). rb_ca_set_parent sets the second; the hook's
@@ -481,70 +1415,20 @@ VALUE ca_computing_run_nodes (int n, CArray **ops, VALUE (*body)(VALUE), VALUE a
481
1415
  /* Refuses as cyclic a cell among cells[0..n) that is an array reaching a
482
1416
  lazy node being evaluated: computing it would come back into the node. */
483
1417
  void ca_refuse_cells_reaching_evaluation (ca_size_t n, const VALUE *cells);
1418
+ /* Runs body(arg), a walk into the cells of an object array, one level deeper
1419
+ in this fiber; past the limit it raises ArgumentError. A cell can give a
1420
+ new array each time it is opened, which the records above never meet
1421
+ twice. */
1422
+ VALUE ca_cell_walk_run (VALUE (*body)(VALUE), VALUE arg);
484
1423
 
485
1424
  /* Registers a view type whose transfers only move cells between its parents
486
1425
  and the caller (an index view: CAGrid, CASelect, CAWindow, CAStack, ...),
487
1426
  calling no Ruby. A transfer through such views down to entities cannot
488
1427
  come back into an array, and is not recorded (ca_with_cyclic_check). */
489
- void ca_register_cell_moving_view (int8_t obj_type);
1428
+ void ca_register_cell_moving_view (int16_t obj_type);
490
1429
  VALUE ca_iter_ensure_computing (CArray *ca, int32_t n, ca_iter_state *states,
491
1430
  VALUE (*body)(VALUE), VALUE arg);
492
1431
 
493
- #define CA_CHECK_DATA_TYPE_GIVEN(data_type) \
494
- do { \
495
- int ca_dt_ = (data_type); \
496
- if ( ca_dt_ <= CA_NONE || ca_dt_ >= CA_NTYPE ) { \
497
- rb_raise(rb_eArgError, "%s: invalid data_type id %i", \
498
- ca_calling_method_name(), ca_dt_); \
499
- } \
500
- if ( ! ca_valid[ca_dt_] ) { \
501
- rb_raise(rb_eCADataTypeError, "%s: data_type %s is disabled", \
502
- ca_calling_method_name(), ca_type_name[ca_dt_]); \
503
- } \
504
- } while (0)
505
-
506
- #define CA_CHECK_DIM_GIVEN(ndim, dim) \
507
- do { \
508
- int8_t i_; \
509
- for (i_ = 0; i_ < (ndim); i_++) { \
510
- if ( (dim)[i_] < 0 ) { \
511
- rb_raise(rb_eArgError, "%s: negative size dimension at %i-dim", \
512
- ca_calling_method_name(), i_); \
513
- } \
514
- } \
515
- } while (0)
516
-
517
- #define CA_CHECK_BYTES_GIVEN(data_type, bytes) \
518
- do { \
519
- if ( (data_type) == CA_FIXLEN ) { \
520
- if ( (bytes) <= 0 ) { \
521
- rb_raise(rb_eArgError, \
522
- "%s: invalid bytes (a fixlen needs bytes: of 1 or more)", \
523
- ca_calling_method_name()); \
524
- } \
525
- } \
526
- else { \
527
- (bytes) = ca_sizeof[(data_type)]; \
528
- if ( (bytes) <= 0 ) { \
529
- rb_raise(rb_eArgError, "%s: invalid bytes", ca_calling_method_name()); \
530
- } \
531
- } \
532
- } while (0)
533
-
534
- /* CA_CHECK_RANK inside the library: no axes at all is no shape, and above
535
- CA_RANK_MAX goes to the part, which names the method. The public macro
536
- stays as it is for extensions. */
537
- #define CA_CHECK_RANK_GIVEN(ndim) \
538
- do { \
539
- long ca_nd_ = (long) (ndim); \
540
- if ( ca_nd_ <= 0 ) { \
541
- rb_raise(rb_eArgError, "invalid ndim"); \
542
- } \
543
- ca_check_rank_fits(ca_nd_, NULL); \
544
- } while (0)
545
- /* An `axis` argument as an axis in [0, ndim): the rule every entry taking
546
- an axis from Ruby calls (carray_utils.c). */
547
- int ca_normalize_axis_arg (VALUE raxis, int ndim, const char *name);
548
1432
  VALUE ca_fill_as (VALUE fill, int8_t data_type);
549
1433
  /* ---- Face MemoryView export (ca_obj_face.c) -------------------------------
550
1434
 
@@ -552,7 +1436,7 @@ VALUE ca_fill_as (VALUE fill, int8_t data_type);
552
1436
  Face (CARecord, CAFixlenString); the MemoryView producer refuses every
553
1437
  other Face. Unregistered Faces are refused. */
554
1438
 
555
- void ca_face_register_memory_view (int obj_type, int exportable);
1439
+ void ca_face_register_memory_view (int16_t obj_type, int exportable);
556
1440
  int ca_face_memory_view_exportable (CArray *ca);
557
1441
 
558
1442
  /* The reference and the operand of a comparison brought into one space (see
@@ -561,55 +1445,6 @@ int ca_face_memory_view_exportable (CArray *ca);
561
1445
  void ca_face_reconcile_pair (volatile VALUE *pref, volatile VALUE *poperand,
562
1446
  const char *name);
563
1447
 
564
- /* ---- Empty regions ---------------------------------------------------------
565
-
566
- A region with a zero count on some axis has no cells. The per-cell walks
567
- visit their first cell before they test the counts, so every region
568
- transfer returns on an empty region before it walks: a zero-cell caller
569
- buffer has no room for that first cell. */
570
-
571
- static inline int
572
- ca_region_is_empty (int8_t ndim, const ca_size_t *counts)
573
- {
574
- int8_t k;
575
- for ( k = 0; k < ndim; k++ ) {
576
- if ( counts[k] <= 0 ) return 1;
577
- }
578
- return 0;
579
- }
580
-
581
- /* ---- An index into n places ----------------------------------------------
582
-
583
- A negative index counts from the end; one outside 0...n after that raises
584
- IndexError, naming the method the user called, `what` the index is (an
585
- "index", an "address") and the value given. Every index door decides
586
- this here. */
587
-
588
- /* ca_index_out_of_range is declared in carray.h (CA_CHECK_INDEX uses it). */
589
-
590
- static inline ca_size_t
591
- ca_index_in_range (ca_size_t idx, ca_size_t n, const char *what)
592
- {
593
- ca_size_t i = ( idx < 0 ) ? idx + n : idx;
594
- if ( i < 0 || i >= n ) {
595
- ca_index_out_of_range(idx, n, what);
596
- }
597
- return i;
598
- }
599
-
600
- /* The same for an index on axis `axis` of a multi-axis index. */
601
- NORETURN(void ca_index_out_of_range_at (ca_size_t idx, ca_size_t n, int axis));
602
-
603
- static inline ca_size_t
604
- ca_index_in_range_at (ca_size_t idx, ca_size_t n, int axis)
605
- {
606
- ca_size_t i = ( idx < 0 ) ? idx + n : idx;
607
- if ( i < 0 || i >= n ) {
608
- ca_index_out_of_range_at(idx, n, axis);
609
- }
610
- return i;
611
- }
612
-
613
1448
  /* ---- How many cells a result cell needs ---------------------------------
614
1449
 
615
1450
  A reduction's result cell is UNDEF when fewer cells contribute to it than
@@ -626,21 +1461,30 @@ ca_too_few_present (ca_size_t present, ca_size_t min_count, ca_size_t least)
626
1461
  }
627
1462
 
628
1463
  long ca_min_count_arg (VALUE v, const char *name);
1464
+
1465
+ /* The sum, the product and the weighted sum (integer weights) of an
1466
+ integer array: the exact int64 / uint64 total, or the float64 one where
1467
+ that is out of range
1468
+ (carray_integer_total.c). The generated sum / prod dispatchers call it
1469
+ with their float64 fold (`general`) and their wrapping integer fold. */
1470
+ enum { CA_INTEGER_TOTAL_SUM, CA_INTEGER_TOTAL_PROD, CA_INTEGER_TOTAL_WSUM };
1471
+ VALUE ca_integer_total (int argc, VALUE *argv, VALUE self,
1472
+ VALUE (*general) (int, VALUE *, VALUE),
1473
+ VALUE (*wrapping) (int, VALUE *, VALUE),
1474
+ int kind);
1475
+ /* The running sum and product of an integer array (cumsum / cumprod), by
1476
+ the same rule: the int64 / uint64 running values, or the float64 scan
1477
+ where one of them is out of range. */
1478
+ VALUE ca_integer_scan (int argc, VALUE *argv, VALUE self,
1479
+ VALUE (*general) (int, VALUE *, VALUE),
1480
+ VALUE (*wrapping) (int, VALUE *, VALUE),
1481
+ int kind);
629
1482
  VALUE ca_reduce_fill (VALUE result, VALUE fill, int whole);
1483
+ VALUE ca_reduce_fill_widening (VALUE result, VALUE fill, int whole,
1484
+ const char *name);
630
1485
  int ca_symbol_choice (VALUE v, const char *arg, const char *c0,
631
1486
  const char *c1, const char *name);
632
1487
 
633
- /* Return the value of `call`, keeping the array `v` reachable until the call
634
- has returned. A kernel reads its source through the CArray pointer only,
635
- so a view made just to be passed in (a flattened or common-type view)
636
- would otherwise be left to a garbage collection that runs while its
637
- cells are read: `return kernel(v, ...)` lets the compiler drop `v`. */
638
- #define CA_RETURN_KEEPING(v, call) do { \
639
- VALUE ca_kept_result_ = (call); \
640
- RB_GC_GUARD(v); \
641
- return ca_kept_result_; \
642
- } while (0)
643
-
644
1488
  /* A NaN loses every contest of an extremum (min / max / minmax / the
645
1489
  positions / the running cummin / cummax / pmin / pmax): it never displaces
646
1490
  another value, the first number displaces it, and nothing but NaN answers
@@ -786,7 +1630,7 @@ void ca_multi_parent_value_trial_through (CArray *m, VALUE (*body)(VALUE)
786
1630
  volatile VALUE keep_ = rb_ary_new(); \
787
1631
  CArray *t = ca_multi_parent_value_trial((CArray *) (ap), keep_); \
788
1632
  call; \
789
- RB_GC_GUARD(keep_); \
1633
+ (void) keep_; /* the call reads the stand-ins, which keep_ holds */ \
790
1634
  } \
791
1635
  } while (0)
792
1636
  int ca_monop_writable_cast_target (CArray *ca, int8_t *target);
@@ -829,7 +1673,7 @@ int8_t ca_query_match_type (int8_t ref, VALUE query);
829
1673
  VALUE ca_fixlen_widen_for_query (VALUE self, VALUE query);
830
1674
 
831
1675
  static inline int
832
- ca_query_match_applies (CArray *ref, VALUE query)
1676
+ ca_query_match_applies (CArray *ref, volatile VALUE query)
833
1677
  {
834
1678
  if ( ! ( ca_is_integer_type(ref) || ca_is_float_type(ref)
835
1679
  || ref->data_type == CA_BOOLEAN ) ) {
@@ -837,7 +1681,7 @@ ca_query_match_applies (CArray *ref, VALUE query)
837
1681
  }
838
1682
  if ( rb_obj_is_carray(query) ) {
839
1683
  CArray *q;
840
- TypedData_Get_Struct(query, CArray, &carray_data_type, q);
1684
+ GetCArray(query, q);
841
1685
  if ( ca_is_face(q) ) {
842
1686
  return 0;
843
1687
  }
@@ -845,6 +1689,56 @@ ca_query_match_applies (CArray *ref, VALUE query)
845
1689
  return 1;
846
1690
  }
847
1691
 
1692
+ /* A Ruby value an integer, float, complex or boolean array can not hold:
1693
+ anything but a Numeric, true, false and UNDEF that does not make an
1694
+ array (an Array, a #to_ca, a memory view). nil, a String, a Symbol and
1695
+ other objects are foreign. An object or fixlen array and a Face hold or
1696
+ read such values, so nothing is foreign to them (ca_cast.c). */
1697
+ int ca_value_foreign_to (CArray *ca, VALUE v);
1698
+
1699
+ /* An operator refuses a foreign value as Float#+ does (TypeError), and an
1700
+ ordering refuses it as Float#< does (ArgumentError). `array` is the
1701
+ CArray operand (ca_cast.c). */
1702
+ void ca_refuse_foreign_operand (VALUE v, VALUE array);
1703
+ void ca_refuse_foreign_order (VALUE v, VALUE array);
1704
+
1705
+ /* true and false as the numbers 1 and 0 for a value query on an integer,
1706
+ float or complex array; any other value, or array, is returned as is
1707
+ (ca_cast.c). */
1708
+ VALUE ca_truth_as_number (CArray *ca, VALUE v);
1709
+
1710
+ /* eq / ne when no cell can match (carray_operator.c). */
1711
+ VALUE ca_bincmp_no_match (VALUE self, int ne);
1712
+
1713
+ /* A value a Face says none of its cells can be (its private method
1714
+ foreign_value?, asked when the Face defines it) (ca_cast.c). */
1715
+ int ca_face_value_foreign (VALUE self, VALUE v);
1716
+
1717
+ /* A value an array can not hold matches no cell as a value query: eq, ne,
1718
+ count and search answer as they do for an Integer outside the array's
1719
+ type. nil is such a value for every array but an object one, which
1720
+ holds nil and is asked. A Face answers for itself (ca_face_value_foreign):
1721
+ a time array, for one, is asked about a time and a time written as a
1722
+ String, and a Symbol or a Float is no time. */
1723
+ static inline int
1724
+ ca_query_matches_nothing (VALUE self, CArray *ca, VALUE query)
1725
+ {
1726
+ if ( NIL_P(query) ) {
1727
+ return ca->data_type != CA_OBJECT;
1728
+ }
1729
+ if ( ca_is_face(ca) ) {
1730
+ return ca_face_value_foreign(self, query);
1731
+ }
1732
+ /* a Complex with an imaginary part is no value of a real cell (one whose
1733
+ imaginary part is 0 is its real part, as eq reads it) */
1734
+ if ( RB_TYPE_P(query, T_COMPLEX) &&
1735
+ ( ca_is_integer_type(ca) || ca_is_float_type(ca) ) &&
1736
+ ! RTEST(rb_equal(rb_complex_imag(query), INT2FIX(0))) ) {
1737
+ return 1;
1738
+ }
1739
+ return ca_value_foreign_to(ca, query);
1740
+ }
1741
+
848
1742
  /* Equality of two object cells, as the float lanes answer it: Ruby's `==`,
849
1743
  except that a Float NaN equals nothing, itself included. rb_equal alone
850
1744
  answers true for the same object, so whether two NaN cells matched would
@@ -986,4 +1880,318 @@ int ca_index_key_is_scalar (VALUE arg);
986
1880
  /* Whether this runs in the main Ractor (carray_utils.c). */
987
1881
  int ca_in_main_ractor (void);
988
1882
 
1883
+ /* Take a native lock that guards a table shared by every Ractor, waiting
1884
+ without the GVL (carray_utils.c). A Ractor that waits for the lock while
1885
+ it holds its GVL cannot stop for a GC, so one that holds the lock and
1886
+ allocates (a GC) would wait for it, and it for the lock. The lock is only
1887
+ ever taken outside the wait, so an interrupt that raises there leaves it
1888
+ free. The lock must not be held across a call that can raise. */
1889
+ void ca_native_lock_without_gvl (rb_nativethread_lock_t *lock);
1890
+
1891
+ /* ---- The kernel drivers (carray_operator.c) -------------------------- */
1892
+
1893
+ /* carray's own drivers, which run a table over arrays. An extension
1894
+ calls the Ruby method instead (rb_funcall), which reaches the table
1895
+ through them. */
1896
+ VALUE rb_ca_call_monop (VALUE self, ca_monop_func_t func[]);
1897
+ VALUE rb_ca_call_monop_bang (VALUE self, ca_monop_func_t func[]);
1898
+ /* Dtype-changing monop dispatch: out_data_types[in_data_type] gives the output
1899
+ data_type for each input data_type (= -1 if not implemented). Used by
1900
+ monops with output: { numeric: ..., complex: ... } Hash form (e.g. abs
1901
+ cmplx128 -> f64). Allocates output array of out_data_types[in_data_type]. */
1902
+ VALUE rb_ca_call_monop_typed (VALUE self, ca_monop_func_t func[], int8_t out_data_types[]);
1903
+ VALUE rb_ca_call_binop (VALUE self, VALUE other, ca_binop_func_t func[]);
1904
+ VALUE rb_ca_call_binop_bang (VALUE self, VALUE other, ca_binop_func_t func[]);
1905
+
1906
+ /* Kleene three-valued mask fixup for boolean AND (is_or=0) / OR (is_or=1),
1907
+ applied by the generated `&`/`|` wrappers after rb_ca_call_binop. No-op
1908
+ unless the output is boolean with a mask. */
1909
+ VALUE ca_kleene_bool_fixup (VALUE vout, VALUE vself, VALUE vother, int is_or);
1910
+
1911
+ /* Triop driver: applies a 3-input / 1-output kernel. Operand 1 = self,
1912
+ operands 2/3 are passed in. Common data_type promotion via
1913
+ rb_ca_cast_self_or_other applied pairwise. Scalar / array
1914
+ combinations (= 2^3 = 8 cases) are handled uniformly via
1915
+ ca_set_iterator(3, ...) which collapses scalar operands to stride 0. */
1916
+ VALUE rb_ca_call_triop (VALUE self, VALUE other2, VALUE other3,
1917
+ ca_triop_func_t func[]);
1918
+ VALUE rb_ca_call_triop_bang (VALUE self, VALUE other2, VALUE other3,
1919
+ ca_triop_func_t func[]);
1920
+ VALUE rb_ca_call_moncmp (VALUE self, ca_moncmp_func_t func[]);
1921
+ VALUE rb_ca_call_bincmp (VALUE self, VALUE other, ca_bincmp_func_t func[], double tol);
1922
+
1923
+ VALUE ca_math_call (VALUE mod, VALUE arg, ID id);
1924
+
1925
+ /* ---- The monop dispatch lookups (ca_monop_dispatch.c, ca_obj_monop.c) */
1926
+
1927
+ /* Look up the per-data_type kernel for a normal monop op_id. Returns NULL
1928
+ for cast op_ids (caller handles cast via ca_cast_block). */
1929
+ ca_monop_func_t ca_monop_kernel_lookup (uint16_t op_id, int8_t in_data_type);
1930
+ int ca_monop_op_id_of_table (ca_monop_func_t table[]);
1931
+
1932
+ /* A CAMonOp node for op_id over cary (CAMonOp.__build__ in Ruby): a cast
1933
+ node when op_id is CA_MONOP_CAST_BASE + a data type. */
1934
+ VALUE rb_ca_monop_build (VALUE cary, uint16_t op_id);
1935
+
1936
+ /* Returns true iff op_id encodes a cast operation. */
1937
+ int ca_monop_is_cast (uint16_t op_id);
1938
+
1939
+ /* Returns true iff op_id widens an integer parent to CA_FLOAT64 (the
1940
+ math functions: sqrt, sin, exp, ...). */
1941
+ int ca_monop_is_widening (uint16_t op_id);
1942
+
1943
+ /* Returns true iff op_id is a "writable view" op
1944
+ (= cast or byte_swap). These ops:
1945
+ - support writable view lifecycle (CA_FLAG_READ_ONLY not set)
1946
+ - have an inverse mapping for write-back (cast = reverse cast;
1947
+ byte_swap = involution = same op applied twice)
1948
+ - use CAFake-style attach/sync/detach lifecycle in CAMonOp
1949
+ (ca_attach(parent) in allocate/attach, op apply to fill ca.ptr,
1950
+ inverse op + memcpy to parent.ptr in sync, ca_detach(parent))
1951
+ Non-cast / non-byte_swap monop / monfunc remain read-only with
1952
+ chain materialise lifecycle. */
1953
+ int ca_monop_is_writable_view (uint16_t op_id);
1954
+
1955
+ /* Returns true iff `view` is a single-node CAMonOp with a cast op_id
1956
+ (= no further CAMonOp in the parent chain). The kernel iterator's
1957
+ per-fiber fused path takes such a view.
1958
+
1959
+ For chain CAMonOp (= depth ≥ 2, e.g. `a.lazy.sqrt.cast`), returns 0
1960
+ so the existing chain materialise path (= arena-pooled scratches)
1961
+ continues to handle the chain end-to-end.
1962
+
1963
+ `view` must be a non-NULL CArray*. Returns 0 for non-CAMonOp views.
1964
+ Defined in ca_obj_monop.c so it can access the CAMonOp struct internals
1965
+ (op_id tail field, parent pointer). */
1966
+ int ca_monop_view_is_single_cast (CArray *view);
1967
+
1968
+ /* Compute the output data_type for (op_id, parent_data_type). Mirrors existing
1969
+ eager mkmath rules. */
1970
+ int8_t ca_lazy_promote_monop (uint16_t op_id, int8_t parent_data_type);
1971
+
1972
+ /* Compute the data_type the kernel expects as input. When != parent_data_type,
1973
+ a CAMonOp(:cast_<data_type>) node must be inserted between this op and
1974
+ its parent (finding #1, cast-before). For cast op_ids, returns
1975
+ parent_data_type unchanged. */
1976
+ int8_t ca_monop_kernel_input_data_type (uint16_t op_id, int8_t parent_data_type);
1977
+
1978
+ /* ---- The binop dispatch lookups (ca_binop_dispatch.c) ---------------- */
1979
+
1980
+ /* Look up the same-type vv kernel for (op_id, common_dt). Returns NULL
1981
+ if not implemented. */
1982
+ ca_binop_func_t ca_binop_kernel_lookup_vv (uint16_t op_id, int8_t common_dt);
1983
+
1984
+ /* Compute output (= common) data_type via the existing eager promotion rule
1985
+ (ca_promote_type). */
1986
+ int8_t ca_lazy_promote_binop (uint16_t op_id, int8_t l_dt, int8_t r_dt);
1987
+
1988
+ /* What data_type does each operand need to be cast to before reaching the
1989
+ kernel? Returns common data_type for both (= cast-before route). When
1990
+ common != operand.data_type, caller inserts CAMonOp(:cast_<common>). */
1991
+ void ca_binop_kernel_input_data_types (uint16_t op_id, int8_t l_dt, int8_t r_dt,
1992
+ int8_t *out_l_in_dt, int8_t *out_r_in_dt);
1993
+
1994
+ /* The op id of a kernel table, and properties of an op id. */
1995
+ int ca_binop_op_id_of_table (ca_binop_func_t table[]);
1996
+ int ca_binop_picks_by_order (int op_id);
1997
+ int ca_binop_is_ring (int op_id);
1998
+ int ca_binop_scalar_operand_mode (int op_id);
1999
+
2000
+ /* ---- The triop dispatch lookups (ca_triop_dispatch.c) ---------------- */
2001
+
2002
+ /* Look up the triop kernel for (op_id, common_dt). Returns NULL if
2003
+ not implemented at the requested data_type. */
2004
+ ca_triop_func_t ca_triop_kernel_lookup_vvv (uint16_t op_id, int8_t common_dt);
2005
+
2006
+ /* 3-way promotion: common data_type of (dt1, dt2, dt3). For select it is
2007
+ that of the two branches (dt2, dt3); the condition is converted to it. */
2008
+ int8_t ca_lazy_promote_triop (uint16_t op_id, int8_t dt1, int8_t dt2, int8_t dt3);
2009
+
2010
+ /* Each operand's cast-target data_type (= common data_type for all three,
2011
+ the cast-before invariant). */
2012
+ void ca_triop_kernel_input_data_types (uint16_t op_id,
2013
+ int8_t dt1, int8_t dt2, int8_t dt3,
2014
+ int8_t *out_dt1, int8_t *out_dt2,
2015
+ int8_t *out_dt3);
2016
+
2017
+ /* ---- The bincmp dispatch lookups (ca_bincmp_dispatch.c) -------------- */
2018
+
2019
+ /* Look up the bincmp kernel for (op_id, common_dt). Returns NULL if
2020
+ not implemented at the requested data_type. All bincmp kernels write
2021
+ boolean8_t to ptr3. */
2022
+ ca_bincmp_func_t ca_bincmp_kernel_lookup (uint16_t op_id, int8_t common_dt);
2023
+
2024
+ /* Compute input common data_type via ca_promote_type. Output data_type is
2025
+ always CA_BOOLEAN (NOT returned here; CABinCmp.data_type is set
2026
+ directly). */
2027
+ int8_t ca_lazy_bincmp_promote (uint16_t op_id, int8_t l_dt, int8_t r_dt);
2028
+
2029
+ /* What data_type does each operand need to be cast to before reaching the
2030
+ kernel? Returns common data_type for both (= cast-before route, same as
2031
+ CABinOp). */
2032
+ void ca_bincmp_kernel_input_data_types (uint16_t op_id, int8_t l_dt, int8_t r_dt,
2033
+ int8_t *out_l_in_dt, int8_t *out_r_in_dt);
2034
+
2035
+ /* ---- The moncmp dispatch lookup (ca_moncmp_dispatch.c) --------------- */
2036
+
2037
+ /* Look up the moncmp kernel for (op_id, in_dt). Returns NULL if not
2038
+ implemented at the requested data_type. All moncmp kernels write
2039
+ boolean8_t to ptr2. No cast insertion needed (= kernel exists for
2040
+ all numeric data_types including integer, where it const-folds inside
2041
+ the kernel + preserves mask via skip semantics). */
2042
+ ca_moncmp_func_t ca_moncmp_kernel_lookup (uint16_t op_id, int8_t in_dt);
2043
+
2044
+ /* ---- carray's own Face parts (ca_obj_face.c) ------------------------- */
2045
+
2046
+ #include "ca_obj_face.h" /* the Face hook types the parts below use */
2047
+
2048
+ /* -- wrapper lift (Face or CALazyMarker) --
2049
+
2050
+ For carray's own view-making methods. An extension's view-making
2051
+ method lifts a Face with CA_FACE_LIFT_IF_FACE.
2052
+
2053
+ A Face and a CALazyMarker are both storage-identical wrappers: they add
2054
+ an interpretation over their parent's bytes without changing them. Both
2055
+ want the same invariant at a view-creating method -- the wrapper stays on
2056
+ top, and the view is built against what the wrapper wraps. For a Face
2057
+ that keeps `dt.shift(1)` a CATime; for a marker it keeps a fuse block's
2058
+ expression lazy instead of dropping out of the chain.
2059
+
2060
+ The flag test comes first and covers both bits in one mask, so an
2061
+ ordinary array leaves through a single AND. That matters: `[]` is the
2062
+ hottest method this is deployed on.
2063
+
2064
+ NOT interchangeable with CA_FACE_LIFT_IF_FACE. Face is lifted at ~24
2065
+ sites, including ones a marker must not follow it through -- `copy` owns
2066
+ its data, `sort` reorders values, `value` and `strip_mask` change what
2067
+ the mask means. Deploy this only where the result is a view whose shape
2068
+ was fixed at construction and which only moves positions. */
2069
+ #define CA_WRAPPER_LIFT(obj, self, ca) do { \
2070
+ if ( ca_test_flag((ca), CA_FLAG_IS_FACE | CA_FLAG_IS_LAZY_MARKER) \
2071
+ && rb_obj_is_kind_of((obj), rb_cCArray) ) { \
2072
+ (obj) = ca_wrapper_lift((obj), (self), (ca)); \
2073
+ } \
2074
+ } while (0)
2075
+
2076
+ VALUE ca_wrapper_lift (VALUE view, VALUE wrapper, void *wrapper_ca);
2077
+
2078
+ /* -- carray's own Face parts --
2079
+
2080
+ These parts are for carray's own sources. An extension lifts a view
2081
+ with CA_FACE_LIFT_IF_FACE, converts through the hooks it registers, and
2082
+ gates its ordering and search kernels with the parts in ca_obj_face.h. */
2083
+
2084
+ /* Struct duplicate + parent + shape swap. The primitive under
2085
+ ca_face_lift; also called directly where a result is re-wrapped under
2086
+ a Face without going through a view (partition, generated reduce). */
2087
+ VALUE rb_ca_face_template (VALUE original_face, CArray *new_parent,
2088
+ ca_size_t *new_dim);
2089
+
2090
+ /* Lift a fresh array `result` (one a reduction, a scan or a partition built
2091
+ from the Face's storage) into the Face `face`: the same Face class over
2092
+ `result`, carrying the subclass tail, `result`'s mask and the state
2093
+ copy_state carries. `result` stays reachable through the new Face. */
2094
+ VALUE ca_face_relift_entity (VALUE face, VALUE result);
2095
+
2096
+ /* Face gate for the element-wise comparison operators (< <= > >=, eq, ne).
2097
+ When self is a Face over numeric storage, descends both operands to
2098
+ storage (ORDERABLE required) and reconciles a Face RHS via to_comparable;
2099
+ a fixlen-storage Face or a non-Face self is left untouched. Called from
2100
+ rb_ca_call_bincmp. See ca_obj_face.c for the full contract. */
2101
+ void ca_face_reconcile_comparison (volatile VALUE *pself,
2102
+ volatile VALUE *pother);
2103
+
2104
+ /* CAFace abstract base class init.
2105
+ Called from ruby_carray.c's Init after rb_cCAView is defined, before
2106
+ CATime / CATimedelta Init. */
2107
+ void Init_ca_face (void);
2108
+
2109
+ /* Composite Face data_class dispatch. Extern for direct C access from
2110
+ `rb_ca_data_class` (carray_attribute.c) into the CARecord tail.
2111
+ CA_OBJ_RECORD identifies the obj_type (assigned at runtime by
2112
+ ca_install_obj_type). A further composite Face follows the same
2113
+ pattern: an extern here and one case in the switch in
2114
+ carray_attribute.c. */
2115
+ extern int16_t CA_OBJ_RECORD;
2116
+ VALUE rb_ca_record_get_data_class (CArray *ca);
2117
+
2118
+ /* CA_OBJ_STRING identifies CAString, read by the store path
2119
+ (carray_access.c) to send an array source through the write hook. */
2120
+ extern int16_t CA_OBJ_STRING;
2121
+
2122
+ /* Single source of truth for the surface -> storage-domain conversion.
2123
+ Returns `obj` unchanged when `ca` is not a Face or when no hook is
2124
+ registered / defined (so a bare Integer / String passes through to the
2125
+ existing storage cast). Otherwise returns the storage-domain value the
2126
+ Face produced. Called from rb_ca_obj2ptr (single-cell / fill / masked /
2127
+ range stores) and from the Array-of-scalars bulk store branch. */
2128
+ VALUE ca_face_element_to_storage (VALUE self, CArray *ca, VALUE obj);
2129
+
2130
+ /* The tables the registration functions write, one entry per obj_type. */
2131
+ extern ca_face_element_to_storage_fn ca_face_element_to_storage_table[];
2132
+ extern ca_face_state_compatible_fn ca_face_state_compatible_table[];
2133
+ extern int8_t ca_face_state_portable_table[];
2134
+
2135
+ /* Dispatch order:
2136
+ 1. C hook registered for a->obj_type → call hook, return its result
2137
+ 2. Else if the Ruby class of `va` defines `face_state_compatible?`
2138
+ on a Face subclass (= overrides CArray's default) → invoke with
2139
+ `vb`, return RTEST of result
2140
+ 3. Else → 1 (= default compatible, class identity only)
2141
+ Caller must pre-check a->obj_type == b->obj_type. `va` / `vb` are
2142
+ the Ruby VALUEs of the same instances (used only for the Ruby fallback
2143
+ path). */
2144
+ int ca_face_state_compatible (VALUE va, CArray *a, VALUE vb, CArray *b);
2145
+
2146
+ /* Dispatch:
2147
+ 1. C table entry registered for obj_type → return that value
2148
+ 2. Else if the Ruby class `klass` defines a singleton method
2149
+ `face_state_portable?` (= subclass owns it) → invoke and RTEST
2150
+ 3. Else → 1 (= default portable)
2151
+ `klass` is the Ruby class of the Face instance (used for the Ruby
2152
+ fallback path). Pass Qnil when the class is unknown / unavailable
2153
+ (= falls through to default). */
2154
+ int ca_face_state_portable (int16_t obj_type, VALUE klass);
2155
+
2156
+ /* Check a list of arrays about to be combined into one (CAStack, CAMeld,
2157
+ promote_list). Returns the Face class every element shares, or Qnil
2158
+ when none of them is a Face; the combined array is then lifted into
2159
+ that class. Raises ArgumentError for a mix of Face and non-Face arrays
2160
+ or of two Face classes, for two or more of a Face whose state is not
2161
+ portable, and for elements whose state differs from the first one's.
2162
+ `who` names the caller at the head of the message. */
2163
+ VALUE ca_face_list_class (VALUE list, const char *who);
2164
+
2165
+ /* A Face array's storage-side parent, one level down; any other array
2166
+ as it is. */
2167
+ VALUE ca_face_strip_one (VALUE v);
2168
+
2169
+ /* CArray#parent: for a Face, a read-only view of its storage; for any other
2170
+ array, its parent as it is. */
2171
+ VALUE rb_ca_parent_method (VALUE self);
2172
+
2173
+ /* The conversion a refusal of a Face whose cells encode its values points
2174
+ at (#to_string for a string Face, to_type(:object) for any other). */
2175
+ const char *ca_face_conversion_hint (VALUE face);
2176
+
2177
+ /* The method that gives the storage under a Face (#ticks, #codes), or NULL
2178
+ when the Face has none of those. */
2179
+ const char *ca_face_storage_method (VALUE face);
2180
+
2181
+ /* Raise eclass naming a Face that does not define the numeric operation
2182
+ `name`; returns when self is not a Face. See ca_obj_face.c. */
2183
+ void ca_face_refuse_numeric (VALUE self, const char *name, VALUE eclass);
2184
+
2185
+ /* Bring a Face operand into the reference's storage space, or refuse. A
2186
+ Face passes only if it declares COMPARABLE_STORAGE, i.e. its storage
2187
+ cells are the values it shows; anything else (a byte range, a code, a
2188
+ tick) is refused rather than compared as bytes. Non-Face operands are
2189
+ returned untouched. `name` opens the message. */
2190
+ VALUE ca_face_operand_descend (VALUE operand, const char *name);
2191
+
2192
+ /* `==` (strict = 0) / `eql?` (strict = 1) when an operand is a Face other
2193
+ than a record array. See ca_obj_face.c. */
2194
+ int ca_face_equal (VALUE va, VALUE vb, int strict);
2195
+
2196
+
989
2197
  #endif /* CARRAY_INTERNAL_H */