# bend-float64's fast balls: (-1)^neg * m * 2^e with |true value - that| <= err * 2^e, m a 128-bit integer with # its top bit set (or 0), err a count of units of 2^e that every operation updates, rounding up. A count from 2^30 # up marks a failed ball, and every operation keeps it failed. The same idea as # ball.bend at a fixed 128 bits: no lists, so a few hundred native word operations per step. An error count past # 2^30 means the ball has lost the precision to answer; decide then answers nothing and the caller falls back to the # arbitrary-precision balls. Pure. import Base import ./wide.bend as W import ./float64.bend as F type FB is Data: FB{neg: Bool, m: W.W128, e: U32, err: U32} def sat() -> U32: 2147483648 def is_neg(+x: U32) -> Bool: U32.is_le(2147483648, x) def s_lt(+a: U32, +b: U32) -> Bool: F.s_lt(a, b) # an error count from 2^30 up means the ball has failed: it stays failed through every operation def lost(+x: U32) -> Bool: U32.is_le(1073741824, x) # a + b, stuck at 2^31 once either is failed def sadd(+a: U32, +b: U32) -> U32: Bool.pick(U32, Bool.or(lost(a), lost(b)), sat(), Bool.pick(U32, lost((a + b : U32)), sat(), (a + b : U32))) # ceil(x / 2^d), d >= 0 def ceil_down(+x: U32, +d: U32) -> U32: Bool.pick(U32, U32.is_lt(d, 32), (W.shr32(x, d) + Bool.to_u32(U32.is_ne(U32.and(x, (W.shl32(1, d) - 1 : U32)), 0)) : U32), Bool.to_u32(U32.is_ne(x, 0))) # ceil(x * 2^k) for a signed k, stuck at 2^31 def scale_up(+x: U32, +k: U32) -> U32: Bool.pick(U32, U32.is_eq(x, 0), 0, Bool.pick(U32, lost(x), sat(), Bool.pick(U32, is_neg(k), ceil_down(x, (0 - k : U32)), Bool.pick(U32, Bool.and(U32.is_lt(k, 30), U32.is_le(x, W.shr32(1073741823, k))), W.shl32(x, k), sat())))) def halve(+v: U32) -> U32: Bool.pick(U32, is_neg(v), (0 - U32.div((0 - v : U32), 2) : U32), U32.div(v, 2)) def zero128() -> W.W128: W.W128{W.w(0), W.w(0)} def fail() -> FB: FB{False{}, zero128(), 0, sat()} def top32(m: W.W128) -> U32: W.high(W.hi128(m)) def is_zero(m: W.W128) -> Bool: W.is_zero128(m) # m * 2^e with m shifted until its top bit is set (err scaled with it) def norm(+neg: Bool, +m: W.W128, +e: U32, +err: U32) -> FB: +n = W.clz128(m) Bool.pick(FB, U32.is_eq(n, 128), FB{neg, m, e, err}, FB{neg, W.shl128g(m, n), (e - n : U32), scale_up(err, n)}) # a finite float64, exactly def of_cls(c: F.Cls) -> FB: match c: case F.FFin{s, +e, m}: norm(U32.is_ne(s, 0), W.W128{m, W.w(0)}, (e - 1075 - 64 : U32), 0) case F.FZero{s}: FB{U32.is_ne(s, 0), zero128(), 0, 0} case _: fail() def of_f64(x: F.F64) -> FB: of_cls(F.cls(x)) def of_u32(+n: U32) -> FB: norm(False{}, W.W128{W.w(0), W.w(n)}, 0, 0) def one() -> FB: FB{False{}, W.W128{W.W64{2147483648, 0}, W.w(0)}, (0 - 127 : U32), 0} def neg(a: FB) -> FB: match a: case FB{n, m, e, r}: FB{Bool.not(n), m, e, r} def abs(a: FB) -> FB: match a: case FB{n, m, e, r}: FB{False{}, m, e, r} def with_neg(+n: Bool, a: FB) -> FB: match a: case FB{x, m, e, r}: FB{n, m, e, r} def neg_of(a: FB) -> Bool: match a: case FB{n, m, e, r}: n def exp_of(a: FB) -> U32: match a: case FB{n, m, e, r}: e def err_of(a: FB) -> U32: match a: case FB{n, m, e, r}: r def mid_of(a: FB) -> W.W128: match a: case FB{n, m, e, r}: m def mul_pow2(a: FB, +k: U32) -> FB: match a: case FB{n, m, e, r}: FB{n, m, (e + k : U32), r} def widen(a: FB, +x: U32) -> FB: match a: case FB{n, m, e, r}: FB{n, m, e, sadd(r, x)} # the exponent of the top bit plus one (a's magnitude is below 2^top); very low for 0 def top(a: FB) -> U32: match a: case FB{n, +m, e, r}: Bool.pick(U32, is_zero(m), 2147483648, (e + 128 : U32)) # err units of 2^from as units of 2^to, rounded up def err_to(+err: U32, +from: U32, +to: U32) -> U32: scale_up(err, (from - to : U32)) # ---- multiplication ---- # p = floor(ma mb / 2^128); exact when the factors' trailing zeros reach 128 (the low half of the product is 0) def mul_go(+neg: Bool, +p: W.W128, +e: U32, +tz: U32, +ca: U32, +cb: U32, +cab: U32) -> FB: Bool.pick(FB, U32.is_le(2147483648, top32(p)), FB{neg, p, (e + 128 : U32), sadd(Bool.to_u32(U32.is_lt(tz, 128)), sadd(sadd(ca, cb), cab))}, FB{neg, W.shl128g(p, 1), (e + 127 : U32), sadd(scale_up(Bool.to_u32(U32.is_lt(tz, 129)), 1), scale_up(sadd(sadd(ca, cb), cab), 1))}) # a's top word times eb, over 2^32, rounded up (the part of a b's error that comes from b's) def cross(+ta: U32, +eb: U32) -> U32: Bool.pick(U32, U32.is_eq(eb, 0), 0, Bool.pick(U32, lost(eb), sat(), sadd(W.high(W.mul32(ta, eb)), 2))) # a * b def mul(a: FB, b: FB) -> FB: match a b: case FB{+na, +ma, +ea, +ra} FB{+nb, +mb, +eb, +rb}: Bool.pick(FB, Bool.or(is_zero(ma), is_zero(mb)), Bool.pick(FB, Bool.or(Bool.and(is_zero(ma), U32.is_eq(ra, 0)), Bool.and(is_zero(mb), U32.is_eq(rb, 0))), FB{Bool.xor(na, nb), zero128(), 0, 0}, fail()), mul_go(Bool.xor(na, nb), W.mulhi(ma, mb), (ea + eb : U32), (W.ctz128(ma) + W.ctz128(mb) : U32), cross(top32(ma), rb), cross(top32(mb), ra), Bool.to_u32(Bool.and(U32.is_ne(ra, 0), U32.is_ne(rb, 0))))) def mul_u(a: FB, +n: U32) -> FB: mul(a, of_u32(n)) def sqr(+a: FB) -> FB: mul(a, a) # ---- addition ---- def add_same(+n: Bool, +a: W.W128, +b: W.W128, +e: U32, +r: U32) -> FB: +s = W.add128(a, b) Bool.pick(FB, W.lt128(s, a), FB{n, W.W128{W.or(W.shr(W.hi128(s), 1), W.W64{2147483648, 0}), W.or(W.shr(W.lo128(s), 1), W.shl(W.hi128(s), 63))}, (e + 1 : U32), sadd(scale_up(r, (0 - 1 : U32)), 1)}, FB{n, s, e, r}) def add_diff(+na: Bool, +a: W.W128, +nb: Bool, +b: W.W128, +e: U32, +r: U32) -> FB: +lt = W.lt128(a, b) norm(Bool.pick(Bool, lt, nb, na), Bool.pick(W.W128, lt, W.sub128(b, a), W.sub128(a, b)), e, r) def add_sign(diff: Bool, +nx: Bool, +mx: W.W128, +ny: Bool, +ys: W.W128, +e: U32, +r: U32) -> FB: match diff: case True{}: add_diff(nx, mx, ny, ys, e, r) case False{}: add_same(nx, mx, ys, e, r) # x the larger exponent: y's mantissa shifted down to x's units (a unit more error when a set bit is shifted out) def add_al(+nx: Bool, +mx: W.W128, +ex: U32, +rx: U32, +ny: Bool, +my: W.W128, +ey: U32, +ry: U32) -> FB: +d = (ex - ey : U32) add_sign(Bool.xor(nx, ny), nx, mx, ny, W.shr128(my, d), ex, sadd(sadd(rx, err_to(ry, ey, ex)), Bool.to_u32(U32.is_lt(W.ctz128(my), d)))) def add_ord(swap: Bool, +na: Bool, +ma: W.W128, +ea: U32, +ra: U32, +nb: Bool, +mb: W.W128, +eb: U32, +rb: U32) -> FB: match swap: case True{}: add_al(nb, mb, eb, rb, na, ma, ea, ra) case False{}: add_al(na, ma, ea, ra, nb, mb, eb, rb) def add_go(a: FB, b: FB) -> FB: match a b: case FB{na, ma, +ea, ra} FB{nb, mb, +eb, rb}: add_ord(s_lt(ea, eb), na, ma, ea, ra, nb, mb, eb, rb) def add_z(za: Bool, zb: Bool, +a: FB, +b: FB) -> FB: match za zb: case False{} False{}: add_go(a, b) case True{} False{}: widen(b, err_to(err_of(a), exp_of(a), exp_of(b))) case False{} True{}: widen(a, err_to(err_of(b), exp_of(b), exp_of(a))) case True{} True{}: Bool.pick(FB, U32.is_eq(U32.or(err_of(a), err_of(b)), 0), a, fail()) # a + b def add(+a: FB, +b: FB) -> FB: add_z(is_zero(mid_of(a)), is_zero(mid_of(b)), a, b) # a - b def sub(+a: FB, b: FB) -> FB: add(a, neg(b)) # ---- division and square root: Newton steps from a float64 start, the residual bounding the error ---- def mid(a: FB) -> FB: match a: case FB{n, m, e, r}: FB{n, m, e, 0} # m / 2^127 as a float64 in [1, 2) (its top 53 bits) def mant_f64(+m: W.W128) -> F.F64: F.of_bits(U32.or(1072693248, U32.and(W.high(W.shr(W.hi128(m), 11)), 1048575)), W.low(W.shr(W.hi128(m), 11))) # |a - b| <= n 2^e for two positive balls of about the same size (exponents at most 1 apart), without normalizing # the small difference (which would make its error count overflow): n counts the midpoints' difference and both errors type RS is Data: RS{n: U32, e: U32} def resid_go(+mh: W.W128, +eh: U32, +rh: U32, +ml: W.W128, +el: U32, +rl: U32) -> RS: +d = (eh - el : U32) +ls = W.shr128(ml, d) +df = Bool.pick(W.W128, W.lt128(mh, ls), W.sub128(ls, mh), W.sub128(mh, ls)) RS{Bool.pick(U32, Bool.or(U32.is_lt(1, d), Bool.not(W.lt128(df, W.W128{W.w(0), W.w(1073741824)}))), sat(), sadd(sadd(W.low(W.lo128(df)), rh), sadd(err_to(rl, el, eh), Bool.to_u32(U32.is_lt(W.ctz128(ml), d))))), eh} def resid(a: FB, b: FB) -> RS: match a b: case FB{na, +ma, +ea, +ra} FB{nb, +mb, +eb, +rb}: Bool.pick(RS, s_lt(ea, eb), resid_go(mb, eb, rb, ma, ea, ra), resid_go(ma, ea, ra, mb, eb, rb)) # ceil(n 2^(e + k) (1 + 2^-8)) def resid_units(r: RS, +k: U32) -> U32: match r: case RS{+n, e}: scale_up(sadd(sadd(n, W.shr32(n, 8)), 1), (e + k : U32)) def recip_step(+b: FB, +r: FB) -> FB: mid(add(r, mul(r, sub(one(), mul(b, r))))) def div_go(+a: FB, +b: FB) -> FB: +bm = mid(abs(b)) +r0 = mul_pow2(of_f64(F.div(F.F64{1072693248, 0}, mant_f64(mid_of(b)))), (0 - exp_of(b) - 127 : U32)) +r = recip_step(bm, recip_step(bm, r0)) +q = mul(abs(a), r) with_neg(Bool.xor(neg_of(a), neg_of(b)), widen(q, resid_units(resid(one(), mul(abs(b), r)), 128))) # a / b, b's ball away from 0 (else a failed ball) def div(+a: FB, +b: FB) -> FB: Bool.pick(FB, Bool.or(is_zero(mid_of(b)), U32.is_le(1073741824, err_of(b))), fail(), div_go(a, b)) def rsqrt_step(+x: FB, +r: FB) -> FB: mid(add(r, mul_pow2(mul(r, sub(one(), mul(x, sqr(r)))), (0 - 1 : U32)))) def sqrt_go(+x: FB) -> FB: +t = (exp_of(x) + 127 : U32) +odd = U32.and(t, 1) +mant = F.mul(mant_f64(mid_of(x)), Bool.pick(F.F64, U32.is_eq(odd, 1), F.F64{1073741824, 0}, F.F64{1072693248, 0})) +xm = mid(x) +r0 = mul_pow2(of_f64(F.div(F.F64{1072693248, 0}, F.sqrt(mant))), (0 - halve((t - odd : U32)) : U32)) +r = rsqrt_step(xm, rsqrt_step(xm, r0)) +s = mid(mul(xm, r)) widen(s, resid_units(resid(x, sqr(s)), (0 - 127 - exp_of(s) - exp_of(s) : U32))) # sqrt(x), x's ball positive (else a failed ball) def sqrt(+x: FB) -> FB: Bool.pick(FB, Bool.or(Bool.or(neg_of(x), is_zero(mid_of(x))), U32.is_le(1073741824, err_of(x))), fail(), sqrt_go(x)) # x = mant 2^t, mant in [1, 2): 1/sqrt(x) from the float64 1/sqrt(mant) (or of 2 mant, t odd), two Newton steps; # s = x r, widened by |x - s^2| / s in units of s (s >= 2^127 2^es) # ---- deciding ---- def round_w_m(+rm: F.Rnd, +neg: Bool, w: W.W128, +e: U32) -> F.F64: match w: case W.W128{h, l}: F.round_pack_m(rm, Bool.pick(U32, neg, 2147483648, 0), (1084 + e + 64 : U32), W.jam_or(h, Bool.not(W.is_zero(l)))) def round_w(+neg: Bool, w: W.W128, +e: U32) -> F.F64: round_w_m(F.RNearEven{}, neg, w, e) def same(a: F.F64, b: F.F64) -> Bool: match a b: case F.F64{ah, al} F.F64{bh, bl}: Bool.and(U32.is_eq(ah, bh), U32.is_eq(al, bl)) def decide_go(+rm: F.Rnd, +n: Bool, +m: W.W128, +e: U32, +r: U32) -> Maybe<&2, F.F64>: +lo = round_w_m(rm, n, W.sub128(m, W.w128(W.w(r))), e) +hi = W.add128(m, W.w128(W.w(r))) Bool.pick(Maybe<&2, F.F64>, Bool.and(Bool.not(W.lt128(hi, m)), same(lo, round_w_m(rm, n, hi, e))), Some{lo}, None{}) # the float64 every point of the ball rounds to in mode rm, when they all round to the same one def decide_m(+rm: F.Rnd, a: FB) -> Maybe<&2, F.F64>: match a: case FB{+n, +m, +e, +r}: Bool.pick(Maybe<&2, F.F64>, Bool.or(is_zero(m), U32.is_le(1073741824, r)), None{}, decide_go(rm, n, m, e, r)) # the float64 every point of the ball rounds to (to nearest), when they all round to the same one def decide(a: FB) -> Maybe<&2, F.F64>: decide_m(F.RNearEven{}, a)