numo-narray-alt 0.10.7 → 0.10.9

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.
@@ -29,6 +29,7 @@ typedef struct NA_BUFFER_COPY {
29
29
  char* buf_ptr;
30
30
  na_loop_iter_t* src_iter;
31
31
  na_loop_iter_t* buf_iter;
32
+ bool is_bit;
32
33
  } na_buffer_copy_t;
33
34
 
34
35
  typedef struct NA_LOOP_XARGS {
@@ -52,6 +53,7 @@ typedef struct NA_MD_LOOP {
52
53
  int init_aidx; // index of initializer argument
53
54
  int reduce_dim;
54
55
  int* trans_map;
56
+ size_t sub_narray_len;
55
57
  VALUE vargs;
56
58
  VALUE reduce;
57
59
  VALUE loop_opt;
@@ -185,6 +187,7 @@ static void print_ndloop(na_md_loop_t* lp) {
185
187
  if (lp->xargs[j].bufcp) {
186
188
  printf(" xargs[%d].bufcp->ndim = %d\n", j, lp->xargs[j].bufcp->ndim);
187
189
  printf(" xargs[%d].bufcp->elmsz = %" SZF "d\n", j, lp->xargs[j].bufcp->elmsz);
190
+ printf(" xargs[%d].bufcp->is_bit = %d\n", j, lp->xargs[j].bufcp->is_bit);
188
191
  printf(" xargs[%d].bufcp->n = 0x%" SZF "x\n", j, (size_t)lp->xargs[j].bufcp->n);
189
192
  printf(
190
193
  " xargs[%d].bufcp->src_ptr = 0x%" SZF "x\n", j, (size_t)lp->xargs[j].bufcp->src_ptr
@@ -520,7 +523,7 @@ static void ndloop_set_stepidx(na_md_loop_t* lp, int j, VALUE vna, int* dim_map,
520
523
  if (n > 1 || nd > 0) {
521
524
  i = dim_map[k];
522
525
  LITER(lp, i, j).step = s;
523
- // LITER(lp,i,j).idx = NULL;
526
+ LITER(lp, i, j).idx = NULL;
524
527
  }
525
528
  s *= n;
526
529
  nd--;
@@ -539,7 +542,7 @@ static void ndloop_set_stepidx(na_md_loop_t* lp, int j, VALUE vna, int* dim_map,
539
542
  LITER(lp, i, j).idx = SDX_GET_INDEX(sdx);
540
543
  } else {
541
544
  LITER(lp, i, j).step = SDX_GET_STRIDE(sdx);
542
- // LITER(lp,i,j).idx = NULL;
545
+ LITER(lp, i, j).idx = NULL;
543
546
  }
544
547
  } else if (n == 1) {
545
548
  if (SDX_IS_INDEX(sdx)) {
@@ -888,6 +891,47 @@ skip_ud:
888
891
  }
889
892
  }
890
893
 
894
+ static void ndloop_copy_bits(na_buffer_copy_t* lp, bool to_buffer) {
895
+ int i;
896
+ int nd = lp->ndim;
897
+ size_t* c;
898
+ size_t k, t, pos;
899
+ size_t total = 1;
900
+ size_t buf_pos = 0;
901
+ BIT_DIGIT x;
902
+
903
+ for (i = 0; i < nd; i++) {
904
+ total *= lp->n[i];
905
+ }
906
+ c = ALLOCA_N(size_t, nd + 1);
907
+ for (i = 0; i <= nd; i++) c[i] = 0;
908
+ for (t = 0; t < total; t++) {
909
+ pos = LITER_SRC(lp, 0).pos;
910
+ for (i = 0; i < nd; i++) {
911
+ if (LITER_SRC(lp, i).idx) {
912
+ pos += LITER_SRC(lp, i).idx[c[i]];
913
+ } else {
914
+ pos += LITER_SRC(lp, i).step * c[i];
915
+ }
916
+ }
917
+ for (k = 0; k < lp->elmsz; k++) {
918
+ if (to_buffer) {
919
+ LOAD_BIT(lp->src_ptr, pos + k, x);
920
+ STORE_BIT(lp->buf_ptr, buf_pos + k, x);
921
+ } else {
922
+ LOAD_BIT(lp->buf_ptr, buf_pos + k, x);
923
+ STORE_BIT(lp->src_ptr, pos + k, x);
924
+ }
925
+ }
926
+ buf_pos += lp->elmsz;
927
+ for (i = nd; i > 0;) {
928
+ i--;
929
+ if (++c[i] < lp->n[i]) break;
930
+ c[i] = 0;
931
+ }
932
+ }
933
+ }
934
+
891
935
  static void ndfunc_set_bufcp(na_md_loop_t* lp, unsigned int loop_spec) {
892
936
  unsigned int f;
893
937
  int i, j;
@@ -975,7 +1019,14 @@ static void ndfunc_set_bufcp(na_md_loop_t* lp, unsigned int loop_spec) {
975
1019
  LBUFCP(lp, j)->buf_iter = buf_iter;
976
1020
  LARG(lp, j).iter = buf_iter;
977
1021
  LBUFCP(lp, j)->src_ptr = LARG(lp, j).ptr;
978
- LARG(lp, j).ptr = LBUFCP(lp, j)->buf_ptr = xmalloc(sz);
1022
+ // The positions and sizes of a Bit array count bits, not bytes.
1023
+ LBUFCP(lp, j)->is_bit = RTEST(rb_obj_is_kind_of(LARG(lp, j).value, numo_cBit));
1024
+ if (LBUFCP(lp, j)->is_bit) {
1025
+ LBUFCP(lp, j)->buf_ptr = (char*)ALLOC_N(BIT_DIGIT, (sz + NB - 1) / NB);
1026
+ } else {
1027
+ LBUFCP(lp, j)->buf_ptr = xmalloc(sz);
1028
+ }
1029
+ LARG(lp, j).ptr = LBUFCP(lp, j)->buf_ptr;
979
1030
  }
980
1031
  }
981
1032
 
@@ -1012,6 +1063,23 @@ static void ndfunc_set_bufcp(na_md_loop_t* lp, unsigned int loop_spec) {
1012
1063
  }
1013
1064
  }
1014
1065
 
1066
+ // The copy loops below and loop_narray run their body once before they check
1067
+ // any count, so they must not start on an empty axis.
1068
+ static int ndloop_has_empty_axis(const size_t* n, int ndim) {
1069
+ int i;
1070
+ for (i = 0; i < ndim; i++) {
1071
+ if (n[i] == 0) {
1072
+ return 1;
1073
+ }
1074
+ }
1075
+ return 0;
1076
+ }
1077
+
1078
+ // A contiguous empty last axis is folded into elmsz, which is then 0.
1079
+ static int ndloop_buffer_is_empty(const na_buffer_copy_t* lp) {
1080
+ return lp->elmsz == 0 || ndloop_has_empty_axis(lp->n, lp->ndim);
1081
+ }
1082
+
1015
1083
  static void ndloop_copy_to_buffer(na_buffer_copy_t* lp) {
1016
1084
  size_t* c;
1017
1085
  char *src, *buf;
@@ -1021,6 +1089,10 @@ static void ndloop_copy_to_buffer(na_buffer_copy_t* lp) {
1021
1089
  size_t buf_pos = 0;
1022
1090
  DBG(size_t j);
1023
1091
 
1092
+ if (lp->is_bit) {
1093
+ ndloop_copy_bits(lp, true);
1094
+ return;
1095
+ }
1024
1096
  DBG(printf("<to buf> ["));
1025
1097
  // zero-dimension
1026
1098
  if (nd == 0) {
@@ -1030,6 +1102,9 @@ static void ndloop_copy_to_buffer(na_buffer_copy_t* lp) {
1030
1102
  DBG(for (j = 0; j < elmsz / 8; j++) { printf("%g,", ((double*)(buf))[j]); });
1031
1103
  goto loop_end;
1032
1104
  }
1105
+ if (ndloop_buffer_is_empty(lp)) {
1106
+ goto loop_end;
1107
+ }
1033
1108
  // initialize loop counter
1034
1109
  c = ALLOCA_N(size_t, nd + 1);
1035
1110
  for (i = 0; i <= nd; i++) c[i] = 0;
@@ -1069,6 +1144,10 @@ static void ndloop_copy_from_buffer(na_buffer_copy_t* lp) {
1069
1144
  size_t buf_pos = 0;
1070
1145
  DBG(size_t j);
1071
1146
 
1147
+ if (lp->is_bit) {
1148
+ ndloop_copy_bits(lp, false);
1149
+ return;
1150
+ }
1072
1151
  DBG(printf("<from buf> ["));
1073
1152
  // zero-dimension
1074
1153
  if (nd == 0) {
@@ -1078,6 +1157,9 @@ static void ndloop_copy_from_buffer(na_buffer_copy_t* lp) {
1078
1157
  DBG(for (j = 0; j < elmsz / 8; j++) { printf("%g,", ((double*)(src))[j]); });
1079
1158
  goto loop_end;
1080
1159
  }
1160
+ if (ndloop_buffer_is_empty(lp)) {
1161
+ goto loop_end;
1162
+ }
1081
1163
  // initialize loop counter
1082
1164
  c = ALLOCA_N(size_t, nd + 1);
1083
1165
  for (i = 0; i <= nd; i++) c[i] = 0;
@@ -1129,7 +1211,7 @@ static VALUE ndloop_extract(VALUE results, ndfunc_t* nf) {
1129
1211
  case 0:
1130
1212
  return Qnil;
1131
1213
  case 1:
1132
- x = RARRAY_AREF(results, 0);
1214
+ x = rb_ary_entry(results, 0);
1133
1215
  if (NDF_TEST(nf, NDF_EXTRACT)) {
1134
1216
  if (IsNArray(x)) {
1135
1217
  GetNArray(x, na);
@@ -1243,6 +1325,12 @@ static void loop_narray(ndfunc_t* nf, na_md_loop_t* lp) {
1243
1325
  return;
1244
1326
  }
1245
1327
 
1328
+ // An iterator without NDF_HAS_LOOP does not check a count, so on an empty
1329
+ // axis it would touch an element that does not exist.
1330
+ if (!NDF_TEST(nf, NDF_HAS_LOOP) && ndloop_has_empty_axis(lp->n, nd)) {
1331
+ return;
1332
+ }
1333
+
1246
1334
  // initialize loop counter
1247
1335
  c = ALLOCA_N(size_t, nd + 1);
1248
1336
  for (i = 0; i <= nd; i++) c[i] = 0;
@@ -1518,6 +1606,7 @@ na_ndloop_inspect(VALUE nary, na_text_func_t func, VALUE opt) {
1518
1606
  static void loop_store_subnarray(ndfunc_t* nf, na_md_loop_t* lp, int i0, size_t* c, VALUE a) {
1519
1607
  int nd = lp->ndim;
1520
1608
  int i, j;
1609
+ bool* reach;
1521
1610
  narray_t* na;
1522
1611
  int* dim_map;
1523
1612
  VALUE a_type;
@@ -1539,24 +1628,32 @@ static void loop_store_subnarray(ndfunc_t* nf, na_md_loop_t* lp, int i0, size_t*
1539
1628
  for (i = 0; i < na->ndim; i++) {
1540
1629
  dim_map[i] = lp->trans_map[i + i0];
1541
1630
  }
1631
+ for (i = 0; i <= nd + lp->user.ndim; i++) {
1632
+ LITER(lp, i, 1).pos = 0;
1633
+ LITER(lp, i, 1).step = 0;
1634
+ LITER(lp, i, 1).idx = NULL;
1635
+ }
1542
1636
  ndloop_set_stepidx(lp, 1, a, dim_map, NDL_READ);
1543
- LARG(lp, 1).shape = &(na->shape[na->ndim - 1]);
1637
+ LITER(lp, i0, 1).pos = LITER(lp, 0, 1).pos;
1638
+ LARG(lp, 1).shape = &(lp->sub_narray_len);
1544
1639
 
1545
1640
  // loop body
1641
+ reach = ALLOCA_N(bool, nd + 1);
1642
+ reach[i0] = true;
1546
1643
  for (i = i0;;) {
1547
- LARG(lp, 1).value = Qtrue;
1548
1644
  for (; i < nd; i++) {
1645
+ reach[i + 1] = reach[i] && c[i] < na->shape[i - i0];
1549
1646
  for (j = 0; j < 2; j++) {
1550
- if (LITER(lp, i, j).idx) {
1647
+ if (j == 1 && !reach[i + 1]) {
1648
+ LITER(lp, i + 1, j).pos = LITER(lp, i, j).pos;
1649
+ } else if (LITER(lp, i, j).idx) {
1551
1650
  LITER(lp, i + 1, j).pos = LITER(lp, i, j).pos + LITER(lp, i, j).idx[c[i]];
1552
1651
  } else {
1553
1652
  LITER(lp, i + 1, j).pos = LITER(lp, i, j).pos + LITER(lp, i, j).step * c[i];
1554
1653
  }
1555
1654
  }
1556
- if (c[i] >= na->shape[i - i0]) {
1557
- LARG(lp, 1).value = Qfalse;
1558
- }
1559
1655
  }
1656
+ lp->sub_narray_len = reach[nd] ? na->shape[na->ndim - 1] : 0;
1560
1657
 
1561
1658
  (*(nf->func))(&(lp->user));
1562
1659
 
@@ -1569,6 +1666,7 @@ static void loop_store_subnarray(ndfunc_t* nf, na_md_loop_t* lp, int i0, size_t*
1569
1666
  }
1570
1667
  }
1571
1668
  loop_end:
1669
+ LARG(lp, 1).value = Qnil;
1572
1670
  LARG(lp, 1).ptr = NULL;
1573
1671
  }
1574
1672
 
@@ -13,10 +13,10 @@ extern "C" {
13
13
  #endif
14
14
  #endif
15
15
 
16
- #define NARRAY_VERSION "0.10.7"
16
+ #define NARRAY_VERSION "0.10.9"
17
17
  #define NARRAY_VERSION_MAJOR 0
18
18
  #define NARRAY_VERSION_MINOR 10
19
- #define NARRAY_VERSION_PATCH 7
19
+ #define NARRAY_VERSION_PATCH 9
20
20
  #define NARRAY_VERSION_CODE \
21
21
  (NARRAY_VERSION_MAJOR * 10000 + NARRAY_VERSION_MINOR * 100 + NARRAY_VERSION_PATCH)
22
22
 
@@ -1,6 +1,32 @@
1
1
  #ifndef NUMO_NARRAY_MH_BINCOUNT_H
2
2
  #define NUMO_NARRAY_MH_BINCOUNT_H 1
3
3
 
4
+ /* The items below index the output, whose size was fixed by an earlier scan of
5
+ the same array. Ruby code can run in between, so the bound is rechecked. */
6
+ static inline void na_bincount_check_item(size_t x, size_t n) {
7
+ if (x >= n) {
8
+ rb_raise(
9
+ rb_eIndexError, "item %" SZF "u is out of range of the output(size:%" SZF "u)", x, n
10
+ );
11
+ }
12
+ }
13
+
14
+ /* The loop does not call the iterator when the weight has an empty outer axis,
15
+ so this is checked before it too. The iterator keeps its own check, as Ruby
16
+ code can reshape the weight in between. */
17
+ static inline void na_bincount_check_weight(VALUE self, VALUE weight) {
18
+ narray_t* na_self;
19
+ narray_t* na_weight;
20
+ GetNArray(self, na_self);
21
+ GetNArray(weight, na_weight);
22
+ if (na_self->ndim == 0 || na_weight->ndim == 0) {
23
+ return;
24
+ }
25
+ if (na_self->shape[na_self->ndim - 1] != na_weight->shape[na_weight->ndim - 1]) {
26
+ rb_raise(nary_eShapeError, "size mismatch along last axis between self and weight");
27
+ }
28
+ }
29
+
4
30
  #define DEF_BINCOUNT_FUNCS(tDType, tNAryClass) \
5
31
  static void iter_##tDType##_bincount_32(na_loop_t* const lp) { \
6
32
  char* p1; \
@@ -21,11 +47,13 @@
21
47
  if (idx1) { \
22
48
  for (size_t i = 0; i < m; i++) { \
23
49
  GET_DATA_INDEX(p1, idx1, tDType, x); \
50
+ na_bincount_check_item(x, n); \
24
51
  (*(u_int32_t*)(p2 + s2 * x))++; \
25
52
  } \
26
53
  } else { \
27
54
  for (size_t i = 0; i < m; i++) { \
28
55
  GET_DATA_STRIDE(p1, s1, tDType, x); \
56
+ na_bincount_check_item(x, n); \
29
57
  (*(u_int32_t*)(p2 + s2 * x))++; \
30
58
  } \
31
59
  } \
@@ -60,11 +88,13 @@
60
88
  if (idx1) { \
61
89
  for (size_t i = 0; i < m; i++) { \
62
90
  GET_DATA_INDEX(p1, idx1, tDType, x); \
91
+ na_bincount_check_item(x, n); \
63
92
  (*(u_int64_t*)(p2 + s2 * x))++; \
64
93
  } \
65
94
  } else { \
66
95
  for (size_t i = 0; i < m; i++) { \
67
96
  GET_DATA_STRIDE(p1, s1, tDType, x); \
97
+ na_bincount_check_item(x, n); \
68
98
  (*(u_int64_t*)(p2 + s2 * x))++; \
69
99
  } \
70
100
  } \
@@ -106,6 +136,7 @@
106
136
  for (size_t i = 0; i < l; i++) { \
107
137
  GET_DATA_STRIDE(p1, s1, tDType, x); \
108
138
  GET_DATA_STRIDE(p2, s2, float, w); \
139
+ na_bincount_check_item(x, n); \
109
140
  (*(float*)(p3 + s3 * x)) += w; \
110
141
  } \
111
142
  } \
@@ -117,6 +148,7 @@
117
148
  ndfunc_t ndf = { \
118
149
  iter_##tDType##_bincount_sf, NO_LOOP | NDF_STRIDE_LOOP, 2, 1, ain, aout \
119
150
  }; \
151
+ na_bincount_check_weight(self, weight); \
120
152
  return na_ndloop(&ndf, 2, self, weight); \
121
153
  } \
122
154
  \
@@ -146,6 +178,7 @@
146
178
  for (size_t i = 0; i < l; i++) { \
147
179
  GET_DATA_STRIDE(p1, s1, tDType, x); \
148
180
  GET_DATA_STRIDE(p2, s2, double, w); \
181
+ na_bincount_check_item(x, n); \
149
182
  (*(double*)(p3 + s3 * x)) += w; \
150
183
  } \
151
184
  } \
@@ -157,6 +190,7 @@
157
190
  ndfunc_t ndf = { \
158
191
  iter_##tDType##_bincount_df, NO_LOOP | NDF_STRIDE_LOOP, 2, 1, ain, aout \
159
192
  }; \
193
+ na_bincount_check_weight(self, weight); \
160
194
  return na_ndloop(&ndf, 2, self, weight); \
161
195
  }
162
196
 
@@ -170,6 +204,14 @@
170
204
  rb_scan_args(argc, argv, "01:", &weight, &kw); \
171
205
  rb_get_kwargs(kw, table, 0, 1, opts); \
172
206
  \
207
+ size_t minlength = 0; \
208
+ if (opts[0] != Qundef) { \
209
+ minlength = NUM2SIZET(opts[0]); \
210
+ } \
211
+ if (!NIL_P(weight) && rb_obj_class(weight) != numo_cSFloat) { \
212
+ weight = rb_funcall(numo_cDFloat, id_cast, 1, weight); \
213
+ } \
214
+ \
173
215
  VALUE v = tDType##_minmax(0, 0, self); \
174
216
  if (m_num_to_data(RARRAY_AREF(v, 0)) < 0) { \
175
217
  rb_raise(rb_eArgError, "array items must be non-netagive"); \
@@ -177,11 +219,8 @@
177
219
  v = RARRAY_AREF(v, 1); \
178
220
  \
179
221
  size_t length = NUM2SIZET(v) + 1; \
180
- if (opts[0] != Qundef) { \
181
- const size_t minlength = NUM2SIZET(opts[0]); \
182
- if (minlength > length) { \
183
- length = minlength; \
184
- } \
222
+ if (minlength > length) { \
223
+ length = minlength; \
185
224
  } \
186
225
  \
187
226
  if (NIL_P(weight)) { \
@@ -207,14 +246,23 @@
207
246
  rb_scan_args(argc, argv, "01:", &weight, &kw); \
208
247
  rb_get_kwargs(kw, table, 0, 1, opts); \
209
248
  \
249
+ size_t minlength = 0; \
250
+ if (opts[0] != Qundef) { \
251
+ minlength = NUM2SIZET(opts[0]); \
252
+ } \
253
+ if (!NIL_P(weight) && rb_obj_class(weight) != numo_cSFloat) { \
254
+ weight = rb_funcall(numo_cDFloat, id_cast, 1, weight); \
255
+ } \
256
+ \
210
257
  VALUE v = tDType##_max(0, 0, self); \
211
258
  \
212
- size_t length = NUM2SIZET(v) + 1; \
213
- if (opts[0] != Qundef) { \
214
- const size_t minlength = NUM2SIZET(opts[0]); \
215
- if (minlength > length) { \
216
- length = minlength; \
217
- } \
259
+ const size_t max = NUM2SIZET(v); \
260
+ if (max == SIZE_MAX) { \
261
+ rb_raise(rb_eRangeError, "maximum item %" SZF "u is too large to size the output", max); \
262
+ } \
263
+ size_t length = max + 1; \
264
+ if (minlength > length) { \
265
+ length = minlength; \
218
266
  } \
219
267
  \
220
268
  if (NIL_P(weight)) { \
@@ -30,7 +30,7 @@
30
30
  } \
31
31
  if (is_aligned_step(s1, sizeof(tDType)) && is_aligned_step(s2, sizeof(tDType)) && \
32
32
  is_aligned_step(s3, sizeof(tDType))) { \
33
- if (s2 == 0) { \
33
+ if (s2 == 0 && n > 0) { \
34
34
  const tDType v2 = *(tDType*)p2; \
35
35
  if (s1 == sizeof(tDType) && s3 == sizeof(tDType)) { \
36
36
  if (p1 == p3) { \
@@ -108,7 +108,7 @@
108
108
  INIT_PTR(lp, 0, p1, s1); \
109
109
  INIT_PTR(lp, 1, p2, s2); \
110
110
  INIT_PTR(lp, 2, p3, s3); \
111
- if (s2 == 0) { \
111
+ if (s2 == 0 && n > 0) { \
112
112
  const tDType v2 = *(tDType*)p2; \
113
113
  if (s1 == sizeof(tDType) && s3 == sizeof(tDType)) { \
114
114
  if (p1 == p3) { \
@@ -174,7 +174,7 @@
174
174
  INIT_PTR(lp, 0, p1, s1); \
175
175
  INIT_PTR(lp, 1, p2, s2); \
176
176
  INIT_PTR(lp, 2, p3, s3); \
177
- if (s2 == 0) { \
177
+ if (s2 == 0 && n > 0) { \
178
178
  const robject v2 = *(robject*)p2; \
179
179
  if (s1 == sizeof(robject) && s3 == sizeof(robject)) { \
180
180
  if (p1 == p3) { \
@@ -30,7 +30,7 @@
30
30
  } \
31
31
  if (is_aligned_step(s1, sizeof(tDType)) && is_aligned_step(s2, sizeof(tDType)) && \
32
32
  is_aligned_step(s3, sizeof(tDType))) { \
33
- if (s2 == 0) { \
33
+ if (s2 == 0 && n > 0) { \
34
34
  const tDType v2 = *(tDType*)p2; \
35
35
  if (s1 == sizeof(tDType) && s3 == sizeof(tDType)) { \
36
36
  if (p1 == p3) { \
@@ -108,7 +108,7 @@
108
108
  INIT_PTR(lp, 0, p1, s1); \
109
109
  INIT_PTR(lp, 1, p2, s2); \
110
110
  INIT_PTR(lp, 2, p3, s3); \
111
- if (s2 == 0) { \
111
+ if (s2 == 0 && n > 0) { \
112
112
  const tDType v2 = *(tDType*)p2; \
113
113
  if (s1 == sizeof(tDType) && s3 == sizeof(tDType)) { \
114
114
  if (p1 == p3) { \
@@ -174,7 +174,7 @@
174
174
  INIT_PTR(lp, 0, p1, s1); \
175
175
  INIT_PTR(lp, 1, p2, s2); \
176
176
  INIT_PTR(lp, 2, p3, s3); \
177
- if (s2 == 0) { \
177
+ if (s2 == 0 && n > 0) { \
178
178
  const robject v2 = *(robject*)p2; \
179
179
  if (s1 == sizeof(robject) && s3 == sizeof(robject)) { \
180
180
  if (p1 == p3) { \
@@ -38,8 +38,8 @@
38
38
  char* p2 = NDL_PTR(lp, 1); \
39
39
  tDType* buf = (tDType*)p1; \
40
40
  tDType##_qsort_ignan(buf, n, sizeof(tDType)); \
41
- for (size_t i = 0; i < n; i++) { \
42
- if (!isnan(buf[i])) break; \
41
+ for (; n; n--) { \
42
+ if (!isnan(buf[n - 1])) break; \
43
43
  } \
44
44
  if (n == 0) { \
45
45
  *(tDType*)p2 = buf[0]; \
@@ -57,8 +57,8 @@
57
57
  char* p2 = NDL_PTR(lp, 1); \
58
58
  tDType* buf = (tDType*)p1; \
59
59
  tDType##_qsort_prnan(buf, n, sizeof(tDType)); \
60
- for (size_t i = 0; i < n; i++) { \
61
- if (!isnan(buf[i])) break; \
60
+ for (; n; n--) { \
61
+ if (!isnan(buf[n - 1])) break; \
62
62
  } \
63
63
  if (n == 0) { \
64
64
  *(tDType*)p2 = buf[0]; \
@@ -43,7 +43,7 @@
43
43
  if (is_aligned_step(s1, sizeof(tDType)) && is_aligned_step(s2, sizeof(tDType)) && \
44
44
  is_aligned_step(s3, sizeof(tDType))) { \
45
45
  if (s2 == 0) { \
46
- if ((*(tDType*)p2) == 0) { \
46
+ if (n > 0 && (*(tDType*)p2) == 0) { \
47
47
  lp->err_type = rb_eZeroDivError; \
48
48
  return; \
49
49
  } \
@@ -67,7 +67,7 @@
67
67
  static void iter_##tDType##_div(na_loop_t* const lp) { \
68
68
  ITER_BINARY_INIT_VARS() \
69
69
  if (s2 == 0) { \
70
- if ((*(tDType*)p2) == 0) { \
70
+ if (n > 0 && (*(tDType*)p2) == 0) { \
71
71
  lp->err_type = rb_eZeroDivError; \
72
72
  return; \
73
73
  } \
@@ -87,7 +87,7 @@
87
87
  static void iter_robject_div(na_loop_t* const lp) { \
88
88
  ITER_BINARY_INIT_VARS() \
89
89
  if (s2 == 0) { \
90
- if ((*(robject*)p2) == 0) { \
90
+ if (n > 0 && (*(robject*)p2) == 0) { \
91
91
  lp->err_type = rb_eZeroDivError; \
92
92
  return; \
93
93
  } \
@@ -43,7 +43,7 @@
43
43
  if (is_aligned_step(s1, sizeof(tDType)) && is_aligned_step(s2, sizeof(tDType)) && \
44
44
  is_aligned_step(s3, sizeof(tDType))) { \
45
45
  if (s2 == 0) { \
46
- if ((*(tDType*)p2) == 0) { \
46
+ if (n > 0 && (*(tDType*)p2) == 0) { \
47
47
  lp->err_type = rb_eZeroDivError; \
48
48
  return; \
49
49
  } \
@@ -67,7 +67,7 @@
67
67
  static void iter_##tDType##_mod(na_loop_t* const lp) { \
68
68
  ITER_BINARY_INIT_VARS() \
69
69
  if (s2 == 0) { \
70
- if ((*(tDType*)p2) == 0) { \
70
+ if (n > 0 && (*(tDType*)p2) == 0) { \
71
71
  lp->err_type = rb_eZeroDivError; \
72
72
  return; \
73
73
  } \
@@ -87,7 +87,7 @@
87
87
  static void iter_robject_mod(na_loop_t* const lp) { \
88
88
  ITER_BINARY_INIT_VARS() \
89
89
  if (s2 == 0) { \
90
- if ((*(robject*)p2) == 0) { \
90
+ if (n > 0 && (*(robject*)p2) == 0) { \
91
91
  lp->err_type = rb_eZeroDivError; \
92
92
  return; \
93
93
  } \