~/bend-docscommunity

big.bend source

big.bend on the hub · documented module

# 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 Baseimport ./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 topdef 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 >= ysdef 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 * mdef 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^ndef 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^ndef 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:      xdef 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 downdef 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 setdef 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 0def 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)