# bend-float64's unbounded natural numbers, for exact decimal conversion: a list of U32 limbs, least significant first, # with no zero limb at the top (0 is the empty list). Add, subtract, compare, multiply by a word or a power of ten, # shift by bits, bit length, and the top 64 bits with a sticky bit for the rest. Pure. import Base import ./wide.bend as W # ---- normal form ---- def cons_trim(+x: U32, t: List<&2, U32>) -> List<&2, U32>: match t: case []: Bool.pick(List<&2, U32>, U32.is_eq(x, 0), [], [x]) case h <> r: x <> h <> r # drop zero limbs at the top def trim(xs: List<&2, U32>) -> List<&2, U32>: match xs: case []: [] case x <> t: cons_trim(x, trim(t)) def of_u32(+x: U32) -> List<&2, U32>: cons_trim(x, []) def of_w64(x: W.W64) -> List<&2, U32>: match x: case W.W64{h, +l}: trim([l, h]) def is_zero(xs: List<&2, U32>) -> Bool: match xs: case []: True{} case x <> t: Bool.and(U32.is_eq(x, 0), is_zero(t)) # ---- addition, subtraction, comparison ---- def add_go(xs: List<&2, U32>, ys: List<&2, U32>, +c: U32) -> List<&2, U32>: match xs ys: case Nil{} Nil{}: Bool.pick(List<&2, U32>, U32.is_eq(c, 0), [], [c]) case +x <> xt Nil{}: +s = (x + c : U32) s <> add_go(xt, [], Bool.to_u32(U32.is_lt(s, x))) case Nil{} +y <> yt: +s = (y + c : U32) s <> add_go([], yt, Bool.to_u32(U32.is_lt(s, y))) case +x <> xt y <> yt: +s1 = (x + y : U32) +s = (s1 + c : U32) s <> add_go(xt, yt, (Bool.to_u32(U32.is_lt(s1, x)) + Bool.to_u32(U32.is_lt(s, s1)) : U32)) def add(xs: List<&2, U32>, ys: List<&2, U32>) -> List<&2, U32>: add_go(xs, ys, 0) def sub_go(xs: List<&2, U32>, ys: List<&2, U32>, +b: U32) -> List<&2, U32>: match xs ys: case Nil{} _: [] case +x <> xt Nil{}: (x - b : U32) <> sub_go(xt, [], Bool.to_u32(U32.is_lt(x, b))) case +x <> xt +y <> yt: +d1 = (x - y : U32) (d1 - b : U32) <> sub_go(xt, yt, (Bool.to_u32(U32.is_lt(x, y)) + Bool.to_u32(U32.is_lt(d1, b)) : U32)) # xs - ys, for xs >= ys def sub(xs: List<&2, U32>, ys: List<&2, U32>) -> List<&2, U32>: trim(sub_go(xs, ys, 0)) def cmp_top(rest: Cmp, +x: U32, +y: U32) -> Cmp: match rest: case EQ{}: U32.cmp(x, y) case LT{}: LT{} case GT{}: GT{} # compares limb by limb from the top (a missing limb counts as 0) def cmp(xs: List<&2, U32>, ys: List<&2, U32>) -> Cmp: match xs ys: case Nil{} Nil{}: EQ{} case x <> xt Nil{}: cmp_top(cmp(xt, []), x, 0) case Nil{} y <> yt: cmp_top(cmp([], yt), 0, y) case x <> xt y <> yt: cmp_top(cmp(xt, yt), x, y) def lt(xs: List<&2, U32>, ys: List<&2, U32>) -> Bool: Cmp.is_lt(cmp(xs, ys)) def le(xs: List<&2, U32>, ys: List<&2, U32>) -> Bool: Cmp.is_le(cmp(xs, ys)) # ---- multiplication ---- def mul_small_go(xs: List<&2, U32>, +m: U32, +c: U32) -> List<&2, U32>: match xs: case []: Bool.pick(List<&2, U32>, U32.is_eq(c, 0), [], [c]) case x <> t: +p = W.mul32(x, m) +s = (W.low(p) + c : U32) s <> mul_small_go(t, m, (W.high(p) + Bool.to_u32(U32.is_lt(s, W.low(p))) : U32)) # xs * m def mul_small(xs: List<&2, U32>, +m: U32) -> List<&2, U32>: trim(mul_small_go(xs, m, 0)) def mul_pow10_go(n: Nat, +xs: List<&2, U32>) -> List<&2, U32>: match n: case 0n: xs case 1n+k: mul_pow10_go(k, mul_small(xs, 1000000000)) def pow10_small(+r: U32) -> U32: Bool.pick(U32, U32.is_lt(r, 5), Bool.pick(U32, U32.is_lt(r, 2), Bool.pick(U32, U32.is_eq(r, 0), 1, 10), Bool.pick(U32, U32.is_eq(r, 2), 100, Bool.pick(U32, U32.is_eq(r, 3), 1000, 10000))), Bool.pick(U32, U32.is_lt(r, 7), Bool.pick(U32, U32.is_eq(r, 5), 100000, 1000000), Bool.pick(U32, U32.is_eq(r, 7), 10000000, 100000000))) # xs * 10^n def mul_pow10(xs: List<&2, U32>, +n: U32) -> List<&2, U32>: mul_small(mul_pow10_go(U32.to_nat(U32.div(n, 9)), xs), pow10_small(U32.mod(n, 9))) def pow10(+n: U32) -> List<&2, U32>: mul_pow10([1], n) # ---- shifts ---- def zeros(n: Nat, xs: List<&2, U32>) -> List<&2, U32>: match n: case 0n: xs case 1n+k: 0 <> zeros(k, xs) def shl_bits(xs: List<&2, U32>, +k: U32, +c: U32) -> List<&2, U32>: match xs: case []: Bool.pick(List<&2, U32>, U32.is_eq(c, 0), [], [c]) case +x <> t: U32.or(W.shl32(x, k), c) <> shl_bits(t, k, Bool.pick(U32, U32.is_eq(k, 0), 0, W.shr32(x, (32 - k : U32)))) # xs * 2^n def shl(xs: List<&2, U32>, +n: U32) -> List<&2, U32>: trim(zeros(U32.to_nat(U32.div(n, 32)), shl_bits(xs, U32.mod(n, 32), 0))) def drop(n: Nat, xs: List<&2, U32>) -> List<&2, U32>: match n xs: case 0n ys: ys case 1n+k Nil{}: [] case 1n+k y <> t: drop(k, t) def take(n: Nat, xs: List<&2, U32>) -> List<&2, U32>: match n xs: case 0n ys: [] case 1n+k Nil{}: [] case 1n+k y <> t: y <> take(k, t) def head0(xs: List<&2, U32>) -> U32: match xs: case []: 0 case x <> t: x def shr_bits(xs: List<&2, U32>, +k: U32) -> List<&2, U32>: match xs: case []: [] case x <> +t: U32.or(W.shr32(x, k), Bool.pick(U32, U32.is_eq(k, 0), 0, W.shl32(head0(t), (32 - k : U32)))) <> shr_bits(t, k) # xs / 2^n rounded down def shr(xs: List<&2, U32>, +n: U32) -> List<&2, U32>: trim(shr_bits(drop(U32.to_nat(U32.div(n, 32)), xs), U32.mod(n, 32))) # whether any of the n lowest bits of xs is set def low_nonzero(+xs: List<&2, U32>, +n: U32) -> Bool: Bool.or(Bool.not(is_zero(take(U32.to_nat(U32.div(n, 32)), xs))), U32.is_ne(U32.and(head0(drop(U32.to_nat(U32.div(n, 32)), xs)), (W.shl32(1, U32.mod(n, 32)) - 1 : U32)), 0)) # ---- size ---- def bitlen_go(xs: List<&2, U32>, +acc: U32) -> U32: match xs: case []: 0 case x <> Nil{}: (acc + 32 - W.clz32(x) : U32) case x <> t: bitlen_go(t, (acc + 32 : U32)) # the number of bits, 0 for 0 def bitlen(xs: List<&2, U32>) -> U32: bitlen_go(xs, 0) def w64_of(xs: List<&2, U32>) -> W.W64: match xs: case []: W.w(0) case l <> t: W.W64{head0(t), l} # the top 64 bits of a nonzero xs as a W64 whose lowest bit also records whether any bit below them was set, and how # far they were shifted down (0 when xs has 64 bits or fewer) type Top is Data: Top{sig: W.W64, shift: U32} def top64_at(+xs: List<&2, U32>, +k: U32) -> Top: Top{W.jam_or(w64_of(shr(xs, k)), low_nonzero(xs, k)), k} def top64_go(long: Bool, +xs: List<&2, U32>, +n: U32) -> Top: match long: case True{}: top64_at(xs, (n - 64 : U32)) case False{}: Top{w64_of(xs), 0} def top64(+xs: List<&2, U32>) -> Top: +n = bitlen(xs) top64_go(U32.is_lt(64, n), xs, n) # xs * 10, as xs * 8 + xs * 2, by doubling (additions only) def mul10(+xs: List<&2, U32>) -> List<&2, U32>: +x2 = add(xs, xs) +x4 = add(x2, x2) add(add(x4, x4), x2) # ---- products, quotients, roots ---- def mul_go(+xs: List<&2, U32>, ys: List<&2, U32>) -> List<&2, U32>: match ys: case []: [] case y <> t: add(mul_small(xs, y), shl(mul_go(xs, t), 32)) # xs * ys def mul(+xs: List<&2, U32>, ys: List<&2, U32>) -> List<&2, U32>: trim(mul_go(xs, ys)) type QR is Data: QR{q: List<&2, U32>, r: List<&2, U32>} # n steps of binary long division, quotient bit i first def divq_go(n: Nat, +i: U32, +num: List<&2, U32>, +den: List<&2, U32>, +q: List<&2, U32>) -> QR: match n: case 0n: QR{q, num} case 1n+k: +t = shl(den, i) +ge = le(t, num) divq_go(k, (i - 1 : U32), Bool.pick(List<&2, U32>, ge, sub(num, t), num), den, Bool.pick(List<&2, U32>, ge, add(q, shl([1], i)), q)) def divq_n(big: Bool, +num: List<&2, U32>, +den: List<&2, U32>, +n: U32) -> QR: match big: case True{}: divq_go(U32.to_nat(n), (n - 1 : U32), num, den, []) case False{}: QR{[], num} # num / den and num mod den, den nonzero def divq(+num: List<&2, U32>, +den: List<&2, U32>) -> QR: +n = (bitlen(num) - bitlen(den) + 1 : U32) divq_n(Bool.and(U32.is_lt(0, n), U32.is_lt(n, 2147483648)), num, den, n) type DS is Data: DS{q: List<&2, U32>, r: U32} def div_small_go(xs: List<&2, U32>, +d: U32, +r: U32, +acc: List<&2, U32>) -> DS: match xs: case []: DS{acc, r} case +x <> t: +h = U32.or(U32.shln(r, 16n), U32.shrn(x, 16n)) +r1 = U32.mod(h, d) +l = U32.or(U32.shln(r1, 16n), U32.and(x, 65535)) div_small_go(t, d, U32.mod(l, d), U32.or(U32.shln(U32.div(h, d), 16n), U32.div(l, d)) <> acc) def div_small_q(x: DS) -> DS: match x: case DS{q, r}: DS{trim(q), r} # xs / d and xs mod d, for 0 < d < 2^16 def div_small(xs: List<&2, U32>, +d: U32) -> DS: div_small_q(div_small_go(List.reverse(&2, U32, xs), d, 0, [])) # n steps of the bit-by-bit square root: root bit i first def isqrt_go(n: Nat, +i: U32, +x: List<&2, U32>, +q: List<&2, U32>) -> QR: match n: case 0n: QR{q, x} case 1n+k: +t = add(shl(q, (i + 1 : U32)), shl([1], (i + i : U32))) +ge = le(t, x) isqrt_go(k, (i - 1 : U32), Bool.pick(List<&2, U32>, ge, sub(x, t), x), Bool.pick(List<&2, U32>, ge, add(q, shl([1], i)), q)) # floor(sqrt(x)) and x minus its square def isqrt(+x: List<&2, U32>) -> QR: +n = U32.div((bitlen(x) + 1 : U32), 2) isqrt_go(U32.to_nat(n), (n - 1 : U32), x, []) def cube(+x: List<&2, U32>) -> List<&2, U32>: mul(mul(x, x), x) # n steps of the bit-by-bit cube root: root bit i first def icbrt_go(n: Nat, +i: U32, +x: List<&2, U32>, +q: List<&2, U32>) -> List<&2, U32>: match n: case 0n: q case 1n+k: +c = add(q, shl([1], i)) icbrt_go(k, (i - 1 : U32), x, Bool.pick(List<&2, U32>, le(cube(c), x), c, q)) # floor(cbrt(x)) def icbrt(+x: List<&2, U32>) -> List<&2, U32>: +n = U32.div((bitlen(x) + 2 : U32), 3) icbrt_go(U32.to_nat(n), (n - 1 : U32), x, []) def pow_go(n: Nat, +x: List<&2, U32>, +acc: List<&2, U32>) -> List<&2, U32>: match n: case 0n: acc case 1n+k: pow_go(k, x, mul(acc, x)) # x^n def pow(+x: List<&2, U32>, +n: U32) -> List<&2, U32>: pow_go(U32.to_nat(n), x, [1]) # xs / 2^k rounded down, for 0 < k < 32 (no division of k: cheap for the checker) def shr_small(xs: List<&2, U32>, +k: U32) -> List<&2, U32>: trim(shr_bits(xs, k)) def isq_step(ge: Bool, +x: List<&2, U32>, +res: List<&2, U32>, +bit: List<&2, U32>) -> QR: match ge: case True{}: QR{add(shr_small(res, 1), bit), sub(x, add(res, bit))} case False{}: QR{shr_small(res, 1), x} # the shift-by-two square root: res and x as QR, bit falling by 4 each step def isq_go(n: Nat, +bit: List<&2, U32>, p: QR) -> QR: match n p: case 0n q: q case 1n+k QR{+res, +x}: isq_go(k, shr_small(bit, 2), isq_step(le(add(res, bit), x), x, res, bit)) # floor(sqrt(x)) and x minus its square, shifting only by constants def isqrt2(+x: List<&2, U32>) -> QR: +n = U32.div((bitlen(x) + 1 : U32), 2) isq_go(U32.to_nat(n), shl([1], ((n + n - 2) : U32)), QR{[], x})