math.bend source
math.bend on the hub · documented module
# bend-float64's math functions, NumPy's float64 set, every result correctly rounded (to nearest, ties to even).# Each function is evaluated on balls (ball.bend: big-integer values with a rigorous error bound) at 128 bits, and# again at 256, 512, 1024, 2048 until every point of the ball rounds to the same float64 (Ziv's strategy). Inputs# whose answer is exact, or exactly halfway between two float64s, never settle that way, so they are found first and# computed exactly: cbrt and hypot always are (integer roots with a remainder), exp2 of an integer, log2 of a power of# two, log10 of 10^k, and every power x^y that is exact. Special values follow C99's Annex F; a NaN result has the# bits NumPy gives on x86-64 (a NaN operand passes through quieted). Pure.import Baseimport ./wide.bend as Wimport ./big.bend as Bimport ./float64.bend as Fimport ./ball.bend as Limport ./consts.bend as K# ---- helpers ----def fp(+h: U32, +l: U32) -> F.F64: F.of_bits(h, l)def is_neg(+x: U32) -> Bool: U32.is_le(2147483648, x)def neg_nan() -> F.F64: F.dnan()def pos_nan() -> F.F64: F.F64{2146959360, 0}def one() -> F.F64: F.F64{1072693248, 0}def sign_word(+neg: Bool) -> U32: Bool.pick(U32, neg, 2147483648, 0)# the float64 nearest the ball's midpoint, ignoring its error (for choosing branches, never for answers)def approx(b: L.Ball) -> F.F64: match b: case L.Ball{n, m, e, r}: L.round_big(n, m, e)# |b| < 2^k, judged on the midpointdef below(+b: L.Ball, +k: U32) -> Bool: L.s_lt(L.top(b), k)def pi_b(+p: U32) -> L.Ball: L.constant(K.pi(), K.pi_bits(), (p + 8 : U32))def ln2_b(+p: U32) -> L.Ball: L.constant(K.ln2(), K.ln2_bits(), (p + 8 : U32))def ln10_b(+p: U32) -> L.Ball: L.constant(K.ln10(), K.ln10_bits(), (p + 8 : U32))# b * k for a signed word kdef mul_i(+b: L.Ball, +k: U32) -> L.Ball: Bool.pick(L.Ball, is_neg(k), L.negate(L.mul_u(b, (0 - k : U32))), L.mul_u(b, k))def signed(+neg: Bool, +b: L.Ball) -> L.Ball: Bool.pick(L.Ball, neg, L.negate(b), b)# ---- series: add terms while they matter; the rest is at most twice the first one left out (every series here# shrinks by at least half a term) ----def small(+term: L.Ball, +sum: L.Ball, +p: U32) -> Bool: L.s_lt(L.top(term), (L.top(sum) - p - 8 : U32))def series_pick(stop: Bool, +sum: L.Ball, +term: L.Ball, more: Unit -> L.Ball) -> L.Ball: match stop: case True{}: L.widen(sum, L.mag_shift(L.mag_of(term), 1)) case False{}: more(Unit{})# sum + term + term * x / (n + 1) + ...def exp_series(fuel: Nat, +sum: L.Ball, +term: L.Ball, +x: L.Ball, +n: U32, +p: U32) -> L.Ball: match fuel: case 0n: L.widen(sum, L.mag_shift(L.mag_of(term), 1)) case 1n+f: series_pick(small(term, sum, p), sum, term, u => exp_series(f, L.add(sum, term, p), L.div_u(L.mul(term, x, p), (n + 1 : U32), p), x, (n + 1 : U32), p))# sum + pw * z2 / (2n + 1) + pw * z2^2 / (2n + 3) + ..., alternating when altdef odd_series(fuel: Nat, +sum: L.Ball, +pw: L.Ball, +z2: L.Ball, +n: U32, +alt: Bool, +p: U32) -> L.Ball: match fuel: case 0n: sum case 1n+f: +pw2 = Bool.pick(L.Ball, alt, L.negate(L.mul(pw, z2, p)), L.mul(pw, z2, p)) +term = L.div_u(pw2, (n + n + 1 : U32), p) series_pick(small(term, sum, p), sum, term, u => odd_series(f, L.add(sum, term, p), pw2, z2, (n + 1 : U32), alt, p))# atanh(z) = z + z^3/3 + z^5/5 + ... (|z| < 0.7)def atanh_series(+z: L.Ball, +p: U32) -> L.Ball: odd_series(4000n, z, z, L.mul(z, z, p), 1, False{}, p)# atan(z) = z - z^3/3 + z^5/5 - ... (|z| < 0.7)def atan_series(+z: L.Ball, +p: U32) -> L.Ball: odd_series(4000n, z, z, L.mul(z, z, p), 1, True{}, p)# sum + term * x2 / ((n + 1)(n + 2)) + ...: sin, cos, sinh, cosh by their Taylor series (x2 = +-x^2)def trig_series(fuel: Nat, +sum: L.Ball, +term: L.Ball, +x2: L.Ball, +n: U32, +p: U32) -> L.Ball: match fuel: case 0n: sum case 1n+f: +t = L.div_u(L.div_u(L.mul(term, x2, p), (n + 1 : U32), p), (n + 2 : U32), p) series_pick(small(t, sum, p), sum, t, u => trig_series(f, L.add(sum, t, p), t, x2, (n + 2 : U32), p))# ---- exp and log on balls ----def square_n(n: Nat, +b: L.Ball, +p: U32) -> L.Ball: match n: case 0n: b case 1n+k: square_n(k, L.mul(b, b, p), p)# e^a: a = k ln 2 + r, e^r = (e^(r / 256))^256 by its seriesdef exp_go(+a: L.Ball, +p: U32) -> L.Ball: +k = F.to_i32(F.rint(F.mul(approx(a), fp(1073157447, 1697350398)))) +q = (p + 24 : U32) +r8 = L.mul_pow2(L.sub(a, mul_i(ln2_b((q + 12 : U32)), k), q), (0 - 8 : U32)) L.mul_pow2(square_n(8n, exp_series(4000n, L.one(), r8, r8, 1, q), q), k)def exp_huge(huge: Bool, +a: L.Ball, +p: U32) -> L.Ball: match huge: case True{}: L.exact(False{}, [1], Bool.pick(U32, L.neg_of(a), (0 - 3000 : U32), 3000)) case False{}: exp_go(a, p)# e^a; past |a| = 1500 every point overflows or underflows, so a power of two far out stands for them alldef exp_core(+a: L.Ball, +p: U32) -> L.Ball: exp_huge(F.lt(fp(1083666432, 0), F.abs(approx(a))), a, p)def expm1_go(near: Bool, +a: L.Ball, +p: U32) -> L.Ball: match near: case True{}: exp_series(4000n, a, L.div_u(L.mul(a, a, (p + 16 : U32)), 2, (p + 16 : U32)), a, 2, (p + 16 : U32)) case False{}: L.sub(exp_core(a, p), L.one(), (p + 16 : U32))# e^a - 1 (exact near 0)def expm1_core(+a: L.Ball, +p: U32) -> L.Ball: expm1_go(below(a, (0 - 1 : U32)), a, p)def log_m(+m: L.Ball, +k: U32, +p: U32) -> L.Ball: +q = (p + 24 : U32) L.add(L.mul_pow2(atanh_series(L.div(L.sub(m, L.one(), q), L.add(m, L.one(), q), q), q), 1), mul_i(ln2_b((q + 12 : U32)), k), q)# log(a), a > 0: a = 2^k m, m in [1/sqrt 2, sqrt 2], log m = 2 atanh((m - 1) / (m + 1))def log_core(+a: L.Ball, +p: U32) -> L.Ball: +k0 = (L.top(a) - 1 : U32) +m0 = L.mul_pow2(a, (0 - k0 : U32)) +big = F.lt(fp(1073127582, 1719614413), approx(m0)) log_m(Bool.pick(L.Ball, big, L.mul_pow2(m0, (0 - 1 : U32)), m0), (k0 + Bool.to_u32(big) : U32), p)# log(1 + u), exact near 0def log1p_go(near: Bool, +u: L.Ball, +p: U32) -> L.Ball: match near: case True{}: L.mul_pow2(atanh_series(L.div(u, L.add(L.of_u32(2), u, (p + 24 : U32)), (p + 24 : U32)), (p + 24 : U32)), 1) case False{}: log_core(L.add(L.one(), u, (p + 24 : U32)), p)def log1p_core(+u: L.Ball, +p: U32) -> L.Ball: log1p_go(below(u, (0 - 1 : U32)), u, p)# ---- trigonometry on balls ----# atan(t), |t| <= 1: halve the angle twice (t / (1 + sqrt(1 + t^2))), then the seriesdef atan_small(+t: L.Ball, +p: U32) -> L.Ball: +q = (p + 24 : U32) +t1 = L.div(t, L.add(L.one(), L.sqrt(L.add(L.one(), L.mul(t, t, q), q), q), q), q) +t2 = L.div(t1, L.add(L.one(), L.sqrt(L.add(L.one(), L.mul(t1, t1, q), q), q), q), q) L.mul_pow2(atan_series(t2, q), 2)# atan(t), any t: past 1, pi/2 - atan(1/t)def atan_go(small: Bool, +t: L.Ball, +p: U32) -> L.Ball: match small: case True{}: atan_small(t, p) case False{}: signed(L.neg_of(t), L.sub(L.mul_pow2(pi_b((p + 24 : U32)), (0 - 1 : U32)), atan_small(L.div(L.one(), L.abs(t), (p + 24 : U32)), p), (p + 24 : U32)))def atan_any(+t: L.Ball, +p: U32) -> L.Ball: atan_go(below(t, 1), t, p)# x = (k + f) pi / 2 with |f| <= 1/2: k mod 4, and r = f pi / 2, from 3600 bits of 2 / pi (x > 0)type Red is Data: Red{k: U32, r: L.Ball}def red_frac(+i: List<&2, U32>, +t: List<&2, U32>, +fb: U32, +xm: List<&2, U32>, +ex: U32, +p: U32) -> Red: +f = B.sub(t, B.shl(i, fb)) +up = B.le(B.shl([1], (fb - 1 : U32)), f) +k = (U32.and(B.head0(i), 3) + Bool.to_u32(up) : U32) +fr = L.trunc(L.Ball{up, Bool.pick(List<&2, U32>, up, B.sub(B.shl([1], fb), f), f), (0 - fb : U32), L.mag_big(xm, (ex - 3600 : U32))}, (p + 32 : U32)) Red{U32.and(k, 3), L.mul(fr, L.mul_pow2(pi_b((p + 32 : U32)), (0 - 1 : U32)), (p + 24 : U32))}def reduce(+xm: List<&2, U32>, +ex: U32, +p: U32) -> Red: +t = B.mul(xm, K.two_over_pi()) +fb = (3600 - ex : U32) red_frac(B.shr(t, fb), t, fb, xm, ex, p)def sin_r(+r: L.Ball, +p: U32) -> L.Ball: trig_series(4000n, r, r, L.negate(L.mul(r, r, p)), 1, p)def cos_r(+r: L.Ball, +p: U32) -> L.Ball: trig_series(4000n, L.one(), L.one(), L.negate(L.mul(r, r, p)), 0, p)# ---- the functions on balls, by name ----type Fn is Data: ZExp{x: F.F64} ZExp2{k: U32, f: F.F64} ZExpm1{x: F.F64} ZLog{x: F.F64} ZLog2{x: F.F64} ZLog10{x: F.F64} ZLog1p{x: F.F64} ZSin{x: F.F64} ZCos{x: F.F64} ZTan{x: F.F64} ZAsin{x: F.F64} ZAcos{x: F.F64} ZAtan{x: F.F64} ZAtan2{y: F.F64, x: F.F64} ZSinh{x: F.F64} ZCosh{x: F.F64} ZTanh{x: F.F64} ZAsinh{x: F.F64} ZAcosh{x: F.F64} ZAtanh{x: F.F64} ZPow{neg: Bool, x: F.F64, y: F.F64} ZHalfPi{neg: Bool} ZPi{neg: Bool} ZQuarterPi{neg: Bool, three: Bool}def ball(+x: F.F64) -> L.Ball: L.of_f64(x)# which of sin r, cos r, sin r / cos r, cos r / sin r a quadrant needs, and its signtype TQ is Data: TSin{} TCos{} TTan{} TCot{}def tq_ball(t: TQ, +r: L.Ball, +q: U32) -> L.Ball: match t: case TSin{}: sin_r(r, q) case TCos{}: cos_r(r, q) case TTan{}: L.div(sin_r(r, q), cos_r(r, q), q) case TCot{}: L.div(cos_r(r, q), sin_r(r, q), q)# which: 0 sin, 1 cos, 2 tan; quadrant k: sin = (s, c, -s, -c), cos = (c, -s, -c, s), tan = (s/c, -c/s)def trig_q(+which: U32, red: Red, +neg: Bool, +p: U32) -> L.Ball: match red: case Red{+k, r}: +odd = U32.is_eq(U32.and(k, 1), 1) +t = Bool.pick(TQ, U32.is_eq(which, 2), Bool.pick(TQ, odd, TCot{}, TTan{}), Bool.pick(TQ, Bool.xor(odd, U32.is_eq(which, 1)), TCos{}, TSin{})) +minus = Bool.pick(Bool, U32.is_eq(which, 2), odd, Bool.pick(Bool, U32.is_eq(which, 0), U32.is_le(2, k), Bool.or(U32.is_eq(k, 1), U32.is_eq(k, 2)))) signed(Bool.xor(neg, minus), tq_ball(t, r, (p + 24 : U32)))# which: 0 sin, 1 cos, 2 tan; x finite and nonzerodef trig_cl(+which: U32, c: F.Cls, +p: U32) -> L.Ball: match c: case F.FFin{s, +e, m}: trig_q(which, reduce(B.of_w64(m), (e - 1075 : U32), p), Bool.and(U32.is_ne(s, 0), U32.is_ne(which, 1)), p) case _: L.bad()def trig(+which: U32, x: F.F64, +p: U32) -> L.Ball: trig_cl(which, F.cls(x), p)# sinh b, b >= 0: the series below 1, (e^b - e^-b) / 2 abovedef sinh_go(near: Bool, +b: L.Ball, +q: U32) -> L.Ball: match near: case True{}: trig_series(4000n, b, b, L.mul(b, b, q), 1, q) case False{}: +ex = exp_core(b, q) L.mul_pow2(L.sub(ex, L.div(L.one(), ex, q), q), (0 - 1 : U32))# asinh b, b >= 0: log1p(b + b^2 / (1 + sqrt(1 + b^2))) below 1, log(b + sqrt(b^2 + 1)) abovedef asinh_go(near: Bool, +b: L.Ball, +p: U32) -> L.Ball: match near: case True{}: +q = (p + 24 : U32) +b2 = L.mul(b, b, q) log1p_core(L.add(b, L.div(b2, L.add(L.one(), L.sqrt(L.add(L.one(), b2, q), q), q), q), q), p) case False{}: +q = (p + 24 : U32) log_core(L.add(b, L.sqrt(L.add(L.mul(b, b, q), L.one(), q), q), q), p)# atanh b, 0 <= b < 1: the series below 1/2, log1p(2b / (1 - b)) / 2 abovedef atanh_go(near: Bool, +b: L.Ball, +q: U32) -> L.Ball: match near: case True{}: atanh_series(b, q) case False{}: L.mul_pow2(log1p_core(L.div(L.mul_pow2(b, 1), L.sub(L.one(), b, q), q), q), (0 - 1 : U32))def eval(f: Fn, +p: U32) -> L.Ball: match f: case ZExp{x}: exp_core(ball(x), p) case ZExp2{k, x}: L.mul_pow2(exp_core(L.mul(ball(x), ln2_b((p + 32 : U32)), (p + 32 : U32)), p), k) case ZExpm1{x}: expm1_core(ball(x), p) case ZLog{x}: log_core(ball(x), p) case ZLog2{x}: L.div(log_core(ball(x), (p + 16 : U32)), ln2_b((p + 32 : U32)), (p + 16 : U32)) case ZLog10{x}: L.div(log_core(ball(x), (p + 16 : U32)), ln10_b((p + 32 : U32)), (p + 16 : U32)) case ZLog1p{x}: log1p_core(ball(x), p) case ZSin{x}: trig(0, x, p) case ZCos{x}: trig(1, x, p) case ZTan{x}: trig(2, x, p) case ZAsin{+x}: +q = (p + 24 : U32) +b = ball(x) atan_any(L.div(b, L.sqrt(L.sub(L.one(), L.mul(b, b, q), q), q), q), p) case ZAcos{+x}: +q = (p + 24 : U32) +b = ball(x) +a = atan_any(L.div(L.sqrt(L.sub(L.one(), L.mul(b, b, q), q), q), L.abs(b), q), p) Bool.pick(L.Ball, L.neg_of(b), L.sub(pi_b(q), a, q), a) case ZAtan{x}: atan_any(ball(x), p) case ZAtan2{+y, +x}: +q = (p + 24 : U32) +a = atan_any(L.div(L.abs(ball(y)), L.abs(ball(x)), q), p) signed(F.signbit(y), Bool.pick(L.Ball, F.signbit(x), L.sub(pi_b(q), a, q), a)) case ZSinh{+x}: signed(F.signbit(x), sinh_go(below(L.abs(ball(x)), 0), L.abs(ball(x)), (p + 24 : U32))) case ZCosh{x}: +q = (p + 24 : U32) +ex = exp_core(L.abs(ball(x)), q) L.mul_pow2(L.add(ex, L.div(L.one(), ex, q), q), (0 - 1 : U32)) case ZTanh{+x}: +q = (p + 24 : U32) +b2 = L.mul_pow2(L.abs(ball(x)), 1) +u = expm1_core(b2, q) signed(F.signbit(x), L.div(u, L.add(u, L.of_u32(2), q), q)) case ZAsinh{+x}: signed(F.signbit(x), asinh_go(below(L.abs(ball(x)), 0), L.abs(ball(x)), p)) case ZAcosh{x}: +q = (p + 24 : U32) +t = L.sub(ball(x), L.one(), q) log1p_core(L.add(t, L.sqrt(L.add(L.mul_pow2(t, 1), L.mul(t, t, q), q), q), q), p) case ZAtanh{+x}: signed(F.signbit(x), atanh_go(below(L.abs(ball(x)), (0 - 1 : U32)), L.abs(ball(x)), (p + 24 : U32))) case ZPow{neg, x, y}: +q = (p + 40 : U32) signed(neg, exp_core(L.mul(log_core(ball(x), q), ball(y), q), p)) case ZHalfPi{neg}: signed(neg, L.mul_pow2(pi_b(p), (0 - 1 : U32))) case ZPi{neg}: signed(neg, pi_b(p)) case ZQuarterPi{neg, three}: signed(neg, Bool.pick(L.Ball, three, L.mul_u(L.mul_pow2(pi_b(p), (0 - 2 : U32)), 3), L.mul_pow2(pi_b(p), (0 - 2 : U32))))def ziv_pick(m: Maybe<&2, F.F64>, more: Unit -> F.F64) -> F.F64: match m: case Some{x}: x case None{}: more(Unit{})# evaluate at p bits, doubling p until the ball decides (at most n times)def ziv(n: Nat, +f: Fn, +p: U32) -> F.F64: match n: case 0n: approx(eval(f, (p / 2 : U32))) case 1n+k: ziv_pick(L.decide(eval(f, p)), u => ziv(k, f, (p + p : U32)))def run(+f: Fn) -> F.F64: ziv(5n, f, 128)# ---- exact answers ----def round_top(+s: U32, t: B.Top, +sticky: Bool, +e: U32) -> F.F64: match t: case B.Top{sig, k}: F.round_pack(s, (1084 + k + e : U32), W.jam_or(sig, sticky))# the float64 nearest q * 2^e (plus a little, when sticky), q a nonzero big integerdef round_q(+s: U32, +q: List<&2, U32>, +sticky: Bool, +e: U32) -> F.F64: round_top(s, B.top64(q), sticky, e)def qr_q(d: B.QR) -> List<&2, U32>: match d: case B.QR{q, r}: qdef qr_r(d: B.QR) -> List<&2, U32>: match d: case B.QR{q, r}: rdef div3(+v: U32) -> U32: Bool.pick(U32, is_neg(v), (0 - U32.div((0 - v : U32), 3) : U32), U32.div(v, 3))def cbrt_go(+s: U32, +m: List<&2, U32>, +ex: U32) -> F.F64: +s0 = (200 - B.bitlen(m) : U32) +sh = (s0 + U32.mod((ex - s0 + 3000 : U32), 3) : U32) +n = B.shl(m, sh) +q = B.icbrt(n) round_q(s, q, Bool.not(B.is_zero(B.sub(n, B.cube(q)))), div3((ex - sh : U32)))def cbrt_c(c: F.Cls, +x: F.F64) -> F.F64: match c: case F.FFin{s, +e, m}: cbrt_go(s, B.of_w64(m), (e - 1075 : U32)) case F.FNan{}: F.quiet(x) case _: xdef cbrt_body(+x: F.F64) -> F.F64: cbrt_c(F.cls(x), x)def hyp_go(+mx: List<&2, U32>, +ex: U32, +my: List<&2, U32>, +ey: U32) -> F.F64: +e = L.s_min(ex, ey) +a = B.shl(mx, (ex - e : U32)) +b = B.shl(my, (ey - e : U32)) +ss = B.add(B.mul(a, a), B.mul(b, b)) +s0 = (132 - B.bitlen(ss) : U32) +s1 = Bool.pick(U32, is_neg(s0), 0, s0) +sh = (s1 + U32.and(s1, 1) : U32) +qr = B.isqrt2(B.shl(ss, sh)) round_q(0, qr_q(qr), Bool.not(B.is_zero(qr_r(qr))), (e - U32.div(sh, 2) : U32))def hyp_far(far: Bool, +x: F.F64, +y: F.F64, +mx: List<&2, U32>, +ex: U32, +my: List<&2, U32>, +ey: U32) -> F.F64: match far: case True{}: F.maximum(F.abs(x), F.abs(y)) case False{}: hyp_go(mx, ex, my, ey)def hyp_c(cx: F.Cls, cy: F.Cls, +x: F.F64, +y: F.F64) -> F.F64: match cx cy: case F.FFin{sx, +ex, mx} F.FFin{sy, +ey, my}: +a = (ex - 1075 : U32) +b = (ey - 1075 : U32) hyp_far(Bool.or(L.s_lt((a + 64 : U32), b), L.s_lt((b + 64 : U32), a)), x, y, B.of_w64(mx), a, B.of_w64(my), b) case F.FZero{sx} _: F.abs(y) case _ F.FZero{sy}: F.abs(x) case _ _: F.dnan()def is_snan(+x: F.F64) -> Bool: Bool.and(F.is_nan(x), U32.is_eq(U32.and(F.hi_bits(x), 524288), 0))# sqrt(x^2 + y^2) without overflow (exact: the integer square root of the exact sum of squares). An infinity wins over# a quiet NaN, as C99 says; a signalling NaN does not, as glibc does it.def hypot(+x: F.F64, +y: F.F64) -> F.F64: Bool.pick(F.F64, Bool.or(is_snan(x), is_snan(y)), Bool.pick(F.F64, F.is_nan(x), F.quiet(x), F.quiet(y)), Bool.pick(F.F64, Bool.or(F.is_inf(x), F.is_inf(y)), F.inf(0), Bool.pick(F.F64, Bool.or(F.is_nan(x), F.is_nan(y)), Bool.pick(F.F64, F.is_nan(x), F.quiet(x), F.quiet(y)), hyp_c(F.cls(x), F.cls(y), x, y))))# ---- the functions: special values first, each giving a value (Val) or a ball evaluation to run (Go) ----type Out is Data: Val{x: F.F64} Go{f: Fn}def out(o: Out) -> F.F64: match o: case Val{x}: x case Go{f}: run(f)def is_int(+x: F.F64) -> Bool: Bool.and(F.is_finite(x), F.eq(F.trunc(x), x))# an odd integer (every float64 from 2^53 up is even)def is_odd(+y: F.F64) -> Bool: Bool.and(is_int(y), F.ne(F.mul(F.trunc(F.mul(y, fp(1071644672, 0))), fp(1073741824, 0)), y))def nanv(+x: F.F64) -> Out: Val{F.quiet(x)}def exp_c(c: F.Cls, +x: F.F64) -> Out: match c: case F.FInf{s}: Val{Bool.pick(F.F64, U32.is_eq(s, 0), x, F.zero(0))} case F.FZero{s}: Val{one()} case F.FFin{s, e, m}: Bool.pick(Out, F.lt(fp(1082535936, 0), x), Val{F.inf(0)}, Bool.pick(Out, F.lt(x, fp(3230093312, 0)), Val{F.zero(0)}, Go{ZExp{x}})) case F.FNan{}: nanv(x)def exp_body(+x: F.F64) -> F.F64: out(exp_c(F.cls(x), x))def exp2_fin(+x: F.F64) -> Out: +k = F.rint(x) Bool.pick(Out, F.lt(fp(1083180032, 0), x), Val{F.inf(0)}, Bool.pick(Out, F.lt(x, fp(3230715904, 0)), Val{F.zero(0)}, Bool.pick(Out, F.eq(k, x), Val{F.round_pack(0, (1084 + F.to_i32(k) : U32), W.w(1))}, Go{ZExp2{F.to_i32(k), F.sub(x, k)}})))def exp2_c(c: F.Cls, +x: F.F64) -> Out: match c: case F.FInf{s}: Val{Bool.pick(F.F64, U32.is_eq(s, 0), x, F.zero(0))} case F.FZero{s}: Val{one()} case F.FFin{s, e, m}: exp2_fin(x) case F.FNan{}: nanv(x)def exp2_body(+x: F.F64) -> F.F64: out(exp2_c(F.cls(x), x))def expm1_c(c: F.Cls, +x: F.F64) -> Out: match c: case F.FInf{s}: Val{Bool.pick(F.F64, U32.is_eq(s, 0), x, F.F64{3220176896, 0})} case F.FZero{s}: Val{x} case F.FFin{s, e, m}: Bool.pick(Out, F.lt(fp(1082535936, 0), x), Val{F.inf(0)}, Bool.pick(Out, F.lt(x, fp(3225681920, 0)), Val{F.F64{3220176896, 0}}, Go{ZExpm1{x}})) case F.FNan{}: nanv(x)def expm1_body(+x: F.F64) -> F.F64: out(expm1_c(F.cls(x), x))# k when x = 2^kdef pow2_c(c: F.Cls) -> Maybe<&2, U32>: match c: case F.FFin{s, +e, +m}: Bool.pick(Maybe<&2, U32>, W.is_zero(W.and(m, W.sub(m, W.w(1)))), Some{(e - 1075 + 63 - W.clz(m) : U32)}, None{}) case _: None{}def pow10_f(+k: U32) -> F.F64: round_q(0, B.pow10(k), False{}, 0)# k when x = 10^k exactly, 0 <= k <= 22def pow10_k(n: Nat, +x: F.F64, +k: U32) -> Maybe<&2, U32>: match n: case 0n: None{} case 1n+j: Bool.pick(Maybe<&2, U32>, F.eq(x, pow10_f(k)), Some{k}, pow10_k(j, x, (k + 1 : U32)))def log_exact(m: Maybe<&2, U32>, f: Fn) -> Out: match m: case Some{k}: Val{F.from_i32(k)} case None{}: Go{f}def log10_of(int: Bool, +x: F.F64) -> Out: match int: case True{}: log_exact(pow10_k(23n, x, 0), ZLog10{x}) case False{}: Go{ZLog10{x}}type LK is Data: KLog{} KLog2{} KLog10{}# x finite, positive, not 1def log_fin(k: LK, +x: F.F64) -> Out: match k: case KLog{}: Go{ZLog{x}} case KLog2{}: log_exact(pow2_c(F.cls(x)), ZLog2{x}) case KLog10{}: log10_of(Bool.and(is_int(x), F.lt(x, F.F64{1152724226, 3353430774})), x)def invalid_of(k: LK) -> F.F64: match k: case KLog10{}: pos_nan() case _: neg_nan()def log_c(c: F.Cls, +k: LK, +x: F.F64) -> Out: match c: case F.FNan{}: nanv(x) case F.FInf{s}: Val{Bool.pick(F.F64, U32.is_eq(s, 0), x, invalid_of(k))} case F.FZero{s}: Val{F.inf(2147483648)} case F.FFin{s, e, m}: Bool.pick(Out, U32.is_ne(s, 0), Val{invalid_of(k)}, Bool.pick(Out, F.eq(x, one()), Val{F.zero(0)}, log_fin(k, x)))def log_body(+x: F.F64) -> F.F64: out(log_c(F.cls(x), KLog{}, x))def log2_body(+x: F.F64) -> F.F64: out(log_c(F.cls(x), KLog2{}, x))def log10_body(+x: F.F64) -> F.F64: out(log_c(F.cls(x), KLog10{}, x))def log1p_c(c: F.Cls, +x: F.F64) -> Out: match c: case F.FNan{}: nanv(x) case F.FInf{s}: Val{Bool.pick(F.F64, U32.is_eq(s, 0), x, neg_nan())} case F.FZero{s}: Val{x} case F.FFin{s, e, m}: Bool.pick(Out, F.eq(x, F.F64{3220176896, 0}), Val{F.inf(2147483648)}, Bool.pick(Out, F.lt(x, F.F64{3220176896, 0}), Val{neg_nan()}, Go{ZLog1p{x}}))def log1p_body(+x: F.F64) -> F.F64: out(log1p_c(F.cls(x), x))def trig_c(c: F.Cls, +which: U32, +x: F.F64) -> Out: match c: case F.FNan{}: nanv(x) case F.FInf{s}: Val{neg_nan()} case F.FZero{s}: Val{Bool.pick(F.F64, U32.is_eq(which, 1), one(), x)} case F.FFin{s, e, m}: Go{Bool.pick(Fn, U32.is_eq(which, 0), ZSin{x}, Bool.pick(Fn, U32.is_eq(which, 1), ZCos{x}, ZTan{x}))}def sin_body(+x: F.F64) -> F.F64: out(trig_c(F.cls(x), 0, x))def cos_body(+x: F.F64) -> F.F64: out(trig_c(F.cls(x), 1, x))def tan_body(+x: F.F64) -> F.F64: out(trig_c(F.cls(x), 2, x))def above_one(+x: F.F64) -> Bool: F.lt(one(), F.abs(x))def asin_c(c: F.Cls, +x: F.F64) -> Out: match c: case F.FNan{}: nanv(x) case F.FInf{s}: Val{pos_nan()} case F.FZero{s}: Val{x} case F.FFin{s, e, m}: Bool.pick(Out, above_one(x), Val{pos_nan()}, Go{Bool.pick(Fn, F.eq(F.abs(x), one()), ZHalfPi{F.signbit(x)}, ZAsin{x})})def arcsin_body(+x: F.F64) -> F.F64: out(asin_c(F.cls(x), x))def acos_c(c: F.Cls, +x: F.F64) -> Out: match c: case F.FNan{}: nanv(x) case F.FInf{s}: Val{pos_nan()} case F.FZero{s}: Go{ZHalfPi{False{}}} case F.FFin{s, e, m}: Bool.pick(Out, above_one(x), Val{pos_nan()}, Bool.pick(Out, F.eq(x, one()), Val{F.zero(0)}, Go{Bool.pick(Fn, F.eq(x, F.F64{3220176896, 0}), ZPi{False{}}, ZAcos{x})}))def arccos_body(+x: F.F64) -> F.F64: out(acos_c(F.cls(x), x))def atan_c(c: F.Cls, +x: F.F64) -> Out: match c: case F.FNan{}: nanv(x) case F.FInf{s}: Go{ZHalfPi{U32.is_ne(s, 0)}} case F.FZero{s}: Val{x} case F.FFin{s, e, m}: Go{ZAtan{x}}def arctan_body(+x: F.F64) -> F.F64: out(atan_c(F.cls(x), x))def atan2_c(cy: F.Cls, cx: F.Cls, +y: F.F64, +x: F.F64) -> Out: match cy cx: case F.FZero{sy} _: Bool.pick(Out, F.signbit(x), Go{ZPi{U32.is_ne(sy, 0)}}, Val{y}) case F.FInf{sy} F.FInf{sx}: Go{ZQuarterPi{U32.is_ne(sy, 0), U32.is_ne(sx, 0)}} case F.FInf{sy} _: Go{ZHalfPi{U32.is_ne(sy, 0)}} case _ F.FZero{sx}: Go{ZHalfPi{F.signbit(y)}} case _ F.FInf{sx}: Bool.pick(Out, U32.is_eq(sx, 0), Val{F.zero(F.sign_of(F.hi_bits(y)))}, Go{ZPi{F.signbit(y)}}) case _ _: Go{ZAtan2{y, x}}def atan2_n(nx: Bool, ny: Bool, +y: F.F64, +x: F.F64) -> Out: match nx ny: case True{} _: nanv(x) case False{} True{}: nanv(y) case False{} False{}: atan2_c(F.cls(y), F.cls(x), y, x)# atan(y / x) in the quadrant of (x, y), in [-pi, pi] (C99's signed zeros and infinities; of two NaNs, x's)def arctan2(+y: F.F64, +x: F.F64) -> F.F64: out(atan2_n(F.is_nan(x), F.is_nan(y), y, x))def hyp_big(+x: F.F64, +k: U32) -> Bool: F.lt(F.F64{k, 0}, F.abs(x))def sinh_c(c: F.Cls, +x: F.F64) -> Out: match c: case F.FNan{}: nanv(x) case F.FFin{s, e, m}: Bool.pick(Out, hyp_big(x, 1082537984), Val{F.inf(s)}, Go{ZSinh{x}}) case _: Val{x}def sinh_body(+x: F.F64) -> F.F64: out(sinh_c(F.cls(x), x))def cosh_c(c: F.Cls, +x: F.F64) -> Out: match c: case F.FNan{}: nanv(x) case F.FInf{s}: Val{F.inf(0)} case F.FZero{s}: Val{one()} case F.FFin{s, e, m}: Bool.pick(Out, hyp_big(x, 1082537984), Val{F.inf(0)}, Go{ZCosh{x}})def cosh_body(+x: F.F64) -> F.F64: out(cosh_c(F.cls(x), x))def tanh_c(c: F.Cls, +x: F.F64) -> Out: match c: case F.FNan{}: Val{F.F64{2146959360, 0}} case F.FInf{s}: Val{F.F64{U32.or(s, 1072693248), 0}} case F.FZero{s}: Val{x} case F.FFin{+s, e, m}: Bool.pick(Out, hyp_big(x, 1077280768), Val{F.F64{U32.or(s, 1072693248), 0}}, Go{ZTanh{x}})# the hyperbolic tangent (a NaN gives the plain quiet NaN, as NumPy's own tanh does)def tanh_body(+x: F.F64) -> F.F64: out(tanh_c(F.cls(x), x))def asinh_c(c: F.Cls, +x: F.F64) -> Out: match c: case F.FNan{}: nanv(x) case F.FFin{s, e, m}: Go{ZAsinh{x}} case _: Val{x}def arcsinh_body(+x: F.F64) -> F.F64: out(asinh_c(F.cls(x), x))def acosh_c(c: F.Cls, +x: F.F64) -> Out: match c: case F.FNan{}: nanv(x) case F.FInf{s}: Val{Bool.pick(F.F64, U32.is_eq(s, 0), x, neg_nan())} case F.FZero{s}: Val{neg_nan()} case F.FFin{s, e, m}: Bool.pick(Out, F.lt(x, one()), Val{neg_nan()}, Bool.pick(Out, F.eq(x, one()), Val{F.zero(0)}, Go{ZAcosh{x}}))def arccosh_body(+x: F.F64) -> F.F64: out(acosh_c(F.cls(x), x))def atanh_c(c: F.Cls, +x: F.F64) -> Out: match c: case F.FNan{}: nanv(x) case F.FInf{s}: Val{neg_nan()} case F.FZero{s}: Val{x} case F.FFin{+s, e, m}: Bool.pick(Out, above_one(x), Val{neg_nan()}, Bool.pick(Out, F.eq(F.abs(x), one()), Val{F.inf(s)}, Go{ZAtanh{x}}))def arctanh_body(+x: F.F64) -> F.F64: out(atanh_c(F.cls(x), x))# ---- power ----def ctz_w(+m: W.W64) -> U32: (63 - W.clz(W.and(m, W.sub(W.w(0), m))) : U32)# v / 2^j for a signed v that 2^j divides (j below 32)def sdiv2j(+v: U32, +j: U32) -> U32: Bool.pick(U32, is_neg(v), (0 - W.shr32((0 - v : U32), j) : U32), W.shr32(v, j))# x = w 2^g, w odd; |y| = Y 2^f, Y oddtype PX is Data: PX{w: List<&2, U32>, g: U32, yo: W.W64, f: U32}def px_of(cx: F.Cls, cy: F.Cls) -> PX: match cx cy: case F.FFin{sx, +ex, +mx} F.FFin{sy, +ey, +my}: +tx = ctz_w(mx) +ty = ctz_w(my) PX{B.of_w64(W.shr(mx, tx)), (ex - 1075 + tx : U32), W.shr(my, ty), (ey - 1075 + ty : U32)} case _ _: PX{[1], 0, W.w(1), 0}# (w 2^g)^n exactly, 0 < |n| <= 64def pow_int(+s: U32, +w: List<&2, U32>, +g: U32, +n: U32) -> F.F64: +a = Bool.pick(U32, is_neg(n), (0 - n : U32), n) +pw = B.pow(w, a) +sh = (B.bitlen(pw) + 70 : U32) +qr = B.divq(B.shl([1], sh), pw) Bool.pick(F.F64, is_neg(n), round_q(s, qr_q(qr), Bool.not(B.is_zero(qr_r(qr))), (0 - sh - g * a : U32)), round_q(s, pw, False{}, (g * a : U32)))# 2^r for a signed r, or past the range on r's side when hugedef two_to(+s: U32, +huge: Bool, +up: Bool, +r: U32) -> F.F64: Bool.pick(F.F64, huge, Bool.pick(F.F64, up, F.inf(s), F.zero(s)), F.round_pack(s, (1084 + r : U32), W.w(1)))# x = 2^g: x^y = 2^(g y), exact when g y is an integer (x = 1 here only as |-1|: the answer is +-1)def pow_two(+s: U32, +g: U32, +yo: W.W64, +f: U32, +yneg: Bool, +neg: Bool, +x: F.F64, +y: F.F64) -> Out: +big = Bool.or(Bool.not(W.lt(yo, W.w(4096))), Bool.and(Bool.not(is_neg(f)), U32.is_lt(12, f))) +up = Bool.not(Bool.xor(yneg, is_neg(g))) +yv = Bool.pick(U32, yneg, (0 - W.low(yo) : U32), W.low(yo)) +j = (0 - f : U32) Bool.pick(Out, U32.is_eq(g, 0), Val{F.F64{U32.or(s, 1072693248), 0}}, Bool.pick(Out, Bool.not(is_neg(f)), Val{two_to(s, big, up, (g * yv * W.shl32(1, f) : U32))}, Bool.pick(Out, Bool.and(U32.is_le(j, 11), U32.is_eq(U32.and(g, (W.shl32(1, j) - 1 : U32)), 0)), Val{two_to(s, big, up, (sdiv2j(g, j) * yv : U32))}, Go{ZPow{neg, x, y}})))# the 2^j-th root of w, when w is a perfect 2^j-th powerdef root2j(n: Nat, +w: List<&2, U32>) -> Maybe<&2, List<&2, U32>>: match n: case 0n: Some{w} case 1n+k: +qr = B.isqrt2(w) Bool.pick(Maybe<&2, List<&2, U32>>, B.is_zero(qr_r(qr)), root2j(k, qr_q(qr)), None{})# y = Y / 2^j > 0: when w = t^(2^j) and 2^j divides g Y, x^y = t^Y 2^(g Y / 2^j) exactlydef pow_root(r: Maybe<&2, List<&2, U32>>, +s: U32, +g: U32, +ya: U32, +j: U32, +neg: Bool, +x: F.F64, +y: F.F64) -> Out: match r: case Some{t}: Bool.pick(Out, U32.is_eq(U32.and((g * ya : U32), (W.shl32(1, j) - 1 : U32)), 0), Val{round_q(s, B.pow(t, ya), False{}, sdiv2j((g * ya : U32), j))}, Go{ZPow{neg, x, y}}) case None{}: Go{ZPow{neg, x, y}}# which way x^y is computed: x a power of two, y a small integer, y a small odd multiple of 2^-j (j <= 5), or ballstype PK is Data: KTwo{} KInt{} KRoot{} KBall{}def pk_of(+w: List<&2, U32>, +f: U32, +y: F.F64, +yo: W.W64) -> PK: Bool.pick(PK, B.lt(w, [2]), KTwo{}, Bool.pick(PK, Bool.and(Bool.not(is_neg(f)), F.le(F.abs(y), F.F64{1078984704, 0})), KInt{}, Bool.pick(PK, Bool.and(Bool.and(is_neg(f), U32.is_le((0 - f : U32), 5)), Bool.and(Bool.not(F.signbit(y)), W.lt(yo, W.w(65)))), KRoot{}, KBall{})))def pow_k(k: PK, +w: List<&2, U32>, +g: U32, +yo: W.W64, +f: U32, +neg: Bool, +x: F.F64, +y: F.F64) -> Out: match k: case KTwo{}: pow_two(sign_word(neg), g, yo, f, F.signbit(y), neg, x, y) case KInt{}: Val{pow_int(sign_word(neg), w, g, F.to_i32(y))} case KRoot{}: pow_root(root2j(U32.to_nat((0 - f : U32)), w), sign_word(neg), g, W.low(yo), (0 - f : U32), neg, x, y) case KBall{}: Go{ZPow{neg, x, y}}def pow_px(p: PX, +neg: Bool, +x: F.F64, +y: F.F64) -> Out: match p: case PX{+w, +g, +yo, +f}: pow_k(pk_of(w, f, y, yo), w, g, yo, f, neg, F.abs(x), y)# C99's table, in order; x and y not NaN unless saiddef pow_y_inf(+x: F.F64, +y: F.F64) -> Out: Bool.pick(Out, F.eq(F.abs(x), one()), Val{one()}, Val{Bool.pick(F.F64, Bool.xor(F.lt(F.abs(x), one()), F.signbit(y)), F.zero(0), F.inf(0))})def pow_x_inf(+x: F.F64, +y: F.F64) -> Out: +odd = Bool.and(F.signbit(x), is_odd(y)) Val{Bool.pick(F.F64, F.signbit(y), F.zero(sign_word(odd)), F.inf(sign_word(odd)))}def pow_x_zero(+x: F.F64, +y: F.F64) -> Out: +odd = is_odd(y) Val{Bool.pick(F.F64, F.signbit(y), F.inf(Bool.pick(U32, odd, F.sign_of(F.hi_bits(x)), 0)), Bool.pick(F.F64, odd, x, F.zero(0)))}def pow_fin(+x: F.F64, +y: F.F64) -> Out: Bool.pick(Out, F.signbit(x), Bool.pick(Out, is_int(y), pow_px(px_of(F.cls(x), F.cls(y)), is_odd(y), x, y), Val{neg_nan()}), pow_px(px_of(F.cls(x), F.cls(y)), False{}, x, y))type PC is Data: CY0{} CX1{} CXNan{} CYNan{} CYInf{} CXInf{} CXZero{} CFin{}def pc_of(+x: F.F64, +y: F.F64) -> PC: Bool.pick(PC, F.eq(y, F.zero(0)), CY0{}, Bool.pick(PC, F.eq(x, one()), CX1{}, Bool.pick(PC, F.is_nan(x), CXNan{}, Bool.pick(PC, F.is_nan(y), CYNan{}, Bool.pick(PC, F.is_inf(y), CYInf{}, Bool.pick(PC, F.is_inf(x), CXInf{}, Bool.pick(PC, F.eq(x, F.zero(0)), CXZero{}, CFin{})))))))def pow_pc(c: PC, +x: F.F64, +y: F.F64) -> Out: match c: case CY0{}: Bool.pick(Out, is_snan(x), nanv(x), Val{one()}) case CX1{}: Bool.pick(Out, is_snan(y), nanv(y), Val{one()}) case CXNan{}: Bool.pick(Out, is_odd(y), Val{F.quiet(F.abs(x))}, nanv(x)) case CYNan{}: nanv(y) case CYInf{}: pow_y_inf(x, y) case CXInf{}: pow_x_inf(x, y) case CXZero{}: pow_x_zero(x, y) case CFin{}: pow_fin(x, y)# x^y (C99's special cases; exact whenever the answer is exact or halfway, every other answer correctly rounded). A NaN# x passes through quieted, its sign cleared when y is an odd integer, as glibc's pow does.def power(+x: F.F64, +y: F.F64) -> F.F64: out(pow_pc(pc_of(x, y), x, y))# ---- every one-argument function: a NaN first ----type UK is Data: UExp{} UExp2{} UExpm1{} ULog{} ULog2{} ULog10{} ULog1p{} USin{} UCos{} UTan{} UArcsin{} UArccos{} UArctan{} USinh{} UCosh{} UTanh{} UArcsinh{} UArccosh{} UArctanh{} UCbrt{}def nan_of(k: UK, +x: F.F64) -> F.F64: match k: case UTanh{}: F.F64{2146959360, 0} case _: F.quiet(x)def unary_go(k: UK, +x: F.F64) -> F.F64: match k: case UExp{}: exp_body(x) case UExp2{}: exp2_body(x) case UExpm1{}: expm1_body(x) case ULog{}: log_body(x) case ULog2{}: log2_body(x) case ULog10{}: log10_body(x) case ULog1p{}: log1p_body(x) case USin{}: sin_body(x) case UCos{}: cos_body(x) case UTan{}: tan_body(x) case UArcsin{}: arcsin_body(x) case UArccos{}: arccos_body(x) case UArctan{}: arctan_body(x) case USinh{}: sinh_body(x) case UCosh{}: cosh_body(x) case UTanh{}: tanh_body(x) case UArcsinh{}: arcsinh_body(x) case UArccosh{}: arccosh_body(x) case UArctanh{}: arctanh_body(x) case UCbrt{}: cbrt_body(x)# k's function at x, a NaN passing through quieted (tanh's gives the plain quiet NaN, as NumPy's own tanh does)def unary(na: Bool, k: UK, +x: F.F64) -> F.F64: match na: case True{}: nan_of(k, x) case False{}: unary_go(k, x)# e^xdef exp(+x: F.F64) -> F.F64: unary(F.is_nan(x), UExp{}, x)# 2^x (exact for integers)def exp2(+x: F.F64) -> F.F64: unary(F.is_nan(x), UExp2{}, x)# e^x - 1, exact near 0def expm1(+x: F.F64) -> F.F64: unary(F.is_nan(x), UExpm1{}, x)# the natural logarithmdef log(+x: F.F64) -> F.F64: unary(F.is_nan(x), ULog{}, x)# the base-2 logarithm (exact for powers of two)def log2(+x: F.F64) -> F.F64: unary(F.is_nan(x), ULog2{}, x)# the base-10 logarithm (exact for 10^k)def log10(+x: F.F64) -> F.F64: unary(F.is_nan(x), ULog10{}, x)# log(1 + x), exact near 0def log1p(+x: F.F64) -> F.F64: unary(F.is_nan(x), ULog1p{}, x)# the sine (argument reduction with 3600 bits of 2/pi, so exact for any x)def sin(+x: F.F64) -> F.F64: unary(F.is_nan(x), USin{}, x)# the cosinedef cos(+x: F.F64) -> F.F64: unary(F.is_nan(x), UCos{}, x)# the tangentdef tan(+x: F.F64) -> F.F64: unary(F.is_nan(x), UTan{}, x)# the arcsine, in [-pi/2, pi/2]def arcsin(+x: F.F64) -> F.F64: unary(F.is_nan(x), UArcsin{}, x)# the arccosine, in [0, pi]def arccos(+x: F.F64) -> F.F64: unary(F.is_nan(x), UArccos{}, x)# the arctangent, in [-pi/2, pi/2]def arctan(+x: F.F64) -> F.F64: unary(F.is_nan(x), UArctan{}, x)# the hyperbolic sinedef sinh(+x: F.F64) -> F.F64: unary(F.is_nan(x), USinh{}, x)# the hyperbolic cosinedef cosh(+x: F.F64) -> F.F64: unary(F.is_nan(x), UCosh{}, x)# the hyperbolic tangentdef tanh(+x: F.F64) -> F.F64: unary(F.is_nan(x), UTanh{}, x)# the inverse hyperbolic sinedef arcsinh(+x: F.F64) -> F.F64: unary(F.is_nan(x), UArcsinh{}, x)# the inverse hyperbolic cosine, x >= 1def arccosh(+x: F.F64) -> F.F64: unary(F.is_nan(x), UArccosh{}, x)# the inverse hyperbolic tangent, |x| <= 1def arctanh(+x: F.F64) -> F.F64: unary(F.is_nan(x), UArctanh{}, x)# the cube root (exact: an integer cube root with its remainder)def cbrt(+x: F.F64) -> F.F64: unary(F.is_nan(x), UCbrt{}, x)