# bend-float64's balls: arbitrary-precision numbers with a rigorous error bound, for the correctly rounded math # functions. A ball is (-1)^neg * m * 2^e with |true value - that| <= r: m a big integer, e a signed word, r an error # bound kept as a 30-bit number times a power of two and always rounded up. Each operation keeps p bits and widens r # by everything it drops, so the true result is always inside. decide answers the float64 every point of a ball # rounds to, when they all round to the same one. Pure. import Base import ./wide.bend as W import ./big.bend as B import ./float64.bend as F # ---- signed exponents ---- def s_lt(+a: U32, +b: U32) -> Bool: F.s_lt(a, b) def s_max(+a: U32, +b: U32) -> U32: Bool.pick(U32, s_lt(a, b), b, a) def s_min(+a: U32, +b: U32) -> U32: Bool.pick(U32, s_lt(a, b), a, b) def is_neg(+x: U32) -> Bool: U32.is_le(2147483648, x) # ---- error bounds: m * 2^e, m in [2^30, 2^31] (or 0) ---- type Mag is Data: Mag{m: U32, e: U32} def mzero() -> Mag: Mag{0, 0} def mag_is_zero(a: Mag) -> Bool: match a: case Mag{m, e}: U32.is_eq(m, 0) def wbits(+w: W.W64) -> U32: (64 - W.clz(w) : U32) # w * 2^e as a Mag, rounded up (up) or down def mag_w_n(+up: Bool, +w: W.W64, +e: U32, +n: U32) -> Mag: Bool.pick(Mag, U32.is_eq(n, 0), mzero(), Bool.pick(Mag, U32.is_le(n, 31), Mag{W.low(W.shl(w, (31 - n : U32))), (e - (31 - n) : U32)}, Mag{(W.low(W.shr(w, (n - 31 : U32))) + Bool.to_u32(Bool.and(up, Bool.not(W.eq(W.shl(W.shr(w, (n - 31 : U32)), (n - 31 : U32)), w)))) : U32), (e + n - 31 : U32)})) def mag_w(+w: W.W64, +e: U32) -> Mag: mag_w_n(True{}, w, e, wbits(w)) def mag_w_low(+w: W.W64, +e: U32) -> Mag: mag_w_n(False{}, w, e, wbits(w)) def pow2(+e: U32) -> Mag: Mag{1073741824, (e - 30 : U32)} # m / 2^d rounded up, as a W64 def ceil_shr(+m: U32, +d: U32) -> W.W64: Bool.pick(W.W64, U32.is_lt(d, 63), W.shr(W.add(W.w(m), W.sub(W.shl(W.w(1), d), W.w(1))), d), W.w(Bool.to_u32(U32.is_ne(m, 0)))) def mag_add_go(a: Mag, b: Mag) -> Mag: match a b: case Mag{+am, +ae} Mag{+bm, +be}: +e = s_max(ae, be) mag_w(W.add(ceil_shr(am, (e - ae : U32)), ceil_shr(bm, (e - be : U32))), e) # a + b, rounded up def mag_add(+a: Mag, +b: Mag) -> Mag: Bool.pick(Mag, mag_is_zero(a), b, Bool.pick(Mag, mag_is_zero(b), a, mag_add_go(a, b))) # a * b, rounded up def mag_mul(a: Mag, b: Mag) -> Mag: match a b: case Mag{am, +ae} Mag{bm, be}: mag_w(W.mul32(am, bm), (ae + be : U32)) def mag_shift(a: Mag, +k: U32) -> Mag: match a: case Mag{m, e}: Mag{m, (e + k : U32)} def mag_small(+n: U32) -> Mag: mag_w(W.w(n), 0) def mag_top(long: Bool, +xs: List<&2, U32>, +e: U32, +n: U32, +up: Bool) -> Mag: match long: case True{}: Mag{(B.head0(B.shr(xs, (n - 31 : U32))) + Bool.to_u32(up) : U32), (e + n - 31 : U32)} case False{}: mag_w(W.w(B.head0(xs)), e) # xs * 2^e rounded up (an upper bound), or down def mag_big(+xs: List<&2, U32>, +e: U32) -> Mag: +n = B.bitlen(xs) mag_top(U32.is_lt(31, n), xs, e, n, True{}) def mag_big_low(+xs: List<&2, U32>, +e: U32) -> Mag: +n = B.bitlen(xs) mag_top(U32.is_lt(31, n), xs, e, n, False{}) # a < b (exactly) def mag_lt(a: Mag, b: Mag) -> Bool: match a b: case Mag{+am, +ae} Mag{+bm, +be}: Bool.pick(Bool, U32.is_eq(bm, 0), False{}, Bool.pick(Bool, U32.is_eq(am, 0), True{}, Bool.pick(Bool, U32.is_eq(ae, be), U32.is_lt(am, bm), Bool.pick(Bool, s_lt(ae, be), Bool.or(s_lt((ae + 1 : U32), be), W.lt(W.w(am), W.shl(W.w(bm), 1))), Bool.and(Bool.not(s_lt((be + 1 : U32), ae)), W.lt(W.shl(W.w(am), 1), W.w(bm))))))) # a - b rounded down, 0 when b >= a (b not 0) def mag_sub_go(a: Mag, b: Mag) -> Mag: match a b: case Mag{+am, +ae} Mag{+bm, +be}: +e = (s_max(ae, be) - 31 : U32) +x = Bool.pick(W.W64, s_lt(ae, e), W.w(0), W.shl(W.w(am), (ae - e : U32))) +y = Bool.pick(W.W64, s_lt(be, e), ceil_shr(bm, (e - be : U32)), W.shl(W.w(bm), (be - e : U32))) Bool.pick(Mag, W.lt(y, x), mag_w_low(W.sub(x, y), e), mzero()) # a - b rounded down, 0 when b >= a def mag_sub_low(+a: Mag, +b: Mag) -> Mag: Bool.pick(Mag, mag_is_zero(b), a, mag_sub_go(a, b)) # a / b rounded up, b > 0 (b's top 16 bits divide, so the result can only be too large) def mag_div_up(a: Mag, b: Mag) -> Mag: match a b: case Mag{+am, +ae} Mag{bm, +be}: +b15 = U32.shrn(bm, 15n) mag_w(W.w(U32.div((am + b15 - 1 : U32), b15)), (ae - be - 15 : U32)) # ---- balls ---- type Ball is Data: Ball{neg: Bool, m: List<&2, U32>, e: U32, r: Mag} def exact(+neg: Bool, +m: List<&2, U32>, +e: U32) -> Ball: Ball{neg, m, e, mzero()} def of_u32(+n: U32) -> Ball: Ball{False{}, B.of_u32(n), 0, mzero()} def one() -> Ball: of_u32(1) # a finite float64, exactly def of_cls(c: F.Cls) -> Ball: match c: case F.FFin{s, +e, m}: Ball{U32.is_ne(s, 0), B.of_w64(m), (e - 1075 : U32), mzero()} case F.FZero{s}: Ball{U32.is_ne(s, 0), [], 0, mzero()} case _: Ball{False{}, [], 0, Mag{1073741824, 100000}} def of_f64(x: F.F64) -> Ball: of_cls(F.cls(x)) # a ball no answer can come from: its error covers everything def bad() -> Ball: Ball{False{}, [], 0, Mag{1073741824, 4000}} def negate(b: Ball) -> Ball: match b: case Ball{n, m, e, r}: Ball{Bool.not(n), m, e, r} def with_neg(+n: Bool, b: Ball) -> Ball: match b: case Ball{x, m, e, r}: Ball{n, m, e, r} def abs(b: Ball) -> Ball: with_neg(False{}, b) def neg_of(b: Ball) -> Bool: match b: case Ball{n, m, e, r}: n # the exponent just above the midpoint's top bit (signed; very low for 0) def top(b: Ball) -> U32: match b: case Ball{n, +m, e, r}: Bool.pick(U32, B.is_zero(m), 2147483648, (e + B.bitlen(m) : U32)) # an upper bound on |true value| def mag_of(b: Ball) -> Mag: match b: case Ball{n, +m, +e, +r}: mag_add(mag_big(m, e), r) def widen(b: Ball, +x: Mag) -> Ball: match b: case Ball{n, m, e, +r}: Ball{n, m, e, mag_add(r, x)} def mul_pow2(b: Ball, +k: U32) -> Ball: match b: case Ball{n, m, e, +r}: Ball{n, m, (e + k : U32), Bool.pick(Mag, mag_is_zero(r), r, mag_shift(r, k))} def trunc_go(long: Bool, +n: Bool, +m: List<&2, U32>, +e: U32, +r: Mag, +s: U32) -> Ball: match long: case True{}: Ball{n, B.shr(m, s), (e + s : U32), mag_add(r, pow2((e + s : U32)))} case False{}: Ball{n, m, e, r} # at most p bits of midpoint def trunc(b: Ball, +p: U32) -> Ball: match b: case Ball{+n, +m, +e, +r}: +k = B.bitlen(m) trunc_go(U32.is_lt(p, k), n, m, e, r, (k - p : U32)) # ---- addition ---- type Sh is Data: Sh{m: List<&2, U32>, lost: Bool} def shift_out(all: Bool, +m: List<&2, U32>, +d: U32) -> Sh: match all: case True{}: Sh{[], Bool.not(B.is_zero(m))} case False{}: Sh{B.shr(m, d), True{}} def shift_dir(down: Bool, +m: List<&2, U32>, +d: U32) -> Sh: match down: case True{}: shift_out(U32.is_le(B.bitlen(m), d), m, d) case False{}: Sh{B.shl(m, (0 - d : U32)), False{}} # m * 2^e as a multiple of 2^t, truncated def shift_to(+m: List<&2, U32>, +e: U32, +t: U32) -> Sh: shift_dir(s_lt(e, t), m, (t - e : U32)) def lost_mag(s: Sh, +t: U32) -> Mag: match s: case Sh{m, l}: Bool.pick(Mag, l, pow2(t), mzero()) def sh_m(s: Sh) -> List<&2, U32>: match s: case Sh{m, l}: m def signed_sum(same: Bool, +nx: Bool, +ny: Bool, +x: List<&2, U32>, +y: List<&2, U32>, +t: U32, +r: Mag) -> Ball: match same: case True{}: Ball{nx, B.add(x, y), t, r} case False{}: Bool.pick(Ball, B.le(y, x), Ball{nx, B.sub(x, y), t, r}, Ball{ny, B.sub(y, x), t, r}) def add_go(x: Ball, y: Ball, +p: U32) -> Ball: match x y: case Ball{+nx, +mx, +ex, rx} Ball{+ny, +my, +ey, ry}: +t = s_max(s_min(ex, ey), (s_max((ex + B.bitlen(mx) : U32), (ey + B.bitlen(my) : U32)) - p - 4 : U32)) +sx = shift_to(mx, ex, t) +sy = shift_to(my, ey, t) trunc(signed_sum(Bool.not(Bool.xor(nx, ny)), nx, ny, sh_m(sx), sh_m(sy), t, mag_add(mag_add(rx, ry), mag_add(lost_mag(sx, t), lost_mag(sy, t)))), p) def zero_mid(b: Ball) -> Bool: match b: case Ball{n, m, e, r}: B.is_zero(m) def err_of(b: Ball) -> Mag: match b: case Ball{n, m, e, r}: r def add_z(zx: Bool, zy: Bool, +x: Ball, +y: Ball, +p: U32) -> Ball: match zx zy: case True{} _: trunc(widen(y, err_of(x)), p) case False{} True{}: trunc(widen(x, err_of(y)), p) case False{} False{}: add_go(x, y, p) # x + y to p bits def add(+x: Ball, +y: Ball, +p: U32) -> Ball: add_z(zero_mid(x), zero_mid(y), x, y, p) # x - y to p bits def sub(+x: Ball, y: Ball, +p: U32) -> Ball: add(x, negate(y), p) # ---- multiplication and division ---- # x * y to p bits def mul(x: Ball, y: Ball, +p: U32) -> Ball: match x y: case Ball{nx, +mx, +ex, +rx} Ball{ny, +my, +ey, +ry}: +ax = mag_big(mx, ex) +ay = mag_big(my, ey) trunc(Ball{Bool.xor(nx, ny), B.mul(mx, my), (ex + ey : U32), mag_add(mag_add(mag_mul(ax, ry), mag_mul(ay, rx)), mag_mul(rx, ry))}, p) # x * n, n a word def mul_u(x: Ball, +n: U32) -> Ball: match x: case Ball{nx, m, e, r}: Ball{nx, B.mul_small(m, n), e, mag_mul(r, mag_small(n))} def ds_q(d: B.DS) -> List<&2, U32>: match d: case B.DS{q, r}: q def qr_q(d: B.QR) -> List<&2, U32>: match d: case B.QR{q, r}: q def lshift(pos: Bool, +m: List<&2, U32>, +s: U32) -> List<&2, U32>: match pos: case True{}: B.shl(m, s) case False{}: m # x / n to p bits, 0 < n < 2^16 def div_u(x: Ball, +n: U32, +p: U32) -> Ball: match x: case Ball{nx, +m, +e, r}: +s = (p + 20 - B.bitlen(m) : U32) +s2 = Bool.pick(U32, is_neg(s), 0, s) trunc(Ball{nx, ds_q(B.div_small(lshift(Bool.not(is_neg(s)), m, s), n)), (e - s2 : U32), mag_add(mag_div_up(r, mag_small(n)), pow2((e - s2 : U32)))}, p) def round_top_m(+rm: F.Rnd, +neg: Bool, t: B.Top, +e: U32) -> F.F64: match t: case B.Top{sig, k}: F.round_pack_m(rm, Bool.pick(U32, neg, 2147483648, 0), (1084 + k + e : U32), sig) def round_top(+neg: Bool, t: B.Top, +e: U32) -> F.F64: round_top_m(F.RNearEven{}, neg, t, e) def mid(b: Ball) -> Ball: match b: case Ball{n, m, e, r}: Ball{n, m, e, mzero()} def ball_m(b: Ball) -> List<&2, U32>: match b: case Ball{n, m, e, r}: m def bits_of(b: Ball) -> U32: match b: case Ball{n, m, e, r}: B.bitlen(m) def exp_of(b: Ball) -> U32: match b: case Ball{n, m, e, r}: e def newton_pick(done: Bool, +r: Ball, more: Unit -> Ball) -> Ball: match done: case True{}: r case False{}: more(Unit{}) # Newton steps for 1/y: r + r (1 - y r), the precision doubling from 50 bits to past p def recip_go(n: Nat, +y: Ball, +r: Ball, +q: U32, +p: U32) -> Ball: match n: case 0n: r case 1n+k: +w = (q + q + 16 : U32) newton_pick(U32.is_lt(p, q), r, u => recip_go(k, y, mid(add(r, mul(r, sub(one(), mul(y, r, w), w), w), w)), (q + q : U32), p)) # an approximation of 1/y to about p bits (no error bound: the residual bounds it), y nonzero def recip(+y: Ball, +p: U32) -> Ball: +yv = abs(mid(y)) +l = (bits_of(yv) + exp_of(yv) : U32) +f = F.div(F.F64{1072693248, 0}, round_top(False{}, B.top64(ball_m(yv)), (exp_of(yv) - l : U32))) recip_go(8n, yv, mul_pow2(of_cls(F.cls(f)), (0 - l : U32)), 50, p) def div_ok(ok: Bool, +x: Ball, +y: Ball, +low: Mag, +p: U32) -> Ball: match ok: case False{}: bad() case True{}: +r = recip(y, (p + 8 : U32)) +res = sub(one(), mul(abs(y), r, (p + p + 40 : U32)), (p + p + 40 : U32)) with_neg(Bool.xor(neg_of(x), neg_of(y)), widen(mul(abs(x), r, (p + 4 : U32)), mag_div_up(mag_mul(mag_of(x), mag_of(res)), low))) # x / y to p bits (a bad ball when y's ball reaches 0): x times an approximate reciprocal R of y, widened by # |x| |1 - y R| / |y|, the exact difference def div(+x: Ball, y: Ball, +p: U32) -> Ball: match y: case Ball{ny, +my, +ey, +ry}: +low = mag_sub_low(mag_big_low(my, ey), ry) div_ok(Bool.and(Bool.not(B.is_zero(my)), Bool.not(mag_is_zero(low))), x, Ball{ny, my, ey, ry}, low, p) def halve(+v: U32) -> U32: Bool.pick(U32, is_neg(v), (0 - U32.div((0 - v : U32), 2) : U32), U32.div(v, 2)) # Newton steps for 1/sqrt(x): r + r (1 - x r^2) / 2 def rsqrt_go(n: Nat, +x: Ball, +r: Ball, +q: U32, +p: U32) -> Ball: match n: case 0n: r case 1n+k: +w = (q + q + 16 : U32) newton_pick(U32.is_lt(p, q), r, u => rsqrt_go(k, x, mid(add(r, mul_pow2(mul(r, sub(one(), mul(x, mul(r, r, w), w), w), w), (0 - 1 : U32)), w)), (q + q : U32), p)) def sqrt_ok(ok: Bool, +x: Ball, +p: U32) -> Ball: match ok: case False{}: bad() case True{}: +xv = mid(x) +u = (bits_of(xv) + exp_of(xv) : U32) +v = (u + U32.and(u, 1) : U32) +f = F.div(F.F64{1072693248, 0}, F.sqrt(round_top(False{}, B.top64(ball_m(xv)), (exp_of(xv) - v : U32)))) +r = rsqrt_go(8n, xv, mul_pow2(of_cls(F.cls(f)), (0 - halve(v) : U32)), 50, (p + 8 : U32)) +s = mid(mul(xv, r, (p + 4 : U32))) Ball{False{}, ball_m(s), exp_of(s), mag_div_up(mag_of(sub(x, mul(s, s, (p + p + 40 : U32)), (p + p + 40 : U32))), mag_big_low(ball_m(s), exp_of(s)))} # sqrt(x) to p bits, x's ball positive and its error under a quarter of it (else a bad ball): s = x R for an # approximate 1/sqrt(x) R, widened by |x - s^2| / s (sqrt(x) - s = (x - s^2) / (sqrt(x) + s)) def sqrt(x: Ball, +p: U32) -> Ball: match x: case Ball{+n, +m, +e, +r}: sqrt_ok(Bool.and(Bool.and(Bool.not(n), Bool.not(B.is_zero(m))), mag_lt(mag_shift(r, 2), mag_big_low(m, e))), Ball{n, m, e, r}, p) # a constant floor(c * 2^bits), as a ball of p bits def constant(+c: List<&2, U32>, +bits: U32, +p: U32) -> Ball: trunc(Ball{False{}, c, (0 - bits : U32), pow2((0 - bits : U32))}, p) # ---- deciding the float64 ---- def round_big_m(+rm: F.Rnd, +neg: Bool, +xs: List<&2, U32>, +e: U32) -> F.F64: Bool.pick(F.F64, B.is_zero(xs), F.zero(Bool.pick(U32, neg, 2147483648, 0)), round_top_m(rm, neg, B.top64(xs), e)) def round_big(+neg: Bool, +xs: List<&2, U32>, +e: U32) -> F.F64: round_big_m(F.RNearEven{}, neg, xs, 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_lohi(+rm: F.Rnd, below: Bool, +neg: Bool, +m: List<&2, U32>, +rr: List<&2, U32>, +e: U32) -> Maybe<&2, F.F64>: match below: case True{}: None{} case False{}: +lo = round_big_m(rm, neg, B.sub(m, rr), e) Bool.pick(Maybe<&2, F.F64>, same(lo, round_big_m(rm, neg, B.add(m, rr), e)), Some{lo}, None{}) def err_dir(below: Bool, +rm: U32, +re: U32, +e: U32) -> List<&2, U32>: match below: case True{}: B.of_w64(ceil_shr(rm, (e - re : U32))) case False{}: B.shl(B.of_u32(rm), (re - e : U32)) # r in units of 2^e, rounded up def err_units(r: Mag, +e: U32) -> List<&2, U32>: match r: case Mag{+rm, +re}: err_dir(s_lt(re, e), rm, re, e) # the float64 every point of b rounds to in mode rm, if they all round to the same one def decide_m(+rm: F.Rnd, b: Ball) -> Maybe<&2, F.F64>: match b: case Ball{+n, +m, +e, r}: +rr = err_units(r, e) decide_lohi(rm, B.lt(m, rr), n, m, rr, e) # the float64 every point of b rounds to (to nearest), if they all round to the same one def decide(b: Ball) -> Maybe<&2, F.F64>: decide_m(F.RNearEven{}, b)