@zakkster/lite-bisect 0.0.0-stage → 1.0.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.
- package/Bisect.d.ts +243 -0
- package/Bisect.js +450 -0
- package/CHANGELOG.md +125 -0
- package/LICENSE.txt +21 -0
- package/README.md +365 -2
- package/llms.txt +105 -0
- package/package.json +67 -4
package/Bisect.js
ADDED
|
@@ -0,0 +1,450 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zakkster/lite-bisect -- zero-GC binary search kernels for sorted typed arrays.
|
|
3
|
+
* One monomorphic copy of every kernel per array type, suffix F64 / F32 / U32 / U16 / I32
|
|
4
|
+
* (Float64Array, Float32Array, Uint32Array, Uint16Array, Int32Array).
|
|
5
|
+
*
|
|
6
|
+
* Contract (every kernel):
|
|
7
|
+
* a a sorted, non-decreasing typed array of the suffix's type, no NaN inside.
|
|
8
|
+
* [lo, hi) the half-open range searched: integers, 0 <= lo <= hi <= a.length, hi - lo <= 2^30 - 1.
|
|
9
|
+
* lower lowerBound*: the first i in [lo, hi) with a[i] >= x, else hi.
|
|
10
|
+
* upper upperBound*: the first i in [lo, hi) with a[i] > x, else hi.
|
|
11
|
+
* hint lowerBoundHint*(a, lo, hi, x, h): lower, galloping out from h. h is an integer index or
|
|
12
|
+
* NaN (NaN -> lo; outside [lo, hi] clamped); a non-integer h is a caller error that may
|
|
13
|
+
* return a wrong index or never return.
|
|
14
|
+
* segment upper - 1: the last i with a[i] <= x; lo - 1 when x < a[lo], on NaN, on an empty range.
|
|
15
|
+
* nearest the index closest to x: a tie between the last value below x and the first above goes
|
|
16
|
+
* to the LOWER one; an exact match wins (the first i with a[i] === x); within a run of
|
|
17
|
+
* equal values, the index next to x; an empty range -> lo, NaN -> lo.
|
|
18
|
+
* A NaN query compares false everywhere: every lower / upper / hint kernel returns lo.
|
|
19
|
+
*
|
|
20
|
+
* Allocation: zero allocation where the kernel inlines into its caller, or every number argument (the query; the hint of lowerBoundHint*; lo / hi) is a Smi; a non-Smi number computed at run time or read from a typed array (a fractional query, a NaN hint read from a Float64Array, -0 for lo / hi) and passed across a call V8 does not inline is boxed by the caller: one 16 B HeapNumber per such argument on Node. For no hint pass lo or the literal NaN: neither boxes.
|
|
21
|
+
*
|
|
22
|
+
* The hot kernels trust the caller: no range check, no throw. A range outside the
|
|
23
|
+
* contract may return a wrong index or never return. The cold checks assertRange(a, lo, hi) and
|
|
24
|
+
* assertSorted(a, lo, hi) throw RangeError; call them off the hot path. No kernel calls them.
|
|
25
|
+
*
|
|
26
|
+
* MIT (c) Zahary Shinikchiev <shinikchiev@yahoo.com>
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
// BEGIN GENERATED by scripts/gen.mjs from scripts/bisect.template.js -- edit the template, then npm run gen
|
|
30
|
+
export function lowerBoundBranchlessF64(a, lo, hi, x) {
|
|
31
|
+
let n = hi - lo;
|
|
32
|
+
if (n === 0) return lo; // a[b] below would read outside the range
|
|
33
|
+
let b = lo;
|
|
34
|
+
while (n > 1) {
|
|
35
|
+
const h = n >>> 1;
|
|
36
|
+
b += (0 - ((a[b + h] < x) | 0)) & h;
|
|
37
|
+
n -= h;
|
|
38
|
+
}
|
|
39
|
+
return b + ((a[b] < x) | 0);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function upperBoundBranchlessF64(a, lo, hi, x) {
|
|
43
|
+
let n = hi - lo;
|
|
44
|
+
if (n === 0) return lo;
|
|
45
|
+
let b = lo;
|
|
46
|
+
while (n > 1) {
|
|
47
|
+
const h = n >>> 1;
|
|
48
|
+
b += (0 - ((a[b + h] <= x) | 0)) & h;
|
|
49
|
+
n -= h;
|
|
50
|
+
}
|
|
51
|
+
return b + ((a[b] <= x) | 0);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function lowerBoundBranchyF64(a, lo, hi, x) {
|
|
55
|
+
while (lo < hi) {
|
|
56
|
+
const m = lo + ((hi - lo) >>> 1);
|
|
57
|
+
if (a[m] < x) lo = m + 1;
|
|
58
|
+
else hi = m;
|
|
59
|
+
}
|
|
60
|
+
return lo;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function upperBoundBranchyF64(a, lo, hi, x) {
|
|
64
|
+
while (lo < hi) {
|
|
65
|
+
const m = lo + ((hi - lo) >>> 1);
|
|
66
|
+
if (a[m] <= x) lo = m + 1;
|
|
67
|
+
else hi = m;
|
|
68
|
+
}
|
|
69
|
+
return lo;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function lowerBoundHintF64(a, lo, hi, x, h) {
|
|
73
|
+
if (!(h >= lo)) h = lo; // NaN hint -> lo
|
|
74
|
+
if (h > hi) h = hi;
|
|
75
|
+
let step = 1;
|
|
76
|
+
if (h < hi && a[h] < x) {
|
|
77
|
+
// Answer in (h, hi]. Gallop right: prev is the last index known < x.
|
|
78
|
+
let prev = h, r = h + 1;
|
|
79
|
+
while (r < hi && a[r] < x) { prev = r; step <<= 1; r = h + step; }
|
|
80
|
+
if (r > hi) r = hi;
|
|
81
|
+
return lowerBoundBranchlessF64(a, prev + 1, r, x);
|
|
82
|
+
}
|
|
83
|
+
// a[h] >= x or h === hi: answer in [lo, h]. Gallop left: top is the lowest index known >= x.
|
|
84
|
+
let top = h, l = h - 1;
|
|
85
|
+
// !(a[l] < x), never a[l] >= x: a NaN query gallops to lo.
|
|
86
|
+
while (l >= lo && !(a[l] < x)) { top = l; step <<= 1; l = h - step; }
|
|
87
|
+
if (l < lo) l = lo - 1;
|
|
88
|
+
return lowerBoundBranchlessF64(a, l + 1, top, x);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function segmentF64(a, lo, hi, x) {
|
|
92
|
+
return upperBoundBranchlessF64(a, lo, hi, x) - 1;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function nearestF64(a, lo, hi, x) {
|
|
96
|
+
const i = lowerBoundBranchlessF64(a, lo, hi, x);
|
|
97
|
+
if (i === lo) return lo;
|
|
98
|
+
if (i === hi) return hi - 1;
|
|
99
|
+
const p = a[i - 1], q = a[i], d = x - p, u = q - x;
|
|
100
|
+
// Rounding is monotone: unequal doubles order the exact distances (a NaN u is an exact +Infinity match).
|
|
101
|
+
if (d !== u) return d < u ? i - 1 : i;
|
|
102
|
+
// Equal after rounding: compare the exact rounding errors of d and u (TwoSum; no allocation). They are
|
|
103
|
+
// NaN when d is Infinity (an infinite neighbour or one overflow): then only a -Infinity lower neighbour
|
|
104
|
+
// with a finite upper one is the farther of the two.
|
|
105
|
+
const s = d - x, t = u - q;
|
|
106
|
+
return i - ((!((x - (d - s)) - (p + s) > (q - (u - t)) - (x + t)) && p * 0.5 + q * 0.5 !== -Infinity) | 0);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function lowerBoundBranchlessF32(a, lo, hi, x) {
|
|
110
|
+
let n = hi - lo;
|
|
111
|
+
if (n === 0) return lo; // a[b] below would read outside the range
|
|
112
|
+
let b = lo;
|
|
113
|
+
while (n > 1) {
|
|
114
|
+
const h = n >>> 1;
|
|
115
|
+
b += (0 - ((a[b + h] < x) | 0)) & h;
|
|
116
|
+
n -= h;
|
|
117
|
+
}
|
|
118
|
+
return b + ((a[b] < x) | 0);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function upperBoundBranchlessF32(a, lo, hi, x) {
|
|
122
|
+
let n = hi - lo;
|
|
123
|
+
if (n === 0) return lo;
|
|
124
|
+
let b = lo;
|
|
125
|
+
while (n > 1) {
|
|
126
|
+
const h = n >>> 1;
|
|
127
|
+
b += (0 - ((a[b + h] <= x) | 0)) & h;
|
|
128
|
+
n -= h;
|
|
129
|
+
}
|
|
130
|
+
return b + ((a[b] <= x) | 0);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function lowerBoundBranchyF32(a, lo, hi, x) {
|
|
134
|
+
while (lo < hi) {
|
|
135
|
+
const m = lo + ((hi - lo) >>> 1);
|
|
136
|
+
if (a[m] < x) lo = m + 1;
|
|
137
|
+
else hi = m;
|
|
138
|
+
}
|
|
139
|
+
return lo;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export function upperBoundBranchyF32(a, lo, hi, x) {
|
|
143
|
+
while (lo < hi) {
|
|
144
|
+
const m = lo + ((hi - lo) >>> 1);
|
|
145
|
+
if (a[m] <= x) lo = m + 1;
|
|
146
|
+
else hi = m;
|
|
147
|
+
}
|
|
148
|
+
return lo;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function lowerBoundHintF32(a, lo, hi, x, h) {
|
|
152
|
+
if (!(h >= lo)) h = lo; // NaN hint -> lo
|
|
153
|
+
if (h > hi) h = hi;
|
|
154
|
+
let step = 1;
|
|
155
|
+
if (h < hi && a[h] < x) {
|
|
156
|
+
// Answer in (h, hi]. Gallop right: prev is the last index known < x.
|
|
157
|
+
let prev = h, r = h + 1;
|
|
158
|
+
while (r < hi && a[r] < x) { prev = r; step <<= 1; r = h + step; }
|
|
159
|
+
if (r > hi) r = hi;
|
|
160
|
+
return lowerBoundBranchlessF32(a, prev + 1, r, x);
|
|
161
|
+
}
|
|
162
|
+
// a[h] >= x or h === hi: answer in [lo, h]. Gallop left: top is the lowest index known >= x.
|
|
163
|
+
let top = h, l = h - 1;
|
|
164
|
+
// !(a[l] < x), never a[l] >= x: a NaN query gallops to lo.
|
|
165
|
+
while (l >= lo && !(a[l] < x)) { top = l; step <<= 1; l = h - step; }
|
|
166
|
+
if (l < lo) l = lo - 1;
|
|
167
|
+
return lowerBoundBranchlessF32(a, l + 1, top, x);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export function segmentF32(a, lo, hi, x) {
|
|
171
|
+
return upperBoundBranchlessF32(a, lo, hi, x) - 1;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export function nearestF32(a, lo, hi, x) {
|
|
175
|
+
const i = lowerBoundBranchlessF32(a, lo, hi, x);
|
|
176
|
+
if (i === lo) return lo;
|
|
177
|
+
if (i === hi) return hi - 1;
|
|
178
|
+
const p = a[i - 1], q = a[i], d = x - p, u = q - x;
|
|
179
|
+
// Rounding is monotone: unequal doubles order the exact distances (a NaN u is an exact +Infinity match).
|
|
180
|
+
if (d !== u) return d < u ? i - 1 : i;
|
|
181
|
+
// Equal after rounding: compare the exact rounding errors of d and u (TwoSum; no allocation). They are
|
|
182
|
+
// NaN when d is Infinity (an infinite neighbour or one overflow): then only a -Infinity lower neighbour
|
|
183
|
+
// with a finite upper one is the farther of the two.
|
|
184
|
+
const s = d - x, t = u - q;
|
|
185
|
+
return i - ((!((x - (d - s)) - (p + s) > (q - (u - t)) - (x + t)) && p * 0.5 + q * 0.5 !== -Infinity) | 0);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export function lowerBoundBranchlessU32(a, lo, hi, x) {
|
|
189
|
+
let n = hi - lo;
|
|
190
|
+
if (n === 0) return lo; // a[b] below would read outside the range
|
|
191
|
+
let b = lo;
|
|
192
|
+
while (n > 1) {
|
|
193
|
+
const h = n >>> 1;
|
|
194
|
+
b += (0 - ((a[b + h] < x) | 0)) & h;
|
|
195
|
+
n -= h;
|
|
196
|
+
}
|
|
197
|
+
return b + ((a[b] < x) | 0);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export function upperBoundBranchlessU32(a, lo, hi, x) {
|
|
201
|
+
let n = hi - lo;
|
|
202
|
+
if (n === 0) return lo;
|
|
203
|
+
let b = lo;
|
|
204
|
+
while (n > 1) {
|
|
205
|
+
const h = n >>> 1;
|
|
206
|
+
b += (0 - ((a[b + h] <= x) | 0)) & h;
|
|
207
|
+
n -= h;
|
|
208
|
+
}
|
|
209
|
+
return b + ((a[b] <= x) | 0);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export function lowerBoundBranchyU32(a, lo, hi, x) {
|
|
213
|
+
while (lo < hi) {
|
|
214
|
+
const m = lo + ((hi - lo) >>> 1);
|
|
215
|
+
if (a[m] < x) lo = m + 1;
|
|
216
|
+
else hi = m;
|
|
217
|
+
}
|
|
218
|
+
return lo;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export function upperBoundBranchyU32(a, lo, hi, x) {
|
|
222
|
+
while (lo < hi) {
|
|
223
|
+
const m = lo + ((hi - lo) >>> 1);
|
|
224
|
+
if (a[m] <= x) lo = m + 1;
|
|
225
|
+
else hi = m;
|
|
226
|
+
}
|
|
227
|
+
return lo;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export function lowerBoundHintU32(a, lo, hi, x, h) {
|
|
231
|
+
if (!(h >= lo)) h = lo; // NaN hint -> lo
|
|
232
|
+
if (h > hi) h = hi;
|
|
233
|
+
let step = 1;
|
|
234
|
+
if (h < hi && a[h] < x) {
|
|
235
|
+
// Answer in (h, hi]. Gallop right: prev is the last index known < x.
|
|
236
|
+
let prev = h, r = h + 1;
|
|
237
|
+
while (r < hi && a[r] < x) { prev = r; step <<= 1; r = h + step; }
|
|
238
|
+
if (r > hi) r = hi;
|
|
239
|
+
return lowerBoundBranchlessU32(a, prev + 1, r, x);
|
|
240
|
+
}
|
|
241
|
+
// a[h] >= x or h === hi: answer in [lo, h]. Gallop left: top is the lowest index known >= x.
|
|
242
|
+
let top = h, l = h - 1;
|
|
243
|
+
// !(a[l] < x), never a[l] >= x: a NaN query gallops to lo.
|
|
244
|
+
while (l >= lo && !(a[l] < x)) { top = l; step <<= 1; l = h - step; }
|
|
245
|
+
if (l < lo) l = lo - 1;
|
|
246
|
+
return lowerBoundBranchlessU32(a, l + 1, top, x);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export function segmentU32(a, lo, hi, x) {
|
|
250
|
+
return upperBoundBranchlessU32(a, lo, hi, x) - 1;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export function nearestU32(a, lo, hi, x) {
|
|
254
|
+
const i = lowerBoundBranchlessU32(a, lo, hi, x);
|
|
255
|
+
if (i === lo) return lo;
|
|
256
|
+
if (i === hi) return hi - 1;
|
|
257
|
+
const p = a[i - 1], q = a[i], d = x - p, u = q - x;
|
|
258
|
+
// Rounding is monotone: unequal doubles order the exact distances (a NaN u is an exact +Infinity match).
|
|
259
|
+
if (d !== u) return d < u ? i - 1 : i;
|
|
260
|
+
// Equal after rounding: compare the exact rounding errors of d and u (TwoSum; no allocation). They are
|
|
261
|
+
// NaN when d is Infinity (an infinite neighbour or one overflow): then only a -Infinity lower neighbour
|
|
262
|
+
// with a finite upper one is the farther of the two.
|
|
263
|
+
const s = d - x, t = u - q;
|
|
264
|
+
return i - ((!((x - (d - s)) - (p + s) > (q - (u - t)) - (x + t)) && p * 0.5 + q * 0.5 !== -Infinity) | 0);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export function lowerBoundBranchlessU16(a, lo, hi, x) {
|
|
268
|
+
let n = hi - lo;
|
|
269
|
+
if (n === 0) return lo; // a[b] below would read outside the range
|
|
270
|
+
let b = lo;
|
|
271
|
+
while (n > 1) {
|
|
272
|
+
const h = n >>> 1;
|
|
273
|
+
b += (0 - ((a[b + h] < x) | 0)) & h;
|
|
274
|
+
n -= h;
|
|
275
|
+
}
|
|
276
|
+
return b + ((a[b] < x) | 0);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
export function upperBoundBranchlessU16(a, lo, hi, x) {
|
|
280
|
+
let n = hi - lo;
|
|
281
|
+
if (n === 0) return lo;
|
|
282
|
+
let b = lo;
|
|
283
|
+
while (n > 1) {
|
|
284
|
+
const h = n >>> 1;
|
|
285
|
+
b += (0 - ((a[b + h] <= x) | 0)) & h;
|
|
286
|
+
n -= h;
|
|
287
|
+
}
|
|
288
|
+
return b + ((a[b] <= x) | 0);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export function lowerBoundBranchyU16(a, lo, hi, x) {
|
|
292
|
+
while (lo < hi) {
|
|
293
|
+
const m = lo + ((hi - lo) >>> 1);
|
|
294
|
+
if (a[m] < x) lo = m + 1;
|
|
295
|
+
else hi = m;
|
|
296
|
+
}
|
|
297
|
+
return lo;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
export function upperBoundBranchyU16(a, lo, hi, x) {
|
|
301
|
+
while (lo < hi) {
|
|
302
|
+
const m = lo + ((hi - lo) >>> 1);
|
|
303
|
+
if (a[m] <= x) lo = m + 1;
|
|
304
|
+
else hi = m;
|
|
305
|
+
}
|
|
306
|
+
return lo;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
export function lowerBoundHintU16(a, lo, hi, x, h) {
|
|
310
|
+
if (!(h >= lo)) h = lo; // NaN hint -> lo
|
|
311
|
+
if (h > hi) h = hi;
|
|
312
|
+
let step = 1;
|
|
313
|
+
if (h < hi && a[h] < x) {
|
|
314
|
+
// Answer in (h, hi]. Gallop right: prev is the last index known < x.
|
|
315
|
+
let prev = h, r = h + 1;
|
|
316
|
+
while (r < hi && a[r] < x) { prev = r; step <<= 1; r = h + step; }
|
|
317
|
+
if (r > hi) r = hi;
|
|
318
|
+
return lowerBoundBranchlessU16(a, prev + 1, r, x);
|
|
319
|
+
}
|
|
320
|
+
// a[h] >= x or h === hi: answer in [lo, h]. Gallop left: top is the lowest index known >= x.
|
|
321
|
+
let top = h, l = h - 1;
|
|
322
|
+
// !(a[l] < x), never a[l] >= x: a NaN query gallops to lo.
|
|
323
|
+
while (l >= lo && !(a[l] < x)) { top = l; step <<= 1; l = h - step; }
|
|
324
|
+
if (l < lo) l = lo - 1;
|
|
325
|
+
return lowerBoundBranchlessU16(a, l + 1, top, x);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
export function segmentU16(a, lo, hi, x) {
|
|
329
|
+
return upperBoundBranchlessU16(a, lo, hi, x) - 1;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
export function nearestU16(a, lo, hi, x) {
|
|
333
|
+
const i = lowerBoundBranchlessU16(a, lo, hi, x);
|
|
334
|
+
if (i === lo) return lo;
|
|
335
|
+
if (i === hi) return hi - 1;
|
|
336
|
+
const p = a[i - 1], q = a[i], d = x - p, u = q - x;
|
|
337
|
+
// Rounding is monotone: unequal doubles order the exact distances (a NaN u is an exact +Infinity match).
|
|
338
|
+
if (d !== u) return d < u ? i - 1 : i;
|
|
339
|
+
// Equal after rounding: compare the exact rounding errors of d and u (TwoSum; no allocation). They are
|
|
340
|
+
// NaN when d is Infinity (an infinite neighbour or one overflow): then only a -Infinity lower neighbour
|
|
341
|
+
// with a finite upper one is the farther of the two.
|
|
342
|
+
const s = d - x, t = u - q;
|
|
343
|
+
return i - ((!((x - (d - s)) - (p + s) > (q - (u - t)) - (x + t)) && p * 0.5 + q * 0.5 !== -Infinity) | 0);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
export function lowerBoundBranchlessI32(a, lo, hi, x) {
|
|
347
|
+
let n = hi - lo;
|
|
348
|
+
if (n === 0) return lo; // a[b] below would read outside the range
|
|
349
|
+
let b = lo;
|
|
350
|
+
while (n > 1) {
|
|
351
|
+
const h = n >>> 1;
|
|
352
|
+
b += (0 - ((a[b + h] < x) | 0)) & h;
|
|
353
|
+
n -= h;
|
|
354
|
+
}
|
|
355
|
+
return b + ((a[b] < x) | 0);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
export function upperBoundBranchlessI32(a, lo, hi, x) {
|
|
359
|
+
let n = hi - lo;
|
|
360
|
+
if (n === 0) return lo;
|
|
361
|
+
let b = lo;
|
|
362
|
+
while (n > 1) {
|
|
363
|
+
const h = n >>> 1;
|
|
364
|
+
b += (0 - ((a[b + h] <= x) | 0)) & h;
|
|
365
|
+
n -= h;
|
|
366
|
+
}
|
|
367
|
+
return b + ((a[b] <= x) | 0);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
export function lowerBoundBranchyI32(a, lo, hi, x) {
|
|
371
|
+
while (lo < hi) {
|
|
372
|
+
const m = lo + ((hi - lo) >>> 1);
|
|
373
|
+
if (a[m] < x) lo = m + 1;
|
|
374
|
+
else hi = m;
|
|
375
|
+
}
|
|
376
|
+
return lo;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
export function upperBoundBranchyI32(a, lo, hi, x) {
|
|
380
|
+
while (lo < hi) {
|
|
381
|
+
const m = lo + ((hi - lo) >>> 1);
|
|
382
|
+
if (a[m] <= x) lo = m + 1;
|
|
383
|
+
else hi = m;
|
|
384
|
+
}
|
|
385
|
+
return lo;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
export function lowerBoundHintI32(a, lo, hi, x, h) {
|
|
389
|
+
if (!(h >= lo)) h = lo; // NaN hint -> lo
|
|
390
|
+
if (h > hi) h = hi;
|
|
391
|
+
let step = 1;
|
|
392
|
+
if (h < hi && a[h] < x) {
|
|
393
|
+
// Answer in (h, hi]. Gallop right: prev is the last index known < x.
|
|
394
|
+
let prev = h, r = h + 1;
|
|
395
|
+
while (r < hi && a[r] < x) { prev = r; step <<= 1; r = h + step; }
|
|
396
|
+
if (r > hi) r = hi;
|
|
397
|
+
return lowerBoundBranchlessI32(a, prev + 1, r, x);
|
|
398
|
+
}
|
|
399
|
+
// a[h] >= x or h === hi: answer in [lo, h]. Gallop left: top is the lowest index known >= x.
|
|
400
|
+
let top = h, l = h - 1;
|
|
401
|
+
// !(a[l] < x), never a[l] >= x: a NaN query gallops to lo.
|
|
402
|
+
while (l >= lo && !(a[l] < x)) { top = l; step <<= 1; l = h - step; }
|
|
403
|
+
if (l < lo) l = lo - 1;
|
|
404
|
+
return lowerBoundBranchlessI32(a, l + 1, top, x);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
export function segmentI32(a, lo, hi, x) {
|
|
408
|
+
return upperBoundBranchlessI32(a, lo, hi, x) - 1;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
export function nearestI32(a, lo, hi, x) {
|
|
412
|
+
const i = lowerBoundBranchlessI32(a, lo, hi, x);
|
|
413
|
+
if (i === lo) return lo;
|
|
414
|
+
if (i === hi) return hi - 1;
|
|
415
|
+
const p = a[i - 1], q = a[i], d = x - p, u = q - x;
|
|
416
|
+
// Rounding is monotone: unequal doubles order the exact distances (a NaN u is an exact +Infinity match).
|
|
417
|
+
if (d !== u) return d < u ? i - 1 : i;
|
|
418
|
+
// Equal after rounding: compare the exact rounding errors of d and u (TwoSum; no allocation). They are
|
|
419
|
+
// NaN when d is Infinity (an infinite neighbour or one overflow): then only a -Infinity lower neighbour
|
|
420
|
+
// with a finite upper one is the farther of the two.
|
|
421
|
+
const s = d - x, t = u - q;
|
|
422
|
+
return i - ((!((x - (d - s)) - (p + s) > (q - (u - t)) - (x + t)) && p * 0.5 + q * 0.5 !== -Infinity) | 0);
|
|
423
|
+
}
|
|
424
|
+
// END GENERATED by scripts/gen.mjs
|
|
425
|
+
|
|
426
|
+
// --- cold checks: one copy each, called by no kernel --------------------------------------------
|
|
427
|
+
|
|
428
|
+
/** RangeError unless a.length is a number and a.length, lo, hi are safe integers, 0 <= lo <= hi <= a.length, hi - lo <= 2^30 - 1. Reads only a.length. */
|
|
429
|
+
export function assertRange(a, lo, hi) {
|
|
430
|
+
const n = a.length;
|
|
431
|
+
if (typeof n === 'number' && Number.isSafeInteger(n) && Number.isSafeInteger(lo) && Number.isSafeInteger(hi) &&
|
|
432
|
+
0 <= lo && lo <= hi && hi <= n && hi - lo <= 1073741823) return;
|
|
433
|
+
// typeof, never String(), on a non-number: String() runs caller code (a toString) or throws TypeError.
|
|
434
|
+
throw new RangeError('lite-bisect: bad range [' + (typeof lo === 'number' ? String(lo) : typeof lo) +
|
|
435
|
+
', ' + (typeof hi === 'number' ? String(hi) : typeof hi) +
|
|
436
|
+
') for length ' + (typeof n === 'number' ? String(n) : typeof n) +
|
|
437
|
+
' (need integers 0 <= lo <= hi <= length, hi - lo <= 1073741823)');
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/** assertRange, then RangeError on a NaN in [lo, hi) or a descent (a[i] < a[i - 1]). */
|
|
441
|
+
export function assertSorted(a, lo, hi) {
|
|
442
|
+
assertRange(a, lo, hi);
|
|
443
|
+
for (let i = lo; i < hi; i++) {
|
|
444
|
+
const v = a[i];
|
|
445
|
+
if (v !== v) throw new RangeError('lite-bisect: NaN at index ' + i);
|
|
446
|
+
if (i > lo && v < a[i - 1]) {
|
|
447
|
+
throw new RangeError('lite-bisect: not sorted at index ' + i + ' (' + String(a[i - 1]) + ' then ' + String(v) + ')');
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `@zakkster/lite-bisect` will be documented here.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [1.0.0] - 2026-11-23
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- 37 exports in `Bisect.js`: 7 kernel families x 5 array types (suffix `F64` Float64Array,
|
|
15
|
+
`F32` Float32Array, `U32` Uint32Array, `U16` Uint16Array, `I32` Int32Array) plus 2 cold asserts.
|
|
16
|
+
- `lowerBoundBranchless*`, `upperBoundBranchless*` (branchless lower / upper bound).
|
|
17
|
+
- `lowerBoundBranchy*`, `upperBoundBranchy*` (branchy lower / upper bound).
|
|
18
|
+
- `lowerBoundHint*` (lower bound galloping out from a hint `h`; for coherent query streams).
|
|
19
|
+
- `segment*` (the last index with `a[i] <= x`; `lo - 1` when none, on NaN, on an empty range).
|
|
20
|
+
- `nearest*` (the closest index; a tie goes to the LOWER index; an exact match wins; an empty range or NaN returns `lo`).
|
|
21
|
+
- `assertRange(a, lo, hi)`, `assertSorted(a, lo, hi)`: cold checks that throw `RangeError`; no kernel calls them.
|
|
22
|
+
- The contract: sorted non-decreasing typed arrays with no NaN, integer ranges
|
|
23
|
+
`0 <= lo <= hi <= a.length` with `hi - lo <= 2^30 - 1`; the hot kernels trust the caller; a NaN query
|
|
24
|
+
returns `lo` (`lo - 1` from `segment`); the hint `h` is an integer index or NaN, and a non-integer `h`
|
|
25
|
+
is a caller error. Node >= 22.
|
|
26
|
+
- The allocation statement: zero allocation where the kernel inlines into its caller, or every number
|
|
27
|
+
argument (the query; the hint of lowerBoundHint*; lo / hi) is a Smi; a non-Smi number computed at run
|
|
28
|
+
time or read from a typed array (a fractional query, a NaN hint read from a Float64Array, -0 for lo / hi)
|
|
29
|
+
and passed across a call V8 does not inline is boxed by the caller: one 16 B HeapNumber per such argument on Node.
|
|
30
|
+
For no hint pass lo or the literal NaN: neither boxes.
|
|
31
|
+
- Hand-written TypeScript declarations (`Bisect.d.ts`), checked by tsc 7.0.2 in node16 / nodenext /
|
|
32
|
+
bundler resolution and by a parity test against the runtime exports; `npm run test:types`.
|
|
33
|
+
- Kernels generated from one template (`scripts/gen.mjs`); `npm run gen:check` verifies the generated region.
|
|
34
|
+
- Gates: a naive-reference oracle per type (4 seeds), a boundary table, mutant kills (106 rows), a torture
|
|
35
|
+
run with a retained-heap gate, c8 coverage at 100% lines / branches, golden parity, a TurboFan inline pin
|
|
36
|
+
(traced per kernel x type, pinned Node), alloc and bytecode gates (`npm run gates` on the devDependency `@zakkster/lite-utils` ^1.1.0, run by the
|
|
37
|
+
ci.yml `gates` job; `npm run gates:local` on a LiteUtils checkout; pinned Node only; not in `npm test`), tsc type checks, a doc-truth test, and a size / pack gate (`npm run size`: the tarball file list and
|
|
38
|
+
committed size budgets; DETERMINISTIC, not in `npm test`). `npm run verify` chains test, torture, coverage,
|
|
39
|
+
size and gates:local, fail-fast.
|
|
40
|
+
- A release A/B (`bench/ab-gate.mjs` in the source repository, hosted ab.yml on ubuntu-latest + macos-latest,
|
|
41
|
+
dispatch-only, once per release; STATISTICAL): 13 gated F64 lanes (hint vs branchless, branchless vs branchy
|
|
42
|
+
read-only and feeds-write); 1.0.0 is gated by run 43145179770 (VERDICT PASS gated=13/13 on AMD EPYC 9V74 x86-64
|
|
43
|
+
and Apple M1 (Virtual) arm64; re-judged 2026-11-23 under AB-G3).
|
|
44
|
+
- Tree-shaking gate under esbuild (`test/Bisect.shake.test.mjs`, in `npm test`); devDependency esbuild ^0.28.2.
|
|
45
|
+
- README, `llms.txt`, MIT `LICENSE.txt`.
|
|
46
|
+
|
|
47
|
+
### Fixed
|
|
48
|
+
|
|
49
|
+
- R-NEAREST: `nearest*` (F64, F32, I32) was wrong on a rounded tie (the two gaps compare equal after
|
|
50
|
+
rounding but are not equal); it now decides the exact rounding error and returns the exactly closer index.
|
|
51
|
+
The body grows 86 -> 189 B, so Maglev is expected not to inline it (189 B >= its 100 B cap; untraced); TurboFan does (inline pin).
|
|
52
|
+
- R-ORACLE: the test oracles (`naiveNearest`, `nearestWith`, `cfNearest`) shared the kernel's rounded
|
|
53
|
+
expression; they now decide with exact integer arithmetic, backed by an exact-reference fuzz with a
|
|
54
|
+
coverage floor of false rounded ties.
|
|
55
|
+
- R-TORTURE-MINOR: the torture now gates steady minor GCs (0) with a pinned 1 MB semi-space
|
|
56
|
+
(`--expose-gc --no-concurrent-recompilation --single-threaded-gc --min-semi-space-size=1 --max-semi-space-size=1`);
|
|
57
|
+
it prints NO VERDICT and exits 2 without the two semi-space flags, and NO VERDICT without --single-threaded-gc
|
|
58
|
+
(concurrent sweeping made the retained term flaky under load); a must-fail 16 B/op plant exits 1.
|
|
59
|
+
A lane whose GC observer is still undrained after a 2000 ms re-settle (lite-gc-profiler settle()'s 200 ms default
|
|
60
|
+
timed out, by elimination; a host stall inferred; nightly 45024298534) but is otherwise ok, or a control neither
|
|
61
|
+
flagged nor drained, reads UNDRAINED and the run NO VERDICT (exit 2), never a pass; a FAIL still
|
|
62
|
+
wins (exit 1); a test-only LITE_BISECT_TORTURE_STALL_MS hook drives the must-fail controls.
|
|
63
|
+
- R-LLMS-SNIPPETS: the `llms.txt` usage results are checked `// -> n` lines; the docs test runs the
|
|
64
|
+
`llms.txt` js fences and fails on a bare `// <number>` result comment in README / `llms.txt`.
|
|
65
|
+
- BIS-neon-1: the nearest oracle (`test/oracle.mjs`) and the mutant gate had no +-Infinity, so a flipped
|
|
66
|
+
`d < u` in `nearestF32` / `nearestF64` (wrong on an exact +Infinity match) and the F64 overflow guard removed
|
|
67
|
+
survived. The oracle now answers F64 / F32 infinity groups by its own rule; three mutant rows
|
|
68
|
+
(`nearest.inf-gt.F64`, `nearest.inf-gt.F32`, `nearest.au-r1a.F64`; 106 rows) are KILLED;
|
|
69
|
+
`test/Bisect.nearest-exact.test.mjs` runs its Infinity rows on F32 too. Test-only: no kernel byte changed.
|
|
70
|
+
- BIS-neon-2: `bench/ab-gate.mjs` (the ab.yml release verdict) read VERDICT PASS on a negative bop cell, a NaN
|
|
71
|
+
`aa.q1` / `aa.q3` / `slow.q3`, a tampered HEADER `NQ2` / `minWindow`, and a dropped or doubled `PROV start`
|
|
72
|
+
line. It now needs finite medians and quartiles, every bop cell `>= 0`, `NQ2` and `minWindow` equal to the
|
|
73
|
+
gate's own, and exactly one `PROV start`; `--selftest` with `--K` is refused. Fixture controls F37-F45.
|
|
74
|
+
- BIS-neon-3: the alloc gate's empty-lane harness check had no floor; a negative window now reads NO VERDICT
|
|
75
|
+
("outside [0, 1] B/op").
|
|
76
|
+
- BIS-neon-4: `test/gates/lu.mjs` (the LiteUtils tool resolver) now checks the package name (env dir and
|
|
77
|
+
installed), requires a `LITE_UTILS_DIR` to be its own git top level (a parent repo's commit is refused),
|
|
78
|
+
compares real paths in its CLI guard (a symlinked path no longer skips the CLI), and runs git without
|
|
79
|
+
`GIT_DIR` / `GIT_INDEX_FILE` / `GIT_WORK_TREE`.
|
|
80
|
+
- BIS-neon-5: the doc-truth test (`test/Bisect.docs.test.mjs`) failed open: a `.md` cited with a `./`, `../` or
|
|
81
|
+
`dir/` prefix was invisible; the pointer sentence excused every unshipped `.md`; README and `llms.txt` speed
|
|
82
|
+
words were unchecked; and a following block could lend its tier word. Cited paths are now checked as written
|
|
83
|
+
against `files[]`; the pointer sentence excuses only the LEDGER and RESEARCH files; README and `llms.txt` join
|
|
84
|
+
the speed check, which splits at sentences, paragraphs, JSDoc open / close, list items and table cells and
|
|
85
|
+
exempts only a quoted, negated speed word naming a statement (`no "faster than" statement`). Three README / `llms.txt` speed sentences that
|
|
86
|
+
took their tier from the next sentence now carry it themselves (same evidence, PRELIMINARY; the two hint sentences are STATISTICAL since R-README-TIERS).
|
|
87
|
+
- BIS-neon-7: `package.json` had no `repository` while README and `llms.txt` point to the source repository:
|
|
88
|
+
`repository` added (NEON-R2), and a docs row requires it whenever a shipped doc holds the pointer sentence.
|
|
89
|
+
README offered `lowerBoundHintF64` as the hint for the `segmentF64` playhead; it answers a lower bound, not a
|
|
90
|
+
segment (checked lines added; there is no `segmentHint*` export). README counts and provenance refreshed from
|
|
91
|
+
this cycle's runs.
|
|
92
|
+
- BIS-neon-8: `assertRange` / `assertSorted` throw RangeError on a non-number or unsafe `a.length` and on unsafe
|
|
93
|
+
lo / hi; every call on the five typed arrays keeps its result and message (parity). `gen.mjs` refuses
|
|
94
|
+
trailing text after the end marker.
|
|
95
|
+
- ABOBS: a failing `bench/ab-gate.mjs --selftest` row printed only the child's last 8 lines, so a red selftest
|
|
96
|
+
in ab.yml lost the child's RESULT / RAW lines (macOS run 45079558712, the red itself still unexplained). The selftest now
|
|
97
|
+
replays a failing child's RESULT / RAW / VOID / ORACLE / STOP / CRASH lines from before those 8 lines, then the same 8
|
|
98
|
+
lines; a first selftest row proves it on a stub output. No verdict, word or threshold changed; no kernel byte changed.
|
|
99
|
+
|
|
100
|
+
### Changed
|
|
101
|
+
|
|
102
|
+
- R-NANHINT-BOX: the allocation sentence now names every number argument and says a non-Smi number
|
|
103
|
+
computed at run time or read from a typed array (a NaN hint from a Float64Array) is boxed by the
|
|
104
|
+
caller; for no hint pass `lo` or the literal `NaN`. Alloc lanes `lowerBoundHint*.nanhint` and `.nanlit`.
|
|
105
|
+
- R-RESEARCH-REF: README and `llms.txt` say LEDGER and RESEARCH ids refer to files in the source
|
|
106
|
+
repository, not the npm tarball; a docs row checks every cited `<NAME>.md`.
|
|
107
|
+
- R-DTS-TIER: the `lowerBoundHint*` declarations say the incoherent-stream slowdown is STATISTICAL for F64
|
|
108
|
+
(ab.yml run 43145179770, two CPUs, n <= 65,536), no longer PRELIMINARY; a docs row fails an untiered "slower" / "faster" in `Bisect.d.ts`.
|
|
109
|
+
- R-README-TIERS: the choice-table rows for branchless / branchy / hint carry STATISTICAL words from ab.yml run
|
|
110
|
+
43145179770 (both CPUs, F64; branchy n <= 128, the others n <= 65,536); the branchy row moved from INHERITED
|
|
111
|
+
to STATISTICAL (Q-AB-FEEDSWRITE).
|
|
112
|
+
- NEON-R1 (BIS-neon-6; amends AU-R3 / R-NANHINT-BOX): the allocation sentence said "one 16 B HeapNumber per
|
|
113
|
+
call"; two non-Smi arguments across a call V8 does not inline box 2 x 16 B, so it now says "one 16 B
|
|
114
|
+
HeapNumber per such argument on Node" in `Bisect.js`, `Bisect.d.ts`, README, `llms.txt` and the docs-test
|
|
115
|
+
literal; README's allocation table says one box per non-Smi argument and gains a two-argument row. Gated by
|
|
116
|
+
the alloc lanes `lowerBoundHint*.fracnanhint` (fractional query and NaN hint, both read from Float64Arrays):
|
|
117
|
+
run B 28..36 B/op, run A the floor.
|
|
118
|
+
|
|
119
|
+
### Not shipped from the draft
|
|
120
|
+
|
|
121
|
+
- `lowerBound` (the length switch), `LINEAR_MAX` and `lowerBoundLinear`: refused (LEDGER A4); the switch
|
|
122
|
+
never read faster and read slower at every small length measured (PRELIMINARY: one Apple M4 Pro, one strict run, P0 2026-11-10; no number is claimed).
|
|
123
|
+
- `buildEytzinger` and `lowerBoundEytzinger`: parked (LEDGER A6); never measured on this package.
|
|
124
|
+
- No selector and no per-call type front that picks a kernel (LEDGER A3, A5): you call the per-type
|
|
125
|
+
kernel by name.
|
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Zahary Shinikchiev <shinikchiev@yahoo.com>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|