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.
- checksums.yaml +4 -4
- data/.yardopts +1 -1
- data/CHANGELOG.md +7 -1830
- data/README.md +62 -35
- data/carray.gemspec +16 -4
- data/docs/changelogs/CHANGELOG-3.0.md +2464 -0
- data/ext/ca_array_pool.c +4 -5
- data/ext/ca_axis_descriptor.h +45 -30
- data/ext/ca_axis_dispatch.c +7 -4
- data/ext/ca_axis_group.c +12 -15
- data/ext/ca_bincmp_dispatch.h +16 -38
- data/ext/ca_binop_dispatch.c +35 -0
- data/ext/ca_binop_dispatch.h +22 -46
- data/ext/ca_categorical_iterator.c +36 -34
- data/ext/ca_compare.c +0 -1
- data/ext/ca_compare.h +8 -1
- data/ext/ca_composite_dispatch.h +6 -0
- data/ext/ca_for_buffer.h +6 -1
- data/ext/ca_group_iter.c +17 -18
- data/ext/ca_iter_state_fields.h +196 -0
- data/ext/ca_iter_substrate.h +84 -103
- data/ext/ca_kernel_iterator.c +125 -89
- data/ext/ca_kernel_iterator.h +229 -1504
- data/ext/ca_kernel_iterator_internal.h +1167 -0
- data/ext/ca_moncmp_dispatch.h +17 -24
- data/ext/ca_monop_dispatch.h +25 -74
- data/ext/ca_obj_array.c +92 -165
- data/ext/ca_obj_bincmp.c +65 -48
- data/ext/ca_obj_binop.c +74 -49
- data/ext/ca_obj_bitarray.c +7 -21
- data/ext/ca_obj_bitfield.c +93 -26
- data/ext/ca_obj_block.c +11 -12
- data/ext/ca_obj_byte_swap.c +34 -34
- data/ext/ca_obj_const_string.c +464 -304
- data/ext/ca_obj_face.c +167 -121
- data/ext/ca_obj_face.h +32 -170
- data/ext/ca_obj_fake.c +127 -34
- data/ext/ca_obj_farray.c +13 -9
- data/ext/ca_obj_field.c +17 -13
- data/ext/ca_obj_fixlen_string.c +57 -34
- data/ext/ca_obj_grid.c +149 -113
- data/ext/ca_obj_meld.c +28 -26
- data/ext/ca_obj_moncmp.c +10 -21
- data/ext/ca_obj_monop.c +88 -73
- data/ext/ca_obj_object.c +60 -46
- data/ext/ca_obj_record.c +74 -22
- data/ext/ca_obj_reduce.c +10 -14
- data/ext/ca_obj_refer.c +146 -39
- data/ext/ca_obj_remap.c +11 -29
- data/ext/ca_obj_repeat.c +41 -23
- data/ext/ca_obj_roll.c +20 -28
- data/ext/ca_obj_select.c +17 -27
- data/ext/ca_obj_select_axis.c +35 -42
- data/ext/ca_obj_shift.c +22 -42
- data/ext/ca_obj_source.c +1 -5
- data/ext/ca_obj_stack.c +33 -29
- data/ext/ca_obj_stride.c +99 -62
- data/ext/ca_obj_string.c +8 -20
- data/ext/ca_obj_tile.c +14 -16
- data/ext/ca_obj_time.c +12 -23
- data/ext/ca_obj_timedelta.c +12 -23
- data/ext/ca_obj_transpose.c +8 -10
- data/ext/ca_obj_triop.c +48 -49
- data/ext/ca_obj_window.c +87 -99
- data/ext/ca_op_cmplx64.h +4 -0
- data/ext/ca_op_ipower.c +97 -27
- data/ext/ca_op_powi.h +9 -28
- data/ext/ca_pow5_table.h +2 -1
- data/ext/ca_rng_normal.h +4 -0
- data/ext/ca_rng_xoshiro256pp.h +3 -0
- data/ext/ca_sort_kernels.h +23 -26
- data/ext/ca_sweep_engine.c +92 -26
- data/ext/ca_sweep_engine.h +22 -15
- data/ext/ca_transform_common.c +3 -8
- data/ext/ca_triop_dispatch.h +20 -32
- data/ext/caframe_csv_reader.c +12 -12
- data/ext/carray.h +300 -1009
- data/ext/carray_access.c +320 -207
- data/ext/carray_address_basis.c +128 -59
- data/ext/carray_attribute.c +81 -104
- data/ext/carray_bincount.c +12 -14
- data/ext/carray_broadcast.c +31 -27
- data/ext/carray_call_cfunc.c +174 -285
- data/ext/carray_call_cfunc.h +36 -28
- data/ext/carray_cast.c +1191 -351
- data/ext/carray_cast_func.rb +68 -8
- data/ext/carray_class.c +6 -11
- data/ext/carray_conversion.c +98 -39
- data/ext/carray_copy.c +52 -26
- data/ext/carray_core.c +286 -132
- data/ext/carray_count.c +53 -22
- data/ext/carray_element.c +49 -28
- data/ext/carray_factorize.c +64 -63
- data/ext/carray_generate.c +26 -19
- data/ext/carray_histogram.c +9 -11
- data/ext/carray_hold.c +7 -9
- data/ext/carray_index_classifier.c +127 -230
- data/ext/carray_index_classifier.h +11 -10
- data/ext/carray_integer_total.c +523 -0
- data/ext/carray_internal.h +1364 -156
- data/ext/carray_key_windows.c +3 -3
- data/ext/carray_lazy.c +37 -37
- data/ext/carray_loop.c +27 -23
- data/ext/carray_mask.c +235 -150
- data/ext/carray_math_kernel.h +23 -47
- data/ext/carray_mathfunc.c +10 -3
- data/ext/carray_median_percentile.c +106 -100
- data/ext/carray_memory_view.c +83 -69
- data/ext/carray_operator.c +490 -128
- data/ext/carray_order.c +163 -84
- data/ext/carray_partition.c +14 -16
- data/ext/carray_random.c +48 -42
- data/ext/carray_round.c +33 -5
- data/ext/carray_scatter.c +57 -39
- data/ext/carray_segment.c +42 -46
- data/ext/carray_slab.c +77 -51
- data/ext/carray_slab.h +44 -33
- data/ext/carray_sort.c +37 -31
- data/ext/carray_sort_kernel.c +36 -10
- data/ext/carray_struct.c +44 -34
- data/ext/carray_test.c +131 -81
- data/ext/carray_time_text.c +30 -31
- data/ext/carray_undef.c +50 -7
- data/ext/carray_utils.c +373 -139
- data/ext/extconf.rb +24 -17
- data/ext/mk_call_cfunc.rb +25 -31
- data/ext/mk_pow5_table.rb +2 -1
- data/ext/mkkernel.rb +819 -488
- data/ext/ruby_carray.c +47 -42
- data/ext/version.h +7 -5
- data/lib/carray/attribute.rb +19 -18
- data/lib/carray/autoload_carray.rb +20 -15
- data/lib/carray/autoload_method_extension.rb +64 -57
- data/lib/carray/axis_group.rb +272 -185
- data/lib/carray/basics.rb +298 -56
- data/lib/carray/bincount_nd.rb +41 -18
- data/lib/carray/block_iterator.rb +167 -119
- data/lib/carray/boolean_reduce.rb +7 -4
- data/lib/carray/categorical.rb +84 -59
- data/lib/carray/categorical_iterator.rb +90 -67
- data/lib/carray/complex.rb +61 -41
- data/lib/carray/conditional.rb +98 -23
- data/lib/carray/const_string.rb +112 -46
- data/lib/carray/core_extensions.rb +20 -69
- data/lib/carray/distance_limit.rb +7 -5
- data/lib/carray/factories.rb +269 -0
- data/lib/carray/fixlen_string.rb +95 -12
- data/lib/carray/frame/column_verbs.rb +76 -9
- data/lib/carray/frame/concat.rb +51 -7
- data/lib/carray/frame/convert.rb +2 -1
- data/lib/carray/frame/csv_parser.rb +20 -5
- data/lib/carray/frame/frame.rb +73 -31
- data/lib/carray/frame/group.rb +247 -187
- data/lib/carray/frame/io.rb +36 -12
- data/lib/carray/frame/join.rb +56 -17
- data/lib/carray/frame/records.rb +7 -5
- data/lib/carray/frame/reshape.rb +27 -7
- data/lib/carray/frame/sort.rb +10 -8
- data/lib/carray/frame/verbs.rb +217 -134
- data/lib/carray/frame/windows.rb +254 -19
- data/lib/carray/frame.rb +1 -2
- data/lib/carray/fuse_source.rb +28 -23
- data/lib/carray/fusion.rb +30 -30
- data/lib/carray/histogram.rb +35 -16
- data/lib/carray/inspect.rb +7 -7
- data/lib/carray/iterator.rb +104 -23
- data/lib/carray/key_windows.rb +93 -61
- data/lib/carray/lazy.rb +208 -111
- data/lib/carray/limits.rb +80 -0
- data/lib/carray/mask_gap_fill.rb +21 -16
- data/lib/carray/math.rb +87 -50
- data/lib/carray/meld_reduce.rb +30 -8
- data/lib/carray/methods/align_addr.rb +29 -17
- data/lib/carray/methods/bin.rb +8 -1
- data/lib/carray/methods/bincount.rb +4 -2
- data/lib/carray/methods/bit_string.rb +19 -31
- data/lib/carray/methods/broadcast.rb +8 -3
- data/lib/carray/methods/choose.rb +5 -5
- data/lib/carray/methods/composition.rb +73 -72
- data/lib/carray/methods/{index.rb → coordinate.rb} +13 -13
- data/lib/carray/methods/discovery_along.rb +3 -3
- data/lib/carray/methods/factorize.rb +2 -0
- data/lib/carray/methods/gather_nd.rb +106 -103
- data/lib/carray/methods/insert_block.rb +2 -2
- data/lib/carray/methods/is_between.rb +6 -4
- data/lib/carray/methods/is_in.rb +183 -143
- data/lib/carray/methods/join.rb +2 -1
- data/lib/carray/methods/lag.rb +6 -2
- data/lib/carray/methods/locate_addr.rb +157 -94
- data/lib/carray/methods/mask_duplicates.rb +1 -1
- data/lib/carray/methods/meshgrid.rb +28 -16
- data/lib/carray/methods/mode.rb +67 -67
- data/lib/carray/methods/nunique.rb +9 -7
- data/lib/carray/methods/repeat.rb +23 -13
- data/lib/carray/methods/resize.rb +1 -1
- data/lib/carray/methods/snap.rb +24 -20
- data/lib/carray/methods/string_format.rb +2 -2
- data/lib/carray/methods/unique.rb +4 -2
- data/lib/carray/methods/value_counts.rb +1 -1
- data/lib/carray/mkmf.rb +25 -28
- data/lib/carray/percentile_arguments.rb +82 -0
- data/lib/carray/record_order.rb +50 -20
- data/lib/carray/rng.rb +1 -0
- data/lib/carray/runtime.rb +195 -17
- data/lib/carray/segment_iterator.rb +262 -137
- data/lib/carray/serialize.rb +635 -566
- data/lib/carray/slab_iterator.rb +114 -114
- data/lib/carray/stack.rb +14 -15
- data/lib/carray/string.rb +16 -22
- data/lib/carray/string_operation_extension.rb +97 -52
- data/lib/carray/struct.rb +122 -164
- data/lib/carray/struct_builder.rb +247 -73
- data/lib/carray/time.rb +663 -365
- data/lib/carray/window_iterator.rb +232 -155
- data/lib/carray.rb +6 -11
- data/yard-stubs/ca_obj_array.rb +20 -135
- data/yard-stubs/ca_obj_bitarray.rb +1 -8
- data/yard-stubs/ca_obj_bitfield.rb +6 -2
- data/yard-stubs/ca_obj_byte_swap.rb +21 -7
- data/yard-stubs/ca_obj_fake.rb +3 -1
- data/yard-stubs/ca_obj_farray.rb +1 -1
- data/yard-stubs/ca_obj_field.rb +8 -6
- data/yard-stubs/ca_obj_refer.rb +10 -5
- data/yard-stubs/ca_obj_roll.rb +7 -6
- data/yard-stubs/ca_obj_shift.rb +2 -3
- data/yard-stubs/ca_obj_stride.rb +5 -7
- data/yard-stubs/ca_obj_tile.rb +1 -1
- data/yard-stubs/ca_obj_time.rb +43 -0
- data/yard-stubs/ca_obj_transpose.rb +1 -1
- data/yard-stubs/ca_obj_window.rb +28 -16
- data/yard-stubs/carray_access.rb +55 -6
- data/yard-stubs/carray_attribute.rb +26 -38
- data/yard-stubs/carray_cast.rb +121 -162
- data/yard-stubs/carray_class.rb +3 -4
- data/yard-stubs/carray_conversion.rb +6 -7
- data/yard-stubs/carray_copy.rb +2 -1
- data/yard-stubs/carray_count.rb +10 -5
- data/yard-stubs/carray_element.rb +4 -3
- data/yard-stubs/carray_limits.rb +100 -0
- data/yard-stubs/carray_loop.rb +0 -20
- data/yard-stubs/carray_mask.rb +59 -41
- data/yard-stubs/carray_math.rb +162 -100
- data/yard-stubs/carray_mathfunc.rb +415 -3
- data/yard-stubs/carray_median_percentile.rb +29 -11
- data/yard-stubs/carray_memory_view.rb +2 -2
- data/yard-stubs/carray_order.rb +72 -33
- data/yard-stubs/carray_random.rb +6 -6
- data/yard-stubs/carray_reduce.rb +353 -0
- data/yard-stubs/carray_rng.rb +92 -0
- data/yard-stubs/carray_scatter.rb +12 -3
- data/yard-stubs/carray_slab.rb +7 -4
- data/yard-stubs/carray_sort.rb +7 -6
- data/yard-stubs/carray_test.rb +18 -5
- data/yard-stubs/carray_undef.rb +44 -5
- data/yard-stubs/ruby_carray.rb +75 -40
- metadata +63 -25
- data/CHANGELOG.v1.md +0 -126
- data/ext/carray_build_flags.h +0 -3
- data/ext/carray_config.h +0 -28
- data/ext/carray_data_type.c +0 -90
- data/ext/carray_kernels_bincmp.c +0 -6457
- data/ext/carray_kernels_binop.c +0 -31534
- data/ext/carray_kernels_init.c +0 -1734
- data/ext/carray_kernels_moncmp.c +0 -2104
- data/ext/carray_kernels_monop.c +0 -25387
- data/ext/carray_kernels_reduce_aggregate.c +0 -27545
- data/ext/carray_kernels_reduce_boolean.c +0 -1924
- data/ext/carray_kernels_reduce_cumulative.c +0 -11646
- data/ext/carray_kernels_reduce_extreme.c +0 -21631
- data/ext/carray_kernels_reduce_variance.c +0 -5981
- data/ext/carray_kernels_scan.c +0 -4302
- data/ext/carray_kernels_search.c +0 -37776
- data/ext/carray_kernels_sort.c +0 -10814
- data/ext/carray_kernels_triop.c +0 -1864
- data/lib/carray/construct.rb +0 -360
- data/lib/carray/data_type_extension.rb +0 -261
- data/lib/carray/data_type_limits.rb +0 -91
|
@@ -0,0 +1,523 @@
|
|
|
1
|
+
/* ---------------------------------------------------------------------------
|
|
2
|
+
|
|
3
|
+
carray_integer_total.c
|
|
4
|
+
|
|
5
|
+
The sum, the product and the weighted sum (integer weights) of an
|
|
6
|
+
integer array.
|
|
7
|
+
|
|
8
|
+
A signed array totals in int64 and an unsigned one in uint64. Where the
|
|
9
|
+
total is out of that range, the answer is the float64 total instead: it
|
|
10
|
+
is not exact there, but it is close, where a wrapped integer would not be.
|
|
11
|
+
`accumulate` is the fold that keeps the array's own type and wraps.
|
|
12
|
+
cumsum and cumprod follow the same rule for their running values
|
|
13
|
+
(ca_integer_scan): one float64 cell out of range makes the whole answer
|
|
14
|
+
float64, as it does for an axis reduction.
|
|
15
|
+
|
|
16
|
+
- A sum over cells of 4 bytes or fewer, fewer than 2**31 of them, cannot
|
|
17
|
+
leave the range, so the integer fold alone answers, in one pass
|
|
18
|
+
(sum_wrapping).
|
|
19
|
+
- A sum over 8-byte cells, fewer than 2**31 of them, keeps the wrapping
|
|
20
|
+
sum and the sum of the cells' high 32 bits, in one pass (sum_parts),
|
|
21
|
+
which together give the exact total as its low 64 bits and the bits
|
|
22
|
+
above them. The total is in range or not, and is converted to float64
|
|
23
|
+
where it is not.
|
|
24
|
+
- A product takes the float64 fold first, which settles it where the
|
|
25
|
+
product is small, zero or far out of range (ca_prod_from_float).
|
|
26
|
+
- Anything else -- a product between 2**53 and the range's end, a sum
|
|
27
|
+
over more cells -- takes two passes: the integer fold, which wraps at
|
|
28
|
+
2**64 and so gives the true total modulo 2**64, and the float64 fold,
|
|
29
|
+
which gives the true total up to a small relative error. Where the
|
|
30
|
+
true total is in range the two agree to well within 2**62; where it is
|
|
31
|
+
not, they differ by a multiple of 2**64. That difference is the test,
|
|
32
|
+
cell by cell.
|
|
33
|
+
|
|
34
|
+
fill_value: is taken off the call and applied to the answer.
|
|
35
|
+
|
|
36
|
+
--------------------------------------------------------------------------- */
|
|
37
|
+
|
|
38
|
+
#include "carray.h"
|
|
39
|
+
#include <math.h>
|
|
40
|
+
#include <string.h>
|
|
41
|
+
|
|
42
|
+
VALUE rb_ca_sum_parts_ki (int argc, VALUE *argv, VALUE self);
|
|
43
|
+
|
|
44
|
+
#define CA_TWO_POW_62 4611686018427387904.0
|
|
45
|
+
|
|
46
|
+
static int
|
|
47
|
+
ca_is_unsigned_type (int8_t data_type)
|
|
48
|
+
{
|
|
49
|
+
return ( data_type == CA_UINT8 || data_type == CA_UINT16 ||
|
|
50
|
+
data_type == CA_UINT32 || data_type == CA_UINT64 );
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/* Whether the integer value `i` and the float64 value `f` of the same
|
|
54
|
+
total agree, i.e. the integer did not wrap. */
|
|
55
|
+
static int
|
|
56
|
+
ca_integer_total_agrees (double i, double f)
|
|
57
|
+
{
|
|
58
|
+
return isfinite(f) && fabs(i - f) < CA_TWO_POW_62;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/* Of an integer answer `iv` and a float64 answer `fv` of the same cells,
|
|
62
|
+
the integer one where every cell agrees, else the float64 one. */
|
|
63
|
+
static VALUE
|
|
64
|
+
ca_integer_total_choose (volatile VALUE iv, volatile VALUE fv, int is_unsigned)
|
|
65
|
+
{
|
|
66
|
+
CArray *ci, *cf;
|
|
67
|
+
ca_size_t n, k;
|
|
68
|
+
boolean8_t *mf;
|
|
69
|
+
|
|
70
|
+
if ( ! rb_obj_is_carray(iv) ) {
|
|
71
|
+
if ( iv == CA_UNDEF || fv == CA_UNDEF ) {
|
|
72
|
+
return iv;
|
|
73
|
+
}
|
|
74
|
+
return ca_integer_total_agrees(is_unsigned ? (double) NUM2ULL(iv)
|
|
75
|
+
: (double) NUM2LL(iv),
|
|
76
|
+
NUM2DBL(fv)) ? iv : fv;
|
|
77
|
+
}
|
|
78
|
+
/* gc: iv and fv are held by the caller's frame */
|
|
79
|
+
GetCArray(iv, ci);
|
|
80
|
+
/* gc: held by the caller's frame */
|
|
81
|
+
GetCArray(fv, cf);
|
|
82
|
+
ca_update_mask(cf);
|
|
83
|
+
mf = cf->mask ? (boolean8_t *) cf->mask->ptr : NULL;
|
|
84
|
+
n = ci->elements;
|
|
85
|
+
for (k = 0; k < n; k++) {
|
|
86
|
+
double i;
|
|
87
|
+
if ( mf && mf[k] ) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
i = is_unsigned ? (double) ((uint64_t *) ci->ptr)[k]
|
|
91
|
+
: (double) ((int64_t *) ci->ptr)[k];
|
|
92
|
+
if ( ! ca_integer_total_agrees(i, ((double *) cf->ptr)[k]) ) {
|
|
93
|
+
return fv;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return iv;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/* Whether the total top * 2**64 + low is in range: top is the sign
|
|
100
|
+
extension of low, or 0 for an unsigned array. */
|
|
101
|
+
static int
|
|
102
|
+
ca_parts_in_range (int64_t low, int64_t top, int is_unsigned)
|
|
103
|
+
{
|
|
104
|
+
return is_unsigned ? ( top == 0 ) : ( top == ( low >> 63 ) );
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/* top * 2**64 + low as a float64, rounded once where a 128-bit integer is
|
|
108
|
+
at hand. */
|
|
109
|
+
static double
|
|
110
|
+
ca_parts_to_float (int64_t low, int64_t top)
|
|
111
|
+
{
|
|
112
|
+
#ifdef __SIZEOF_INT128__
|
|
113
|
+
return (double) ( (__int128) top * ((__int128) 1 << 64) + (__int128) (uint64_t) low );
|
|
114
|
+
#else
|
|
115
|
+
return (double) top * 18446744073709551616.0 + (double) (uint64_t) low;
|
|
116
|
+
#endif
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/* The sum of 8-byte cells from the [low, top] answer of sum_parts: the low
|
|
120
|
+
array where every total is in range (it already has the answer's type
|
|
121
|
+
and mask), else a float64 one. */
|
|
122
|
+
static VALUE
|
|
123
|
+
ca_integer_total_from_parts (VALUE pair, int is_unsigned)
|
|
124
|
+
{
|
|
125
|
+
volatile VALUE vlow = RARRAY_AREF(pair, 0), vtop = RARRAY_AREF(pair, 1);
|
|
126
|
+
volatile VALUE out;
|
|
127
|
+
CArray *cl, *ct, *co;
|
|
128
|
+
ca_size_t n, k;
|
|
129
|
+
boolean8_t *ml;
|
|
130
|
+
int64_t *low, *top;
|
|
131
|
+
int in_range = 1;
|
|
132
|
+
|
|
133
|
+
if ( ! rb_obj_is_carray(vlow) ) {
|
|
134
|
+
int64_t l, t;
|
|
135
|
+
if ( vlow == CA_UNDEF ) {
|
|
136
|
+
return CA_UNDEF;
|
|
137
|
+
}
|
|
138
|
+
l = is_unsigned ? (int64_t) NUM2ULL(vlow) : NUM2LL(vlow);
|
|
139
|
+
t = is_unsigned ? (int64_t) NUM2ULL(vtop) : NUM2LL(vtop);
|
|
140
|
+
return ca_parts_in_range(l, t, is_unsigned) ? vlow
|
|
141
|
+
: rb_float_new(ca_parts_to_float(l, t));
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/* gc: vlow and vtop are held by this frame */
|
|
145
|
+
GetCArray(vlow, cl);
|
|
146
|
+
/* gc: held by this frame */
|
|
147
|
+
GetCArray(vtop, ct);
|
|
148
|
+
ca_update_mask(cl);
|
|
149
|
+
ml = cl->mask ? (boolean8_t *) cl->mask->ptr : NULL;
|
|
150
|
+
low = (int64_t *) cl->ptr;
|
|
151
|
+
top = (int64_t *) ct->ptr;
|
|
152
|
+
n = cl->elements;
|
|
153
|
+
if ( ml ) {
|
|
154
|
+
for (k = 0; k < n; k++) {
|
|
155
|
+
in_range &= ml[k] | ca_parts_in_range(low[k], top[k], is_unsigned);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
for (k = 0; k < n; k++) {
|
|
160
|
+
in_range &= ca_parts_in_range(low[k], top[k], is_unsigned);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if ( in_range ) {
|
|
164
|
+
return vlow;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
out = rb_carray_new(CA_FLOAT64, cl->ndim, cl->dim, 0, NULL);
|
|
168
|
+
/* gc: out is held by this frame */
|
|
169
|
+
GetCArray(out, co);
|
|
170
|
+
for (k = 0; k < n; k++) {
|
|
171
|
+
((double *) co->ptr)[k] = ( ml && ml[k] ) ? 0.0 : ca_parts_to_float(low[k], top[k]);
|
|
172
|
+
}
|
|
173
|
+
if ( ml ) {
|
|
174
|
+
ca_create_mask(co);
|
|
175
|
+
memcpy(co->mask->ptr, ml, (size_t) n);
|
|
176
|
+
}
|
|
177
|
+
return out;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/* A product from its float64 answer alone, where that settles it:
|
|
181
|
+
- a product of nonzero integers grows in magnitude with each factor, so
|
|
182
|
+
where the float64 answer is below 2**53 every partial product was an
|
|
183
|
+
exact integer and the answer is exact; a zero factor gives 0 exactly;
|
|
184
|
+
- where it is at or above 2**65 the true product is out of range;
|
|
185
|
+
- NaN is an infinite partial product times a zero factor: the true
|
|
186
|
+
product is 0.
|
|
187
|
+
Qundef where some cell lies between, and the integer fold is needed. */
|
|
188
|
+
#define CA_TWO_POW_53 9007199254740992.0
|
|
189
|
+
#define CA_TWO_POW_65 36893488147419103232.0
|
|
190
|
+
|
|
191
|
+
static VALUE
|
|
192
|
+
ca_prod_from_float (volatile VALUE fv, int is_unsigned)
|
|
193
|
+
{
|
|
194
|
+
volatile VALUE out;
|
|
195
|
+
CArray *cf, *co;
|
|
196
|
+
ca_size_t n, k;
|
|
197
|
+
boolean8_t *mf;
|
|
198
|
+
int big = 0;
|
|
199
|
+
|
|
200
|
+
if ( ! rb_obj_is_carray(fv) ) {
|
|
201
|
+
double f;
|
|
202
|
+
if ( fv == CA_UNDEF ) {
|
|
203
|
+
return fv;
|
|
204
|
+
}
|
|
205
|
+
f = NUM2DBL(fv);
|
|
206
|
+
if ( isnan(f) ) {
|
|
207
|
+
f = 0.0;
|
|
208
|
+
}
|
|
209
|
+
if ( fabs(f) < CA_TWO_POW_53 ) {
|
|
210
|
+
return is_unsigned ? ULL2NUM((uint64_t) f) : LL2NUM((int64_t) f);
|
|
211
|
+
}
|
|
212
|
+
return ( fabs(f) >= CA_TWO_POW_65 ) ? fv : Qundef;
|
|
213
|
+
}
|
|
214
|
+
/* gc: fv is held by the caller's frame */
|
|
215
|
+
GetCArray(fv, cf);
|
|
216
|
+
ca_update_mask(cf);
|
|
217
|
+
mf = cf->mask ? (boolean8_t *) cf->mask->ptr : NULL;
|
|
218
|
+
n = cf->elements;
|
|
219
|
+
for (k = 0; k < n; k++) { /* fv is the fold's own new array */
|
|
220
|
+
if ( isnan(((double *) cf->ptr)[k]) ) {
|
|
221
|
+
((double *) cf->ptr)[k] = 0.0;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
for (k = 0; k < n; k++) {
|
|
225
|
+
double f;
|
|
226
|
+
if ( mf && mf[k] ) {
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
f = fabs(((double *) cf->ptr)[k]);
|
|
230
|
+
if ( f >= CA_TWO_POW_65 ) {
|
|
231
|
+
big = 1;
|
|
232
|
+
}
|
|
233
|
+
else if ( f >= CA_TWO_POW_53 ) {
|
|
234
|
+
return Qundef;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
if ( big ) {
|
|
238
|
+
return fv;
|
|
239
|
+
}
|
|
240
|
+
out = rb_carray_new(is_unsigned ? CA_UINT64 : CA_INT64, cf->ndim, cf->dim, 0, NULL);
|
|
241
|
+
GetCArray(out, co);
|
|
242
|
+
for (k = 0; k < n; k++) {
|
|
243
|
+
double f = ( mf && mf[k] ) ? 0.0 : ((double *) cf->ptr)[k];
|
|
244
|
+
if ( is_unsigned ) {
|
|
245
|
+
((uint64_t *) co->ptr)[k] = (uint64_t) f;
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
((int64_t *) co->ptr)[k] = (int64_t) f;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
if ( mf ) {
|
|
252
|
+
ca_create_mask(co);
|
|
253
|
+
memcpy(co->mask->ptr, mf, (size_t) n);
|
|
254
|
+
}
|
|
255
|
+
return out;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/* The weighted sum with integer weights. Each product is taken modulo
|
|
259
|
+
2**64 by the wrapping fold, so its total is exact modulo 2**64, and the
|
|
260
|
+
float64 fold settles which multiple of 2**64 it is, as for a sum. That
|
|
261
|
+
test needs every product below 2**64 in magnitude (the float64 total is
|
|
262
|
+
then off by far less than 2**62); where a product may be larger, the
|
|
263
|
+
total is taken exactly over object (Ruby Integer) cells instead. Where
|
|
264
|
+
the largest value times the largest weight times the cells is below
|
|
265
|
+
2**63, no total can leave the range and the integer fold alone answers.
|
|
266
|
+
|
|
267
|
+
The weights, argv[0], are integer where they are an Integer, or an
|
|
268
|
+
integer or boolean array; any other weight leaves the float64 fold
|
|
269
|
+
alone to answer (Qundef). */
|
|
270
|
+
|
|
271
|
+
/* Whether `w` is an integer weight; *is_unsigned and *bytes describe it. */
|
|
272
|
+
static int
|
|
273
|
+
ca_wsum_integer_weight (volatile VALUE w, int *is_unsigned, int *bytes)
|
|
274
|
+
{
|
|
275
|
+
if ( RB_INTEGER_TYPE_P(w) ) {
|
|
276
|
+
*is_unsigned = ! RTEST(rb_funcall(w, rb_intern("negative?"), 0));
|
|
277
|
+
*bytes = 8;
|
|
278
|
+
return 1;
|
|
279
|
+
}
|
|
280
|
+
if ( rb_obj_is_carray(w) ) {
|
|
281
|
+
CArray *cw;
|
|
282
|
+
GetCArray(w, cw);
|
|
283
|
+
if ( ca_is_face(cw) ) {
|
|
284
|
+
return 0;
|
|
285
|
+
}
|
|
286
|
+
if ( cw->data_type == CA_BOOLEAN ) {
|
|
287
|
+
*is_unsigned = 1;
|
|
288
|
+
*bytes = 1;
|
|
289
|
+
return 1;
|
|
290
|
+
}
|
|
291
|
+
if ( ca_is_integer_type(cw) ) {
|
|
292
|
+
*is_unsigned = ca_is_unsigned_type(cw->data_type);
|
|
293
|
+
*bytes = (int) cw->bytes;
|
|
294
|
+
return 1;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
return 0;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/* The largest magnitude of the present cells of `v` (an Integer or an
|
|
301
|
+
integer or boolean array), as a Ruby Integer; 0 where no cell is
|
|
302
|
+
present. */
|
|
303
|
+
static VALUE
|
|
304
|
+
ca_wsum_magnitude (VALUE v)
|
|
305
|
+
{
|
|
306
|
+
ID id_abs = rb_intern("abs");
|
|
307
|
+
volatile VALUE mm, lo, hi;
|
|
308
|
+
if ( RB_INTEGER_TYPE_P(v) ) {
|
|
309
|
+
return rb_funcall(v, id_abs, 0);
|
|
310
|
+
}
|
|
311
|
+
mm = rb_funcall(v, rb_intern("minmax"), 0);
|
|
312
|
+
lo = RARRAY_AREF(mm, 0);
|
|
313
|
+
hi = RARRAY_AREF(mm, 1);
|
|
314
|
+
if ( lo == CA_UNDEF || hi == CA_UNDEF ) {
|
|
315
|
+
return INT2FIX(0);
|
|
316
|
+
}
|
|
317
|
+
lo = rb_funcall(lo, id_abs, 0);
|
|
318
|
+
hi = rb_funcall(hi, id_abs, 0);
|
|
319
|
+
return RTEST(rb_funcall(lo, '>', 1, hi)) ? lo : hi;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/* An object-cell total (an Integer or an object array, or UNDEF) as the
|
|
323
|
+
answer: int64 / uint64 where every value is in range, else float64. */
|
|
324
|
+
static VALUE
|
|
325
|
+
ca_wsum_from_objects (volatile VALUE res, int8_t type)
|
|
326
|
+
{
|
|
327
|
+
CArray *cr;
|
|
328
|
+
ca_size_t k;
|
|
329
|
+
boolean8_t *m;
|
|
330
|
+
int in_range = 1;
|
|
331
|
+
|
|
332
|
+
if ( ! rb_obj_is_carray(res) ) {
|
|
333
|
+
if ( res == CA_UNDEF ) {
|
|
334
|
+
return res;
|
|
335
|
+
}
|
|
336
|
+
return ( ca_integer_range_side(res, type) == 0 )
|
|
337
|
+
? res : rb_funcall(res, rb_intern("to_f"), 0);
|
|
338
|
+
}
|
|
339
|
+
/* gc: res is held by the caller's frame */
|
|
340
|
+
GetCArray(res, cr);
|
|
341
|
+
ca_update_mask(cr);
|
|
342
|
+
m = cr->mask ? (boolean8_t *) cr->mask->ptr : NULL;
|
|
343
|
+
for (k = 0; k < cr->elements && in_range; k++) {
|
|
344
|
+
if ( m && m[k] ) {
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
in_range = ( ca_integer_range_side(((VALUE *) cr->ptr)[k], type) == 0 );
|
|
348
|
+
}
|
|
349
|
+
/* coerce: a fixed type, the total's own (or float64 out of its range) */
|
|
350
|
+
return rb_ca_to_type(res, rb_ca_data_type_to_sym(in_range ? type : CA_FLOAT64), Qnil);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
static VALUE
|
|
354
|
+
ca_wsum_integer (int argc, VALUE *argv, VALUE self, CArray *src,
|
|
355
|
+
VALUE (*general) (int, VALUE *, VALUE),
|
|
356
|
+
VALUE (*integer_fold) (int, VALUE *, VALUE))
|
|
357
|
+
{
|
|
358
|
+
volatile VALUE w, receiver = self, iv, fv;
|
|
359
|
+
int w_unsigned, w_bytes, is_unsigned, exact = 0, fold_only = 0;
|
|
360
|
+
|
|
361
|
+
if ( argc < 1 ) {
|
|
362
|
+
return Qundef;
|
|
363
|
+
}
|
|
364
|
+
w = argv[0];
|
|
365
|
+
if ( ! ca_wsum_integer_weight(w, &w_unsigned, &w_bytes) ) {
|
|
366
|
+
return Qundef;
|
|
367
|
+
}
|
|
368
|
+
is_unsigned = ca_is_unsigned_type(src->data_type) && w_unsigned;
|
|
369
|
+
|
|
370
|
+
/* Products of 2-byte cells are below 2**32, and fewer than 2**31 of them
|
|
371
|
+
cannot leave the range: the integer fold alone answers. */
|
|
372
|
+
if ( src->bytes <= 2 && w_bytes <= 2 && src->elements < ( (ca_size_t) 1 << 31 ) ) {
|
|
373
|
+
fold_only = 1;
|
|
374
|
+
}
|
|
375
|
+
else if ( RB_INTEGER_TYPE_P(w) && ca_integer_range_side(w, CA_INT64) != 0 ) {
|
|
376
|
+
exact = 1; /* the fold reads its weights as int64 */
|
|
377
|
+
}
|
|
378
|
+
else {
|
|
379
|
+
/* bound = the largest product in magnitude. Where the cells times
|
|
380
|
+
bound stay below 2**63, no total can leave the range and the
|
|
381
|
+
integer fold alone answers; where bound passes 2**64, the float64
|
|
382
|
+
test cannot be trusted. */
|
|
383
|
+
volatile VALUE bound = rb_funcall(ca_wsum_magnitude(self), '*', 1,
|
|
384
|
+
ca_wsum_magnitude(w));
|
|
385
|
+
volatile VALUE limit = rb_funcall(INT2FIX(2), rb_intern("**"), 1, INT2FIX(63));
|
|
386
|
+
volatile VALUE total = rb_funcall(bound, '*', 1, SIZE2NUM(src->elements));
|
|
387
|
+
if ( RTEST(rb_funcall(total, '<', 1, limit)) ) {
|
|
388
|
+
fold_only = 1;
|
|
389
|
+
}
|
|
390
|
+
else {
|
|
391
|
+
exact = RTEST(rb_funcall(bound, '>', 1, rb_funcall(limit, '*', 1, INT2FIX(2))));
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if ( exact ) {
|
|
396
|
+
volatile VALUE args = rb_ary_new_from_values(argc, argv), res;
|
|
397
|
+
VALUE sym_object = ID2SYM(rb_intern("object"));
|
|
398
|
+
if ( rb_obj_is_carray(w) ) {
|
|
399
|
+
rb_ary_store(args, 0, rb_ca_to_type(w, sym_object, Qnil));
|
|
400
|
+
}
|
|
401
|
+
res = rb_funcallv(rb_ca_to_type(self, sym_object, Qnil),
|
|
402
|
+
rb_intern("wsum"), argc, (VALUE *) RARRAY_CONST_PTR(args));
|
|
403
|
+
return ca_wsum_from_objects(res, is_unsigned ? CA_UINT64 : CA_INT64);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/* A signed total of an unsigned array: the fold reads it as int64,
|
|
407
|
+
which keeps its bits. */
|
|
408
|
+
if ( ! is_unsigned && ca_is_unsigned_type(src->data_type) ) {
|
|
409
|
+
receiver = rb_ca_wrap_readonly(self, INT2NUM(CA_INT64));
|
|
410
|
+
}
|
|
411
|
+
if ( fold_only ) {
|
|
412
|
+
return integer_fold(argc, argv, receiver);
|
|
413
|
+
}
|
|
414
|
+
iv = integer_fold(argc, argv, receiver);
|
|
415
|
+
fv = general(argc, argv, self);
|
|
416
|
+
return ca_integer_total_choose(iv, fv, is_unsigned);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/* The total without fill_value: (the caller fills the undefined cells
|
|
420
|
+
afterwards, see ca_integer_total). */
|
|
421
|
+
static VALUE
|
|
422
|
+
ca_integer_total_unfilled (int argc, VALUE *argv, VALUE self, CArray *src,
|
|
423
|
+
VALUE (*general) (int, VALUE *, VALUE),
|
|
424
|
+
VALUE (*integer_fold) (int, VALUE *, VALUE), int kind)
|
|
425
|
+
{
|
|
426
|
+
volatile VALUE iv, fv;
|
|
427
|
+
int is_unsigned = ca_is_unsigned_type(src->data_type);
|
|
428
|
+
|
|
429
|
+
if ( kind == CA_INTEGER_TOTAL_WSUM ) {
|
|
430
|
+
volatile VALUE r = ca_wsum_integer(argc, argv, self, src, general, integer_fold);
|
|
431
|
+
return ( r == Qundef ) ? general(argc, argv, self) : r;
|
|
432
|
+
}
|
|
433
|
+
if ( kind == CA_INTEGER_TOTAL_SUM ) {
|
|
434
|
+
if ( src->bytes <= 4 && src->elements < ( (ca_size_t) 1 << 31 ) ) {
|
|
435
|
+
return integer_fold(argc, argv, self); /* sum_wrapping: cannot wrap */
|
|
436
|
+
}
|
|
437
|
+
if ( src->bytes == 8 && src->elements < ( (ca_size_t) 1 << 31 ) ) {
|
|
438
|
+
return ca_integer_total_from_parts(rb_ca_sum_parts_ki(argc, argv, self),
|
|
439
|
+
is_unsigned);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
else {
|
|
443
|
+
volatile VALUE pv;
|
|
444
|
+
fv = general(argc, argv, self);
|
|
445
|
+
pv = ca_prod_from_float(fv, is_unsigned);
|
|
446
|
+
if ( pv != Qundef ) {
|
|
447
|
+
return pv;
|
|
448
|
+
}
|
|
449
|
+
iv = integer_fold(argc, argv, self);
|
|
450
|
+
return ca_integer_total_choose(iv, fv, is_unsigned);
|
|
451
|
+
}
|
|
452
|
+
iv = integer_fold(argc, argv, self);
|
|
453
|
+
fv = general(argc, argv, self);
|
|
454
|
+
return ca_integer_total_choose(iv, fv, is_unsigned);
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
VALUE
|
|
458
|
+
ca_integer_total (int argc, VALUE *argv, volatile VALUE self,
|
|
459
|
+
VALUE (*general) (int, VALUE *, VALUE),
|
|
460
|
+
VALUE (*integer_fold) (int, VALUE *, VALUE), int kind)
|
|
461
|
+
{
|
|
462
|
+
volatile VALUE opt, args, result;
|
|
463
|
+
VALUE fill = Qnil, key_fill = ID2SYM(rb_intern("fill_value"));
|
|
464
|
+
CArray *src;
|
|
465
|
+
int whole = 0;
|
|
466
|
+
|
|
467
|
+
GetCArray(self, src);
|
|
468
|
+
|
|
469
|
+
/* fill_value: is taken off and applied to the answer, as the reduction
|
|
470
|
+
dispatchers do (ca_reduce_fill): the answer is not known until both
|
|
471
|
+
folds, or both part sums, are in. */
|
|
472
|
+
if ( argc > 0 && RB_TYPE_P(argv[argc-1], T_HASH) &&
|
|
473
|
+
rb_hash_lookup2(argv[argc-1], key_fill, Qundef) != Qundef ) {
|
|
474
|
+
opt = rb_hash_dup(argv[argc-1]);
|
|
475
|
+
fill = rb_hash_delete(opt, key_fill);
|
|
476
|
+
if ( fill == CA_UNDEF ) {
|
|
477
|
+
fill = Qnil;
|
|
478
|
+
}
|
|
479
|
+
if ( ! NIL_P(fill) ) {
|
|
480
|
+
int8_t axes[CA_RANK_MAX];
|
|
481
|
+
VALUE key_keep = ID2SYM(rb_intern("keep_axis"));
|
|
482
|
+
int8_t naxes = rb_ca_parse_reduce_axes_kw(rb_hash_lookup2(opt, ID2SYM(rb_intern("axis")), Qnil),
|
|
483
|
+
src, axes);
|
|
484
|
+
if ( naxes == src->ndim && ! RTEST(rb_hash_lookup2(opt, key_keep, Qnil)) ) {
|
|
485
|
+
whole = 1;
|
|
486
|
+
rb_hash_aset(opt, key_keep, Qtrue);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
args = rb_ary_new_from_values(argc, argv);
|
|
490
|
+
rb_ary_store(args, argc - 1, opt);
|
|
491
|
+
result = ca_integer_total_unfilled(argc, (VALUE *) RARRAY_CONST_PTR(args), self, src,
|
|
492
|
+
general, integer_fold, kind);
|
|
493
|
+
return NIL_P(fill) ? result
|
|
494
|
+
: ca_reduce_fill_widening(result, fill, whole,
|
|
495
|
+
kind == CA_INTEGER_TOTAL_SUM ? "sum"
|
|
496
|
+
: kind == CA_INTEGER_TOTAL_PROD ? "prod" : "wsum");
|
|
497
|
+
}
|
|
498
|
+
return ca_integer_total_unfilled(argc, argv, self, src, general, integer_fold, kind);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/* The running sum or product of an integer array. A running sum over
|
|
502
|
+
cells of 4 bytes or fewer, fewer than 2**31 of them, cannot leave the
|
|
503
|
+
int64 / uint64 range, so the integer scan alone answers; otherwise the
|
|
504
|
+
integer scan (which wraps) and the float64 scan both run, and the
|
|
505
|
+
integer one is the answer where every cell agrees with the float64 one,
|
|
506
|
+
as ca_integer_total_choose decides for an axis sum. */
|
|
507
|
+
VALUE
|
|
508
|
+
ca_integer_scan (int argc, VALUE *argv, volatile VALUE self,
|
|
509
|
+
VALUE (*general) (int, VALUE *, VALUE),
|
|
510
|
+
VALUE (*integer_scan) (int, VALUE *, VALUE), int kind)
|
|
511
|
+
{
|
|
512
|
+
volatile VALUE iv, fv;
|
|
513
|
+
CArray *src;
|
|
514
|
+
|
|
515
|
+
GetCArray(self, src);
|
|
516
|
+
if ( kind == CA_INTEGER_TOTAL_SUM && src->bytes <= 4 &&
|
|
517
|
+
src->elements < ( (ca_size_t) 1 << 31 ) ) {
|
|
518
|
+
return integer_scan(argc, argv, self);
|
|
519
|
+
}
|
|
520
|
+
iv = integer_scan(argc, argv, self);
|
|
521
|
+
fv = general(argc, argv, self);
|
|
522
|
+
return ca_integer_total_choose(iv, fv, ca_is_unsigned_type(src->data_type));
|
|
523
|
+
}
|