@amritk/nish-aarch64-linux 0.46.0 → 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
  }
@@ -2769,6 +2769,8 @@ export class Http1Server {
2769
2769
  private expireAfter: i64 = 0
2770
2770
  /** How many calls of `expire` walked every slot, for a program measuring its cost. */
2771
2771
  expireScans: i32 = 0
2772
+ /** The instant `tick` gave, in `monotonicNanos`; 0 while the server reads the clock itself. */
2773
+ private ticked: i64 = 0
2772
2774
 
2773
2775
  /**
2774
2776
  * A pool of `size` slots, at least one, serving `config` on `listener`.
@@ -3062,7 +3064,7 @@ export class Http1Server {
3062
3064
  this.armed[k] = H1_WANT_READ
3063
3065
  // The slot starts resting: it is given a connection when it first reads.
3064
3066
  this.at[k] = -1
3065
- this.rested[k] = monotonicNanos()
3067
+ this.rested[k] = this.clock()
3066
3068
  this.owner[k] = this.accepts
3067
3069
  // A serial only keys a kept head to its peer: in u64, which wraps where
3068
3070
  // an i64 would be a site, after more accepts than a server ever sees.
@@ -3074,6 +3076,25 @@ export class Http1Server {
3074
3076
  return k
3075
3077
  }
3076
3078
 
3079
+ /**
3080
+ * Sets the instant, in `monotonicNanos`, that the server stamps activity
3081
+ * with until the next `tick`: a loop that calls it once a pass spares every
3082
+ * `readable` and `flush` of the pass its own clock read, and a timeout,
3083
+ * counted in seconds, moves by the pass's length at most. 0 goes back to
3084
+ * reading the clock at each stamp, which is what a server that is never
3085
+ * ticked does.
3086
+ *
3087
+ * @param now the current time in `monotonicNanos`, or 0
3088
+ */
3089
+ tick(now: i64): void {
3090
+ this.ticked = now
3091
+ }
3092
+
3093
+ /** The instant `tick` gave, or the clock when it gave none. */
3094
+ private clock(): i64 {
3095
+ return this.ticked !== 0 ? this.ticked : monotonicNanos()
3096
+ }
3097
+
3077
3098
  /**
3078
3099
  * The socket of `slot` is readable: reads it while the connection has room
3079
3100
  * and the socket has more, then sends. Answers the interest. It begins a
@@ -3091,13 +3112,16 @@ export class Http1Server {
3091
3112
  this.date.hold()
3092
3113
  const fd: i32 = conn.fd
3093
3114
  let moved: boolean = false
3115
+ const scratch: u8[] = this.scratch
3094
3116
  while (true) {
3095
3117
  const room: i32 = conn.inputRoom()
3096
- const want: i32 = room < toI32(this.scratch.length) ? room : toI32(this.scratch.length)
3097
- if (want <= 0) {
3118
+ const size: i32 = toI32(scratch.length)
3119
+ const want: i32 = room < size ? room : size
3120
+ // `0 < want <= scratch.length`, spelled so the prover reads the window.
3121
+ if (want <= 0 || want > toI32(scratch.length)) {
3098
3122
  break
3099
3123
  }
3100
- const n: i32 = netRead(fd, this.scratch, H1_ZERO, want)
3124
+ const n: i32 = netRead(fd, scratch, 0, want)
3101
3125
  if (n <= 0) {
3102
3126
  if (n === 0) {
3103
3127
  conn.endInput()
@@ -3107,7 +3131,7 @@ export class Http1Server {
3107
3131
  break
3108
3132
  }
3109
3133
  moved = true
3110
- conn.feed(this.scratch, H1_ZERO, n)
3134
+ conn.feed(scratch, H1_ZERO, n)
3111
3135
  // A short read emptied the socket. The loop's readiness is
3112
3136
  // level-triggered, so more input, or the peer's close, wakes it again;
3113
3137
  // reading once more here would only answer -11, at the price of a
@@ -3117,7 +3141,7 @@ export class Http1Server {
3117
3141
  }
3118
3142
  }
3119
3143
  if (moved) {
3120
- const now: i64 = monotonicNanos()
3144
+ const now: i64 = this.clock()
3121
3145
  conn.lastActive = now
3122
3146
  conn.headArrived(now)
3123
3147
  }
@@ -3197,7 +3221,7 @@ export class Http1Server {
3197
3221
  // sends (RFC 9112 §9.6), for `lingerTimeout` and `lingerBytes` at most.
3198
3222
  // Closed with those bytes unread, the socket would answer them with a
3199
3223
  // reset that can destroy the response before the client reads it (H1-2).
3200
- const now: i64 = monotonicNanos()
3224
+ const now: i64 = this.clock()
3201
3225
  const conn: Http1Connection | null = this.attach(slot)
3202
3226
  if (conn === null || !conn.lingerStart(now) || netShutdown(conn.fd, H1_SHUT_WRITE) < 0) {
3203
3227
  return H1_DONE
@@ -3217,13 +3241,14 @@ export class Http1Server {
3217
3241
  */
3218
3242
  private drain(conn: Http1Connection): i32 {
3219
3243
  const fd: i32 = conn.fd
3244
+ const scratch: u8[] = this.scratch
3220
3245
  while (true) {
3221
- const size: i32 = toI32(this.scratch.length)
3246
+ const size: i32 = toI32(scratch.length)
3222
3247
  // `0 <= 0 <= size <= scratch.length`: a scratch with no room drains nothing.
3223
3248
  if (size <= 0) {
3224
3249
  return H1_DONE
3225
3250
  }
3226
- const n: i32 = netRead(fd, this.scratch, H1_ZERO, size)
3251
+ const n: i32 = netRead(fd, scratch, 0, size)
3227
3252
  if (n === H1_AGAIN) {
3228
3253
  return H1_WANT_READ
3229
3254
  }
@@ -3279,7 +3304,7 @@ export class Http1Server {
3279
3304
  conn.abort()
3280
3305
  break
3281
3306
  }
3282
- const n: i32 = netWrite(fd, conn.output, start, http1ServerSub(end, start))
3307
+ const n: i32 = netWrite(fd, conn.output, start, end - start)
3283
3308
  if (n <= 0) {
3284
3309
  if (n !== H1_AGAIN) {
3285
3310
  conn.abort()
@@ -3289,7 +3314,7 @@ export class Http1Server {
3289
3314
  conn.consume(n)
3290
3315
  }
3291
3316
  if (http1ServerSub(conn.outputEnd, conn.outputStart) < pending) {
3292
- conn.lastActive = monotonicNanos()
3317
+ conn.lastActive = this.clock()
3293
3318
  }
3294
3319
  conn.release()
3295
3320
  }
@@ -1166,8 +1166,7 @@ const preforkInvalid = (config: PreforkConfig): string => {
1166
1166
 
1167
1167
  /** Read and drop what a channel holds: `preforkWith`'s callback. */
1168
1168
  const preforkDiscard = (buf: u8[], _index: i32, fd: i32, _events: i32): void => {
1169
- const size: i32 = toI32(buf.length)
1170
- while (netRead(fd, buf, 0, size) > 0) {
1169
+ while (netRead(fd, buf, 0, toI32(buf.length)) > 0) {
1171
1170
  // Dropped.
1172
1171
  }
1173
1172
  }