@amritk/nish-aarch64-linux 0.45.1 → 0.47.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/std/math.ts CHANGED
@@ -77,7 +77,10 @@
77
77
  // Word access, as fdlibm's GET_HIGH_WORD and GET_LOW_WORD.
78
78
  const hiWord = (x: f64): i32 => toI32(f64ToBits(x) >> toI64(32))
79
79
  const loWord = (x: f64): u32 => toU32(toU64(f64ToBits(x)))
80
- const pow2 = (n: i32): f64 => bitsToF64(toI64(n + 1023) << toI64(52))
80
+ // 2^(biased - 1023) for a biased exponent from 1 to 2046, the double whose exponent field is `biased`.
81
+ const exp2Bits = (biased: i64): f64 => bitsToF64(biased << toI64(52))
82
+ // 2^n for n from -1022 to 1023, the exponent sum taken in i64 where it is exact.
83
+ const pow2 = (n: i32): f64 => exp2Bits(toI64(n) + toI64(1023))
81
84
 
82
85
  // biome-ignore lint/suspicious/noSelfCompare: NaN is the only value that is not itself; Nish has no Number.isNaN
83
86
  const isNan = (v: f64): boolean => v !== v
@@ -227,7 +230,7 @@ const ipio2 = (i: i32): i32 => {
227
230
  // Integer Payne-Hanek: 64 bits of 2/pi starting at fraction bit p (b_1 first).
228
231
  const pio2Word = (p: i32): u64 => {
229
232
  if (p < 1) {
230
- return pio2Word(1) >> toU64(1 - p)
233
+ return pio2Word(1) >> toU64(toI64(1) - toI64(p))
231
234
  }
232
235
  const j: i32 = (p - 1) / 24
233
236
  const off: i32 = p - 1 - j * 24
@@ -237,7 +240,8 @@ const pio2Word = (p: i32): u64 => {
237
240
  const c3: u64 = toU64(ipio2(j + 3))
238
241
  const top: u64 = (c0 << toU64(40)) | (c1 << toU64(16)) | (c2 >> toU64(8))
239
242
  const low: u64 = ((c2 & toU64(255)) << toU64(24)) | c3
240
- return (top << toU64(off)) | (low >> toU64(32 - off))
243
+ // 32 - off is taken in i64, where it cannot wrap.
244
+ return (top << toU64(off)) | (low >> toU64(toI64(32) - toI64(off)))
241
245
  }
242
246
  // The high 64 bits of a * b.
243
247
  const mulHi = (a: u64, b: u64): u64 => {
@@ -254,14 +258,17 @@ const mulHi = (a: u64, b: u64): u64 => {
254
258
  }
255
259
  const clz64 = (v: u64): i32 => {
256
260
  const h: u32 = toU32(v >> toU64(32))
257
- return h !== toU32(0) ? Math.clz32(h) : Math.clz32(toU32(v)) + 32
261
+ if (h !== toU32(0)) {
262
+ return Math.clz32(h)
263
+ }
264
+ return Math.clz32(toU32(v)) + 32
258
265
  }
259
266
  // The trigonometric function `mode` names (0 tan, 1 sin, 2 cos) of x,
260
267
  // for |x| >= 2^20 * pi/2 (finite): x * 2/pi mod 4 from a 192-bit window.
261
268
  const trigLarge = (x: f64, ix: i32, hx: i32, mode: i32): f64 => {
262
- const e: i32 = (ix >> 20) - 1023
263
269
  const m: u64 = (toU64(f64ToBits(x)) & ((toU64(1) << toU64(52)) - toU64(1))) | (toU64(1) << toU64(52))
264
- const p: i32 = e - 53
270
+ // The words start at fraction bit p = e - 53, for the unbiased exponent e = (ix >> 20) - 1023.
271
+ const p: i32 = (ix >> 20) - 1076
265
272
  const w0: u64 = pio2Word(p)
266
273
  const w1: u64 = pio2Word(p + 64)
267
274
  const w2: u64 = pio2Word(p + 128)
@@ -275,10 +282,13 @@ const trigLarge = (x: f64, ix: i32, hx: i32, mode: i32): f64 => {
275
282
  const p2: u64 = hi1 + m * w0 + c
276
283
  // Integer part mod 4 is the top two bits; the fraction is the rest, as a
277
284
  // signed 192-bit number q2:q1:q0 in [-1/2, 1/2).
278
- let n: i32 = toI32(p2 >> toU64(62))
279
285
  let q2: u64 = (p2 << toU64(2)) | (p1 >> toU64(62))
280
286
  let q1: u64 = (p1 << toU64(2)) | (p0 >> toU64(62))
281
287
  let q0: u64 = p0 << toU64(2)
288
+ // The quadrant is the top two bits, from 0 to 3 (the & 3 drops no bit of a
289
+ // u64 >> 62; it states the range); a negative fraction rounds it up one, to
290
+ // the nearer multiple of pi/2.
291
+ let n: i32 = toI32(p2 >> toU64(62)) & 3
282
292
  let neg: boolean = false
283
293
  if (q2 >> toU64(63) !== toU64(0)) {
284
294
  n += 1
@@ -304,8 +314,10 @@ const trigLarge = (x: f64, ix: i32, hx: i32, mode: i32): f64 => {
304
314
  q1 = (q1 << toU64(lz)) | (q0 >> toU64(64 - lz))
305
315
  }
306
316
  // f = (q2 * 2^-64 + q1 * 2^-128) * 2^-(sh + lz)
307
- const a: f64 = toF64(q2 >> toU64(11)) * pow2(-53 - sh - lz)
308
- const b: f64 = toF64(((q2 & toU64(2047)) << toU64(53)) | (q1 >> toU64(11))) * pow2(-117 - sh - lz)
317
+ const a: f64 = toF64(q2 >> toU64(11)) * exp2Bits(toI64(970) - toI64(sh) - toI64(lz))
318
+ const b: f64 =
319
+ toF64(((q2 & toU64(2047)) << toU64(53)) | (q1 >> toU64(11))) *
320
+ exp2Bits(toI64(906) - toI64(sh) - toI64(lz))
309
321
  // r = (a + b) * pi/2 as y0 + y1.
310
322
  const p1h: f64 = 1.570796326794896558
311
323
  const p1l: f64 = 6.12323399573676603587e-17
@@ -325,8 +337,8 @@ const trigLarge = (x: f64, ix: i32, hx: i32, mode: i32): f64 => {
325
337
  y0 = -y0
326
338
  y1 = -y1
327
339
  }
328
- // x * 2/pi is -(n + f) for a negative x, so its quadrant is -n.
329
- return trigKernel(y0, y1, hx < 0 ? -n : n, mode)
340
+ // x * 2/pi is -(n + f) for a negative x, so its quadrant is -n, which is 4 - n mod 4.
341
+ return trigKernel(y0, y1, hx < 0 ? 4 - n : n, mode)
330
342
  }
331
343
 
332
344
  // fdlibm's __kernel_tan: tan(x0 + y0) for |x0 + y0| <= pi/4, or -1/tan when `odd`.
@@ -621,9 +633,11 @@ export const atan2F64 = (y: f64, x: f64): f64 => {
621
633
  let a: f64 = Math.abs(y)
622
634
  let b: f64 = Math.abs(x)
623
635
  if (ix < 0x23d00000 || ix > 0x5c100000) {
624
- const h: i32 = -((ix >> 20) - 1023)
625
- const h1: i32 = h >> 1
626
- const s: f64 = pow2(h1) * pow2(h - h1)
636
+ // 2^h for h = 1023 - (ix >> 20), as 2^h1 * 2^(h - h1) with h1 = h >> 1
637
+ // so neither factor overflows; the second exponent, h - h1 biased by
638
+ // 1023, is summed in i64.
639
+ const h1: i32 = -((ix >> 20) - 1023) >> 1
640
+ const s: f64 = pow2(h1) * exp2Bits(toI64(2046) - toI64(ix >> 20) - toI64(h1))
627
641
  a = a * s
628
642
  b = b * s
629
643
  }
@@ -759,8 +773,7 @@ export const hypotF64 = (x: f64, y: f64): f64 => {
759
773
  }
760
774
 
761
775
  // fdlibm's SET_HIGH_WORD and SET_LOW_WORD.
762
- const withHi = (x: f64, hi: i32): f64 =>
763
- bitsToF64((toI64(hi) << toI64(32)) | (f64ToBits(x) & ((toI64(1) << toI64(32)) - toI64(1))))
776
+ const withHi = (x: f64, hi: i32): f64 => bitsToF64((toI64(hi) << toI64(32)) | toI64(loWord(x)))
764
777
  const withLo = (x: f64, lo: u32): f64 => bitsToF64((f64ToBits(x) & (toI64(-1) << toI64(32))) | toI64(lo))
765
778
 
766
779
  // The rational approximation e_exp.c and e_pow.c share: P1 + t * (P2 + ... + t * P5).
@@ -833,7 +846,7 @@ export const expF64 = (x: f64): f64 => {
833
846
  }
834
847
  return y * pow2(k)
835
848
  }
836
- return y * pow2(k + 1000) * 9.332636185032189e-302
849
+ return y * exp2Bits(toI64(k) + toI64(2023)) * 9.332636185032189e-302
837
850
  }
838
851
 
839
852
  /**
@@ -847,7 +860,8 @@ export const expF64 = (x: f64): f64 => {
847
860
  export const logF64 = (x: f64): f64 => {
848
861
  let v: f64 = x
849
862
  let hx: i32 = hiWord(v)
850
- let k: i32 = 0
863
+ // A subnormal x is scaled by 2^54 first, and its exponent k takes the 54 back.
864
+ let scaled: boolean = false
851
865
  if (hx < 0x00100000) {
852
866
  if (((hx & 0x7fffffff) | toI32(loWord(v))) === 0) {
853
867
  return -overflow(1.0)
@@ -855,18 +869,21 @@ export const logF64 = (x: f64): f64 => {
855
869
  if (hx < 0) {
856
870
  return (v - v) / (v - v)
857
871
  }
858
- k = -54
872
+ scaled = true
859
873
  v = v * 1.8014398509481984e16
860
874
  hx = hiWord(v)
861
875
  }
862
876
  if (hx >= 0x7ff00000) {
863
877
  return v + v
864
878
  }
865
- k += (hx >> 20) - 1023
879
+ // hx is not negative here, so this is its exponent field.
880
+ const field: i32 = hx >>> 20
866
881
  hx = hx & 0x000fffff
867
882
  const i: i32 = (hx + 0x95f64) & 0x100000
868
883
  v = withHi(v, hx | (i ^ 0x3ff00000))
869
- k += i >> 20
884
+ // The exponent field, one more when the fraction is halved to put 1 + f near 1.
885
+ const e: i32 = field + (i >> 20)
886
+ const k: i32 = scaled ? e - 1077 : e - 1023
870
887
  const f: f64 = v - 1.0
871
888
  const dk: f64 = toF64(k)
872
889
  if (((hx + 2) & 0x000fffff) < 3) {
@@ -902,7 +919,7 @@ export const logF64 = (x: f64): f64 => {
902
919
  }
903
920
 
904
921
  // 2^n * z for a z near 1 whose product is subnormal, as fdlibm's scalbn, rounded once.
905
- const scaleDown = (z: f64, n: i32): f64 => z * pow2(-1000) * pow2(n + 1000)
922
+ const scaleDown = (z: f64, n: i32): f64 => z * pow2(-1000) * exp2Bits(toI64(n) + toI64(2023))
906
923
 
907
924
  // pow's |y| > 2^31 case when x is not within 2^-20 of 1: an overflow or an underflow.
908
925
  const powHugeY = (ix: i32, hy: i32, iy: i32, s: f64): f64 => {
@@ -924,29 +941,29 @@ const powExp2 = (ph0: f64, pl: f64, s: f64): f64 => {
924
941
  const z0: f64 = pl + ph
925
942
  const j: i32 = hiWord(z0)
926
943
  const i: i32 = toI32(loWord(z0))
927
- if (j >= 0x40900000) {
928
- if (((j - 0x40900000) | i) !== 0 || pl + 8.0085662595372944372e-17 > z0 - ph) {
929
- return overflow(s)
930
- }
931
- } else if ((j & 0x7fffffff) >= 0x4090cc00) {
932
- // 0xc090cc00, the high word of -1075, as an i32.
933
- if (j !== -1064252416 || i !== 0 || pl <= z0 - ph) {
934
- return underflow(s)
935
- }
936
- }
937
944
  const ij: i32 = j & 0x7fffffff
938
- let k: i32 = (ij >> 20) - 0x3ff
945
+ // z0 at least 1024 overflows, but for exactly 1024 that rounds down; z0 at
946
+ // most -1075 underflows, but for exactly -1075 that rounds up.
947
+ if (j > 0x40900000) {
948
+ return overflow(s)
949
+ }
950
+ if (j === 0x40900000 && (i !== 0 || pl + 8.0085662595372944372e-17 > z0 - ph)) {
951
+ return overflow(s)
952
+ }
953
+ if (ij > 0x4090cc00) {
954
+ return underflow(s)
955
+ }
956
+ // 0xc090cc00, the high word of -1075, as an i32.
957
+ if (j === -1064252416 && (i !== 0 || pl <= z0 - ph)) {
958
+ return underflow(s)
959
+ }
939
960
  let n: i32 = 0
940
961
  if (ij > 0x3fe00000) {
941
- const unit: i32 = 0x00100000
942
- n = j + (unit >> (k + 1))
943
- k = ((n & 0x7fffffff) >> 20) - 0x3ff
944
- const t: f64 = withHi(0.0, n & ~(0x000fffff >> k))
945
- n = ((n & 0x000fffff) | 0x00100000) >> (20 - k)
946
- if (j < 0) {
947
- n = -n
948
- }
949
- ph = ph - t
962
+ // z0 rounded half away from zero, from its high word as fdlibm does: exact,
963
+ // as |z0| is below 1076 and its high word keeps 20 fraction bits.
964
+ const zh: f64 = withLo(z0, toU32(0))
965
+ n = toI32(j < 0 ? zh - 0.5 : zh + 0.5)
966
+ ph = ph - toF64(n)
950
967
  }
951
968
  const t: f64 = withLo(pl + ph, toU32(0))
952
969
  const u: f64 = t * 6.93147182464599609375e-1
@@ -957,11 +974,14 @@ const powExp2 = (ph0: f64, pl: f64, s: f64): f64 => {
957
974
  const r: f64 = (z * t1) / (t1 - 2.0) - (w + z * w)
958
975
  const one: f64 = 1.0
959
976
  z = one - (r - z)
960
- const hz: i32 = hiWord(z) + (n << 20)
961
- if (hz >> 20 <= 0) {
977
+ // z's exponent field raised by n, in i64, where the sum of two i32s is
978
+ // exact; z is positive, so its high word's sign bit is clear.
979
+ const hi: i32 = hiWord(z)
980
+ const ze: i64 = toI64(hi >> 20) + toI64(n)
981
+ if (ze <= toI64(0)) {
962
982
  return s * scaleDown(z, n)
963
983
  }
964
- return s * withHi(z, hz)
984
+ return s * bitsToF64(((toI64(hi & 0x000fffff) | (ze << toI64(20))) << toI64(32)) | toI64(loWord(z)))
965
985
  }
966
986
 
967
987
  /**
@@ -1000,15 +1020,16 @@ export const powF64 = (x: f64, y: f64): f64 => {
1000
1020
  if (iy >= 0x43400000) {
1001
1021
  yisint = 2
1002
1022
  } else if (iy >= 0x3ff00000) {
1003
- const k: i32 = (iy >> 20) - 0x3ff
1004
- if (k > 20) {
1005
- const j: u32 = ly >> toU32(52 - k)
1006
- if (j << toU32(52 - k) === ly) {
1023
+ // iy's biased exponent; 52 - k and 20 - k below are 0x433 - e and 0x413 - e.
1024
+ const e: i32 = iy >>> 20
1025
+ if (e > 0x413) {
1026
+ const j: u32 = ly >> toU32(0x433 - e)
1027
+ if (j << toU32(0x433 - e) === ly) {
1007
1028
  yisint = 2 - toI32(j & toU32(1))
1008
1029
  }
1009
1030
  } else if (ly === toU32(0)) {
1010
- const j: i32 = iy >> (20 - k)
1011
- if (j << (20 - k) === iy) {
1031
+ const j: i32 = iy >> (0x413 - e)
1032
+ if (j << (0x413 - e) === iy) {
1012
1033
  const odd: i32 = j & 1
1013
1034
  yisint = 2 - odd
1014
1035
  }
@@ -1061,26 +1082,18 @@ export const powF64 = (x: f64, y: f64): f64 => {
1061
1082
  t1 = withLo(u + v, toU32(0))
1062
1083
  t2 = v - (t1 - u)
1063
1084
  } else {
1064
- let n: i32 = 0
1065
- let jx: i32 = ix
1066
- if (jx < 0x00100000) {
1085
+ // A subnormal x is scaled by 2^53 first, and its exponent lowered by 53.
1086
+ const subnormal: boolean = ix < 0x00100000
1087
+ if (subnormal) {
1067
1088
  ax = ax * 9007199254740992.0
1068
- n = -53
1069
- jx = hiWord(ax)
1070
1089
  }
1071
- n += (jx >> 20) - 0x3ff
1072
- const j: i32 = jx & 0x000fffff
1073
- jx = j | 0x3ff00000
1074
- let k: i32 = 0
1075
- if (j <= 0x3988e) {
1076
- k = 0
1077
- } else if (j < 0xbb67a) {
1078
- k = 1
1079
- } else {
1080
- n += 1
1081
- jx -= 0x00100000
1082
- }
1083
- ax = withHi(ax, jx)
1090
+ const hix: i32 = subnormal ? hiWord(ax) : ix
1091
+ const j: i32 = hix & 0x000fffff
1092
+ // A mantissa at least sqrt(3) is halved, and its exponent raised by one.
1093
+ const big: boolean = j >= 0xbb67a
1094
+ const k: i32 = j > 0x3988e && !big ? 1 : 0
1095
+ const dn: f64 = toF64((hix >>> 20) - 0x3ff) - (subnormal ? 53.0 : 0.0) + (big ? 1.0 : 0.0)
1096
+ ax = withHi(ax, j | (big ? 0x3fe00000 : 0x3ff00000))
1084
1097
  const bp: f64 = k === 0 ? 1.0 : 1.5
1085
1098
  const dph: f64 = k === 0 ? 0.0 : 5.84962487220764160156e-1
1086
1099
  const dpl: f64 = k === 0 ? 0.0 : 1.35003920212974897128e-8
@@ -1089,7 +1102,13 @@ export const powF64 = (x: f64, y: f64): f64 => {
1089
1102
  const v: f64 = one / (ax + bp)
1090
1103
  const ss: f64 = u * v
1091
1104
  const sh: f64 = withLo(ss, toU32(0))
1092
- let th: f64 = withHi(0.0, ((jx >> 1) | 0x20000000) + 0x00080000 + (k << 18))
1105
+ // fdlibm's th, whose high word is 0x3ff80000 + (j >> 1) when ax was halved;
1106
+ // otherwise thn, of high word 0x40000000 + (j >> 1), when bp is 1, and
1107
+ // thn + 0.5 (high word 0x40040000 + (j >> 1)) when bp is 1.5.
1108
+ const half: i32 = j >>> 1
1109
+ const thn: f64 = withHi(0.0, half + 0x40000000)
1110
+ const thSmall: f64 = k === 0 ? thn : thn + 0.5
1111
+ let th: f64 = big ? withHi(0.0, half + 0x3ff80000) : thSmall
1093
1112
  let tl: f64 = ax - (th - bp)
1094
1113
  const sl: f64 = v * (u - sh * th - sh * tl)
1095
1114
  let s2: f64 = ss * ss
@@ -1109,7 +1128,7 @@ export const powF64 = (x: f64, y: f64): f64 => {
1109
1128
  const pl: f64 = pv - (ph - pu)
1110
1129
  const zh: f64 = ph * 9.61796700954437255859e-1
1111
1130
  const zl: f64 = ph * -7.02846165095275826516e-9 + pl * 9.61796693925975554329e-1 + dpl
1112
- const t: f64 = toF64(n)
1131
+ const t: f64 = dn
1113
1132
  t1 = withLo(zh + zl + dph + t, toU32(0))
1114
1133
  t2 = zl - (t1 - t - dph - zh)
1115
1134
  }
@@ -1131,8 +1150,8 @@ export const powF64 = (x: f64, y: f64): f64 => {
1131
1150
  export const fmodF64 = (x: f64, y: f64): f64 => {
1132
1151
  let ux: u64 = toU64(f64ToBits(x))
1133
1152
  let uy: u64 = toU64(f64ToBits(y))
1134
- let ex: i32 = toI32((ux >> toU64(52)) & toU64(0x7ff))
1135
- let ey: i32 = toI32((uy >> toU64(52)) & toU64(0x7ff))
1153
+ const ex: i32 = toI32((ux >> toU64(52)) & toU64(0x7ff))
1154
+ const ey: i32 = toI32((uy >> toU64(52)) & toU64(0x7ff))
1136
1155
  const sign: u64 = ux & (toU64(1) << toU64(63))
1137
1156
  if (uy << toU64(1) === toU64(0) || isNan(y) || ex === 0x7ff) {
1138
1157
  return (x * y) / (x * y)
@@ -1142,52 +1161,42 @@ export const fmodF64 = (x: f64, y: f64): f64 => {
1142
1161
  }
1143
1162
  const mant: u64 = (toU64(1) << toU64(52)) - toU64(1)
1144
1163
  const implicit: u64 = toU64(1) << toU64(52)
1145
- // Each significand to an integer with its leading one at bit 52, the exponent lowered
1146
- // past zero for a subnormal.
1147
- if (ex === 0) {
1148
- let i: u64 = ux << toU64(12)
1149
- while (i >> toU64(63) === toU64(0)) {
1150
- ex -= 1
1151
- i = i << toU64(1)
1152
- }
1153
- ux = ux << toU64(1 - ex)
1154
- } else {
1155
- ux = (ux & mant) | implicit
1156
- }
1157
- if (ey === 0) {
1158
- let i: u64 = uy << toU64(12)
1159
- while (i >> toU64(63) === toU64(0)) {
1160
- ey -= 1
1161
- i = i << toU64(1)
1162
- }
1163
- uy = uy << toU64(1 - ey)
1164
- } else {
1165
- uy = (uy & mant) | implicit
1166
- }
1167
- while (ex > ey) {
1168
- if (ux >= uy) {
1169
- if (ux === uy) {
1170
- return 0.0 * x
1171
- }
1172
- ux = ux - uy
1173
- }
1174
- ux = ux << toU64(1)
1175
- ex -= 1
1164
+ // Each significand to an integer with its leading one at bit 52. A
1165
+ // subnormal's is shifted up past its leading zeros, lz of them, and its
1166
+ // exponent taken as -lz. Both are nonzero here.
1167
+ const lzx: i32 = ex === 0 ? clz64(ux << toU64(12)) : 0
1168
+ const lzy: i32 = ey === 0 ? clz64(uy << toU64(12)) : 0
1169
+ ux = ex === 0 ? (ux << toU64(lzx)) << toU64(1) : (ux & mant) | implicit
1170
+ uy = ey === 0 ? (uy << toU64(lzy)) << toU64(1) : (uy & mant) | implicit
1171
+ // The exponents' difference, at least 0 as |x| > |y|. The exponent sums are
1172
+ // of integers below 2^12, taken in f64, where they are exact.
1173
+ const steps: i32 = toI32(toF64(ex) - toF64(lzx) - (toF64(ey) - toF64(lzy)))
1174
+ // Long division one bit a step, down to y's exponent. A remainder that
1175
+ // reaches zero stays zero, so that is checked once, after.
1176
+ let step: i32 = 0
1177
+ while (step < steps) {
1178
+ ux = (ux >= uy ? ux - uy : ux) << toU64(1)
1179
+ step = step + 1
1176
1180
  }
1177
1181
  if (ux >= uy) {
1178
- if (ux === uy) {
1179
- return 0.0 * x
1180
- }
1181
1182
  ux = ux - uy
1182
1183
  }
1183
- while (ux >> toU64(52) === toU64(0)) {
1184
+ if (ux === toU64(0)) {
1185
+ return 0.0 * x
1186
+ }
1187
+ // The remainder, nonzero and below 2^53, back to its leading one at bit 52,
1188
+ // and its exponent lowered by as much: up is at most 52, which the first
1189
+ // compare says to the prover.
1190
+ let up: i32 = 0
1191
+ while (up < 53 && ux >> toU64(52) === toU64(0)) {
1184
1192
  ux = ux << toU64(1)
1185
- ex -= 1
1193
+ up = up + 1
1186
1194
  }
1187
- if (ex > 0) {
1188
- ux = (ux - implicit) | (toU64(ex) << toU64(52))
1195
+ const e: f64 = toF64(ey) - toF64(lzy) - toF64(up)
1196
+ if (e > 0.0) {
1197
+ ux = (ux - implicit) | (toU64(toI32(e)) << toU64(52))
1189
1198
  } else {
1190
- ux = ux >> toU64(1 - ex)
1199
+ ux = ux >> toU64(toI32(1.0 - e))
1191
1200
  }
1192
1201
  return bitsToF64(toI64(ux | sign))
1193
1202
  }
@@ -1254,13 +1263,15 @@ const hexEnd = (s: string, at: i32): i32 => {
1254
1263
  // bit, so a hex integer of any length rounds once, as a correct strtod does.
1255
1264
  const hexValue = (s: string, start: i32, end: i32): f64 => {
1256
1265
  let m: u64 = toU64(0)
1257
- let scale: i32 = 0
1266
+ // The power of two the kept digits are scaled by: four for each digit
1267
+ // dropped, fewer than 2^33 in all, so the f64 sums are exact.
1268
+ let scale: f64 = 0.0
1258
1269
  let sticky: boolean = false
1259
1270
  let k: i32 = start
1260
1271
  while (k >= 0 && k < end && k < toI32(s.length)) {
1261
1272
  const d: i32 = hexDigit(toI32(s.charCodeAt(k)))
1262
1273
  if (m >> toU64(60) !== toU64(0)) {
1263
- scale = scale + 4
1274
+ scale = scale + 4.0
1264
1275
  sticky = sticky || d !== 0
1265
1276
  } else {
1266
1277
  m = (m << toU64(4)) | toU64(d)
@@ -1276,20 +1287,39 @@ const hexValue = (s: string, start: i32, end: i32): f64 => {
1276
1287
  if (low > half || (low === half && (sticky || (m & toU64(1)) !== toU64(0)))) {
1277
1288
  m = m + toU64(1)
1278
1289
  }
1279
- scale = scale + shift
1290
+ scale = scale + toF64(shift)
1280
1291
  }
1281
1292
  // At most 2^53, so exact; each doubling is exact until it overflows.
1282
1293
  let v: f64 = toF64(m)
1283
- while (scale > 0 && v <= 1.7976931348623157e308) {
1294
+ while (scale > 0.0 && v <= 1.7976931348623157e308) {
1284
1295
  v = v * 2.0
1285
- scale = scale - 1
1296
+ scale = scale - 1.0
1286
1297
  }
1287
1298
  return v
1288
1299
  }
1289
1300
 
1290
- // Whether `s` holds `0x` or `0X` at `at`.
1291
- const isHexPrefix = (s: string, at: i32): boolean =>
1292
- at + 1 < toI32(s.length) && toI32(s.charCodeAt(at)) === 48 && (toI32(s.charCodeAt(at + 1)) | 32) === 120
1301
+ // Where the digits after a `0x` or `0X` at `at` start, or -1 when `s` holds
1302
+ // neither there.
1303
+ const hexDigitsAt = (s: string, at: i32): i32 => {
1304
+ if (
1305
+ at >= 0 &&
1306
+ at < toI32(s.length) - 1 &&
1307
+ toI32(s.charCodeAt(at)) === 48 &&
1308
+ (toI32(s.charCodeAt(at + 1)) | 32) === 120
1309
+ ) {
1310
+ return at + 2
1311
+ }
1312
+ return -1
1313
+ }
1314
+
1315
+ // Where the number after `s`'s leading whitespace and one optional sign starts.
1316
+ const signEnd = (s: string, p: i32): i32 =>
1317
+ p >= 0 && p < toI32(s.length) && (toI32(s.charCodeAt(p)) === 45 || toI32(s.charCodeAt(p)) === 43)
1318
+ ? p + 1
1319
+ : p
1320
+
1321
+ // Whether a `-` sits at `p`.
1322
+ const isMinus = (s: string, p: i32): boolean => p >= 0 && p < toI32(s.length) && toI32(s.charCodeAt(p)) === 45
1293
1323
 
1294
1324
  /**
1295
1325
  * `Number(s)` of a string under `--deterministic` (WP50 §9): the grammar the
@@ -1304,17 +1334,17 @@ const isHexPrefix = (s: string, at: i32): boolean =>
1304
1334
  */
1305
1335
  export const numberExact = (s: string): f64 => {
1306
1336
  const p: i32 = skipSpaces(s, 0)
1307
- const negative: boolean = p < toI32(s.length) && toI32(s.charCodeAt(p)) === 45
1308
- const q: i32 = p < toI32(s.length) && (negative || toI32(s.charCodeAt(p)) === 43) ? p + 1 : p
1309
- if (!isHexPrefix(s, q)) {
1337
+ const negative: boolean = isMinus(s, p)
1338
+ const h: i32 = hexDigitsAt(s, signEnd(s, p))
1339
+ if (h < 0) {
1310
1340
  return Number(s)
1311
1341
  }
1312
- const end: i32 = hexEnd(s, q + 2)
1313
- if (end === q + 2 || skipSpaces(s, end) !== toI32(s.length)) {
1342
+ const end: i32 = hexEnd(s, h)
1343
+ if (end === h || skipSpaces(s, end) !== toI32(s.length)) {
1314
1344
  const zero: f64 = 0.0
1315
1345
  return zero / zero
1316
1346
  }
1317
- const v: f64 = hexValue(s, q + 2, end)
1347
+ const v: f64 = hexValue(s, h, end)
1318
1348
  return negative ? -v : v
1319
1349
  }
1320
1350
 
@@ -1331,12 +1361,12 @@ export const numberExact = (s: string): f64 => {
1331
1361
  */
1332
1362
  export const parseFloatExact = (s: string): f64 => {
1333
1363
  const p: i32 = skipSpaces(s, 0)
1334
- const negative: boolean = p < toI32(s.length) && toI32(s.charCodeAt(p)) === 45
1335
- const q: i32 = p < toI32(s.length) && (negative || toI32(s.charCodeAt(p)) === 43) ? p + 1 : p
1336
- if (!isHexPrefix(s, q)) {
1364
+ const negative: boolean = isMinus(s, p)
1365
+ const h: i32 = hexDigitsAt(s, signEnd(s, p))
1366
+ if (h < 0) {
1337
1367
  return parseFloat(s)
1338
1368
  }
1339
- const end: i32 = hexEnd(s, q + 2)
1340
- const v: f64 = end === q + 2 ? 0.0 : hexValue(s, q + 2, end)
1369
+ const end: i32 = hexEnd(s, h)
1370
+ const v: f64 = end === h ? 0.0 : hexValue(s, h, end)
1341
1371
  return negative ? -v : v
1342
1372
  }
@@ -124,6 +124,7 @@ import {
124
124
  pollModify,
125
125
  pollRemove,
126
126
  pollWait,
127
+ pollWaitBatch,
127
128
  tcpListenFlags,
128
129
  } from "nish:net"
129
130
  import {
@@ -136,6 +137,8 @@ import {
136
137
  httpContentEncoding,
137
138
  } from "nish/net/http-compress"
138
139
 
140
+ /** `HandlerLimits.maxWaitMs`' most: a second, the interval `bodyTimeout` is checked at. */
141
+ const HANDLER_MAX_WAIT_MS: i32 = 1000
139
142
  /** A core's answer when the call broke its contract: `refusal` says how, and the public method panics with it. */
140
143
  const HANDLER_REFUSED: i32 = -24
141
144
  /** An answer sent, or queued to be sent. */
@@ -222,6 +225,24 @@ export class HandlerLimits {
222
225
  jsonBytes: i32 = 65536
223
226
  /** The most readiness events one `wait` takes. */
224
227
  maxEvents: i32 = 256
228
+ /**
229
+ * The events one `wait` gathers before it returns, once the first is in:
230
+ * above 1, a busy ring server takes its connections' events in fewer,
231
+ * larger batches (`pollWaitBatch`), waiting up to `maxWaitMs` for them. A
232
+ * batch is at most `maxEvents` and at most 64 events, whatever this says.
233
+ * It acts only on a listener `listen` opened with `ring` set, on Linux;
234
+ * elsewhere, and at 1 (the default), `wait` is `pollWait` as before. At
235
+ * least 1.
236
+ */
237
+ minEvents: i32 = 1
238
+ /**
239
+ * The most milliseconds one `wait` waits for `minEvents`, after its first
240
+ * event and on top of its own timeout. It is latency added to the batch's
241
+ * first request, and a timer or other descriptor the handler watches may
242
+ * be heard up to this late. 0 (the default) waits no longer than
243
+ * `pollWait` does. From 0 to 1000.
244
+ */
245
+ maxWaitMs: i32 = 0
225
246
  /**
226
247
  * Milliseconds from a request's head to the end of its body. A request
227
248
  * still reading its body past it has its connection closed: the carrier's
@@ -240,9 +261,10 @@ export class HandlerLimits {
240
261
  maxDecoded: i32 = 1048576
241
262
 
242
263
  /**
243
- * The defaults: 256 slots, a 64 KiB JSON buffer, 256 events a turn, a 30
244
- * second body timeout, a one MiB cap on a decoded body, and the carrier's
245
- * own defaults with the body cap lowered to one MiB. Set a field to tune it.
264
+ * The defaults: 256 slots, a 64 KiB JSON buffer, 256 events a turn and no
265
+ * wait for a batch of them, a 30 second body timeout, a one MiB cap on a
266
+ * decoded body, and the carrier's own defaults with the body cap lowered to
267
+ * one MiB. Set a field to tune it.
246
268
  */
247
269
  constructor() {
248
270
  this.http = new Http1Config()
@@ -816,6 +838,10 @@ export class HttpHandler {
816
838
  private owing: boolean[]
817
839
  /** The slots whose send `flush` still owes, `owedCount` of them. */
818
840
  private owed: i32[]
841
+ /** `HandlerLimits.minEvents`: above 1 with `maxWaitMs` above 0, `wait` calls `pollWaitBatch`. */
842
+ private minEvents: i32 = 1
843
+ /** `HandlerLimits.maxWaitMs`. */
844
+ private maxWaitMs: i32 = 0
819
845
  /** `pollWait`'s output. */
820
846
  private readyFds: i32[]
821
847
  /** Per watched id: whether its descriptor was ready in the last `wait`. */
@@ -890,9 +916,9 @@ export class HttpHandler {
890
916
  * before the first `wait`. `limits.ring` acts only through the listener:
891
917
  * open it with `limits.listen`, since a listener from `tcpListen` stays on
892
918
  * descriptors whatever `ring` says.
893
- * @param limits the caps and timeouts. `slots`, `jsonBytes`, `maxEvents`
894
- * and `bodyTimeout` are read once here, so changing them later has no
895
- * effect; `http` is kept by the carrier, which reads its timeouts and
919
+ * @param limits the caps and timeouts. `slots`, `jsonBytes`, `maxEvents`,
920
+ * `bodyTimeout`, `minEvents` and `maxWaitMs` are read once here, so
921
+ * changing them later has no effect; `http` is kept by the carrier, which reads its timeouts and
896
922
  * caps live
897
923
  * @param listener the listening socket's descriptor, still the program's to close
898
924
  * @param router the routes, which the handler keeps and consults at each request head
@@ -910,6 +936,8 @@ export class HttpHandler {
910
936
  this.maxDecoded = limits.maxDecoded
911
937
  this.decoder = new HttpBodyDecoder(limits.maxDecoded > 0 ? limits.maxDecoded : 1)
912
938
  this.bodyTimeout = toI64(limits.bodyTimeout) * HANDLER_NANOS_PER_MS
939
+ this.minEvents = limits.minEvents
940
+ this.maxWaitMs = limits.maxWaitMs
913
941
  this.server = new Http1Server(limits.http, listener, limits.slots)
914
942
  this.router = router
915
943
  this.listener = listener
@@ -1023,7 +1051,11 @@ export class HttpHandler {
1023
1051
  // is the program's to take, and the turn blocks as it always has.
1024
1052
  timeout = 0
1025
1053
  }
1026
- const n: i32 = pollWait(this.loop, this.readyFds, timeout)
1054
+ // A turn that must not block (`timeout` 0) never waits for a batch.
1055
+ const n: i32 =
1056
+ this.minEvents > 1 && this.maxWaitMs > 0 && timeout !== 0
1057
+ ? pollWaitBatch(this.loop, this.readyFds, timeout, this.minEvents, this.maxWaitMs)
1058
+ : pollWait(this.loop, this.readyFds, timeout)
1027
1059
  const now: i64 = monotonicNanos()
1028
1060
  this.turnAt = now
1029
1061
  const fds: i32[] = this.readyFds
@@ -2635,6 +2667,9 @@ const handlerLimitsFault = (limits: HandlerLimits): string => {
2635
2667
  if (limits.maxDecoded < 0) {
2636
2668
  return `HttpHandler: maxDecoded ${limits.maxDecoded} is negative`
2637
2669
  }
2670
+ if (limits.minEvents < 1 || limits.maxWaitMs < 0 || limits.maxWaitMs > HANDLER_MAX_WAIT_MS) {
2671
+ return `HttpHandler: minEvents ${limits.minEvents} and maxWaitMs ${limits.maxWaitMs} need at least 1 and 0 to ${HANDLER_MAX_WAIT_MS}`
2672
+ }
2638
2673
  return ""
2639
2674
  }
2640
2675