@amritk/nish-aarch64-linux 0.37.0 → 0.39.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/INSTALL.md +28 -16
- package/bin/nish +0 -0
- package/docs/AI.md +380 -47
- package/package.json +1 -1
- package/runtime/core/arrays.d.ts +4 -0
- package/runtime/core/arrays.mjs +33 -0
- package/runtime/core/host.d.ts +11 -0
- package/runtime/core/host.mjs +35 -0
- package/runtime/nish.d.ts +645 -66
- package/runtime/nish.h +101 -15
- package/runtime/nish.mjs +69 -6
- package/runtime/node.mjs +117 -0
- package/runtime/runtime-aes.c +12 -4
- package/runtime/runtime-async.c +267 -51
- package/runtime/runtime-checksum.c +207 -15
- package/runtime/runtime-cross.c +2 -1
- package/runtime/runtime-net.c +32 -3
- package/runtime/runtime-os.c +174 -27
- package/runtime/runtime-region.c +170 -8
- package/runtime/runtime-sandbox.c +377 -17
- package/runtime/runtime-sha256.c +11 -4
- package/runtime/runtime-text.c +1068 -0
- package/runtime/runtime-wasm.c +18 -3
- package/runtime/runtime.c +55 -961
- package/runtime/vite.mjs +48 -11
- package/scripts/build.sh +113 -36
- package/std/LICENSE-fdlibm +8 -0
- package/std/README.md +7 -4
- package/std/array.ts +266 -0
- package/std/compress/checksum.ts +13 -0
- package/std/compress/deflate.ts +0 -2
- package/std/crypto/p256.ts +247 -74
- package/std/crypto/p384.ts +3 -1
- package/std/crypto/pki.ts +35 -9
- package/std/crypto/x25519.ts +153 -64
- package/std/crypto/x509.ts +113 -10
- package/std/host/text.ts +86 -0
- package/std/json-tokenizer.ts +20 -0
- package/std/json-typed.ts +217 -8
- package/std/math.ts +743 -41
- package/std/net/dns-resolver.ts +46 -6
- package/std/net/dns.ts +50 -13
- package/std/net/fetch.ts +691 -129
- package/std/net/handler.ts +142 -16
- package/std/net/http-compress.ts +17 -9
- package/std/net/http-fields.ts +19 -4
- package/std/net/http1-codec.ts +517 -154
- package/std/net/http1-server.ts +508 -25
- package/std/net/http2-cleartext.ts +526 -0
- package/std/net/http2-tls.ts +48 -0
- package/std/net/http2.ts +84 -0
- package/std/net/postgres-async.ts +2223 -0
- package/std/net/postgres-carrier.ts +343 -10
- package/std/net/postgres.ts +129 -11
- package/std/net/prefork.ts +195 -11
- package/std/net/quic-datagram.ts +30 -0
- package/std/net/quic-listener.ts +38 -12
- package/std/net/quic-packet.ts +19 -6
- package/std/net/quic-recovery.ts +134 -19
- package/std/net/quic-stream.ts +174 -36
- package/std/net/quic.ts +418 -18
- package/std/net/router.ts +39 -17
- package/std/net/serve.ts +867 -0
- package/std/net/tls/codec.ts +110 -32
- package/std/net/tls/record-server.ts +37 -7
- package/std/net/tls-tcp.ts +56 -1
- package/std/net/tls.ts +196 -50
- package/std/net/websocket.ts +55 -18
- package/std/net/webtransport.ts +179 -1
- package/std/regex.ts +69 -47
- package/std/region.ts +62 -11
- package/std/secret.ts +26 -59
- package/std/text.ts +464 -5
package/std/math.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `std/math` — `Math.atan2`, `Math.tan` and `Math.hypot` on `f64`,
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* `std/math` — `Math.atan2`, `Math.tan` and `Math.hypot` on `f64`, and on a
|
|
3
|
+
* freestanding wasm build `Math.sin`, `Math.cos`, `Math.exp`, `Math.log`,
|
|
4
|
+
* `Math.pow` and the `%` of two `f64`s, in Nish rather than libm, so a program
|
|
5
|
+
* answers the same on every target and the optimiser sees the whole function.
|
|
5
6
|
*
|
|
6
7
|
* export const main = (): i32 => {
|
|
7
8
|
* const slope: f64 = Math.tan(0.5);
|
|
@@ -10,37 +11,50 @@
|
|
|
10
11
|
* return side === 5.0 ? 0 : 1;
|
|
11
12
|
* };
|
|
12
13
|
*
|
|
13
|
-
* **A program does not import this module.** A call to
|
|
14
|
-
* loads it (the checker's `importMath`), and
|
|
15
|
-
* here that the call reaches into the
|
|
16
|
-
* function, so this file writes no `.ll` of
|
|
17
|
-
*
|
|
14
|
+
* **A program does not import this module.** A call to `Math.atan2`,
|
|
15
|
+
* `Math.tan` or `Math.hypot` is what loads it (the checker's `importMath`), and
|
|
16
|
+
* the compiler copies each function here that the call reaches into the
|
|
17
|
+
* calling module as an `internal` function, so this file writes no `.ll` of
|
|
18
|
+
* its own. On `--profile wasm` and `--profile wasm-host`, where no libm is
|
|
19
|
+
* linked, and on every target under `--deterministic`, a call to the other
|
|
20
|
+
* five and an `f64` `%` load it too; elsewhere they stay LLVM's intrinsics and
|
|
21
|
+
* `frem`. Under Node and `tsc` the calls are the platform's `Math`, so nothing
|
|
22
|
+
* here runs there, except under `--deterministic`, whose `--emit-ts` reading
|
|
23
|
+
* imports these functions in place of `Math`.
|
|
18
24
|
*
|
|
19
|
-
* **Exactness.** Each is
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
* million random and edge-case operands: `tan` 0.83
|
|
24
|
-
* `hypot` 0.93 ulp (`tests/cases/math_std_edges` pins
|
|
25
|
+
* **Exactness.** Each is exactly JavaScript's value for every NaN, signed
|
|
26
|
+
* zero and infinity — every `atan2` infinity pairing, and `hypot`'s Infinity
|
|
27
|
+
* when either operand is infinite, even beside a NaN. `tan`, `atan2` and
|
|
28
|
+
* `hypot` are within one ulp of the correctly rounded result, measured
|
|
29
|
+
* against mpmath over 2.8 million random and edge-case operands: `tan` 0.83
|
|
30
|
+
* ulp, `atan2` 0.84 ulp, `hypot` 0.93 ulp (`tests/cases/math_std_edges` pins
|
|
31
|
+
* the special values). `sin`, `cos`, `exp`, `log` and `pow` are fdlibm's, as
|
|
32
|
+
* V8's `Math` is, and are within one ulp of V8 (`tests/run.js`'s `wasm-math`
|
|
33
|
+
* compares them on its operand grid); they are not measured against a
|
|
34
|
+
* correctly rounded reference. `fmodF64` is exact, as `%` is.
|
|
25
35
|
*
|
|
26
|
-
* **`tan`**
|
|
27
|
-
* |x| below 2^20·π/2, with the quadrant
|
|
36
|
+
* **`sin`, `cos` and `tan`** are fdlibm's kernels behind its Cody-Waite
|
|
37
|
+
* reduction for |x| below 2^20·π/2, with the quadrant read from the rounding
|
|
28
38
|
* constant. Larger arguments reduce by an integer Payne-Hanek: 192 bits of
|
|
29
39
|
* 2/π against the 53-bit mantissa, which is exact near every multiple of π/2
|
|
30
40
|
* a double can name. **`atan2`** is fdlibm's, with the quotient `y/x`
|
|
31
41
|
* carried as a double-double, so the argument reduction loses nothing, and
|
|
32
42
|
* π − atan taken in double-double. **`hypot`** is fdlibm's core on operands
|
|
33
43
|
* in [1e-150, 1e150], where neither square can overflow, and a power-of-two
|
|
34
|
-
* rescale into that range otherwise.
|
|
44
|
+
* rescale into that range otherwise. **`exp`**, **`log`** and **`pow`** are
|
|
45
|
+
* fdlibm's, `pow` with JavaScript's answers where C's differ (`x ** NaN` and
|
|
46
|
+
* `(±1) ** ±Infinity` are NaN). **`fmod`** is e_fmod.c's shift-and-subtract
|
|
47
|
+
* on one 64-bit integer rather than two 32-bit words.
|
|
35
48
|
*/
|
|
36
49
|
|
|
37
50
|
/*
|
|
38
51
|
* Ported from fdlibm (https://www.netlib.org/fdlibm/), as FreeBSD msun carries
|
|
39
|
-
* it, lib/msun/src/k_tan.c,
|
|
40
|
-
*
|
|
41
|
-
*
|
|
52
|
+
* it, lib/msun/src/k_tan.c, k_sin.c, k_cos.c, s_tan.c, s_sin.c, s_cos.c,
|
|
53
|
+
* e_rem_pio2.c, k_rem_pio2.c, e_atan2.c, s_atan.c, e_hypot.c, e_exp.c, e_log.c,
|
|
54
|
+
* e_pow.c and e_fmod.c. Copyright (C) 1993 and (C) 2004 by Sun Microsystems,
|
|
55
|
+
* Inc. Used under the fdlibm licence; see std/LICENSE-fdlibm.
|
|
42
56
|
*
|
|
43
|
-
* fdlibm's
|
|
57
|
+
* fdlibm's notices, as those files carry them:
|
|
44
58
|
*
|
|
45
59
|
* ====================================================
|
|
46
60
|
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
|
@@ -50,6 +64,14 @@
|
|
|
50
64
|
* software is freely granted, provided that this notice
|
|
51
65
|
* is preserved.
|
|
52
66
|
* ====================================================
|
|
67
|
+
*
|
|
68
|
+
* ====================================================
|
|
69
|
+
* Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved.
|
|
70
|
+
*
|
|
71
|
+
* Permission to use, copy, modify, and distribute this
|
|
72
|
+
* software is freely granted, provided that this notice
|
|
73
|
+
* is preserved.
|
|
74
|
+
* ====================================================
|
|
53
75
|
*/
|
|
54
76
|
|
|
55
77
|
// Word access, as fdlibm's GET_HIGH_WORD and GET_LOW_WORD.
|
|
@@ -234,8 +256,9 @@ const clz64 = (v: u64): i32 => {
|
|
|
234
256
|
const h: u32 = toU32(v >> toU64(32))
|
|
235
257
|
return h !== toU32(0) ? Math.clz32(h) : Math.clz32(toU32(v)) + 32
|
|
236
258
|
}
|
|
237
|
-
//
|
|
238
|
-
|
|
259
|
+
// The trigonometric function `mode` names (0 tan, 1 sin, 2 cos) of x,
|
|
260
|
+
// for |x| >= 2^20 * pi/2 (finite): x * 2/pi mod 4 from a 192-bit window.
|
|
261
|
+
const trigLarge = (x: f64, ix: i32, hx: i32, mode: i32): f64 => {
|
|
239
262
|
const e: i32 = (ix >> 20) - 1023
|
|
240
263
|
const m: u64 = (toU64(f64ToBits(x)) & ((toU64(1) << toU64(52)) - toU64(1))) | (toU64(1) << toU64(52))
|
|
241
264
|
const p: i32 = e - 53
|
|
@@ -302,7 +325,8 @@ const tanLarge = (x: f64, ix: i32, hx: i32): f64 => {
|
|
|
302
325
|
y0 = -y0
|
|
303
326
|
y1 = -y1
|
|
304
327
|
}
|
|
305
|
-
|
|
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)
|
|
306
330
|
}
|
|
307
331
|
|
|
308
332
|
// fdlibm's __kernel_tan: tan(x0 + y0) for |x0 + y0| <= pi/4, or -1/tan when `odd`.
|
|
@@ -351,28 +375,67 @@ const kernelTan = (x0: f64, y0: f64, odd: boolean): f64 => {
|
|
|
351
375
|
const t: f64 = clearLo(a)
|
|
352
376
|
return t + a * (t * zz + 1.0 + t * vv)
|
|
353
377
|
}
|
|
354
|
-
|
|
355
|
-
*
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
}
|
|
368
|
-
return kernelTan(x, 0.0, false)
|
|
378
|
+
// fdlibm's __kernel_sin: sin(x + y) for |x + y| <= pi/4, y the tail of a reduced argument.
|
|
379
|
+
// `tail` is fdlibm's iy: with none, x + v * (S1 + z * r) rounds as V8's sin does, so it keeps
|
|
380
|
+
// that form rather than the tail form at y = 0.
|
|
381
|
+
const kernelSin = (x: f64, y: f64, tail: boolean): f64 => {
|
|
382
|
+
const z: f64 = x * x
|
|
383
|
+
const w: f64 = z * z
|
|
384
|
+
const r: f64 =
|
|
385
|
+
z * (z * 2.75573137070700676789e-6 - 1.98412698298579493134e-4) +
|
|
386
|
+
8.33333333332248946124e-3 +
|
|
387
|
+
z * w * (z * 1.58969099521155010221e-10 - 2.50507602534068634195e-8)
|
|
388
|
+
const v: f64 = z * x
|
|
389
|
+
if (!tail) {
|
|
390
|
+
return x + v * (z * r + -1.66666666666666324348e-1)
|
|
369
391
|
}
|
|
392
|
+
return x - (z * (y * 0.5 - v * r) - y - v * -1.66666666666666324348e-1)
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// fdlibm's __kernel_cos, as FreeBSD's k_cos.c has it: cos(x + y) for |x + y| <= pi/4.
|
|
396
|
+
const kernelCos = (x: f64, y: f64): f64 => {
|
|
397
|
+
const z: f64 = x * x
|
|
398
|
+
const w: f64 = z * z
|
|
399
|
+
const r: f64 =
|
|
400
|
+
z * (z * (z * 2.48015872894767294178e-5 - 1.38888888888741095749e-3) + 4.16666666666666019037e-2) +
|
|
401
|
+
w * w * (z * (z * -1.13596475577881948265e-11 + 2.0875723212981748279e-9) - 2.75573143513906633035e-7)
|
|
402
|
+
const hz: f64 = z * 0.5
|
|
403
|
+
const one: f64 = 1.0
|
|
404
|
+
const v: f64 = one - hz
|
|
405
|
+
return v + (one - v - hz + (z * r - x * y))
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// The function `mode` names of an argument reduced to quadrant n and y0 + y1 in [-pi/4, pi/4],
|
|
409
|
+
// as s_tan.c, s_sin.c and s_cos.c switch on n & 3.
|
|
410
|
+
const trigKernel = (y0: f64, y1: f64, n: i32, mode: i32): f64 => {
|
|
411
|
+
const q: i32 = n & 3
|
|
412
|
+
if (mode === 0) {
|
|
413
|
+
return kernelTan(y0, y1, (q & 1) !== 0)
|
|
414
|
+
}
|
|
415
|
+
// cos(x) is sin(x + pi/2): one quadrant on.
|
|
416
|
+
const k: i32 = mode === 2 ? (q + 1) & 3 : q
|
|
417
|
+
if (k === 0) {
|
|
418
|
+
return kernelSin(y0, y1, true)
|
|
419
|
+
}
|
|
420
|
+
if (k === 1) {
|
|
421
|
+
return kernelCos(y0, y1)
|
|
422
|
+
}
|
|
423
|
+
if (k === 2) {
|
|
424
|
+
return -kernelSin(y0, y1, true)
|
|
425
|
+
}
|
|
426
|
+
return -kernelCos(y0, y1)
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// The function `mode` names of x, for pi/4 < |x| (`ax`), x finite or not: e_rem_pio2.c's
|
|
430
|
+
// Cody-Waite reduction below 2^20 * pi/2, with the quadrant read from the rounding constant,
|
|
431
|
+
// and `trigLarge` above it.
|
|
432
|
+
const trigReduced = (x: f64, ax: f64, mode: i32): f64 => {
|
|
370
433
|
if (!(ax < 1647099.0)) {
|
|
371
434
|
if (isNan(ax) || ax > 1.7976931348623157e308) {
|
|
372
435
|
return x - x
|
|
373
436
|
}
|
|
374
437
|
const hx: i32 = hiWord(x)
|
|
375
|
-
return
|
|
438
|
+
return trigLarge(x, hx & 0x7fffffff, hx, mode)
|
|
376
439
|
}
|
|
377
440
|
const rounded: f64 = x * 6.36619772367581382433e-1 + 6755399441055744.0
|
|
378
441
|
const fn: f64 = rounded - 6755399441055744.0
|
|
@@ -397,7 +460,63 @@ export const tanF64 = (x: f64): f64 => {
|
|
|
397
460
|
}
|
|
398
461
|
}
|
|
399
462
|
const y1: f64 = r - y0 - w
|
|
400
|
-
|
|
463
|
+
// The rounded quotient is an integer in the low bits of `rounded`, two's complement.
|
|
464
|
+
return trigKernel(y0, y1, toI32(f64ToBits(rounded) & toI64(3)), mode)
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* `Math.tan(x)`: fdlibm's reduction and kernel, with `trigLarge` past
|
|
469
|
+
* 2^20 * pi/2.
|
|
470
|
+
*
|
|
471
|
+
* @param x The angle, in radians.
|
|
472
|
+
* @returns The tangent of `x`; NaN for a NaN or an infinite `x`, and `x`
|
|
473
|
+
* itself when `|x| < 2^-27`, a zero's sign kept.
|
|
474
|
+
*/
|
|
475
|
+
export const tanF64 = (x: f64): f64 => {
|
|
476
|
+
const ax: f64 = Math.abs(x)
|
|
477
|
+
if (ax < 0.7853984832763672) {
|
|
478
|
+
if (ax < 7.450580596923828e-9) {
|
|
479
|
+
return x
|
|
480
|
+
}
|
|
481
|
+
return kernelTan(x, 0.0, false)
|
|
482
|
+
}
|
|
483
|
+
return trigReduced(x, ax, 0)
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* `Math.sin(x)`: fdlibm's `sin`, its reduction shared with `tan`.
|
|
488
|
+
*
|
|
489
|
+
* @param x The angle, in radians.
|
|
490
|
+
* @returns The sine of `x`; NaN for a NaN or an infinite `x`, and `x`
|
|
491
|
+
* itself when `|x| < 2^-26`, a zero's sign kept.
|
|
492
|
+
*/
|
|
493
|
+
export const sinF64 = (x: f64): f64 => {
|
|
494
|
+
const ax: f64 = Math.abs(x)
|
|
495
|
+
if (ax < 0.7853984832763672) {
|
|
496
|
+
if (ax < 1.4901161193847656e-8) {
|
|
497
|
+
return x
|
|
498
|
+
}
|
|
499
|
+
return kernelSin(x, 0.0, false)
|
|
500
|
+
}
|
|
501
|
+
return trigReduced(x, ax, 1)
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* `Math.cos(x)`: fdlibm's `cos`, its reduction shared with `tan`.
|
|
506
|
+
*
|
|
507
|
+
* @param x The angle, in radians.
|
|
508
|
+
* @returns The cosine of `x`; NaN for a NaN or an infinite `x`, and 1
|
|
509
|
+
* when `|x| < 2^-27`.
|
|
510
|
+
*/
|
|
511
|
+
export const cosF64 = (x: f64): f64 => {
|
|
512
|
+
const ax: f64 = Math.abs(x)
|
|
513
|
+
if (ax < 0.7853984832763672) {
|
|
514
|
+
if (ax < 7.450580596923828e-9) {
|
|
515
|
+
return 1.0
|
|
516
|
+
}
|
|
517
|
+
return kernelCos(x, 0.0)
|
|
518
|
+
}
|
|
519
|
+
return trigReduced(x, ax, 2)
|
|
401
520
|
}
|
|
402
521
|
|
|
403
522
|
// fdlibm (FreeBSD msun) s_atan.c / e_atan2.c, ported.
|
|
@@ -638,3 +757,586 @@ export const hypotF64 = (x: f64, y: f64): f64 => {
|
|
|
638
757
|
}
|
|
639
758
|
return hypotRest(ax, ay, a, b)
|
|
640
759
|
}
|
|
760
|
+
|
|
761
|
+
// 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))))
|
|
764
|
+
const withLo = (x: f64, lo: u32): f64 => bitsToF64((f64ToBits(x) & (toI64(-1) << toI64(32))) | toI64(lo))
|
|
765
|
+
|
|
766
|
+
// The rational approximation e_exp.c and e_pow.c share: P1 + t * (P2 + ... + t * P5).
|
|
767
|
+
const expPoly = (t: f64): f64 =>
|
|
768
|
+
t *
|
|
769
|
+
(t * (t * (t * 4.13813679705723846039e-8 - 1.6533902205465251539e-6) + 6.61375632143793436117e-5) -
|
|
770
|
+
2.77777777770155933842e-3) +
|
|
771
|
+
1.66666666666666019037e-1
|
|
772
|
+
|
|
773
|
+
// fdlibm's huge * huge and tiny * tiny, an overflow and an underflow with the sign of `s`.
|
|
774
|
+
const overflow = (s: f64): f64 => s * 1.0e300 * 1.0e300
|
|
775
|
+
const underflow = (s: f64): f64 => s * 1.0e-300 * 1.0e-300
|
|
776
|
+
|
|
777
|
+
/**
|
|
778
|
+
* `Math.exp(x)`: fdlibm's `__ieee754_exp`, a reduction by ln 2 and a
|
|
779
|
+
* rational approximation on [-ln2/2, ln2/2].
|
|
780
|
+
*
|
|
781
|
+
* @param x The exponent.
|
|
782
|
+
* @returns e raised to `x`; Infinity past 709.78, +0 below -745.13, NaN for
|
|
783
|
+
* a NaN, and 0 for -Infinity.
|
|
784
|
+
*/
|
|
785
|
+
export const expF64 = (x: f64): f64 => {
|
|
786
|
+
const hxs: i32 = hiWord(x)
|
|
787
|
+
const negative: boolean = hxs < 0
|
|
788
|
+
const hx: i32 = hxs & 0x7fffffff
|
|
789
|
+
if (hx >= 0x40862e42) {
|
|
790
|
+
if (hx >= 0x7ff00000) {
|
|
791
|
+
if (isNan(x)) {
|
|
792
|
+
return x + x
|
|
793
|
+
}
|
|
794
|
+
return negative ? 0.0 : x
|
|
795
|
+
}
|
|
796
|
+
if (x > 7.09782712893383973096e2) {
|
|
797
|
+
return overflow(1.0)
|
|
798
|
+
}
|
|
799
|
+
if (x < -7.4513321910194110842e2) {
|
|
800
|
+
return 0.0
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
let k: i32 = 0
|
|
804
|
+
let hi: f64 = 0.0
|
|
805
|
+
let lo: f64 = 0.0
|
|
806
|
+
let r: f64 = x
|
|
807
|
+
if (hx > 0x3fd62e42) {
|
|
808
|
+
if (hx < 0x3ff0a2b2) {
|
|
809
|
+
hi = negative ? x + 6.9314718036912381649e-1 : x - 6.9314718036912381649e-1
|
|
810
|
+
lo = negative ? -1.90821492927058770002e-10 : 1.90821492927058770002e-10
|
|
811
|
+
k = negative ? -1 : 1
|
|
812
|
+
} else {
|
|
813
|
+
const half: f64 = negative ? -0.5 : 0.5
|
|
814
|
+
k = toI32(x * 1.442695040888963387 + half)
|
|
815
|
+
const t: f64 = toF64(k)
|
|
816
|
+
hi = x - t * 6.9314718036912381649e-1
|
|
817
|
+
lo = t * 1.90821492927058770002e-10
|
|
818
|
+
}
|
|
819
|
+
r = hi - lo
|
|
820
|
+
} else if (hx < 0x3e300000) {
|
|
821
|
+
return x + 1.0
|
|
822
|
+
}
|
|
823
|
+
const t: f64 = r * r
|
|
824
|
+
const c: f64 = r - t * expPoly(t)
|
|
825
|
+
const one: f64 = 1.0
|
|
826
|
+
if (k === 0) {
|
|
827
|
+
return one - ((r * c) / (c - 2.0) - r)
|
|
828
|
+
}
|
|
829
|
+
const y: f64 = one - (lo - (r * c) / (2.0 - c) - hi)
|
|
830
|
+
if (k >= -1021) {
|
|
831
|
+
if (k === 1024) {
|
|
832
|
+
return y * 2.0 * 8.98846567431158e307
|
|
833
|
+
}
|
|
834
|
+
return y * pow2(k)
|
|
835
|
+
}
|
|
836
|
+
return y * pow2(k + 1000) * 9.332636185032189e-302
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
/**
|
|
840
|
+
* `Math.log(x)`: fdlibm's `__ieee754_log`, `x` split as 2^k * (1 + f) and
|
|
841
|
+
* log(1 + f) taken from a polynomial in f / (2 + f).
|
|
842
|
+
*
|
|
843
|
+
* @param x The operand.
|
|
844
|
+
* @returns The natural logarithm of `x`; -Infinity for a zero of either
|
|
845
|
+
* sign, NaN below zero or for a NaN, and Infinity for Infinity.
|
|
846
|
+
*/
|
|
847
|
+
export const logF64 = (x: f64): f64 => {
|
|
848
|
+
let v: f64 = x
|
|
849
|
+
let hx: i32 = hiWord(v)
|
|
850
|
+
let k: i32 = 0
|
|
851
|
+
if (hx < 0x00100000) {
|
|
852
|
+
if (((hx & 0x7fffffff) | toI32(loWord(v))) === 0) {
|
|
853
|
+
return -overflow(1.0)
|
|
854
|
+
}
|
|
855
|
+
if (hx < 0) {
|
|
856
|
+
return (v - v) / (v - v)
|
|
857
|
+
}
|
|
858
|
+
k = -54
|
|
859
|
+
v = v * 1.8014398509481984e16
|
|
860
|
+
hx = hiWord(v)
|
|
861
|
+
}
|
|
862
|
+
if (hx >= 0x7ff00000) {
|
|
863
|
+
return v + v
|
|
864
|
+
}
|
|
865
|
+
k += (hx >> 20) - 1023
|
|
866
|
+
hx = hx & 0x000fffff
|
|
867
|
+
const i: i32 = (hx + 0x95f64) & 0x100000
|
|
868
|
+
v = withHi(v, hx | (i ^ 0x3ff00000))
|
|
869
|
+
k += i >> 20
|
|
870
|
+
const f: f64 = v - 1.0
|
|
871
|
+
const dk: f64 = toF64(k)
|
|
872
|
+
if (((hx + 2) & 0x000fffff) < 3) {
|
|
873
|
+
if (f === 0.0) {
|
|
874
|
+
return dk * 6.9314718036912381649e-1 + dk * 1.90821492927058770002e-10
|
|
875
|
+
}
|
|
876
|
+
const rr: f64 = f * f * (f * -0.33333333333333333 + 0.5)
|
|
877
|
+
if (k === 0) {
|
|
878
|
+
return f - rr
|
|
879
|
+
}
|
|
880
|
+
return dk * 6.9314718036912381649e-1 - (rr - dk * 1.90821492927058770002e-10 - f)
|
|
881
|
+
}
|
|
882
|
+
const s: f64 = f / (2.0 + f)
|
|
883
|
+
const z: f64 = s * s
|
|
884
|
+
const w: f64 = z * z
|
|
885
|
+
const t1: f64 = w * (w * (w * 1.531383769920937332e-1 + 2.222219843214978396e-1) + 3.999999999940941908e-1)
|
|
886
|
+
const t2: f64 =
|
|
887
|
+
z *
|
|
888
|
+
(w * (w * (w * 1.479819860511658591e-1 + 1.818357216161805012e-1) + 2.857142874366239149e-1) +
|
|
889
|
+
6.66666666666673513e-1)
|
|
890
|
+
const r: f64 = t2 + t1
|
|
891
|
+
if (((hx - 0x6147a) | (0x6b851 - hx)) > 0) {
|
|
892
|
+
const hfsq: f64 = f * f * 0.5
|
|
893
|
+
if (k === 0) {
|
|
894
|
+
return f - (hfsq - s * (hfsq + r))
|
|
895
|
+
}
|
|
896
|
+
return dk * 6.9314718036912381649e-1 - (hfsq - (s * (hfsq + r) + dk * 1.90821492927058770002e-10) - f)
|
|
897
|
+
}
|
|
898
|
+
if (k === 0) {
|
|
899
|
+
return f - s * (f - r)
|
|
900
|
+
}
|
|
901
|
+
return dk * 6.9314718036912381649e-1 - (s * (f - r) - dk * 1.90821492927058770002e-10 - f)
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
// 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)
|
|
906
|
+
|
|
907
|
+
// pow's |y| > 2^31 case when x is not within 2^-20 of 1: an overflow or an underflow.
|
|
908
|
+
const powHugeY = (ix: i32, hy: i32, iy: i32, s: f64): f64 => {
|
|
909
|
+
if (iy > 0x43f00000) {
|
|
910
|
+
if (ix <= 0x3fefffff) {
|
|
911
|
+
return hy < 0 ? overflow(1.0) : 0.0
|
|
912
|
+
}
|
|
913
|
+
return hy > 0 ? overflow(1.0) : 0.0
|
|
914
|
+
}
|
|
915
|
+
if (ix < 0x3fefffff) {
|
|
916
|
+
return hy < 0 ? overflow(s) : underflow(s)
|
|
917
|
+
}
|
|
918
|
+
return hy > 0 ? overflow(s) : underflow(s)
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
// 2^(ph + pl) * s, the tail of e_pow.c once log2(x) * y is split as ph + pl.
|
|
922
|
+
const powExp2 = (ph0: f64, pl: f64, s: f64): f64 => {
|
|
923
|
+
let ph: f64 = ph0
|
|
924
|
+
const z0: f64 = pl + ph
|
|
925
|
+
const j: i32 = hiWord(z0)
|
|
926
|
+
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
|
+
const ij: i32 = j & 0x7fffffff
|
|
938
|
+
let k: i32 = (ij >> 20) - 0x3ff
|
|
939
|
+
let n: i32 = 0
|
|
940
|
+
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
|
|
950
|
+
}
|
|
951
|
+
const t: f64 = withLo(pl + ph, toU32(0))
|
|
952
|
+
const u: f64 = t * 6.93147182464599609375e-1
|
|
953
|
+
const v: f64 = (pl - (t - ph)) * 6.93147180559945286227e-1 + t * -1.90465429995776804525e-9
|
|
954
|
+
let z: f64 = u + v
|
|
955
|
+
const w: f64 = v - (z - u)
|
|
956
|
+
const t1: f64 = z - z * z * expPoly(z * z)
|
|
957
|
+
const r: f64 = (z * t1) / (t1 - 2.0) - (w + z * w)
|
|
958
|
+
const one: f64 = 1.0
|
|
959
|
+
z = one - (r - z)
|
|
960
|
+
const hz: i32 = hiWord(z) + (n << 20)
|
|
961
|
+
if (hz >> 20 <= 0) {
|
|
962
|
+
return s * scaleDown(z, n)
|
|
963
|
+
}
|
|
964
|
+
return s * withHi(z, hz)
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
/**
|
|
968
|
+
* `Math.pow(x, y)`: fdlibm's `__ieee754_pow`, log2(x) in extra precision times
|
|
969
|
+
* `y`, then 2 raised to that, with JavaScript's answers where fdlibm's differ.
|
|
970
|
+
*
|
|
971
|
+
* @param x The base.
|
|
972
|
+
* @param y The exponent.
|
|
973
|
+
* @returns `x` raised to `y`; 1 when `y` is a zero, NaN when `y` is NaN or
|
|
974
|
+
* when `x` is ±1 and `y` infinite (where C's pow gives 1), and NaN for a
|
|
975
|
+
* negative finite `x` and a non-integer `y`.
|
|
976
|
+
*/
|
|
977
|
+
export const powF64 = (x: f64, y: f64): f64 => {
|
|
978
|
+
const hx: i32 = hiWord(x)
|
|
979
|
+
const lx: u32 = loWord(x)
|
|
980
|
+
const hy: i32 = hiWord(y)
|
|
981
|
+
const ly: u32 = loWord(y)
|
|
982
|
+
const ix: i32 = hx & 0x7fffffff
|
|
983
|
+
const iy: i32 = hy & 0x7fffffff
|
|
984
|
+
if ((iy | toI32(ly)) === 0) {
|
|
985
|
+
return 1.0
|
|
986
|
+
}
|
|
987
|
+
if (isNan(x) || isNan(y)) {
|
|
988
|
+
return x + y
|
|
989
|
+
}
|
|
990
|
+
// JavaScript: (±1) ** ±Infinity is NaN.
|
|
991
|
+
if (iy === 0x7ff00000 && ly === toU32(0) && ix === 0x3ff00000 && lx === toU32(0)) {
|
|
992
|
+
return y - y
|
|
993
|
+
}
|
|
994
|
+
if (hx === 0x3ff00000 && lx === toU32(0)) {
|
|
995
|
+
return 1.0
|
|
996
|
+
}
|
|
997
|
+
// Whether y is an integer, for a negative x: 0 no, 1 odd, 2 even.
|
|
998
|
+
let yisint: i32 = 0
|
|
999
|
+
if (hx < 0) {
|
|
1000
|
+
if (iy >= 0x43400000) {
|
|
1001
|
+
yisint = 2
|
|
1002
|
+
} 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) {
|
|
1007
|
+
yisint = 2 - toI32(j & toU32(1))
|
|
1008
|
+
}
|
|
1009
|
+
} else if (ly === toU32(0)) {
|
|
1010
|
+
const j: i32 = iy >> (20 - k)
|
|
1011
|
+
if (j << (20 - k) === iy) {
|
|
1012
|
+
const odd: i32 = j & 1
|
|
1013
|
+
yisint = 2 - odd
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
if (ly === toU32(0)) {
|
|
1019
|
+
if (iy === 0x7ff00000) {
|
|
1020
|
+
if (ix >= 0x3ff00000) {
|
|
1021
|
+
return hy >= 0 ? y : 0.0
|
|
1022
|
+
}
|
|
1023
|
+
return hy < 0 ? -y : 0.0
|
|
1024
|
+
}
|
|
1025
|
+
if (iy === 0x3ff00000) {
|
|
1026
|
+
return hy < 0 ? 1.0 / x : x
|
|
1027
|
+
}
|
|
1028
|
+
if (hy === 0x40000000) {
|
|
1029
|
+
return x * x
|
|
1030
|
+
}
|
|
1031
|
+
if (hy === 0x3fe00000 && hx >= 0) {
|
|
1032
|
+
return Math.sqrt(x)
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
let ax: f64 = Math.abs(x)
|
|
1036
|
+
if (lx === toU32(0) && (ix === 0x7ff00000 || ix === 0 || ix === 0x3ff00000)) {
|
|
1037
|
+
let z: f64 = hy < 0 ? 1.0 / ax : ax
|
|
1038
|
+
if (hx < 0) {
|
|
1039
|
+
if (((ix - 0x3ff00000) | yisint) === 0) {
|
|
1040
|
+
z = (z - z) / (z - z)
|
|
1041
|
+
} else if (yisint === 1) {
|
|
1042
|
+
z = -z
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
return z
|
|
1046
|
+
}
|
|
1047
|
+
if (hx < 0 && yisint === 0) {
|
|
1048
|
+
return (x - x) / (x - x)
|
|
1049
|
+
}
|
|
1050
|
+
const s: f64 = hx < 0 && yisint === 1 ? -1.0 : 1.0
|
|
1051
|
+
let t1: f64 = 0.0
|
|
1052
|
+
let t2: f64 = 0.0
|
|
1053
|
+
if (iy > 0x41e00000) {
|
|
1054
|
+
if (iy > 0x43f00000 || ix < 0x3fefffff || ix > 0x3ff00000) {
|
|
1055
|
+
return powHugeY(ix, hy, iy, s)
|
|
1056
|
+
}
|
|
1057
|
+
const t: f64 = ax - 1.0
|
|
1058
|
+
const w: f64 = t * t * (t * (t * 0.25 - 0.3333333333333333333333) + 0.5)
|
|
1059
|
+
const u: f64 = t * 1.44269502162933349609
|
|
1060
|
+
const v: f64 = t * 1.92596299112661746887e-8 - w * 1.442695040888963387
|
|
1061
|
+
t1 = withLo(u + v, toU32(0))
|
|
1062
|
+
t2 = v - (t1 - u)
|
|
1063
|
+
} else {
|
|
1064
|
+
let n: i32 = 0
|
|
1065
|
+
let jx: i32 = ix
|
|
1066
|
+
if (jx < 0x00100000) {
|
|
1067
|
+
ax = ax * 9007199254740992.0
|
|
1068
|
+
n = -53
|
|
1069
|
+
jx = hiWord(ax)
|
|
1070
|
+
}
|
|
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)
|
|
1084
|
+
const bp: f64 = k === 0 ? 1.0 : 1.5
|
|
1085
|
+
const dph: f64 = k === 0 ? 0.0 : 5.84962487220764160156e-1
|
|
1086
|
+
const dpl: f64 = k === 0 ? 0.0 : 1.35003920212974897128e-8
|
|
1087
|
+
const u: f64 = ax - bp
|
|
1088
|
+
const one: f64 = 1.0
|
|
1089
|
+
const v: f64 = one / (ax + bp)
|
|
1090
|
+
const ss: f64 = u * v
|
|
1091
|
+
const sh: f64 = withLo(ss, toU32(0))
|
|
1092
|
+
let th: f64 = withHi(0.0, ((jx >> 1) | 0x20000000) + 0x00080000 + (k << 18))
|
|
1093
|
+
let tl: f64 = ax - (th - bp)
|
|
1094
|
+
const sl: f64 = v * (u - sh * th - sh * tl)
|
|
1095
|
+
let s2: f64 = ss * ss
|
|
1096
|
+
const l3: f64 =
|
|
1097
|
+
s2 * (s2 * (s2 * 2.06975017800338417784e-1 + 2.30660745775561754067e-1) + 2.72728123808534006489e-1)
|
|
1098
|
+
let r: f64 =
|
|
1099
|
+
s2 *
|
|
1100
|
+
s2 *
|
|
1101
|
+
(s2 * (s2 * (l3 + 3.33333329818377432918e-1) + 4.28571428578550184252e-1) + 5.99999999999994648725e-1)
|
|
1102
|
+
r += sl * (sh + ss)
|
|
1103
|
+
s2 = sh * sh
|
|
1104
|
+
th = withLo(s2 + 3.0 + r, toU32(0))
|
|
1105
|
+
tl = r - (th - 3.0 - s2)
|
|
1106
|
+
const pu: f64 = sh * th
|
|
1107
|
+
const pv: f64 = sl * th + tl * ss
|
|
1108
|
+
const ph: f64 = withLo(pu + pv, toU32(0))
|
|
1109
|
+
const pl: f64 = pv - (ph - pu)
|
|
1110
|
+
const zh: f64 = ph * 9.61796700954437255859e-1
|
|
1111
|
+
const zl: f64 = ph * -7.02846165095275826516e-9 + pl * 9.61796693925975554329e-1 + dpl
|
|
1112
|
+
const t: f64 = toF64(n)
|
|
1113
|
+
t1 = withLo(zh + zl + dph + t, toU32(0))
|
|
1114
|
+
t2 = zl - (t1 - t - dph - zh)
|
|
1115
|
+
}
|
|
1116
|
+
const y1: f64 = withLo(y, toU32(0))
|
|
1117
|
+
return powExp2(y1 * t1, (y - y1) * t1 + y * t2, s)
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
/**
|
|
1121
|
+
* The remainder `x % y` on `f64`, as JavaScript and C's `fmod` define it:
|
|
1122
|
+
* e_fmod.c's shift-and-subtract, exact, over one 64-bit word rather than two
|
|
1123
|
+
* 32-bit ones.
|
|
1124
|
+
*
|
|
1125
|
+
* @param x The dividend.
|
|
1126
|
+
* @param y The divisor.
|
|
1127
|
+
* @returns `x - n * y` for the integer n that truncates `x / y`, exactly,
|
|
1128
|
+
* with `x`'s sign; NaN when `x` is infinite, `y` is a zero or either is
|
|
1129
|
+
* NaN, and `x` itself when `y` is infinite and `x` finite.
|
|
1130
|
+
*/
|
|
1131
|
+
export const fmodF64 = (x: f64, y: f64): f64 => {
|
|
1132
|
+
let ux: u64 = toU64(f64ToBits(x))
|
|
1133
|
+
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))
|
|
1136
|
+
const sign: u64 = ux & (toU64(1) << toU64(63))
|
|
1137
|
+
if (uy << toU64(1) === toU64(0) || isNan(y) || ex === 0x7ff) {
|
|
1138
|
+
return (x * y) / (x * y)
|
|
1139
|
+
}
|
|
1140
|
+
if (ux << toU64(1) <= uy << toU64(1)) {
|
|
1141
|
+
return ux << toU64(1) === uy << toU64(1) ? 0.0 * x : x
|
|
1142
|
+
}
|
|
1143
|
+
const mant: u64 = (toU64(1) << toU64(52)) - toU64(1)
|
|
1144
|
+
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
|
|
1176
|
+
}
|
|
1177
|
+
if (ux >= uy) {
|
|
1178
|
+
if (ux === uy) {
|
|
1179
|
+
return 0.0 * x
|
|
1180
|
+
}
|
|
1181
|
+
ux = ux - uy
|
|
1182
|
+
}
|
|
1183
|
+
while (ux >> toU64(52) === toU64(0)) {
|
|
1184
|
+
ux = ux << toU64(1)
|
|
1185
|
+
ex -= 1
|
|
1186
|
+
}
|
|
1187
|
+
if (ex > 0) {
|
|
1188
|
+
ux = (ux - implicit) | (toU64(ex) << toU64(52))
|
|
1189
|
+
} else {
|
|
1190
|
+
ux = ux >> toU64(1 - ex)
|
|
1191
|
+
}
|
|
1192
|
+
return bitsToF64(toI64(ux | sign))
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
/**
|
|
1196
|
+
* `f64ToBits` under `--deterministic` (WP50 §9): the bits of `x`, except that
|
|
1197
|
+
* every NaN answers the one quiet NaN, `0x7ff8000000000000`. A NaN's sign and
|
|
1198
|
+
* payload are whatever the instruction that made it chose, which differs
|
|
1199
|
+
* between x86-64, arm64, wasm and V8, so the flag makes them one value; a
|
|
1200
|
+
* `bitsToF64` of any NaN pattern is left as it is.
|
|
1201
|
+
*
|
|
1202
|
+
* @param x The value to read.
|
|
1203
|
+
* @returns The IEEE 754 bits of `x`, with every NaN as `0x7ff8000000000000`.
|
|
1204
|
+
*/
|
|
1205
|
+
export const quietBitsF64 = (x: f64): i64 =>
|
|
1206
|
+
// biome-ignore lint/suspicious/noSelfCompare: NaN is the only value that is not itself; Nish has no Number.isNaN
|
|
1207
|
+
x !== x ? toI64(0x7ff8) << toI64(48) : f64ToBits(x)
|
|
1208
|
+
|
|
1209
|
+
/**
|
|
1210
|
+
* `f32ToBits` under `--deterministic`: the bits of `x`, with every NaN as the
|
|
1211
|
+
* one quiet NaN, `0x7fc00000`, for the reason `quietBitsF64` gives.
|
|
1212
|
+
*
|
|
1213
|
+
* @param x The value to read.
|
|
1214
|
+
* @returns The IEEE 754 bits of `x`, with every NaN as `0x7fc00000`.
|
|
1215
|
+
*/
|
|
1216
|
+
export const quietBitsF32 = (x: f32): i32 =>
|
|
1217
|
+
// biome-ignore lint/suspicious/noSelfCompare: NaN is the only value that is not itself; Nish has no Number.isNaN
|
|
1218
|
+
x !== x ? 0x7fc00000 : f32ToBits(x)
|
|
1219
|
+
|
|
1220
|
+
// The value of the hex digit `c` (a UTF-16 code unit), or -1 if it is not one.
|
|
1221
|
+
const hexDigit = (c: i32): i32 => {
|
|
1222
|
+
if (c >= 48 && c <= 57) {
|
|
1223
|
+
return c - 48
|
|
1224
|
+
}
|
|
1225
|
+
const lower: i32 = c | 32
|
|
1226
|
+
return lower >= 97 && lower <= 102 ? lower - 87 : -1
|
|
1227
|
+
}
|
|
1228
|
+
|
|
1229
|
+
// The index of the first code unit at or after `at` that is not ASCII
|
|
1230
|
+
// whitespace, the set `nish_parse_number` skips: space, tab, LF, VT, FF, CR.
|
|
1231
|
+
const skipSpaces = (s: string, at: i32): i32 => {
|
|
1232
|
+
let k: i32 = at
|
|
1233
|
+
while (k >= 0 && k < toI32(s.length)) {
|
|
1234
|
+
const c: i32 = toI32(s.charCodeAt(k))
|
|
1235
|
+
if (c !== 32 && (c < 9 || c > 13)) {
|
|
1236
|
+
return k
|
|
1237
|
+
}
|
|
1238
|
+
k = k + 1
|
|
1239
|
+
}
|
|
1240
|
+
return k
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
// Where the hex digits from `at` stop.
|
|
1244
|
+
const hexEnd = (s: string, at: i32): i32 => {
|
|
1245
|
+
let k: i32 = at
|
|
1246
|
+
while (k >= 0 && k < toI32(s.length) && hexDigit(toI32(s.charCodeAt(k))) >= 0) {
|
|
1247
|
+
k = k + 1
|
|
1248
|
+
}
|
|
1249
|
+
return k
|
|
1250
|
+
}
|
|
1251
|
+
|
|
1252
|
+
// The hex digits from `start` to `end` (at least one) as the nearest double,
|
|
1253
|
+
// ties to even. Digits past 64 bits are kept as a power of two and a sticky
|
|
1254
|
+
// bit, so a hex integer of any length rounds once, as a correct strtod does.
|
|
1255
|
+
const hexValue = (s: string, start: i32, end: i32): f64 => {
|
|
1256
|
+
let m: u64 = toU64(0)
|
|
1257
|
+
let scale: i32 = 0
|
|
1258
|
+
let sticky: boolean = false
|
|
1259
|
+
let k: i32 = start
|
|
1260
|
+
while (k >= 0 && k < end && k < toI32(s.length)) {
|
|
1261
|
+
const d: i32 = hexDigit(toI32(s.charCodeAt(k)))
|
|
1262
|
+
if (m >> toU64(60) !== toU64(0)) {
|
|
1263
|
+
scale = scale + 4
|
|
1264
|
+
sticky = sticky || d !== 0
|
|
1265
|
+
} else {
|
|
1266
|
+
m = (m << toU64(4)) | toU64(d)
|
|
1267
|
+
}
|
|
1268
|
+
k = k + 1
|
|
1269
|
+
}
|
|
1270
|
+
const bits: i32 = m === toU64(0) ? 0 : 64 - clz64(m)
|
|
1271
|
+
if (bits > 53) {
|
|
1272
|
+
const shift: i32 = bits - 53
|
|
1273
|
+
const low: u64 = m & ((toU64(1) << toU64(shift)) - toU64(1))
|
|
1274
|
+
const half: u64 = toU64(1) << toU64(shift - 1)
|
|
1275
|
+
m = m >> toU64(shift)
|
|
1276
|
+
if (low > half || (low === half && (sticky || (m & toU64(1)) !== toU64(0)))) {
|
|
1277
|
+
m = m + toU64(1)
|
|
1278
|
+
}
|
|
1279
|
+
scale = scale + shift
|
|
1280
|
+
}
|
|
1281
|
+
// At most 2^53, so exact; each doubling is exact until it overflows.
|
|
1282
|
+
let v: f64 = toF64(m)
|
|
1283
|
+
while (scale > 0 && v <= 1.7976931348623157e308) {
|
|
1284
|
+
v = v * 2.0
|
|
1285
|
+
scale = scale - 1
|
|
1286
|
+
}
|
|
1287
|
+
return v
|
|
1288
|
+
}
|
|
1289
|
+
|
|
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
|
|
1293
|
+
|
|
1294
|
+
/**
|
|
1295
|
+
* `Number(s)` of a string under `--deterministic` (WP50 §9): the grammar the
|
|
1296
|
+
* `--emit-ts` reading has, on every target. A `0x` integer after optional
|
|
1297
|
+
* ASCII whitespace and sign is read here, correctly rounded whatever the libc,
|
|
1298
|
+
* and anything but whitespace after its digits, a hex float's `.` or `p`
|
|
1299
|
+
* included, makes the string NaN. Every other spelling is the runtime's
|
|
1300
|
+
* `Number`, which already agrees across targets.
|
|
1301
|
+
*
|
|
1302
|
+
* @param s The string to read.
|
|
1303
|
+
* @returns The number `s` spells, or NaN.
|
|
1304
|
+
*/
|
|
1305
|
+
export const numberExact = (s: string): f64 => {
|
|
1306
|
+
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)) {
|
|
1310
|
+
return Number(s)
|
|
1311
|
+
}
|
|
1312
|
+
const end: i32 = hexEnd(s, q + 2)
|
|
1313
|
+
if (end === q + 2 || skipSpaces(s, end) !== toI32(s.length)) {
|
|
1314
|
+
const zero: f64 = 0.0
|
|
1315
|
+
return zero / zero
|
|
1316
|
+
}
|
|
1317
|
+
const v: f64 = hexValue(s, q + 2, end)
|
|
1318
|
+
return negative ? -v : v
|
|
1319
|
+
}
|
|
1320
|
+
|
|
1321
|
+
/**
|
|
1322
|
+
* `parseFloat(s)` under `--deterministic` (WP50 §9): the grammar the
|
|
1323
|
+
* `--emit-ts` reading has, on every target. A `0x` integer after optional
|
|
1324
|
+
* ASCII whitespace and sign is read here, correctly rounded whatever the libc,
|
|
1325
|
+
* and reading stops at its last digit, so `0x1p3` is 1; `0x` with no digit is
|
|
1326
|
+
* the `0` before it. Every other spelling is the runtime's `parseFloat`, which
|
|
1327
|
+
* already agrees across targets.
|
|
1328
|
+
*
|
|
1329
|
+
* @param s The string to read.
|
|
1330
|
+
* @returns The number at the start of `s`, or NaN.
|
|
1331
|
+
*/
|
|
1332
|
+
export const parseFloatExact = (s: string): f64 => {
|
|
1333
|
+
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)) {
|
|
1337
|
+
return parseFloat(s)
|
|
1338
|
+
}
|
|
1339
|
+
const end: i32 = hexEnd(s, q + 2)
|
|
1340
|
+
const v: f64 = end === q + 2 ? 0.0 : hexValue(s, q + 2, end)
|
|
1341
|
+
return negative ? -v : v
|
|
1342
|
+
}
|