# bend-float64's fast path for the math functions: the same evaluations as math.bend's, on 128-bit fast balls # (fb.bend) with tables (fbconsts.bend), so one call costs a few microseconds. Every rounding error is counted by the # balls themselves, and each polynomial adds its own truncation bound, so a ball that decides is the correctly # rounded answer; one that cannot (a hard case, a huge argument) sends the caller to the arbitrary-precision path. # Pure. import Base import ./wide.bend as W import ./float64.bend as F import ./fb.bend as Q import ./fbconsts.bend as C def is_neg(+x: U32) -> Bool: U32.is_le(2147483648, x) # the float64 nearest the ball's midpoint, ignoring its error (for choosing branches, never for answers) def approx(a: Q.FB) -> F.F64: match a: case Q.FB{+n, +m, e, r}: Bool.pick(F.F64, Q.is_zero(m), F.zero(Bool.pick(U32, n, 2147483648, 0)), Q.round_w(n, m, e)) def of_i32(+k: U32) -> Q.FB: Bool.pick(Q.FB, is_neg(k), Q.neg(Q.of_u32((0 - k : U32))), Q.of_u32(k)) def signed(+neg: Bool, +a: Q.FB) -> Q.FB: Bool.pick(Q.FB, neg, Q.neg(a), a) # |a| below 2^k, judged on the midpoint def below(+a: Q.FB, +k: U32) -> Bool: Q.s_lt(Q.top(a), k) # ---- exp: a = (64 k + j) ln2/64 + r, |r| <= ln2/128: e^a = 2^k 2^(j/64) e^r ---- def exp_go(+a: Q.FB) -> Q.FB: +n = F.to_i32(F.rint(F.mul(approx(a), C.c64_ln2()))) +j = U32.and(n, 63) +h = (n - j : U32) +k = Bool.pick(U32, is_neg(h), (0 - U32.shrn((0 - h : U32), 6n) : U32), U32.shrn(h, 6n)) Q.mul_pow2(Q.mul(C.exp_poly(Q.sub(a, Q.mul(of_i32(n), C.ln2_64()))), C.exp_tab(j)), k) def exp_big(huge: Bool, +a: Q.FB) -> Q.FB: match huge: case True{}: Q.FB{False{}, W.W128{W.W64{2147483648, 0}, W.w(0)}, Bool.pick(U32, Q.neg_of(a), (0 - 3127 : U32), 2873), 0} case False{}: exp_go(a) # e^a; past |a| = 1500 every point overflows or underflows, so a power of two far out stands for them all def exp(+a: Q.FB) -> Q.FB: exp_big(F.lt(F.F64{1083666432, 0}, F.abs(approx(a))), a) def expm1_go(near: Bool, +a: Q.FB) -> Q.FB: match near: case True{}: Q.mul(a, C.expm1_inner(a)) case False{}: Q.sub(exp(a), Q.one()) # e^a - 1 (the series below 2^-7, exact near 0) def expm1(+a: Q.FB) -> Q.FB: expm1_go(below(a, (0 - 7 : U32)), a) # ---- log: v = 2^e m, m in [1, 2); r near 2^16 / m from a table: log v = e ln2 - log(r/2^16) + log1p(m r/2^16 - 1) ---- def log1p_small(+t: Q.FB) -> Q.FB: Q.mul(t, C.log1p_inner(t)) def log_tab_go(t: C.LogT, +mant: Q.FB, +e: U32) -> Q.FB: match t: case C.LogT{r, l}: Q.add(Q.add(Q.mul(C.ln2(), of_i32(e)), l), log1p_small(Q.sub(Q.mul_pow2(Q.mul(mant, Q.of_u32(r)), (0 - 16 : U32)), Q.one()))) def log_far(+v: Q.FB) -> Q.FB: match v: case Q.FB{n, +m, +e, r}: log_tab_go(C.log_tab(U32.and(U32.shrn(Q.top32(m), 24n), 127)), Q.FB{False{}, m, (0 - 127 : U32), r}, (e + 127 : U32)) def log_go(near: Bool, +v: Q.FB) -> Q.FB: match near: case True{}: log1p_small(Q.sub(v, Q.one())) case False{}: log_far(v) # log v, v > 0 (near 1, the series of v - 1, exact) def log(+v: Q.FB) -> Q.FB: log_go(F.lt(F.abs(F.sub(approx(v), F.F64{1072693248, 0})), F.F64{1064304640, 0}), v) def log1p_go(near: Bool, +u: Q.FB) -> Q.FB: match near: case True{}: log1p_small(u) case False{}: log(Q.add(Q.one(), u)) # log(1 + u) (the series below 2^-8, exact near 0) def log1p(+u: Q.FB) -> Q.FB: log1p_go(below(u, (0 - 8 : U32)), u) # ---- sin, cos, tan: x = k pi/2 + r with pi/2 in three parts, |x| < 2^30 ---- def sin_r(+r: Q.FB) -> Q.FB: Q.mul(r, C.sin_inner(Q.sqr(r))) def cos_r(+r: Q.FB) -> Q.FB: C.cos_poly(Q.sqr(r)) type TQ is Data: TSin{} TCos{} TTan{} TCot{} def tq(t: TQ, +r: Q.FB) -> Q.FB: match t: case TSin{}: sin_r(r) case TCos{}: cos_r(r) case TTan{}: Q.div(sin_r(r), cos_r(r)) case TCot{}: Q.div(cos_r(r), sin_r(r)) def trig_go(+which: U32, +x: F.F64) -> Q.FB: +k = F.to_i32(F.rint(F.mul(x, C.c2_pi()))) +kb = of_i32(k) +r = Q.sub(Q.sub(Q.sub(Q.of_f64(x), Q.mul(kb, C.pio2_1())), Q.mul(kb, C.pio2_2())), Q.mul(kb, C.pio2_3())) +q = U32.and(k, 3) +odd = U32.is_eq(U32.and(q, 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, q), Bool.or(U32.is_eq(q, 1), U32.is_eq(q, 2)))) signed(minus, tq(t, r)) def trig_big(big: Bool, +which: U32, +x: F.F64) -> Q.FB: match big: case True{}: Q.fail() case False{}: trig_go(which, x) # which: 0 sin, 1 cos, 2 tan (|x| from 2^30 up: a failed ball, for the exact reduction) def trig(+which: U32, +x: F.F64) -> Q.FB: trig_big(F.le(F.F64{1104150528, 0}, F.abs(x)), which, x) # ---- atan: u in [0, 1]: atan u = atan(j/16) + atan((u - j/16) / (1 + u j/16)), j/16 nearest u ---- def atan_poly(+u: Q.FB) -> Q.FB: Q.mul(u, C.atan_inner(Q.sqr(u))) def atan_j(zero: Bool, +u: Q.FB, +j: U32) -> Q.FB: match zero: case True{}: atan_poly(u) case False{}: +c = Q.mul_pow2(Q.of_u32(j), (0 - 4 : U32)) Q.add(C.atan_tab(j), atan_poly(Q.div(Q.sub(u, c), Q.add(Q.one(), Q.mul(u, c))))) def atan_small(+u: Q.FB) -> Q.FB: +j = F.to_i32(F.rint(F.mul(approx(u), C.c16()))) atan_j(U32.is_eq(j, 0), u, j) def atan_go(small: Bool, +u: Q.FB) -> Q.FB: match small: case True{}: atan_small(u) case False{}: Q.sub(C.pi_2(), atan_small(Q.div(Q.one(), u))) # atan t, any t def atan(+t: Q.FB) -> Q.FB: signed(Q.neg_of(t), atan_go(F.le(F.abs(approx(t)), F.F64{1072693248, 0}), Q.abs(t)))