carray 3.0.6 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (277) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -1
  3. data/CHANGELOG.md +7 -1830
  4. data/README.md +62 -35
  5. data/carray.gemspec +16 -4
  6. data/docs/changelogs/CHANGELOG-3.0.md +2464 -0
  7. data/ext/ca_array_pool.c +4 -5
  8. data/ext/ca_axis_descriptor.h +45 -30
  9. data/ext/ca_axis_dispatch.c +7 -4
  10. data/ext/ca_axis_group.c +12 -15
  11. data/ext/ca_bincmp_dispatch.h +16 -38
  12. data/ext/ca_binop_dispatch.c +35 -0
  13. data/ext/ca_binop_dispatch.h +22 -46
  14. data/ext/ca_categorical_iterator.c +36 -34
  15. data/ext/ca_compare.c +0 -1
  16. data/ext/ca_compare.h +8 -1
  17. data/ext/ca_composite_dispatch.h +6 -0
  18. data/ext/ca_for_buffer.h +6 -1
  19. data/ext/ca_group_iter.c +17 -18
  20. data/ext/ca_iter_state_fields.h +196 -0
  21. data/ext/ca_iter_substrate.h +84 -103
  22. data/ext/ca_kernel_iterator.c +125 -89
  23. data/ext/ca_kernel_iterator.h +229 -1504
  24. data/ext/ca_kernel_iterator_internal.h +1167 -0
  25. data/ext/ca_moncmp_dispatch.h +17 -24
  26. data/ext/ca_monop_dispatch.h +25 -74
  27. data/ext/ca_obj_array.c +92 -165
  28. data/ext/ca_obj_bincmp.c +65 -48
  29. data/ext/ca_obj_binop.c +74 -49
  30. data/ext/ca_obj_bitarray.c +7 -21
  31. data/ext/ca_obj_bitfield.c +93 -26
  32. data/ext/ca_obj_block.c +11 -12
  33. data/ext/ca_obj_byte_swap.c +34 -34
  34. data/ext/ca_obj_const_string.c +464 -304
  35. data/ext/ca_obj_face.c +167 -121
  36. data/ext/ca_obj_face.h +32 -170
  37. data/ext/ca_obj_fake.c +127 -34
  38. data/ext/ca_obj_farray.c +13 -9
  39. data/ext/ca_obj_field.c +17 -13
  40. data/ext/ca_obj_fixlen_string.c +57 -34
  41. data/ext/ca_obj_grid.c +149 -113
  42. data/ext/ca_obj_meld.c +28 -26
  43. data/ext/ca_obj_moncmp.c +10 -21
  44. data/ext/ca_obj_monop.c +88 -73
  45. data/ext/ca_obj_object.c +60 -46
  46. data/ext/ca_obj_record.c +74 -22
  47. data/ext/ca_obj_reduce.c +10 -14
  48. data/ext/ca_obj_refer.c +146 -39
  49. data/ext/ca_obj_remap.c +11 -29
  50. data/ext/ca_obj_repeat.c +41 -23
  51. data/ext/ca_obj_roll.c +20 -28
  52. data/ext/ca_obj_select.c +17 -27
  53. data/ext/ca_obj_select_axis.c +35 -42
  54. data/ext/ca_obj_shift.c +22 -42
  55. data/ext/ca_obj_source.c +1 -5
  56. data/ext/ca_obj_stack.c +33 -29
  57. data/ext/ca_obj_stride.c +99 -62
  58. data/ext/ca_obj_string.c +8 -20
  59. data/ext/ca_obj_tile.c +14 -16
  60. data/ext/ca_obj_time.c +12 -23
  61. data/ext/ca_obj_timedelta.c +12 -23
  62. data/ext/ca_obj_transpose.c +8 -10
  63. data/ext/ca_obj_triop.c +48 -49
  64. data/ext/ca_obj_window.c +87 -99
  65. data/ext/ca_op_cmplx64.h +4 -0
  66. data/ext/ca_op_ipower.c +97 -27
  67. data/ext/ca_op_powi.h +9 -28
  68. data/ext/ca_pow5_table.h +2 -1
  69. data/ext/ca_rng_normal.h +4 -0
  70. data/ext/ca_rng_xoshiro256pp.h +3 -0
  71. data/ext/ca_sort_kernels.h +23 -26
  72. data/ext/ca_sweep_engine.c +92 -26
  73. data/ext/ca_sweep_engine.h +22 -15
  74. data/ext/ca_transform_common.c +3 -8
  75. data/ext/ca_triop_dispatch.h +20 -32
  76. data/ext/caframe_csv_reader.c +12 -12
  77. data/ext/carray.h +300 -1009
  78. data/ext/carray_access.c +320 -207
  79. data/ext/carray_address_basis.c +128 -59
  80. data/ext/carray_attribute.c +81 -104
  81. data/ext/carray_bincount.c +12 -14
  82. data/ext/carray_broadcast.c +31 -27
  83. data/ext/carray_call_cfunc.c +174 -285
  84. data/ext/carray_call_cfunc.h +36 -28
  85. data/ext/carray_cast.c +1191 -351
  86. data/ext/carray_cast_func.rb +68 -8
  87. data/ext/carray_class.c +6 -11
  88. data/ext/carray_conversion.c +98 -39
  89. data/ext/carray_copy.c +52 -26
  90. data/ext/carray_core.c +286 -132
  91. data/ext/carray_count.c +53 -22
  92. data/ext/carray_element.c +49 -28
  93. data/ext/carray_factorize.c +64 -63
  94. data/ext/carray_generate.c +26 -19
  95. data/ext/carray_histogram.c +9 -11
  96. data/ext/carray_hold.c +7 -9
  97. data/ext/carray_index_classifier.c +127 -230
  98. data/ext/carray_index_classifier.h +11 -10
  99. data/ext/carray_integer_total.c +523 -0
  100. data/ext/carray_internal.h +1364 -156
  101. data/ext/carray_key_windows.c +3 -3
  102. data/ext/carray_lazy.c +37 -37
  103. data/ext/carray_loop.c +27 -23
  104. data/ext/carray_mask.c +235 -150
  105. data/ext/carray_math_kernel.h +23 -47
  106. data/ext/carray_mathfunc.c +10 -3
  107. data/ext/carray_median_percentile.c +106 -100
  108. data/ext/carray_memory_view.c +83 -69
  109. data/ext/carray_operator.c +490 -128
  110. data/ext/carray_order.c +163 -84
  111. data/ext/carray_partition.c +14 -16
  112. data/ext/carray_random.c +48 -42
  113. data/ext/carray_round.c +33 -5
  114. data/ext/carray_scatter.c +57 -39
  115. data/ext/carray_segment.c +42 -46
  116. data/ext/carray_slab.c +77 -51
  117. data/ext/carray_slab.h +44 -33
  118. data/ext/carray_sort.c +37 -31
  119. data/ext/carray_sort_kernel.c +36 -10
  120. data/ext/carray_struct.c +44 -34
  121. data/ext/carray_test.c +131 -81
  122. data/ext/carray_time_text.c +30 -31
  123. data/ext/carray_undef.c +50 -7
  124. data/ext/carray_utils.c +373 -139
  125. data/ext/extconf.rb +24 -17
  126. data/ext/mk_call_cfunc.rb +25 -31
  127. data/ext/mk_pow5_table.rb +2 -1
  128. data/ext/mkkernel.rb +819 -488
  129. data/ext/ruby_carray.c +47 -42
  130. data/ext/version.h +7 -5
  131. data/lib/carray/attribute.rb +19 -18
  132. data/lib/carray/autoload_carray.rb +20 -15
  133. data/lib/carray/autoload_method_extension.rb +64 -57
  134. data/lib/carray/axis_group.rb +272 -185
  135. data/lib/carray/basics.rb +298 -56
  136. data/lib/carray/bincount_nd.rb +41 -18
  137. data/lib/carray/block_iterator.rb +167 -119
  138. data/lib/carray/boolean_reduce.rb +7 -4
  139. data/lib/carray/categorical.rb +84 -59
  140. data/lib/carray/categorical_iterator.rb +90 -67
  141. data/lib/carray/complex.rb +61 -41
  142. data/lib/carray/conditional.rb +98 -23
  143. data/lib/carray/const_string.rb +112 -46
  144. data/lib/carray/core_extensions.rb +20 -69
  145. data/lib/carray/distance_limit.rb +7 -5
  146. data/lib/carray/factories.rb +269 -0
  147. data/lib/carray/fixlen_string.rb +95 -12
  148. data/lib/carray/frame/column_verbs.rb +76 -9
  149. data/lib/carray/frame/concat.rb +51 -7
  150. data/lib/carray/frame/convert.rb +2 -1
  151. data/lib/carray/frame/csv_parser.rb +20 -5
  152. data/lib/carray/frame/frame.rb +73 -31
  153. data/lib/carray/frame/group.rb +247 -187
  154. data/lib/carray/frame/io.rb +36 -12
  155. data/lib/carray/frame/join.rb +56 -17
  156. data/lib/carray/frame/records.rb +7 -5
  157. data/lib/carray/frame/reshape.rb +27 -7
  158. data/lib/carray/frame/sort.rb +10 -8
  159. data/lib/carray/frame/verbs.rb +217 -134
  160. data/lib/carray/frame/windows.rb +254 -19
  161. data/lib/carray/frame.rb +1 -2
  162. data/lib/carray/fuse_source.rb +28 -23
  163. data/lib/carray/fusion.rb +30 -30
  164. data/lib/carray/histogram.rb +35 -16
  165. data/lib/carray/inspect.rb +7 -7
  166. data/lib/carray/iterator.rb +104 -23
  167. data/lib/carray/key_windows.rb +93 -61
  168. data/lib/carray/lazy.rb +208 -111
  169. data/lib/carray/limits.rb +80 -0
  170. data/lib/carray/mask_gap_fill.rb +21 -16
  171. data/lib/carray/math.rb +87 -50
  172. data/lib/carray/meld_reduce.rb +30 -8
  173. data/lib/carray/methods/align_addr.rb +29 -17
  174. data/lib/carray/methods/bin.rb +8 -1
  175. data/lib/carray/methods/bincount.rb +4 -2
  176. data/lib/carray/methods/bit_string.rb +19 -31
  177. data/lib/carray/methods/broadcast.rb +8 -3
  178. data/lib/carray/methods/choose.rb +5 -5
  179. data/lib/carray/methods/composition.rb +73 -72
  180. data/lib/carray/methods/{index.rb → coordinate.rb} +13 -13
  181. data/lib/carray/methods/discovery_along.rb +3 -3
  182. data/lib/carray/methods/factorize.rb +2 -0
  183. data/lib/carray/methods/gather_nd.rb +106 -103
  184. data/lib/carray/methods/insert_block.rb +2 -2
  185. data/lib/carray/methods/is_between.rb +6 -4
  186. data/lib/carray/methods/is_in.rb +183 -143
  187. data/lib/carray/methods/join.rb +2 -1
  188. data/lib/carray/methods/lag.rb +6 -2
  189. data/lib/carray/methods/locate_addr.rb +157 -94
  190. data/lib/carray/methods/mask_duplicates.rb +1 -1
  191. data/lib/carray/methods/meshgrid.rb +28 -16
  192. data/lib/carray/methods/mode.rb +67 -67
  193. data/lib/carray/methods/nunique.rb +9 -7
  194. data/lib/carray/methods/repeat.rb +23 -13
  195. data/lib/carray/methods/resize.rb +1 -1
  196. data/lib/carray/methods/snap.rb +24 -20
  197. data/lib/carray/methods/string_format.rb +2 -2
  198. data/lib/carray/methods/unique.rb +4 -2
  199. data/lib/carray/methods/value_counts.rb +1 -1
  200. data/lib/carray/mkmf.rb +25 -28
  201. data/lib/carray/percentile_arguments.rb +82 -0
  202. data/lib/carray/record_order.rb +50 -20
  203. data/lib/carray/rng.rb +1 -0
  204. data/lib/carray/runtime.rb +195 -17
  205. data/lib/carray/segment_iterator.rb +262 -137
  206. data/lib/carray/serialize.rb +635 -566
  207. data/lib/carray/slab_iterator.rb +114 -114
  208. data/lib/carray/stack.rb +14 -15
  209. data/lib/carray/string.rb +16 -22
  210. data/lib/carray/string_operation_extension.rb +97 -52
  211. data/lib/carray/struct.rb +122 -164
  212. data/lib/carray/struct_builder.rb +247 -73
  213. data/lib/carray/time.rb +663 -365
  214. data/lib/carray/window_iterator.rb +232 -155
  215. data/lib/carray.rb +6 -11
  216. data/yard-stubs/ca_obj_array.rb +20 -135
  217. data/yard-stubs/ca_obj_bitarray.rb +1 -8
  218. data/yard-stubs/ca_obj_bitfield.rb +6 -2
  219. data/yard-stubs/ca_obj_byte_swap.rb +21 -7
  220. data/yard-stubs/ca_obj_fake.rb +3 -1
  221. data/yard-stubs/ca_obj_farray.rb +1 -1
  222. data/yard-stubs/ca_obj_field.rb +8 -6
  223. data/yard-stubs/ca_obj_refer.rb +10 -5
  224. data/yard-stubs/ca_obj_roll.rb +7 -6
  225. data/yard-stubs/ca_obj_shift.rb +2 -3
  226. data/yard-stubs/ca_obj_stride.rb +5 -7
  227. data/yard-stubs/ca_obj_tile.rb +1 -1
  228. data/yard-stubs/ca_obj_time.rb +43 -0
  229. data/yard-stubs/ca_obj_transpose.rb +1 -1
  230. data/yard-stubs/ca_obj_window.rb +28 -16
  231. data/yard-stubs/carray_access.rb +55 -6
  232. data/yard-stubs/carray_attribute.rb +26 -38
  233. data/yard-stubs/carray_cast.rb +121 -162
  234. data/yard-stubs/carray_class.rb +3 -4
  235. data/yard-stubs/carray_conversion.rb +6 -7
  236. data/yard-stubs/carray_copy.rb +2 -1
  237. data/yard-stubs/carray_count.rb +10 -5
  238. data/yard-stubs/carray_element.rb +4 -3
  239. data/yard-stubs/carray_limits.rb +100 -0
  240. data/yard-stubs/carray_loop.rb +0 -20
  241. data/yard-stubs/carray_mask.rb +59 -41
  242. data/yard-stubs/carray_math.rb +162 -100
  243. data/yard-stubs/carray_mathfunc.rb +415 -3
  244. data/yard-stubs/carray_median_percentile.rb +29 -11
  245. data/yard-stubs/carray_memory_view.rb +2 -2
  246. data/yard-stubs/carray_order.rb +72 -33
  247. data/yard-stubs/carray_random.rb +6 -6
  248. data/yard-stubs/carray_reduce.rb +353 -0
  249. data/yard-stubs/carray_rng.rb +92 -0
  250. data/yard-stubs/carray_scatter.rb +12 -3
  251. data/yard-stubs/carray_slab.rb +7 -4
  252. data/yard-stubs/carray_sort.rb +7 -6
  253. data/yard-stubs/carray_test.rb +18 -5
  254. data/yard-stubs/carray_undef.rb +44 -5
  255. data/yard-stubs/ruby_carray.rb +75 -40
  256. metadata +63 -25
  257. data/CHANGELOG.v1.md +0 -126
  258. data/ext/carray_build_flags.h +0 -3
  259. data/ext/carray_config.h +0 -28
  260. data/ext/carray_data_type.c +0 -90
  261. data/ext/carray_kernels_bincmp.c +0 -6457
  262. data/ext/carray_kernels_binop.c +0 -31534
  263. data/ext/carray_kernels_init.c +0 -1734
  264. data/ext/carray_kernels_moncmp.c +0 -2104
  265. data/ext/carray_kernels_monop.c +0 -25387
  266. data/ext/carray_kernels_reduce_aggregate.c +0 -27545
  267. data/ext/carray_kernels_reduce_boolean.c +0 -1924
  268. data/ext/carray_kernels_reduce_cumulative.c +0 -11646
  269. data/ext/carray_kernels_reduce_extreme.c +0 -21631
  270. data/ext/carray_kernels_reduce_variance.c +0 -5981
  271. data/ext/carray_kernels_scan.c +0 -4302
  272. data/ext/carray_kernels_search.c +0 -37776
  273. data/ext/carray_kernels_sort.c +0 -10814
  274. data/ext/carray_kernels_triop.c +0 -1864
  275. data/lib/carray/construct.rb +0 -360
  276. data/lib/carray/data_type_extension.rb +0 -261
  277. data/lib/carray/data_type_limits.rb +0 -91
data/ext/ca_op_ipower.c CHANGED
@@ -1,7 +1,10 @@
1
1
  /* ---------------------------------------------------------------------------
2
2
 
3
- Float/Complex ** Integer integer-exponent fast path via
4
- binary exponentiation (O(log p)).
3
+ Float/Complex ** Integer: the exponent stays an int64 (no cast of the
4
+ array to a wider type), and each cell is computed exactly as `a ** b`
5
+ computes it when b is an array of that integer: the exponents -2 .. 4
6
+ by at most two multiplications or divisions, the rest by pow / powf /
7
+ cpow.
5
8
 
6
9
  Contents:
7
10
  (1) LAZY kernel table `ca_binop_ipower[CA_NTYPE]` — CABinOp dispatch
@@ -11,14 +14,59 @@
11
14
  `rb_ca_pow{,_bang}` dispatch between ipower (Float/Complex **
12
15
  Integer) and mkkernel-generated `rb_ca_power` (general pow).
13
16
 
14
- Both paths share `op_powi_<type>` from ca_op_powi.h (= repeated
15
- squaring, ~1-3 mul/cell for typical p).
17
+ Both paths share `ca_ipow_<type>` below. The exponent is converted
18
+ the way an int64 cell is converted to the base's type (through double
19
+ for float32 / cmplx64), so the answer is the one the general power
20
+ kernel gives for the same exponent held in an array.
16
21
 
17
22
  ---------------------------------------------------------------------------- */
18
23
 
19
24
  #include "carray.h"
20
25
  #include "ca_binop_dispatch.h"
21
- #include "ca_op_powi.h"
26
+ #include <math.h>
27
+ #include <complex.h>
28
+
29
+ /* The exponents -2 .. 4 take at most two roundings, as in the power
30
+ kernel (MkKernel.pow_short). */
31
+ #define CA_IPOW_SHORT(x, p) do { \
32
+ switch ( p ) { \
33
+ case 2: return (x) * (x); \
34
+ case 3: return (x) * (x) * (x); \
35
+ case 4: return ((x) * (x)) * ((x) * (x)); \
36
+ case 1: return (x); \
37
+ case 0: return 1; \
38
+ case -1: return 1 / (x); \
39
+ case -2: return 1 / ((x) * (x)); \
40
+ } \
41
+ } while (0)
42
+
43
+ static inline float32_t
44
+ ca_ipow_float32_t (float32_t x, int64_t p)
45
+ {
46
+ CA_IPOW_SHORT(x, p);
47
+ return powf(x, (float) (double) p);
48
+ }
49
+
50
+ static inline float64_t
51
+ ca_ipow_float64_t (float64_t x, int64_t p)
52
+ {
53
+ CA_IPOW_SHORT(x, p);
54
+ return pow(x, (double) p);
55
+ }
56
+
57
+ static inline cmplx64_t
58
+ ca_ipow_cmplx64_t (cmplx64_t x, int64_t p)
59
+ {
60
+ CA_IPOW_SHORT(x, p);
61
+ return cpow(x, (float) (double) p);
62
+ }
63
+
64
+ static inline cmplx128_t
65
+ ca_ipow_cmplx128_t (cmplx128_t x, int64_t p)
66
+ {
67
+ CA_IPOW_SHORT(x, p);
68
+ return cpow(x, (double) p);
69
+ }
22
70
 
23
71
  /* Defined in ext/carray_kernels.c (mkkernel-generated general pow /
24
72
  cpow binop). Called by rb_ca_pow{,_bang} when the fast path does
@@ -71,7 +119,7 @@ ca_binop_ipower_##T (ca_size_t n, boolean8_t *m, \
71
119
  p1 = q1 + k*i1; \
72
120
  p2 = q2 + k*i2; \
73
121
  p3 = q3 + k*i3; \
74
- *p3 = op_powi_##T(*p1, *p2); \
122
+ *p3 = ca_ipow_##T(*p1, *p2); \
75
123
  } \
76
124
  } \
77
125
  } else { \
@@ -79,7 +127,7 @@ ca_binop_ipower_##T (ca_size_t n, boolean8_t *m, \
79
127
  p1 = q1 + k*i1; \
80
128
  p2 = q2 + k*i2; \
81
129
  p3 = q3 + k*i3; \
82
- *p3 = op_powi_##T(*p1, *p2); \
130
+ *p3 = ca_ipow_##T(*p1, *p2); \
83
131
  } \
84
132
  } \
85
133
  }
@@ -115,12 +163,12 @@ ca_ipower_float32_t (ca_size_t n, boolean8_t *m,
115
163
  ca_u_float32_t *p1 = (ca_u_float32_t *) ptr1, *p2 = (ca_u_float32_t *) ptr2;
116
164
  if ( m ) {
117
165
  while (n--) {
118
- if ( ! *m++ ) { *p2 = op_powi_float32_t(*p1, ipow); }
166
+ if ( ! *m++ ) { *p2 = ca_ipow_float32_t(*p1, ipow); }
119
167
  p1++; p2++;
120
168
  }
121
169
  }
122
170
  else
123
- while (n--) { (*p2) = op_powi_float32_t(*p1, ipow); p1++; p2++; }
171
+ while (n--) { (*p2) = ca_ipow_float32_t(*p1, ipow); p1++; p2++; }
124
172
  }
125
173
 
126
174
  static void
@@ -130,12 +178,12 @@ ca_ipower_float64_t (ca_size_t n, boolean8_t *m,
130
178
  ca_u_float64_t *p1 = (ca_u_float64_t *) ptr1, *p2 = (ca_u_float64_t *) ptr2;
131
179
  if ( m ) {
132
180
  while (n--) {
133
- if ( ! *m++ ) { *p2 = op_powi_float64_t(*p1, ipow); }
181
+ if ( ! *m++ ) { *p2 = ca_ipow_float64_t(*p1, ipow); }
134
182
  p1++; p2++;
135
183
  }
136
184
  }
137
185
  else
138
- while (n--) { (*p2) = op_powi_float64_t(*p1, ipow); p1++; p2++; }
186
+ while (n--) { (*p2) = ca_ipow_float64_t(*p1, ipow); p1++; p2++; }
139
187
  }
140
188
 
141
189
  static void
@@ -145,12 +193,12 @@ ca_ipower_cmplx64_t (ca_size_t n, boolean8_t *m,
145
193
  ca_u_cmplx64_t *p1 = (ca_u_cmplx64_t *) ptr1, *p2 = (ca_u_cmplx64_t *) ptr2;
146
194
  if ( m ) {
147
195
  while (n--) {
148
- if ( ! *m++ ) { *p2 = op_powi_cmplx64_t(*p1, ipow); }
196
+ if ( ! *m++ ) { *p2 = ca_ipow_cmplx64_t(*p1, ipow); }
149
197
  p1++; p2++;
150
198
  }
151
199
  }
152
200
  else
153
- while (n--) { (*p2) = op_powi_cmplx64_t(*p1, ipow); p1++; p2++; }
201
+ while (n--) { (*p2) = ca_ipow_cmplx64_t(*p1, ipow); p1++; p2++; }
154
202
  }
155
203
 
156
204
  static void
@@ -160,16 +208,16 @@ ca_ipower_cmplx128_t (ca_size_t n, boolean8_t *m,
160
208
  ca_u_cmplx128_t *p1 = (ca_u_cmplx128_t *) ptr1, *p2 = (ca_u_cmplx128_t *) ptr2;
161
209
  if ( m ) {
162
210
  while (n--) {
163
- if ( ! *m++ ) { *p2 = op_powi_cmplx128_t(*p1, ipow); }
211
+ if ( ! *m++ ) { *p2 = ca_ipow_cmplx128_t(*p1, ipow); }
164
212
  p1++; p2++;
165
213
  }
166
214
  }
167
215
  else
168
- while (n--) { (*p2) = op_powi_cmplx128_t(*p1, ipow); p1++; p2++; }
216
+ while (n--) { (*p2) = ca_ipow_cmplx128_t(*p1, ipow); p1++; p2++; }
169
217
  }
170
218
 
171
- /* The data types the ipower kernels cover; checked before any window
172
- opens, so the switches inside cannot fall through to a raise. */
219
+ /* The data types the ipower kernels cover; checked before anything is
220
+ attached, so the switches inside cannot fall through to a raise. */
173
221
  static int
174
222
  ca_ipower_type_ok (CArray *ca)
175
223
  {
@@ -191,7 +239,7 @@ ca_ipower_type_ok (CArray *ca)
191
239
  * mask attach). Called by rb_ca_pow when the fast-path predicate
192
240
  * matches. */
193
241
  static VALUE
194
- rb_ca_ipower (VALUE self, VALUE other)
242
+ rb_ca_ipower (volatile VALUE self, VALUE other)
195
243
  {
196
244
  volatile VALUE obj;
197
245
  CArray *ca, *co;
@@ -200,7 +248,7 @@ rb_ca_ipower (VALUE self, VALUE other)
200
248
 
201
249
  ipow = NUM2LL(other);
202
250
 
203
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
251
+ GetCArray(self, ca);
204
252
 
205
253
  if ( ! ca_ipower_type_ok(ca) ) {
206
254
  rb_raise(rb_eCADataTypeError, "invalid data type for ipower");
@@ -213,7 +261,7 @@ rb_ca_ipower (VALUE self, VALUE other)
213
261
  m = ( co->mask ) ? (boolean8_t *)co->mask->ptr : NULL;
214
262
 
215
263
  if ( ca_attach_is_alias(ca) ) {
216
- ca_attach(ca); /* window: nothing raises inside */
264
+ ca_attach(ca); /* attach: nothing raises before the detach */
217
265
  switch ( ca->data_type ) {
218
266
  case CA_FLOAT32:
219
267
  ca_ipower_float32_t(ca->elements, m, ca->ptr, ipow, co->ptr); break;
@@ -249,21 +297,23 @@ rb_ca_ipower (VALUE self, VALUE other)
249
297
  }
250
298
 
251
299
  static VALUE
252
- rb_ca_ipower_bang (VALUE self, VALUE other)
300
+ rb_ca_ipower_bang (volatile VALUE self, VALUE other)
253
301
  {
254
302
  CArray *ca;
255
303
  int64_t ipow;
256
304
  boolean8_t *m;
257
305
 
306
+ rb_ca_modify(self);
307
+
258
308
  ipow = NUM2LL(other);
259
309
 
260
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
310
+ GetCArray(self, ca);
261
311
 
262
312
  if ( ! ca_ipower_type_ok(ca) ) {
263
313
  rb_raise(rb_eRuntimeError, "invalid data type for ipower");
264
314
  }
265
315
 
266
- ca_attach(ca); /* window: nothing raises inside until ca_sync_detach */
316
+ ca_attach(ca); /* attach: nothing raises before ca_sync_detach */
267
317
 
268
318
  m = ( ca->mask ) ? (boolean8_t *)ca->mask->ptr : NULL;
269
319
 
@@ -283,16 +333,33 @@ rb_ca_ipower_bang (VALUE self, VALUE other)
283
333
  return self;
284
334
  }
285
335
 
336
+ /* An Integer exponent beyond int64 is taken as a Float: the power is then
337
+ * 0, 1, an infinity or NaN, as for the same exponent written as a Float.
338
+ * Within int64 it stays an Integer for the ipower kernels. */
339
+ static VALUE
340
+ ca_pow_wide_exponent (VALUE other)
341
+ {
342
+ if ( RB_TYPE_P(other, T_BIGNUM) &&
343
+ ( RTEST(rb_funcall(other, rb_intern(">"), 1, LL2NUM(INT64_MAX))) ||
344
+ RTEST(rb_funcall(other, rb_intern("<"), 1, LL2NUM(INT64_MIN))) ) ) {
345
+ return DBL2NUM(rb_big2dbl(other));
346
+ }
347
+ return other;
348
+ }
349
+
286
350
  /* CArray#pow(other) (alias `**`) — Float/Complex ** Integer takes the
287
351
  * ipower fast path in this file; everything else falls through to the
288
352
  * mkkernel-generated general pow/cpow (rb_ca_power in
289
353
  * ext/carray_kernels.c). */
290
- static VALUE rb_ca_pow (VALUE self, VALUE other)
354
+ static VALUE rb_ca_pow (volatile VALUE self, VALUE other)
291
355
  {
292
356
  volatile VALUE obj;
293
357
  CArray *ca;
294
358
 
295
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
359
+ GetCArray(self, ca);
360
+ if ( ca_is_float_type(ca) || ca_is_complex_type(ca) ) {
361
+ other = ca_pow_wide_exponent(other);
362
+ }
296
363
  if ( ( ca_is_float_type(ca) || ca_is_complex_type(ca) ) &&
297
364
  rb_obj_is_kind_of(other, rb_cInteger) ) {
298
365
  return rb_ca_ipower(self, other);
@@ -306,10 +373,13 @@ static VALUE rb_ca_pow (VALUE self, VALUE other)
306
373
  /* CArray#pow!(other) — in-place variant of #pow. Dispatch mirrors
307
374
  * rb_ca_pow (fast path for Float/Complex ** Integer, otherwise
308
375
  * general rb_ca_power_bang). */
309
- static VALUE rb_ca_pow_bang (VALUE self, VALUE other)
376
+ static VALUE rb_ca_pow_bang (volatile VALUE self, VALUE other)
310
377
  {
311
378
  CArray *ca;
312
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
379
+ GetCArray(self, ca);
380
+ if ( ca_is_float_type(ca) || ca_is_complex_type(ca) ) {
381
+ other = ca_pow_wide_exponent(other);
382
+ }
313
383
  if ( ( ca_is_float_type(ca) || ca_is_complex_type(ca) ) &&
314
384
  rb_obj_is_kind_of(other, rb_cInteger) ) {
315
385
  return rb_ca_ipower_bang(self, other);
data/ext/ca_op_powi.h CHANGED
@@ -1,19 +1,15 @@
1
1
  /* ---------------------------------------------------------------------------
2
2
 
3
- ca_op_powi.h -- shared `op_powi_<type>` integer-power helpers
3
+ ca_op_powi.h -- `op_powi_<type>`, an integer to an integer power
4
4
 
5
- Used by:
6
- - ext/carray_kernels.c (generated from ext/mkkernel.rb)
7
- the `power` binop kernel's integer variants call
8
- `op_powi_<type>` to compute integer exponentiation by binary
9
- exponentiation (O(log p) vs pow's general path).
10
- - ext/carray_math.c (hand-written, P.5b.5)
11
- the `ipower` family (rb_ca_ipower, rb_ca_ipower_bang) uses
12
- `op_powi_<type>` for the Float**Integer / Complex**Integer
13
- fast path when invoked via CArray#pow with an Integer rhs.
5
+ This header is internal to carray and carries no contract. It is not
6
+ installed; it ships in the gem's ext/ directory only because carray is
7
+ built from it there.
14
8
 
15
- Both files include this header independently; each TU gets its own
16
- `static inline` copy with no linker conflicts.
9
+ The `power` binop kernel's integer variants (generated from
10
+ ext/mkkernel.rb) call `op_powi_<type>`, binary exponentiation in the
11
+ integer type. A float or complex base goes through pow / cpow
12
+ instead (ext/ca_op_ipower.c for an Integer exponent).
17
13
 
18
14
  --------------------------------------------------------------------------- */
19
15
 
@@ -27,7 +23,7 @@
27
23
  an evaluator that compiles a kernel body calling op_powi_<type>
28
24
  (CArray.__kernel_helpers__, written into the build by mkkernel.rb). It
29
25
  may use only <stdint.h>, float32_t / float64_t and ca_zerodiv: what such
30
- an evaluator's C declares. The complex instantiations stay outside. */
26
+ an evaluator's C declares. */
31
27
 
32
28
  /* BEGIN kernel helpers */
33
29
 
@@ -85,17 +81,6 @@ op_powi_## type (type x, int64_t p) \
85
81
  return op_powi_mag_## type(x, (uint64_t) p); \
86
82
  }
87
83
 
88
- #define op_powi_fc(type) \
89
- op_powi_magnitude(type) \
90
- static inline type \
91
- op_powi_## type (type x, int64_t p) \
92
- { \
93
- if ( p < 0 ) { \
94
- return 1 / op_powi_mag_## type(x, (uint64_t) 0 - (uint64_t) p); \
95
- } \
96
- return op_powi_mag_## type(x, (uint64_t) p); \
97
- }
98
-
99
84
  op_powi(int8_t)
100
85
  op_powi_u(uint8_t)
101
86
  op_powi(int16_t)
@@ -104,12 +89,8 @@ op_powi(int32_t)
104
89
  op_powi_u(uint32_t)
105
90
  op_powi(int64_t)
106
91
  op_powi_u(uint64_t)
107
- op_powi_fc(float32_t)
108
- op_powi_fc(float64_t)
109
92
 
110
93
  /* END kernel helpers */
111
94
 
112
- op_powi_fc(cmplx64_t)
113
- op_powi_fc(cmplx128_t)
114
95
 
115
96
  #endif /* CA_OP_POWI_H */
data/ext/ca_pow5_table.h CHANGED
@@ -1,4 +1,5 @@
1
- /* Generated by mk_pow5_table.rb -- do not edit. */
1
+ /* Generated by mk_pow5_table.rb -- do not edit. Internal to carray and
2
+ not installed. */
2
3
  #define CA_POW5_Q_MIN (-342)
3
4
  #define CA_POW5_Q_MAX 308
4
5
  static const uint64_t ca_pow5_table[1302] = {
data/ext/ca_rng_normal.h CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  ca_rng_normal.h -- uniform to standard normal, as text
4
4
 
5
+ As a header this file is internal to carray and carries no contract; it
6
+ is not installed. Its text is handed out through
7
+ CArray::Rng::COMMON_SOURCE.
8
+
5
9
  Read the two ways ca_rng_xoshiro256pp.h is read: compiled into this
6
10
  extension, and handed out through CArray::Rng::COMMON_SOURCE for
7
11
  another gem to paste. The same rules apply -- no include guard,
@@ -2,6 +2,9 @@
2
2
 
3
3
  ca_rng_xoshiro256pp.h -- the xoshiro256++ generator, as text
4
4
 
5
+ As a header this file is internal to carray and carries no contract; it
6
+ is not installed. Its text is handed out through CArray::Rng::SOURCE.
7
+
5
8
  This file is not a header in the usual sense. It carries no include
6
9
  guard and includes nothing, because it is read two ways:
7
10
 
@@ -2,38 +2,32 @@
2
2
 
3
3
  ca_sort_kernels.h
4
4
 
5
- Portable textbook sort kernels (PROPOSAL_PORTABLE_TEXTBOOK_SORT).
5
+ This header is internal to carray and carries no contract. It is not
6
+ installed, and carray.h does not include it: the sources that use it
7
+ (carray_sort_kernel.c, carray_sort.c, carray_partition.c,
8
+ ca_categorical_iterator.c and the generated carray_kernels_*.c)
9
+ include it themselves.
6
10
 
7
- P.1 / P.2 : quicksort + mergesort over the 10 numeric data types
8
- (i8 / u8 / i16 / u16 / i32 / u32 / i64 / u64 / f32 / f64)
9
- P.3 : NaN pre-partition for f32 / f64
10
- P.4 : pair (value + index) variants for argsort kernels —
11
- sort_index / sort_addr / partition_index family
12
- P.9 : value-level quickselect for partition_copy
11
+ Portable sort kernels:
13
12
 
14
- Status: ext-internal. Signatures may change across 3.x while the
15
- textbook sort family evolves. Included from carray.h so internal
16
- consumers (carray_sort_kernel.c, carray_order.c, carray_kernels.c,
17
- mkkernel emit) reach it transparently via `#include "carray.h"`.
18
-
19
- Depends on int8_t .. uint64_t / float32_t / double / ca_size_t
20
- typedefs from carray.h — when this header is included from carray.h
21
- mid-file, those typedefs are already in scope by the time we reach
22
- the include site.
13
+ - quicksort and mergesort over the 10 numeric data types
14
+ (i8 / u8 / i16 / u16 / i32 / u32 / i64 / u64 / f32 / f64)
15
+ - NaN pre-partition for f32 / f64
16
+ - pair (value + index) variants for the sort_index / sort_addr /
17
+ partition_index family
18
+ - value-level quickselect for partition_copy
23
19
 
24
20
  --------------------------------------------------------------------------- */
25
21
 
26
22
  #ifndef CA_SORT_KERNELS_H
27
23
  #define CA_SORT_KERNELS_H 1
28
24
 
29
- /* Needs int8_t .. uint64_t / float32_t / double / ca_size_t. No longer
30
- pulled mid-carray.h (PROPOSAL_CARRAY_H_REORG H.4.1), so include carray.h
31
- directly to be self-sufficient when a consumer includes this header.
32
- Guard-protected, so the re-entry is a no-op when carray.h is already in
33
- flight. */
25
+ /* Needs int8_t .. uint64_t / float32_t / double / ca_size_t, so this
26
+ header includes carray.h itself. carray.h has an include guard, so the
27
+ include is a no-op when a source has already included it. */
34
28
  #include "carray.h"
35
29
 
36
- /* P.1 / P.2: quicksort over 10 numeric data types. */
30
+ /* Quicksort over 10 numeric data types. */
37
31
  void ca_sort_quick_i8 (int8_t *a, ca_size_t n);
38
32
  void ca_sort_quick_u8 (uint8_t *a, ca_size_t n);
39
33
  void ca_sort_quick_i16 (int16_t *a, ca_size_t n);
@@ -45,7 +39,7 @@ void ca_sort_quick_u64 (uint64_t *a, ca_size_t n);
45
39
  void ca_sort_quick_f32 (float32_t *a, ca_size_t n);
46
40
  void ca_sort_quick_f64 (double *a, ca_size_t n);
47
41
 
48
- /* P.1 / P.2: mergesort over 10 numeric data types (`aux` is caller-supplied
42
+ /* Mergesort over 10 numeric data types (`aux` is caller-supplied
49
43
  scratch buffer of the same length / data type as `a`). */
50
44
  void ca_sort_merge_i8 (int8_t *a, int8_t *aux, ca_size_t n);
51
45
  void ca_sort_merge_u8 (uint8_t *a, uint8_t *aux, ca_size_t n);
@@ -58,9 +52,12 @@ void ca_sort_merge_u64 (uint64_t *a, uint64_t *aux, ca_size_t n);
58
52
  void ca_sort_merge_f32 (float32_t *a, float32_t *aux, ca_size_t n);
59
53
  void ca_sort_merge_f64 (double *a, double *aux, ca_size_t n);
60
54
 
61
- /* P.3: NaN pre-partition for float data types (Hoare 1-pass, returns finite count). */
55
+ /* NaN pre-partition for float data types (Hoare 1-pass, returns finite count). */
62
56
  ca_size_t ca_partition_nan_f32 (float32_t *a, ca_size_t n);
63
57
  ca_size_t ca_partition_nan_f64 (double *a, ca_size_t n);
58
+ /* The same, keeping the order within both regions; scratch holds n values. */
59
+ ca_size_t ca_partition_nan_stable_f32 (float32_t *a, ca_size_t n, float32_t *scratch);
60
+ ca_size_t ca_partition_nan_stable_f64 (double *a, ca_size_t n, double *scratch);
64
61
 
65
62
  /* Pair variant of NaN pre-partition for float argsort kernels. scratch
66
63
  holds n pairs; its contents on return are unspecified. */
@@ -71,7 +68,7 @@ ca_size_t ca_partition_nan_pair_f32 (struct ca_pair_f32 *a, ca_size_t n,
71
68
  ca_size_t ca_partition_nan_pair_f64 (struct ca_pair_f64 *a, ca_size_t n,
72
69
  struct ca_pair_f64 *scratch);
73
70
 
74
- /* P.9: value-level quickselect for partition_copy.
71
+ /* Value-level quickselect for partition_copy.
75
72
  Reorders a[0..n) so that a[kth] is exact + left/right regions
76
73
  satisfy <= / >= contracts. Order within each region unspecified. */
77
74
  void ca_partition_quick_i8 (int8_t *a, ca_size_t n, ca_size_t kth);
@@ -85,7 +82,7 @@ void ca_partition_quick_u64 (uint64_t *a, ca_size_t n, ca_size_t kth);
85
82
  void ca_partition_quick_f32 (float32_t *a, ca_size_t n, ca_size_t kth);
86
83
  void ca_partition_quick_f64 (double *a, ca_size_t n, ca_size_t kth);
87
84
 
88
- /* P.4: pair sort kernels (= argsort: sort_index / sort_addr). The pair
85
+ /* Pair sort kernels (sort_index / sort_addr). The pair
89
86
  struct is opaque to mkkernel-emitted callers via void*; each kernel
90
87
  has its own well-defined layout: { TYPE v; ca_size_t i; } with natural
91
88
  alignment padding. */