# 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, heavy cancellation) 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)) # k's quadrant (k mod 4) and the reduced argument r, |r| <= pi/4: sin, cos (which 0, 1) or tan (2) of k pi/2 + r def trig_kr(+which: U32, +k: U32, +r: Q.FB) -> Q.FB: +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)) # |x| below 2^30: k = x 2/pi rounded, r = x - k pi/2 with pi/2 in three parts (the first two exact) 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) trig_kr(which, k, 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()))) # ---- large arguments (Payne and Hanek): |x| = m 2^e, and of 2/pi only the 256 bits that matter ---- # The bits of 2/pi worth 4 or more once multiplied by |x| are dropped (whole turns), the window T holds the next 256, # and those below are left out: under m 2^-254 < 2^-201 of a quadrant. So |x| 2/pi = m T 2^-254 mod 4 to within # 2^-201, and m T's bits 254 and 255 are the quadrant. # bits of a then b: a shifted up by sh (0 .. 31), b's top bits below def funnel(+a: U32, +b: U32, +sh: U32) -> U32: Bool.pick(U32, U32.is_eq(sh, 0), a, U32.or(W.shl32(a, sh), W.shr32(b, (32 - sh : U32)))) def pair(+a: U32, +b: U32, +c: U32, +sh: U32) -> W.W64: W.W64{funnel(a, b, sh), funnel(b, c, sh)} def or128(a: W.W128, b: W.W128) -> W.W128: match a b: case W.W128{ah, al} W.W128{bh, bl}: W.W128{W.or(ah, bh), W.or(al, bl)} def lo_of(+x: W.W64) -> W.W128: W.W128{W.w(0), x} # 256-bit numbers as four 64-bit words, the top first type R4 is Data: R4{r3: W.W64, r2: W.W64, r1: W.W64, r0: W.W64} # m T mod 2^256 def mul_win(+m: W.W64, t: R4) -> R4: match t: case R4{t3, t2, t1, t0}: +p0 = W.mul64(m, t0) +p1 = W.mul64(m, t1) +p2 = W.mul64(m, t2) +p3 = W.mul64(m, t3) +s1 = W.add128(lo_of(W.hi128(p0)), lo_of(W.lo128(p1))) +s2 = W.add128(W.add128(lo_of(W.hi128(s1)), lo_of(W.hi128(p1))), lo_of(W.lo128(p2))) +s3 = W.add128(W.add128(lo_of(W.hi128(s2)), lo_of(W.hi128(p2))), lo_of(W.lo128(p3))) R4{W.lo128(s3), W.lo128(s2), W.lo128(s1), W.lo128(p0)} # with x = m 2^(e - 1075), the window T: its top bit is bit tb = 3601 - (e - 1075) of floor(2/pi 2^3600) # (C.tpi_word), bit tb mod 32 of word tb / 32, so T's words are those from there down, shifted up by sh def window(+e: U32) -> R4: +tb = (4676 - e : U32) +sh = (31 - U32.and(tb, 31) : U32) +j = U32.shrn(tb, 5n) +a0 = C.tpi_word(j) +a2 = C.tpi_word((j - 2 : U32)) +a4 = C.tpi_word((j - 4 : U32)) +a6 = C.tpi_word((j - 6 : U32)) R4{pair(a0, C.tpi_word((j - 1 : U32)), a2, sh), pair(a2, C.tpi_word((j - 3 : U32)), a4, sh), pair(a4, C.tpi_word((j - 5 : U32)), a6, sh), pair(a6, C.tpi_word((j - 7 : U32)), C.tpi_word((j - 8 : U32)), sh)} # |x| 2/pi as a quadrant k (mod 4) and the fraction left, as a ball: |x| = k pi/2 + f pi/2, |f| <= 1/2 type KF is Data: KF{k: U32, f: Q.FB} # the quadrant from bits 254-255 of m T, the fraction f from the 254 below (as f - 1, the next quadrant, from 1/2 up), # normalized to 128 bits: units of 2^(-126 - n) after n leading zeros, 1 for the bits cut and 2^(n - 75) for the # window's end. A fraction under 2^-126 (none is, for a float64) fails the ball. def big_frac(r: R4) -> KF: match r: case R4{+r3, r2, r1, r0}: +h = W.high(r3) +up = U32.is_ne(U32.and(h, 536870912), 0) +fh: W.W128 = W.W128{W.and(r3, W.W64{1073741823, 4294967295}), r2} +fl: W.W128 = W.W128{r1, r0} +gl = Bool.pick(W.W128, up, W.sub128(lo_of(W.w(0)), fl), fl) +gh = Bool.pick(W.W128, up, W.sub128(W.sub128(W.W128{W.W64{1073741824, 0}, W.w(0)}, fh), lo_of(W.w(Bool.to_u32(Bool.not(W.is_zero128(fl)))))), fh) +n = W.clz128(gh) KF{(U32.shrn(h, 30n) + Bool.to_u32(up) : U32), Bool.pick(Q.FB, U32.is_eq(n, 128), Q.fail(), Q.FB{up, or128(W.shl128g(gh, n), W.shr128(gl, (128 - n : U32))), (0 - 126 - n : U32), Q.sadd(1, Q.scale_up(1, (n - 75 : U32)))})} # |x| 2/pi reduced, for |x| from 2^30 up def far_c(c: F.Cls) -> KF: match c: case F.FFin{s, +e, m}: big_frac(mul_win(m, window(e))) case _: KF{0, Q.fail()} def far(x: F.F64) -> KF: far_c(F.cls(x)) def trig_kf(r: KF, +which: U32, +xneg: Bool) -> Q.FB: match r: case KF{k, f}: signed(Bool.and(xneg, U32.is_ne(which, 1)), trig_kr(which, k, Q.mul(f, C.pi_2()))) def trig_big(big: Bool, +which: U32, +x: F.F64) -> Q.FB: match big: case True{}: trig_kf(far(x), which, F.signbit(x)) case False{}: trig_go(which, x) # which: 0 sin, 1 cos, 2 tan (x finite and nonzero) 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)))