assemblyscript
Version:
A TypeScript-like language for WebAssembly.
1,390 lines (1,284 loc) • 106 kB
text/typescript
import { Math as JSMath } from "./bindings/dom";
export { JSMath };
import {
pow_lut, exp_lut, exp2_lut, log_lut, log2_lut,
powf_lut, expf_lut, exp2f_lut, logf_lut, log2f_lut
} from "./util/math";
import {
abs as builtin_abs,
ceil as builtin_ceil,
clz as builtin_clz,
copysign as builtin_copysign,
floor as builtin_floor,
max as builtin_max,
min as builtin_min,
sqrt as builtin_sqrt,
trunc as builtin_trunc
} from "./builtins";
// SUN COPYRIGHT NOTICE
//
// Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
// Developed at SunPro, a Sun Microsystems, Inc. business.
// Permission to use, copy, modify, and distribute this software
// is freely granted, provided that this notice is preserved.
//
// Applies to all functions marked with a comment referring here.
/** @internal */
// @ts-ignore: decorator
@lazy let rempio2_y0: f64, rempio2_y1: f64, res128_hi: u64;
/** @internal */
// @ts-ignore: decorator
@lazy @inline const PIO2_TABLE = memory.data<u64>([
0x00000000A2F9836E, 0x4E441529FC2757D1, 0xF534DDC0DB629599, 0x3C439041FE5163AB,
0xDEBBC561B7246E3A, 0x424DD2E006492EEA, 0x09D1921CFE1DEB1C, 0xB129A73EE88235F5,
0x2EBB4484E99C7026, 0xB45F7E413991D639, 0x835339F49C845F8B, 0xBDF9283B1FF897FF,
0xDE05980FEF2F118B, 0x5A0A6D1F6D367ECF, 0x27CB09B74F463F66, 0x9E5FEA2D7527BAC7,
0xEBE5F17B3D0739F7, 0x8A5292EA6BFB5FB1, 0x1F8D5D0856033046, 0xFC7B6BABF0CFBC20,
0x9AF4361DA9E39161, 0x5EE61B086599855F, 0x14A068408DFFD880, 0x4D73273106061557
]);
/** @internal */
function R(z: f64): f64 { // Rational approximation of (asin(x)-x)/x^3
const // see: musl/src/math/asin.c and SUN COPYRIGHT NOTICE above
pS0 = reinterpret<f64>(0x3FC5555555555555), // 1.66666666666666657415e-01
pS1 = reinterpret<f64>(0xBFD4D61203EB6F7D), // -3.25565818622400915405e-01
pS2 = reinterpret<f64>(0x3FC9C1550E884455), // 2.01212532134862925881e-01
pS3 = reinterpret<f64>(0xBFA48228B5688F3B), // -4.00555345006794114027e-02
pS4 = reinterpret<f64>(0x3F49EFE07501B288), // 7.91534994289814532176e-04
pS5 = reinterpret<f64>(0x3F023DE10DFDF709), // 3.47933107596021167570e-05
qS1 = reinterpret<f64>(0xC0033A271C8A2D4B), // -2.40339491173441421878e+00
qS2 = reinterpret<f64>(0x40002AE59C598AC8), // 2.02094576023350569471e+00
qS3 = reinterpret<f64>(0xBFE6066C1B8D0159), // -6.88283971605453293030e-01
qS4 = reinterpret<f64>(0x3FB3B8C5B12E9282); // 7.70381505559019352791e-02
let p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
let q = 1.0 + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
return p / q;
}
/** @internal */
// @ts-ignore: decorator
@inline
function expo2(x: f64, sign: f64): f64 { // exp(x)/2 for x >= log(DBL_MAX)
const // see: musl/src/math/__expo2.c
k = <u32>2043,
kln2 = reinterpret<f64>(0x40962066151ADD8B); // 0x1.62066151add8bp+10
let scale = reinterpret<f64>(<u64>((<u32>0x3FF + k / 2) << 20) << 32);
// in directed rounding correct sign before rounding or overflow is important
return NativeMath.exp(x - kln2) * (sign * scale) * scale;
}
/** @internal */
/* Helper function to eventually get bits of π/2 * |x|
*
* y = π/4 * (frac << clz(frac) >> 11)
* return clz(frac)
*
* Right shift 11 bits to make upper half fit in `double`
*/
// @ts-ignore: decorator
@inline
function pio2_right(q0: u64, q1: u64): u64 { // see: jdh8/metallic/blob/master/src/math/double/rem_pio2.c
// Bits of π/4
const p0: u64 = 0xC4C6628B80DC1CD1;
const p1: u64 = 0xC90FDAA22168C234;
const Ox1p_64 = reinterpret<f64>(0x3BF0000000000000); // 0x1p-64
const Ox1p_75 = reinterpret<f64>(0x3B40000000000000); // 0x1p-75
let shift = clz(q1);
q1 = q1 << shift | q0 >> (64 - shift);
q0 <<= shift;
let lo = umuldi(p1, q1);
let hi = res128_hi;
let ahi = hi >> 11;
let alo = lo >> 11 | hi << 53;
let blo = <u64>(Ox1p_75 * <f64>p0 * <f64>q1 + Ox1p_75 * <f64>p1 * <f64>q0);
rempio2_y0 = <f64>(ahi + u64(lo < blo));
rempio2_y1 = Ox1p_64 * <f64>(alo + blo);
return shift;
}
/** @internal */
// @ts-ignore: decorator
@inline
function umuldi(u: u64, v: u64): u64 {
let u1: u64 , v1: u64, w0: u64, w1: u64, t: u64;
u1 = u & 0xFFFFFFFF;
v1 = v & 0xFFFFFFFF;
u >>= 32;
v >>= 32;
t = u1 * v1;
w0 = t & 0xFFFFFFFF;
t = u * v1 + (t >> 32);
w1 = t >> 32;
t = u1 * v + (t & 0xFFFFFFFF);
res128_hi = u * v + w1 + (t >> 32);
return (t << 32) + w0;
}
/** @internal */
function pio2_large_quot(x: f64, u: i64): i32 { // see: jdh8/metallic/blob/master/src/math/double/rem_pio2.c
let magnitude = u & 0x7FFFFFFFFFFFFFFF;
let offset = (magnitude >> 52) - 1045;
let shift = offset & 63;
let tblPtr = PIO2_TABLE + (<i32>(offset >> 6) << 3);
let s0: u64, s1: u64, s2: u64;
let b0 = load<u64>(tblPtr, 0 << 3);
let b1 = load<u64>(tblPtr, 1 << 3);
let b2 = load<u64>(tblPtr, 2 << 3);
// Get 192 bits of 0x1p-31 / π with `offset` bits skipped
if (shift) {
let rshift = 64 - shift;
let b3 = load<u64>(tblPtr, 3 << 3);
s0 = b1 >> rshift | b0 << shift;
s1 = b2 >> rshift | b1 << shift;
s2 = b3 >> rshift | b2 << shift;
} else {
s0 = b0;
s1 = b1;
s2 = b2;
}
let significand = (u & 0x000FFFFFFFFFFFFF) | 0x0010000000000000;
// First 128 bits of fractional part of x/(2π)
let blo = umuldi(s1, significand);
let bhi = res128_hi;
let ahi = s0 * significand;
let clo = (s2 >> 32) * (significand >> 32);
let plo = blo + clo;
let phi = ahi + bhi + u64(plo < clo);
// r: u128 = p << 2
let rlo = plo << 2;
let rhi = phi << 2 | plo >> 62;
// s: i128 = r >> 127
let slo = <i64>rhi >> 63;
let shi = slo >> 1;
let q = (<i64>phi >> 62) - slo;
let shifter = 0x3CB0000000000000 - (pio2_right(rlo ^ slo, rhi ^ shi) << 52);
let signbit = (u ^ rhi) & 0x8000000000000000;
let coeff = reinterpret<f64>(shifter | signbit);
rempio2_y0 *= coeff;
rempio2_y1 *= coeff;
return <i32>q;
}
/** @internal */
// @ts-ignore: decorator
@inline
function rempio2(x: f64, u: u64, sign: i32): i32 {
const
pio2_1 = reinterpret<f64>(0x3FF921FB54400000), // 1.57079632673412561417e+00
pio2_1t = reinterpret<f64>(0x3DD0B4611A626331), // 6.07710050650619224932e-11
pio2_2 = reinterpret<f64>(0x3DD0B4611A600000), // 6.07710050630396597660e-11
pio2_2t = reinterpret<f64>(0x3BA3198A2E037073), // 2.02226624879595063154e-21
pio2_3 = reinterpret<f64>(0x3BA3198A2E000000), // 2.02226624871116645580e-21
pio2_3t = reinterpret<f64>(0x397B839A252049C1), // 8.47842766036889956997e-32
invpio2 = reinterpret<f64>(0x3FE45F306DC9C883); // 0.63661977236758134308
let ix = <u32>(u >> 32) & 0x7FFFFFFF;
if (ASC_SHRINK_LEVEL < 1) {
if (ix < 0x4002D97C) { // |x| < 3pi/4, special case with n=+-1
let q = 1, z: f64, y0: f64, y1: f64;
if (!sign) {
z = x - pio2_1;
if (ix != 0x3FF921FB) { // 33+53 bit pi is good enough
y0 = z - pio2_1t;
y1 = (z - y0) - pio2_1t;
} else { // near pi/2, use 33+33+53 bit pi
z -= pio2_2;
y0 = z - pio2_2t;
y1 = (z - y0) - pio2_2t;
}
} else { // negative x
z = x + pio2_1;
if (ix != 0x3FF921FB) { // 33+53 bit pi is good enough
y0 = z + pio2_1t;
y1 = (z - y0) + pio2_1t;
} else { // near pi/2, use 33+33+53 bit pi
z += pio2_2;
y0 = z + pio2_2t;
y1 = (z - y0) + pio2_2t;
}
q = -1;
}
rempio2_y0 = y0;
rempio2_y1 = y1;
return q;
}
}
if (ix < 0x413921FB) { // |x| ~< 2^20*pi/2 (1647099)
// Use precise Cody Waite scheme
let q = nearest(x * invpio2);
let r = x - q * pio2_1;
let w = q * pio2_1t; // 1st round good to 85 bit
let j = ix >> 20;
let y0 = r - w;
let hi = <u32>(reinterpret<u64>(y0) >> 32);
let i = j - ((hi >> 20) & 0x7FF);
if (i > 16) { // 2nd iteration needed, good to 118
let t = r;
w = q * pio2_2;
r = t - w;
w = q * pio2_2t - ((t - r) - w);
y0 = r - w;
hi = <u32>(reinterpret<u64>(y0) >> 32);
i = j - ((hi >> 20) & 0x7FF);
if (i > 49) { // 3rd iteration need, 151 bits acc
let t = r;
w = q * pio2_3;
r = t - w;
w = q * pio2_3t - ((t - r) - w);
y0 = r - w;
}
}
let y1 = (r - y0) - w;
rempio2_y0 = y0;
rempio2_y1 = y1;
return <i32>q;
}
let q = pio2_large_quot(x, u);
return select(-q, q, sign);
}
/** @internal */
// @ts-ignore: decorator
@inline
function sin_kern(x: f64, y: f64, iy: i32): f64 { // see: musl/tree/src/math/__sin.c
const
S1 = reinterpret<f64>(0xBFC5555555555549), // -1.66666666666666324348e-01
S2 = reinterpret<f64>(0x3F8111111110F8A6), // 8.33333333332248946124e-03
S3 = reinterpret<f64>(0xBF2A01A019C161D5), // -1.98412698298579493134e-04
S4 = reinterpret<f64>(0x3EC71DE357B1FE7D), // 2.75573137070700676789e-06
S5 = reinterpret<f64>(0xBE5AE5E68A2B9CEB), // -2.50507602534068634195e-08
S6 = reinterpret<f64>(0x3DE5D93A5ACFD57C); // 1.58969099521155010221e-10
let z = x * x;
let w = z * z;
let r = S2 + z * (S3 + z * S4) + z * w * (S5 + z * S6);
let v = z * x;
if (!iy) {
return x + v * (S1 + z * r);
} else {
return x - ((z * (0.5 * y - v * r) - y) - v * S1);
}
}
/** @internal */
// @ts-ignore: decorator
@inline
function cos_kern(x: f64, y: f64): f64 { // see: musl/tree/src/math/__cos.c
const
C1 = reinterpret<f64>(0x3FA555555555554C), // 4.16666666666666019037e-02
C2 = reinterpret<f64>(0xBF56C16C16C15177), // -1.38888888888741095749e-03
C3 = reinterpret<f64>(0x3EFA01A019CB1590), // 2.48015872894767294178e-05
C4 = reinterpret<f64>(0xBE927E4F809C52AD), // -2.75573143513906633035e-07
C5 = reinterpret<f64>(0x3E21EE9EBDB4B1C4), // 2.08757232129817482790e-09
C6 = reinterpret<f64>(0xBDA8FAE9BE8838D4); // -1.13596475577881948265e-11
let z = x * x;
let w = z * z;
let r = z * (C1 + z * (C2 + z * C3)) + w * w * (C4 + z * (C5 + z * C6));
let hz = 0.5 * z;
w = 1.0 - hz;
return w + (((1.0 - w) - hz) + (z * r - x * y));
}
/** @internal */
function tan_kern(x: f64, y: f64, iy: i32): f64 { // see: src/lib/msun/src/k_tan.c
const
T0 = reinterpret<f64>(0x3FD5555555555563), // 3.33333333333334091986e-01
T1 = reinterpret<f64>(0x3FC111111110FE7A), // 1.33333333333201242699e-01
T2 = reinterpret<f64>(0x3FABA1BA1BB341FE), // 5.39682539762260521377e-02
T3 = reinterpret<f64>(0x3F9664F48406D637), // 2.18694882948595424599e-02
T4 = reinterpret<f64>(0x3F8226E3E96E8493), // 8.86323982359930005737e-03
T5 = reinterpret<f64>(0x3F6D6D22C9560328), // 3.59207910759131235356e-03
T6 = reinterpret<f64>(0x3F57DBC8FEE08315), // 1.45620945432529025516e-03
T7 = reinterpret<f64>(0x3F4344D8F2F26501), // 5.88041240820264096874e-04
T8 = reinterpret<f64>(0x3F3026F71A8D1068), // 2.46463134818469906812e-04
T9 = reinterpret<f64>(0x3F147E88A03792A6), // 7.81794442939557092300e-05
T10 = reinterpret<f64>(0x3F12B80F32F0A7E9), // 7.14072491382608190305e-05
T11 = reinterpret<f64>(0xBEF375CBDB605373), // -1.85586374855275456654e-05
T12 = reinterpret<f64>(0x3EFB2A7074BF7AD4); // 2.59073051863633712884e-05
const
one = reinterpret<f64>(0x3FF0000000000000), // 1.00000000000000000000e+00
pio4 = reinterpret<f64>(0x3FE921FB54442D18), // 7.85398163397448278999e-01
pio4lo = reinterpret<f64>(0x3C81A62633145C07); // 3.06161699786838301793e-17
let z: f64, r: f64, v: f64, w: f64, s: f64;
let hx = <i32>(reinterpret<u64>(x) >> 32); // high word of x
let ix = hx & 0x7FFFFFFF; // high word of |x|
let big = ix >= 0x3FE59428;
if (big) { // |x| >= 0.6744
if (hx < 0) { x = -x, y = -y; }
z = pio4 - x;
w = pio4lo - y;
x = z + w;
y = 0.0;
}
z = x * x;
w = z * z;
r = T1 + w * (T3 + w * (T5 + w * (T7 + w * (T9 + w * T11))));
v = z * (T2 + w * (T4 + w * (T6 + w * (T8 + w * (T10 + w * T12)))));
s = z * x;
r = y + z * (s * (r + v) + y);
r += T0 * s;
w = x + r;
if (big) {
v = iy;
return (1 - ((hx >> 30) & 2)) * (v - 2.0 * (x - (w * w / (w + v) - r)));
}
if (iy == 1) return w;
let a: f64, t: f64;
z = w;
z = reinterpret<f64>(reinterpret<u64>(z) & 0xFFFFFFFF00000000);
v = r - (z - x); // z + v = r + x
t = a = -one / w; // a = -1.0 / w
t = reinterpret<f64>(reinterpret<u64>(t) & 0xFFFFFFFF00000000);
s = one + t * z;
return t + a * (s + t * v);
}
/** @internal */
function dtoi32(x: f64): i32 {
if (ASC_SHRINK_LEVEL > 0) {
const inv32 = 1.0 / 4294967296;
return <i32><i64>(x - 4294967296 * floor(x * inv32));
} else {
let result = 0;
let u = reinterpret<u64>(x);
let e = (u >> 52) & 0x7FF;
if (e <= 1023 + 30) {
result = <i32>x;
} else if (e <= 1023 + 30 + 53) {
let v = (u & ((<u64>1 << 52) - 1)) | (<u64>1 << 52);
v = v << e - 1023 - 52 + 32;
result = <i32>(v >> 32);
result = select<i32>(-result, result, <i64>u < 0);
}
return result;
}
}
// @ts-ignore: decorator
@lazy let random_seeded = false;
// @ts-ignore: decorator
@lazy let random_state0_64: u64, random_state1_64: u64;
// @ts-ignore: decorator
@lazy let random_state0_32: u32, random_state1_32: u32;
function murmurHash3(h: u64): u64 { // Force all bits of a hash block to avalanche
h ^= h >> 33; // see: https://github.com/aappleby/smhasher
h *= 0xFF51AFD7ED558CCD;
h ^= h >> 33;
h *= 0xC4CEB9FE1A85EC53;
h ^= h >> 33;
return h;
}
function splitMix32(h: u32): u32 {
h += 0x6D2B79F5;
h = (h ^ (h >> 15)) * (h | 1);
h ^= h + (h ^ (h >> 7)) * (h | 61);
return h ^ (h >> 14);
}
export namespace NativeMath {
// @ts-ignore: decorator
@lazy
export const E = reinterpret<f64>(0x4005BF0A8B145769); // 2.7182818284590452354
// @ts-ignore: decorator
@lazy
export const LN2 = reinterpret<f64>(0x3FE62E42FEFA39EF); // 0.69314718055994530942
// @ts-ignore: decorator
@lazy
export const LN10 = reinterpret<f64>(0x40026BB1BBB55516); // 2.30258509299404568402
// @ts-ignore: decorator
@lazy
export const LOG2E = reinterpret<f64>(0x3FF71547652B82FE); // 1.4426950408889634074
// @ts-ignore: decorator
@lazy
export const LOG10E = reinterpret<f64>(0x3FDBCB7B1526E50E); // 0.43429448190325182765
// @ts-ignore: decorator
@lazy
export const PI = reinterpret<f64>(0x400921FB54442D18); // 3.14159265358979323846
// @ts-ignore: decorator
@lazy
export const SQRT1_2 = reinterpret<f64>(0x3FE6A09E667F3BCD); // 0.70710678118654752440
// @ts-ignore: decorator
@lazy
export const SQRT2 = reinterpret<f64>(0x3FF6A09E667F3BCD); // 1.41421356237309504880
// @ts-ignore: decorator
@lazy
export let sincos_sin: f64 = 0;
// @ts-ignore: decorator
@lazy
export let sincos_cos: f64 = 0;
// @ts-ignore: decorator
@inline export function abs(x: f64): f64 {
return builtin_abs<f64>(x);
}
export function acos(x: f64): f64 { // see: musl/src/math/acos.c and SUN COPYRIGHT NOTICE above
const
pio2_hi = reinterpret<f64>(0x3FF921FB54442D18), // 1.57079632679489655800e+00
pio2_lo = reinterpret<f64>(0x3C91A62633145C07), // 6.12323399573676603587e-17
Ox1p_120f = reinterpret<f32>(0x03800000);
let hx = <u32>(reinterpret<u64>(x) >> 32);
let ix = hx & 0x7FFFFFFF;
if (ix >= 0x3FF00000) {
let lx = <u32>reinterpret<u64>(x);
if ((ix - 0x3FF00000 | lx) == 0) {
if (<i32>hx < 0) return 2 * pio2_hi + Ox1p_120f;
return 0;
}
return 0 / (x - x);
}
if (ix < 0x3FE00000) {
if (ix <= 0x3C600000) return pio2_hi + Ox1p_120f;
return pio2_hi - (x - (pio2_lo - x * R(x * x)));
}
let s: f64, w: f64, z: f64;
if (<i32>hx < 0) {
// z = (1.0 + x) * 0.5;
z = 0.5 + x * 0.5;
s = builtin_sqrt<f64>(z);
w = R(z) * s - pio2_lo;
return 2 * (pio2_hi - (s + w));
}
// z = (1.0 - x) * 0.5;
z = 0.5 - x * 0.5;
s = builtin_sqrt<f64>(z);
let df = reinterpret<f64>(reinterpret<u64>(s) & 0xFFFFFFFF00000000);
let c = (z - df * df) / (s + df);
w = R(z) * s + c;
return 2 * (df + w);
}
export function acosh(x: f64): f64 { // see: musl/src/math/acosh.c
const s = reinterpret<f64>(0x3FE62E42FEFA39EF);
let u = reinterpret<u64>(x);
// Prevent propagation for all input values less than 1.0.
// Note musl lib didn't fix this yet.
if (<i64>u < 0x3FF0000000000000) return (x - x) / 0.0;
let e = u >> 52 & 0x7FF;
if (e < 0x3FF + 1) return log1p(x - 1 + builtin_sqrt<f64>((x - 1) * (x - 1) + 2 * (x - 1)));
if (e < 0x3FF + 26) return log(2 * x - 1 / (x + builtin_sqrt<f64>(x * x - 1)));
return log(x) + s;
}
export function asin(x: f64): f64 { // see: musl/src/math/asin.c and SUN COPYRIGHT NOTICE above
const
pio2_hi = reinterpret<f64>(0x3FF921FB54442D18), // 1.57079632679489655800e+00
pio2_lo = reinterpret<f64>(0x3C91A62633145C07), // 6.12323399573676603587e-17
Ox1p_120f = reinterpret<f32>(0x03800000);
let hx = <u32>(reinterpret<u64>(x) >> 32);
let ix = hx & 0x7FFFFFFF;
if (ix >= 0x3FF00000) {
let lx = <u32>reinterpret<u64>(x);
if ((ix - 0x3FF00000 | lx) == 0) return x * pio2_hi + Ox1p_120f;
return 0 / (x - x);
}
if (ix < 0x3FE00000) {
if (ix < 0x3E500000 && ix >= 0x00100000) return x;
return x + x * R(x * x);
}
// let z = (1.0 - builtin_abs<f64>(x)) * 0.5;
let z = 0.5 - builtin_abs<f64>(x) * 0.5;
let s = builtin_sqrt<f64>(z);
let r = R(z);
if (ix >= 0x3FEF3333) x = pio2_hi - (2 * (s + s * r) - pio2_lo);
else {
let f = reinterpret<f64>(reinterpret<u64>(s) & 0xFFFFFFFF00000000);
let c = (z - f * f) / (s + f);
x = 0.5 * pio2_hi - (2 * s * r - (pio2_lo - 2 * c) - (0.5 * pio2_hi - 2 * f));
}
return select(-x, x, <i32>hx < 0);
}
export function asinh(x: f64): f64 { // see: musl/src/math/asinh.c
const c = reinterpret<f64>(0x3FE62E42FEFA39EF); // 0.693147180559945309417232121458176568
let u = reinterpret<u64>(x);
let e = u >> 52 & 0x7FF;
let y = reinterpret<f64>(u & 0x7FFFFFFFFFFFFFFF);
if (e >= 0x3FF + 26) y = log(y) + c;
else if (e >= 0x3FF + 1) y = log(2 * y + 1 / (builtin_sqrt<f64>(y * y + 1) + y));
else if (e >= 0x3FF - 26) y = log1p(y + y * y / (builtin_sqrt<f64>(y * y + 1) + 1));
return builtin_copysign(y, x);
}
export function atan(x: f64): f64 { // see musl/src/math/atan.c and SUN COPYRIGHT NOTICE above
const
atanhi0 = reinterpret<f64>(0x3FDDAC670561BB4F), // 4.63647609000806093515e-01
atanhi1 = reinterpret<f64>(0x3FE921FB54442D18), // 7.85398163397448278999e-01
atanhi2 = reinterpret<f64>(0x3FEF730BD281F69B), // 9.82793723247329054082e-01
atanhi3 = reinterpret<f64>(0x3FF921FB54442D18), // 1.57079632679489655800e+00
atanlo0 = reinterpret<f64>(0x3C7A2B7F222F65E2), // 2.26987774529616870924e-17
atanlo1 = reinterpret<f64>(0x3C81A62633145C07), // 3.06161699786838301793e-17
atanlo2 = reinterpret<f64>(0x3C7007887AF0CBBD), // 1.39033110312309984516e-17
atanlo3 = reinterpret<f64>(0x3C91A62633145C07), // 6.12323399573676603587e-17
aT0 = reinterpret<f64>(0x3FD555555555550D), // 3.33333333333329318027e-01
aT1 = reinterpret<f64>(0xBFC999999998EBC4), // -1.99999999998764832476e-01
aT2 = reinterpret<f64>(0x3FC24924920083FF), // 1.42857142725034663711e-01
aT3 = reinterpret<f64>(0xBFBC71C6FE231671), // -1.11111104054623557880e-01,
aT4 = reinterpret<f64>(0x3FB745CDC54C206E), // 9.09088713343650656196e-02
aT5 = reinterpret<f64>(0xBFB3B0F2AF749A6D), // -7.69187620504482999495e-02
aT6 = reinterpret<f64>(0x3FB10D66A0D03D51), // 6.66107313738753120669e-02
aT7 = reinterpret<f64>(0xBFADDE2D52DEFD9A), // -5.83357013379057348645e-02
aT8 = reinterpret<f64>(0x3FA97B4B24760DEB), // 4.97687799461593236017e-02
aT9 = reinterpret<f64>(0xBFA2B4442C6A6C2F), // -3.65315727442169155270e-02
aT10 = reinterpret<f64>(0x3F90AD3AE322DA11), // 1.62858201153657823623e-02
Ox1p_120f = reinterpret<f32>(0x03800000);
let ix = <u32>(reinterpret<u64>(x) >> 32);
let sx = x;
ix &= 0x7FFFFFFF;
let z: f64;
if (ix >= 0x44100000) {
if (isNaN(x)) return x;
z = atanhi3 + Ox1p_120f;
return builtin_copysign<f64>(z, sx);
}
let id: i32;
if (ix < 0x3FDC0000) {
if (ix < 0x3E400000) return x;
id = -1;
} else {
x = builtin_abs<f64>(x);
if (ix < 0x3FF30000) {
if (ix < 0x3FE60000) {
id = 0;
x = (2.0 * x - 1.0) / (2.0 + x);
} else {
id = 1;
x = (x - 1.0) / (x + 1.0);
}
} else {
if (ix < 0x40038000) {
id = 2;
x = (x - 1.5) / (1.0 + 1.5 * x);
} else {
id = 3;
x = -1.0 / x;
}
}
}
z = x * x;
let w = z * z;
let s1 = z * (aT0 + w * (aT2 + w * (aT4 + w * (aT6 + w * (aT8 + w * aT10)))));
let s2 = w * (aT1 + w * (aT3 + w * (aT5 + w * (aT7 + w * aT9))));
let s3 = x * (s1 + s2);
if (id < 0) return x - s3;
switch (id) {
case 0: { z = atanhi0 - ((s3 - atanlo0) - x); break; }
case 1: { z = atanhi1 - ((s3 - atanlo1) - x); break; }
case 2: { z = atanhi2 - ((s3 - atanlo2) - x); break; }
case 3: { z = atanhi3 - ((s3 - atanlo3) - x); break; }
default: unreachable();
}
return builtin_copysign<f64>(z, sx);
}
export function atanh(x: f64): f64 { // see: musl/src/math/atanh.c
let u = reinterpret<u64>(x);
let e = u >> 52 & 0x7FF;
let y = builtin_abs(x);
if (e < 0x3FF - 1) {
if (e >= 0x3FF - 32) y = 0.5 * log1p(2 * y + 2 * y * y / (1 - y));
} else {
y = 0.5 * log1p(2 * (y / (1 - y)));
}
return builtin_copysign<f64>(y, x);
}
export function atan2(y: f64, x: f64): f64 { // see: musl/src/math/atan2.c and SUN COPYRIGHT NOTICE above
const pi_lo = reinterpret<f64>(0x3CA1A62633145C07); // 1.2246467991473531772E-16
if (isNaN(x) || isNaN(y)) return x + y;
let u = reinterpret<u64>(x);
let ix = <u32>(u >> 32);
let lx = <u32>u;
u = reinterpret<u64>(y);
let iy = <u32>(u >> 32);
let ly = <u32>u;
if ((ix - 0x3FF00000 | lx) == 0) return atan(y);
let m = ((iy >> 31) & 1) | ((ix >> 30) & 2);
ix = ix & 0x7FFFFFFF;
iy = iy & 0x7FFFFFFF;
if ((iy | ly) == 0) {
switch (m) {
case 0:
case 1: return y;
case 2: return PI;
case 3: return -PI;
}
}
if ((ix | lx) == 0) return m & 1 ? -PI / 2 : PI / 2;
if (ix == 0x7FF00000) {
if (iy == 0x7FF00000) {
let t = m & 2 ? 3 * PI / 4 : PI / 4;
return m & 1 ? -t : t;
} else {
let t = m & 2 ? PI : 0;
return m & 1 ? -t : t;
}
}
let z: f64;
if (ix + (64 << 20) < iy || iy == 0x7FF00000) return m & 1 ? -PI / 2 : PI / 2;
if ((m & 2) && iy + (64 << 20) < ix) z = 0;
else z = atan(builtin_abs<f64>(y / x));
switch (m) {
case 0: return z;
case 1: return -z;
case 2: return PI - (z - pi_lo);
case 3: return (z - pi_lo) - PI;
}
unreachable();
return 0;
}
export function cbrt(x: f64): f64 { // see: musl/src/math/cbrt.c and SUN COPYRIGHT NOTICE above
const
B1 = <u32>715094163,
B2 = <u32>696219795,
P0 = reinterpret<f64>(0x3FFE03E60F61E692), // 1.87595182427177009643
P1 = reinterpret<f64>(0xBFFE28E092F02420), // -1.88497979543377169875
P2 = reinterpret<f64>(0x3FF9F1604A49D6C2), // 1.621429720105354466140
P3 = reinterpret<f64>(0xBFE844CBBEE751D9), // -0.758397934778766047437
P4 = reinterpret<f64>(0x3FC2B000D4E4EDD7), // 0.145996192886612446982
Ox1p54 = reinterpret<f64>(0x4350000000000000); // 0x1p54
let u = reinterpret<u64>(x);
let hx = <u32>(u >> 32) & 0x7FFFFFFF;
if (hx >= 0x7FF00000) return x + x;
if (hx < 0x00100000) {
u = reinterpret<u64>(x * Ox1p54);
hx = <u32>(u >> 32) & 0x7FFFFFFF;
if (hx == 0) return x;
hx = hx / 3 + B2;
} else {
hx = hx / 3 + B1;
}
u &= 1 << 63;
u |= <u64>hx << 32;
let t = reinterpret<f64>(u);
let r = (t * t) * (t / x);
t = t * ((P0 + r * (P1 + r * P2)) + ((r * r) * r) * (P3 + r * P4));
t = reinterpret<f64>((reinterpret<u64>(t) + 0x80000000) & 0xFFFFFFFFC0000000);
let s = t * t;
r = x / s;
r = (r - t) / (2 * t + r);
t = t + t * r;
return t;
}
// @ts-ignore: decorator
@inline
export function ceil(x: f64): f64 {
return builtin_ceil<f64>(x);
}
export function clz32(x: f64): f64 {
if (!isFinite(x)) return 32;
/*
* Wasm (MVP) and JS have different approaches for double->int conversions.
*
* For emulate JS conversion behavior and avoid trapping from wasm we should modulate by MAX_INT
* our float-point arguments before actual convertion to integers.
*/
return builtin_clz(dtoi32(x));
}
export function cos(x: f64): f64 { // see: musl/src/math/cos.c
let u = reinterpret<u64>(x);
let ux = u32(u >> 32);
let sign = ux >> 31;
ux &= 0x7FFFFFFF;
// |x| ~< pi/4
if (ux <= 0x3FE921FB) {
if (ux < 0x3E46A09E) { // |x| < 2**-27 * sqrt(2)
return 1.0;
}
return cos_kern(x, 0);
}
// sin(Inf or NaN) is NaN
if (ux >= 0x7FF00000) return x - x;
// argument reduction needed
let n = rempio2(x, u, sign);
let y0 = rempio2_y0;
let y1 = rempio2_y1;
x = n & 1 ? sin_kern(y0, y1, 1) : cos_kern(y0, y1);
return (n + 1) & 2 ? -x : x;
}
export function cosh(x: f64): f64 { // see: musl/src/math/cosh.c
let u = reinterpret<u64>(x);
u &= 0x7FFFFFFFFFFFFFFF;
x = reinterpret<f64>(u);
let w = <u32>(u >> 32);
let t: f64;
if (w < 0x3FE62E42) {
if (w < 0x3FF00000 - (26 << 20)) return 1;
t = expm1(x);
// return 1 + t * t / (2 * (1 + t));
return 1 + t * t / (2 + 2 * t);
}
if (w < 0x40862E42) {
t = exp(x);
return 0.5 * (t + 1 / t);
}
t = expo2(x, 1);
return t;
}
export function exp(x: f64): f64 { // see: musl/src/math/exp.c and SUN COPYRIGHT NOTICE above
if (ASC_SHRINK_LEVEL < 1) {
return exp_lut(x);
} else {
const
ln2hi = reinterpret<f64>(0x3FE62E42FEE00000), // 6.93147180369123816490e-01
ln2lo = reinterpret<f64>(0x3DEA39EF35793C76), // 1.90821492927058770002e-10
invln2 = reinterpret<f64>(0x3FF71547652B82FE), // 1.44269504088896338700e+00
P1 = reinterpret<f64>(0x3FC555555555553E), // 1.66666666666666019037e-01
P2 = reinterpret<f64>(0xBF66C16C16BEBD93), // -2.77777777770155933842e-03
P3 = reinterpret<f64>(0x3F11566AAF25DE2C), // 6.61375632143793436117e-05
P4 = reinterpret<f64>(0xBEBBBD41C5D26BF1), // -1.65339022054652515390e-06
P5 = reinterpret<f64>(0x3E66376972BEA4D0), // 4.13813679705723846039e-08
overflow = reinterpret<f64>(0x40862E42FEFA39EF), // 709.782712893383973096
underflow = reinterpret<f64>(0xC0874910D52D3051), // -745.13321910194110842
Ox1p1023 = reinterpret<f64>(0x7FE0000000000000); // 0x1p1023
let hx = u32(reinterpret<u64>(x) >> 32);
let sign = hx >> 31;
hx &= 0x7FFFFFFF;
if (hx >= 0x4086232B) {
if (isNaN(x)) return x;
if (x > overflow) return x * Ox1p1023;
if (x < underflow) return 0;
}
let hi: f64, lo: f64 = 0;
let k = 0;
if (hx > 0x3FD62E42) {
if (hx >= 0x3FF0A2B2) {
k = i32(invln2 * x + builtin_copysign<f64>(0.5, x));
} else {
k = 1 - (sign << 1);
}
hi = x - k * ln2hi;
lo = k * ln2lo;
x = hi - lo;
} else if (hx > 0x3E300000) {
hi = x;
} else return 1.0 + x;
let xs = x * x;
// let c = x - xp2 * (P1 + xp2 * (P2 + xp2 * (P3 + xp2 * (P4 + xp2 * P5))));
let xq = xs * xs;
let c = x - (xs * P1 + xq * ((P2 + xs * P3) + xq * (P4 + xs * P5)));
let y = 1.0 + (x * c / (2 - c) - lo + hi);
return k == 0 ? y : scalbn(y, k);
}
}
export function exp2(x: f64): f64 {
return exp2_lut(x);
}
export function expm1(x: f64): f64 { // see: musl/src/math/expm1.c and SUN COPYRIGHT NOTICE above
const
o_threshold = reinterpret<f64>(0x40862E42FEFA39EF), // 7.09782712893383973096e+02
ln2_hi = reinterpret<f64>(0x3FE62E42FEE00000), // 6.93147180369123816490e-01
ln2_lo = reinterpret<f64>(0x3DEA39EF35793C76), // 1.90821492927058770002e-10
invln2 = reinterpret<f64>(0x3FF71547652B82FE), // 1.44269504088896338700e+00
Q1 = reinterpret<f64>(0xBFA11111111110F4), // -3.33333333333331316428e-02
Q2 = reinterpret<f64>(0x3F5A01A019FE5585), // 1.58730158725481460165e-03
Q3 = reinterpret<f64>(0xBF14CE199EAADBB7), // -7.93650757867487942473e-05
Q4 = reinterpret<f64>(0x3ED0CFCA86E65239), // 4.00821782732936239552e-06
Q5 = reinterpret<f64>(0xBE8AFDB76E09C32D), // -2.01099218183624371326e-07
Ox1p1023 = reinterpret<f64>(0x7FE0000000000000); // 0x1p1023
let u = reinterpret<u64>(x);
let hx = u32(u >> 32) & 0x7FFFFFFF;
let sign = u32(u >> 63);
let k = 0;
if (hx >= 0x4043687A) {
if (isNaN(x)) return x;
if (sign) return -1;
if (x > o_threshold) return x * Ox1p1023;
}
let c = 0.0, t: f64;
if (hx > 0x3FD62E42) {
k = select<i32>(
1 - (sign << 1),
i32(invln2 * x + builtin_copysign<f64>(0.5, x)),
hx < 0x3FF0A2B2
);
t = <f64>k;
let hi = x - t * ln2_hi;
let lo = t * ln2_lo;
x = hi - lo;
c = (hi - x) - lo;
} else if (hx < 0x3C900000) return x;
let hfx = 0.5 * x;
let hxs = x * hfx;
// let r1 = 1.0 + hxs * (Q1 + hxs * (Q2 + hxs * (Q3 + hxs * (Q4 + hxs * Q5))));
let hxq = hxs * hxs;
let r1 = (1.0 + hxs * Q1) + hxq * ((Q2 + hxs * Q3) + hxq * (Q4 + hxs * Q5));
t = 3.0 - r1 * hfx;
let e = hxs * ((r1 - t) / (6.0 - x * t));
if (k == 0) return x - (x * e - hxs);
e = x * (e - c) - c;
e -= hxs;
if (k == -1) return 0.5 * (x - e) - 0.5;
if (k == 1) {
if (x < -0.25) return -2.0 * (e - (x + 0.5));
return 1.0 + 2.0 * (x - e);
}
u = (0x3FF + k) << 52;
let twopk = reinterpret<f64>(u);
let y: f64;
if (k < 0 || k > 56) {
y = x - e + 1.0;
if (k == 1024) y = y * 2.0 * Ox1p1023;
else y = y * twopk;
return y - 1.0;
}
u = (0x3FF - k) << 52;
y = reinterpret<f64>(u);
if (k < 20) y = (1 - y) - e;
else y = 1 - (e + y);
return (x + y) * twopk;
}
// @ts-ignore: decorator
@inline
export function floor(x: f64): f64 {
return builtin_floor<f64>(x);
}
// @ts-ignore: decorator
@inline
export function fround(x: f64): f64 {
return <f32>x;
}
export function hypot(x: f64, y: f64): f64 { // see: musl/src/math/hypot.c
const
SPLIT = reinterpret<f64>(0x41A0000000000000) + 1, // 0x1p27 + 1
Ox1p700 = reinterpret<f64>(0x6BB0000000000000),
Ox1p_700 = reinterpret<f64>(0x1430000000000000);
let ux = reinterpret<u64>(x);
let uy = reinterpret<u64>(y);
ux &= 0x7FFFFFFFFFFFFFFF;
uy &= 0x7FFFFFFFFFFFFFFF;
if (ux < uy) {
let ut = ux;
ux = uy;
uy = ut;
}
let ex = i32(ux >> 52);
let ey = i32(uy >> 52);
y = reinterpret<f64>(uy);
if (ey == 0x7FF) return y;
x = reinterpret<f64>(ux);
if (ex == 0x7FF || uy == 0) return x;
if (ex - ey > 64) return x + y;
let z = 1.0;
if (ex > 0x3FF + 510) {
z = Ox1p700;
x *= Ox1p_700;
y *= Ox1p_700;
} else if (ey < 0x3FF - 450) {
z = Ox1p_700;
x *= Ox1p700;
y *= Ox1p700;
}
let c = x * SPLIT;
let h = x - c + c;
let l = x - h;
let hx = x * x;
let lx = h * h - hx + (2 * h + l) * l;
c = y * SPLIT;
h = y - c + c;
l = y - h;
let hy = y * y;
let ly = h * h - hy + (2 * h + l) * l;
return z * builtin_sqrt(ly + lx + hy + hx);
}
export function imul(x: f64, y: f64): f64 {
/*
* Wasm (MVP) and JS have different approaches for double->int conversions.
*
* For emulate JS conversion behavior and avoid trapping from wasm we should modulate by MAX_INT
* our float-point arguments before actual convertion to integers.
*/
if (!isFinite(x + y)) return 0;
return dtoi32(x) * dtoi32(y);
}
export function log(x: f64): f64 { // see: musl/src/math/log.c and SUN COPYRIGHT NOTICE above
if (ASC_SHRINK_LEVEL < 1) {
return log_lut(x);
} else {
const
ln2_hi = reinterpret<f64>(0x3FE62E42FEE00000), // 6.93147180369123816490e-01
ln2_lo = reinterpret<f64>(0x3DEA39EF35793C76), // 1.90821492927058770002e-10
Lg1 = reinterpret<f64>(0x3FE5555555555593), // 6.666666666666735130e-01
Lg2 = reinterpret<f64>(0x3FD999999997FA04), // 3.999999999940941908e-01
Lg3 = reinterpret<f64>(0x3FD2492494229359), // 2.857142874366239149e-01
Lg4 = reinterpret<f64>(0x3FCC71C51D8E78AF), // 2.222219843214978396e-01
Lg5 = reinterpret<f64>(0x3FC7466496CB03DE), // 1.818357216161805012e-01
Lg6 = reinterpret<f64>(0x3FC39A09D078C69F), // 1.531383769920937332e-01
Lg7 = reinterpret<f64>(0x3FC2F112DF3E5244), // 1.479819860511658591e-01
Ox1p54 = reinterpret<f64>(0x4350000000000000); // 0x1p54
let u = reinterpret<u64>(x);
let hx = u32(u >> 32);
let k = 0;
let sign = hx >> 31;
if (sign || hx < 0x00100000) {
if (u << 1 == 0) return -1 / (x * x);
if (sign) return (x - x) / 0.0;
k -= 54;
x *= Ox1p54;
u = reinterpret<u64>(x);
hx = u32(u >> 32);
} else if (hx >= 0x7FF00000) {
return x;
} else if (hx == 0x3FF00000 && u << 32 == 0) {
return 0;
}
hx += 0x3FF00000 - 0x3FE6A09E;
k += (<i32>hx >> 20) - 0x3FF;
hx = (hx & 0x000FFFFF) + 0x3FE6A09E;
u = <u64>hx << 32 | (u & 0xFFFFFFFF);
x = reinterpret<f64>(u);
let f = x - 1.0;
let hfsq = 0.5 * f * f;
let s = f / (2.0 + f);
let z = s * s;
let w = z * z;
let t1 = w * (Lg2 + w * (Lg4 + w * Lg6));
let t2 = z * (Lg1 + w * (Lg3 + w * (Lg5 + w * Lg7)));
let r = t2 + t1;
let dk = <f64>k;
return s * (hfsq + r) + dk * ln2_lo - hfsq + f + dk * ln2_hi;
}
}
export function log10(x: f64): f64 { // see: musl/src/math/log10.c and SUN COPYRIGHT NOTICE above
const
ivln10hi = reinterpret<f64>(0x3FDBCB7B15200000), // 4.34294481878168880939e-01
ivln10lo = reinterpret<f64>(0x3DBB9438CA9AADD5), // 2.50829467116452752298e-11
log10_2hi = reinterpret<f64>(0x3FD34413509F6000), // 3.01029995663611771306e-01
log10_2lo = reinterpret<f64>(0x3D59FEF311F12B36), // 3.69423907715893078616e-13
Lg1 = reinterpret<f64>(0x3FE5555555555593), // 6.666666666666735130e-01
Lg2 = reinterpret<f64>(0x3FD999999997FA04), // 3.999999999940941908e-01
Lg3 = reinterpret<f64>(0x3FD2492494229359), // 2.857142874366239149e-01
Lg4 = reinterpret<f64>(0x3FCC71C51D8E78AF), // 2.222219843214978396e-01
Lg5 = reinterpret<f64>(0x3FC7466496CB03DE), // 1.818357216161805012e-01
Lg6 = reinterpret<f64>(0x3FC39A09D078C69F), // 1.531383769920937332e-01
Lg7 = reinterpret<f64>(0x3FC2F112DF3E5244), // 1.479819860511658591e-01
Ox1p54 = reinterpret<f64>(0x4350000000000000); // 0x1p54
let u = reinterpret<u64>(x);
let hx = u32(u >> 32);
let k = 0;
let sign = hx >> 31;
if (sign || hx < 0x00100000) {
if (u << 1 == 0) return -1 / (x * x);
if (sign) return (x - x) / 0.0;
k -= 54;
x *= Ox1p54;
u = reinterpret<u64>(x);
hx = u32(u >> 32);
} else if (hx >= 0x7FF00000) {
return x;
} else if (hx == 0x3FF00000 && u << 32 == 0) {
return 0;
}
hx += 0x3FF00000 - 0x3FE6A09E;
k += i32(hx >> 20) - 0x3FF;
hx = (hx & 0x000FFFFF) + 0x3FE6A09E;
u = <u64>hx << 32 | (u & 0xFFFFFFFF);
x = reinterpret<f64>(u);
let f = x - 1.0;
let hfsq = 0.5 * f * f;
let s = f / (2.0 + f);
let z = s * s;
let w = z * z;
let t1 = w * (Lg2 + w * (Lg4 + w * Lg6));
let t2 = z * (Lg1 + w * (Lg3 + w * (Lg5 + w * Lg7)));
let r = t2 + t1;
let hi = f - hfsq;
u = reinterpret<u64>(hi);
u &= 0xFFFFFFFF00000000;
hi = reinterpret<f64>(u);
let lo = f - hi - hfsq + s * (hfsq + r);
let val_hi = hi * ivln10hi;
let dk = <f64>k;
let y = dk * log10_2hi;
let val_lo = dk * log10_2lo + (lo + hi) * ivln10lo + lo * ivln10hi;
w = y + val_hi;
val_lo += (y - w) + val_hi;
return val_lo + w;
}
export function log1p(x: f64): f64 { // see: musl/src/math/log1p.c and SUN COPYRIGHT NOTICE above
const
ln2_hi = reinterpret<f64>(0x3FE62E42FEE00000), // 6.93147180369123816490e-01
ln2_lo = reinterpret<f64>(0x3DEA39EF35793C76), // 1.90821492927058770002e-10
Lg1 = reinterpret<f64>(0x3FE5555555555593), // 6.666666666666735130e-01
Lg2 = reinterpret<f64>(0x3FD999999997FA04), // 3.999999999940941908e-01
Lg3 = reinterpret<f64>(0x3FD2492494229359), // 2.857142874366239149e-01
Lg4 = reinterpret<f64>(0x3FCC71C51D8E78AF), // 2.222219843214978396e-01
Lg5 = reinterpret<f64>(0x3FC7466496CB03DE), // 1.818357216161805012e-01
Lg6 = reinterpret<f64>(0x3FC39A09D078C69F), // 1.531383769920937332e-01
Lg7 = reinterpret<f64>(0x3FC2F112DF3E5244); // 1.479819860511658591e-01
let u = reinterpret<u64>(x);
let hx = u32(u >> 32);
let k = 1;
let c = 0.0, f = 0.0;
if (hx < 0x3FDA827A || bool(hx >> 31)) {
if (hx >= 0xBFF00000) {
if (x == -1) return x / 0.0;
return (x - x) / 0.0;
}
if (hx << 1 < 0x3CA00000 << 1) return x;
if (hx <= 0xBFD2BEC4) {
k = 0;
c = 0;
f = x;
}
} else if (hx >= 0x7FF00000) return x;
if (k) {
u = reinterpret<u64>(1 + x);
let hu = u32(u >> 32);
hu += 0x3FF00000 - 0x3FE6A09E;
k = i32(hu >> 20) - 0x3FF;
if (k < 54) {
let uf = reinterpret<f64>(u);
c = k >= 2 ? 1 - (uf - x) : x - (uf - 1);
c /= uf;
} else c = 0;
hu = (hu & 0x000FFFFF) + 0x3FE6A09E;
u = <u64>hu << 32 | (u & 0xFFFFFFFF);
f = reinterpret<f64>(u) - 1;
}
let hfsq = 0.5 * f * f;
let s = f / (2.0 + f);
let z = s * s;
let w = z * z;
let t1 = w * (Lg2 + w * (Lg4 + w * Lg6));
let t2 = z * (Lg1 + w * (Lg3 + w * (Lg5 + w * Lg7)));
let r = t2 + t1;
let dk = <f64>k;
return s * (hfsq + r) + (dk * ln2_lo + c) - hfsq + f + dk * ln2_hi;
}
export function log2(x: f64): f64 { // see: musl/src/math/log2.c and SUN COPYRIGHT NOTICE above
if (ASC_SHRINK_LEVEL < 1) {
return log2_lut(x);
} else {
const
ivln2hi = reinterpret<f64>(0x3FF7154765200000), // 1.44269504072144627571e+00
ivln2lo = reinterpret<f64>(0x3DE705FC2EEFA200), // 1.67517131648865118353e-10
Lg1 = reinterpret<f64>(0x3FE5555555555593), // 6.666666666666735130e-01
Lg2 = reinterpret<f64>(0x3FD999999997FA04), // 3.999999999940941908e-01
Lg3 = reinterpret<f64>(0x3FD2492494229359), // 2.857142874366239149e-01
Lg4 = reinterpret<f64>(0x3FCC71C51D8E78AF), // 2.222219843214978396e-01
Lg5 = reinterpret<f64>(0x3FC7466496CB03DE), // 1.818357216161805012e-01
Lg6 = reinterpret<f64>(0x3FC39A09D078C69F), // 1.531383769920937332e-01
Lg7 = reinterpret<f64>(0x3FC2F112DF3E5244), // 1.479819860511658591e-01
Ox1p54 = reinterpret<f64>(0x4350000000000000); // 1p54
let u = reinterpret<u64>(x);
let hx = u32(u >> 32);
let k = 0;
let sign = hx >> 31;
if (sign || hx < 0x00100000) {
if (u << 1 == 0) return -1 / (x * x);
if (sign) return (x - x) / 0.0;
k -= 54;
x *= Ox1p54;
u = reinterpret<u64>(x);
hx = u32(u >> 32);
} else if (hx >= 0x7FF00000) {
return x;
} else if (hx == 0x3FF00000 && u << 32 == 0) {
return 0;
}
hx += 0x3FF00000 - 0x3FE6A09E;
k += i32(hx >> 20) - 0x3FF;
hx = (hx & 0x000FFFFF) + 0x3FE6A09E;
u = <u64>hx << 32 | (u & 0xFFFFFFFF);
x = reinterpret<f64>(u);
let f = x - 1.0;
let hfsq = 0.5 * f * f;
let s = f / (2.0 + f);
let z = s * s;
let w = z * z;
let t1 = w * (Lg2 + w * (Lg4 + w * Lg6));
let t2 = z * (Lg1 + w * (Lg3 + w * (Lg5 + w * Lg7)));
let r = t2 + t1;
let hi = f - hfsq;
u = reinterpret<u64>(hi);
u &= 0xFFFFFFFF00000000;
hi = reinterpret<f64>(u);
let lo = f - hi - hfsq + s * (hfsq + r);
let val_hi = hi * ivln2hi;
let val_lo = (lo + hi) * ivln2lo + lo * ivln2hi;
let y = <f64>k;
w = y + val_hi;
val_lo += (y - w) + val_hi;
val_hi = w;
return val_lo + val_hi;
}
}
// @ts-ignore: decorator
@inline
export function max(value1: f64, value2: f64): f64 {
return builtin_max<f64>(value1, value2);
}
// @ts-ignore: decorator
@inline
export function min(value1: f64, value2: f64): f64 {
return builtin_min<f64>(value1, value2);
}
export function pow(x: f64, y: f64): f64 { // see: musl/src/math/pow.c and SUN COPYRIGHT NOTICE above
// TODO: remove this fast pathes after introduced own mid-end IR with "stdlib call simplify" transforms
if (builtin_abs<f64>(y) <= 2) {
if (y == 2.0) return x * x;
if (y == 0.5) {
return select<f64>(
builtin_abs<f64>(builtin_sqrt<f64>(x)),
Infinity,
x != -Infinity
);
}
if (y == -1.0) return 1 / x;
if (y == 1.0) return x;
if (y == 0.0) return 1.0;
}
if (ASC_SHRINK_LEVEL < 1) {
return pow_lut(x, y);
} else {
const
dp_h1 = reinterpret<f64>(0x3FE2B80340000000), // 5.84962487220764160156e-01
dp_l1 = reinterpret<f64>(0x3E4CFDEB43CFD006), // 1.35003920212974897128e-08
two53 = reinterpret<f64>(0x4340000000000000), // 9007199254740992.0
huge = reinterpret<f64>(0x7E37E43C8800759C), // 1e+300
tiny = reinterpret<f64>(0x01A56E1FC2F8F359), // 1e-300
L1 = reinterpret<f64>(0x3FE3333333333303), // 5.99999999999994648725e-01
L2 = reinterpret<f64>(0x3FDB6DB6DB6FABFF), // 4.28571428578550184252e-01
L3 = reinterpret<f64>(0x3FD55555518F264D), // 3.33333329818377432918e-01
L4 = reinterpret<f64>(0x3FD17460A91D4101), // 2.72728123808534006489e-01
L5 = reinterpret<f64>(0x3FCD864A93C9DB65), // 2.30660745775561754067e-01
L6 = reinterpret<f64>(0x3FCA7E284A454EEF), // 2.06975017800338417784e-01
P1 = reinterpret<f64>(0x3FC555555555553E), // 1.66666666666666019037e-01
P2 = reinterpret<f64>(0xBF66C16C16BEBD93), // -2.77777777770155933842e-03
P3 = reinterpret<f64>(0x3F11566AAF25DE2C), // 6.61375632143793436117e-05
P4 = reinterpret<f64>(0xBEBBBD41C5D26BF1), // -1.65339022054652515390e-06
P5 = reinterpret<f64>(0x3E66376972BEA4D0), // 4.13813679705723846039e-08
lg2 = reinterpret<f64>(0x3FE62E42FEFA39EF), // 6.93147180559945286227e-01
lg2_h = reinterpret<f64>(0x3FE62E4300000000), // 6.93147182464599609375e-01
lg2_l = reinterpret<f64>(0xBE205C610CA86C39), // -1.90465429995776804525e-09
ovt = reinterpret<f64>(0x3C971547652B82FE), // 8.0085662595372944372e-017
cp = reinterpret<f64>(0x3FEEC709DC3A03FD), // 9.61796693925975554329e-01
cp_h = reinterpret<f64>(0x3FEEC709E0000000), // 9.61796700954437255859e-01
cp_l = reinterpret<f64>(0xBE3E2FE0145B01F5), // -7.02846165095275826516e-09
ivln2 = reinterpret<f64>(0x3FF71547652B82FE), // 1.44269504088896338700e+00
ivln2_h = reinterpret<f64>(0x3FF7154760000000), // 1.44269502162933349609e+00
ivln2_l = reinterpret<f64>(0x3E54AE0BF85DDF44), // 1.92596299112661746887e-08
inv3 = reinterpret<f64>(0x3FD5555555555555); // 0.3333333333333333333333
let u_ = reinterpret<u64>(x);
let hx = i32(u_ >> 32);
let lx = <u32>u_;
u_ = reinterpret<u64>(y);
let hy = i32(u_ >> 32);
let ly = <u32>u_;
let ix = hx & 0x7FFFFFFF;
let iy = hy & 0x7FFFFFFF;
if ((iy | ly) == 0) return 1.0; // x**0 = 1, even if x is NaN
// if (hx == 0x3FF00000 && lx == 0) return 1.0; // C: 1**y = 1, even if y is NaN, JS: NaN
if ( // NaN if either arg is NaN
ix > 0x7FF00000 || (ix == 0x7FF00000 && lx != 0) ||
iy > 0x7FF00000 || (iy == 0x7FF00000 && ly != 0)
) return x + y;
let yisint = 0, k: i32;
if (hx < 0) {
if (iy >= 0x43400000) yisint = 2;
else if (iy >= 0x3FF00000) {
k = (iy >> 20) - 0x3FF;
let offset = select<u32>(52, 20, k > 20) - k;
let Ly = select<u32>(ly, iy, k > 20);
let jj = Ly >> offset;
if ((jj << offset) == Ly) yisint = 2 - (jj & 1);
}
}
if (ly == 0) {
if (iy == 0x7FF00000) { // y is +-inf
if (((ix - 0x3FF00000) | lx) == 0) return NaN; // C: (-1)**+-inf is 1, JS: NaN
else if (ix >= 0x3FF00000) return hy >= 0 ? y : 0.0; // (|x|>1)**+-inf = inf,0
else return hy >= 0 ? 0.0 : -y; // (|x|<1)**+-inf = 0,inf
}
if (iy == 0x3FF00000) {
if (hy >= 0) return x;
return 1 / x;
}
if (hy == 0x40000000) return x * x;
if (hy == 0x3FE00000) {
if (hx >= 0) return builtin_sqrt(x);
}
}
let ax = builtin_abs<f64>(x), z: f64;
if (lx == 0) {
if (ix == 0 || ix == 0x7FF00000 || ix == 0x3FF00000) {
z = ax;
if (hy < 0) z = 1.0 / z;
if (hx < 0) {
if (((ix - 0x3FF00000) | yisint) == 0) {
let d = z - z;
z = d / d;
} else if (yisint == 1) z = -z;
}
return z;
}
}
let s = 1.0;
if (hx < 0) {
if (yisint == 0) {
let d = x - x;
return d / d;
}
if (yisint == 1) s = -1.0;
}
let t1: f64, t2: f64, p_h: f64, p_l: f64, r: f64, t: f64, u: f64, v: f64, w: f64;
let j: i32, n: i32;
if (iy > 0x41E00000) {
if (iy > 0x43F00000) {
if (ix <= 0x3FEFFFFF) return hy < 0 ? huge * huge : tiny * tiny;
if (ix >= 0x3FF00000) return hy > 0 ? huge * huge : tiny * tiny;
}
if (ix < 0x3FEFFFFF) return hy < 0 ? s * huge * huge : s * tiny * tiny;
if (ix > 0x3FF00000) return hy > 0 ? s * huge * huge : s * tiny * tiny;
t = ax - 1.0;
w = (t * t) * (0.5 - t * (inv3 - t * 0.25));
u = ivln2_h * t;
v = t * ivln2_l - w * ivln2;
t1 = u + v;
t1 = reinterpret<f64>(reinterpret<u64>(t1) & 0xFFFFFFFF00000000);
t2 = v - (t1 - u);
} else {
let ss: f64, s2: f64, s_h: f64, s_l: f64, t_h: f64, t_l: f64;
n = 0;
if (ix < 0x00100000) {
ax *= two53;
n -= 53;
ix = <u32>(reinterpret<u64>(ax) >> 32);
}
n += (ix >> 20) - 0x3FF;
j = ix & 0x000FFFFF;
ix = j | 0x3FF00000;
if (j <= 0x3988E) k = 0;
else if (j < 0xBB67A) k = 1;
else {
k = 0;
n += 1;
ix -= 0x00100000;
}
ax = reinterpret<f64>(reinterpret<u64>(ax) & 0xFFFFFFFF | (<u64>ix << 32));
let bp = select<f64>(1.5, 1.0, k); // k ? 1.5 : 1.0
u = ax - bp;
v = 1.0 / (ax + bp);
ss = u * v;
s_h = ss;
s_h = reinterpret<f64>(reinterpret<u64>(s_h) & 0xFFFFFFFF00000000);
t_h = reinterpret<f64>(u64(((ix >> 1) | 0x20000000) + 0x00080000 + (k << 18)) << 32);
t_l = ax - (t_h - bp);
s_l = v * ((u - s_h * t_h) - s_h * t_l);
s2 = ss * ss;
r = s2 * s2 * (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6)))));
r += s_l * (s_h + ss);
s2 = s_h * s_h;
t_h = 3.0 + s2 + r;
t_h = reinterpret<f64>(reinterpret<u64>(t_h) & 0xFFFFFFFF00000000);
t_l = r - ((t_h - 3.0) - s2);
u = s_h * t_h;
v = s_l * t_h + t_l * ss;
p_h = u + v;
p_h = reinterpret<f64>(reinterpret<u64>(p_h) & 0xFFFFFFFF00000000);
p_l = v - (p_h - u);
let z_h = cp_h * p_h;
let dp_l = select<f64>(dp_l1, 0.0, k);
let z_l = cp_l * p_h + p_l * cp + dp_l;
t = <f64>n;
let dp_h = select<f64>(dp_h1, 0.0, k);
t1 = ((z_h + z_l) + dp_h) + t;
t1 = reinterpret<f64>(reinterpret<u64>(t1) & 0xFFFFFFFF00000000);
t2 = z_l - (((t1 - t) - dp_h) - z_h);
}
let y1 = y;
y1 = reinterpret<f64>(reinterpret<u64>(y1) & 0xFFFFFFFF00000000);
p_l = (y - y1) * t1 + y * t2;
p_h = y1 * t1;
z = p_l + p_h;
u_ = reinterpret<u64>(z);
j = u32(u_ >> 32);
let i = <i32>u_;
if (j >= 0x40900000) {
if (((j - 0x40900000) | i) != 0) return s * huge * huge;
if (p_l + ovt > z - p_h) return s * huge * huge;
} else if ((j & 0x7FFFFFFF) >= 0x4090CC00) {
if (((j - 0xC090CC00) | i) != 0) return s * tiny * tiny;
if (p_l <= z - p_h) return s * tiny * tiny;
}
i = j & 0x7FFFFFFF;
k = (i >> 20) - 0x3FF;