import Base import ../../../../spec/lib/numeric.bend as NM # All bits are represented explicitly: no Nat holds a full-width integer. def zero() -> Word(64n): Word.zero(64n) def inc(w: Word(64n)) -> Word(64n): Word.inc(64n, w) def add(a: Word(64n), b: Word(64n)) -> Word(64n): Word.add(64n, a, b) def neg(w: Word(64n)) -> Word(64n): Word.inc(64n, Word.not(64n, w)) def last(n: Nat, w: Word(n)) -> Bool: match n: case 0n: False{} case 1n+0n: match w: case WCon{b, tail}: b case 1n+1n+p: match w: case WCon{b, tail}: last(1n+p, tail) def is_zero(n: Nat, w: Word(n)) -> Bool: match n: case 0n: True{} case 1n+p: match w: case WCon{b, tail}: Bool.not(b) && is_zero(p, tail) def signed_order(a: Word(64n), b: Word(64n), sa: Bool, sb: Bool) -> Bool: match sa sb: case True{} False{}: True{} case False{} True{}: False{} case x y: Cmp.is_le(Word.cmp(64n, a, b)) def le(+a: Word(64n), +b: Word(64n)) -> Bool: signed_order(a, b, last(64n, a), last(64n, b)) def bit_u32(b: Bool) -> U32: match b: case False{}: 0 case True{}: 1 type Division<-n: Nat> is Data: Div{quotient: Word(n), remainder: U32} def digit_result(p: Nat, q: Word(p), r: U32, subtract: Bool) -> Division<1n+p>: match subtract: case False{}: Div{WCon{False{}, q}, r} case True{}: Div{WCon{True{}, q}, U32.sub(r, 1000000)} def digit_compare(p: Nat, q: Word(p), +r: U32) -> Division<1n+p>: digit_result(p, q, r, U32.is_ge(r, 1000000)) def digit(p: Nat, b: Bool, prior: Division
) -> Division<1n+p>:
Div{q, r} = prior
digit_compare(p, q, U32.add(U32.mul(r, 2), bit_u32(b)))
# Binary long division, processing the most significant bit first. The partial
# remainder stays below 1,000,000; the next candidate fits in U32 (<2,000,000).
def div_million(+n: Nat, w: Word(n)) -> Division