~/bend-docscommunity

w64.bend source

w64.bend on the hub · documented module

# Unsigned 64-bit numbers as two U32 halves, for file offsets and byte counts past 4 GiB. Bend's Nat is unary at run# time, and Base has no 64-bit integer, so offsets are W64{hi, lo} = hi * 2^32 + lo. Overflow is checked: an operation# that would pass 2^64 - 1 answers None.import Basetype W64 is Data:  W64{hi: U32, lo: U32}def W64.zero() -> W64:  W64{0, 0}def W64.of(+x: U32) -> W64:  W64{0, x}def W64.hi(a: W64) -> U32:  match a:    case W64{h, l}:      hdef W64.lo(a: W64) -> U32:  match a:    case W64{h, l}:      ldef W64.is_eq(a: W64, b: W64) -> Bool:  match a b:    case W64{+ah, +al} W64{+bh, +bl}:      Bool.and(U32.is_eq(ah, bh), U32.is_eq(al, bl))def W64.is_lt(a: W64, b: W64) -> Bool:  match a b:    case W64{+ah, +al} W64{+bh, +bl}:      Bool.or(U32.is_lt(ah, bh), Bool.and(U32.is_eq(ah, bh), U32.is_lt(al, bl)))def W64.is_le(a: W64, b: W64) -> Bool:  match a b:    case W64{+ah, +al} W64{+bh, +bl}:      Bool.or(U32.is_lt(ah, bh), Bool.and(U32.is_eq(ah, bh), U32.is_le(al, bl)))def W64.carry(+x: U32, +s: U32) -> U32:  Bool.pick(U32, U32.is_lt(s, x), 1, 0)def W64.ok(fits: Bool, r: W64) -> Maybe<&2, W64>:  match fits:    case True{}:      Some{r}    case False{}:      None{}def W64.add_hi(+ah: U32, +bh: U32, +c: U32, +lo: U32) -> Maybe<&2, W64>:  +h1 = (ah + bh : U32)  +h2 = (h1 + c : U32)  W64.ok(Bool.and(U32.is_le(ah, h1), U32.is_le(h1, h2)), W64{h2, lo})# a + b, or None past 2^64 - 1def W64.add(a: W64, b: W64) -> Maybe<&2, W64>:  match a b:    case W64{+ah, +al} W64{+bh, +bl}:      +lo = (al + bl : U32)      W64.add_hi(ah, bh, W64.carry(al, lo), lo)# a - b, for b <= a (wraps otherwise)def W64.sub(a: W64, b: W64) -> W64:  match a b:    case W64{+ah, +al} W64{+bh, +bl}:      W64{((ah - bh : U32) - Bool.pick(U32, U32.is_lt(al, bl), 1, 0) : U32), (al - bl : U32)}def W64.mul32_fin(+p00: U32, +p11: U32, +mid: U32, +c1: U32) -> W64:  +lo = (p00 + (mid * 65536 : U32) : U32)  W64{(((p11 + (mid / 65536 : U32) : U32) + (c1 * 65536 : U32) : U32) + W64.carry(p00, lo) : U32), lo}# the full 64-bit product of two U32, from 16-bit halvesdef W64.mul32(+a: U32, +b: U32) -> W64:  +a0 = U32.and(a, 65535)  +a1 = (a / 65536 : U32)  +b0 = U32.and(b, 65535)  +b1 = (b / 65536 : U32)  +p01 = (a0 * b1 : U32)  +mid = (p01 + (a1 * b0 : U32) : U32)  W64.mul32_fin((a0 * b0 : U32), (a1 * b1 : U32), mid, W64.carry(p01, mid))def W64.mul_fin(top: W64, low: W64) -> Maybe<&2, W64>:  match top low:    case W64{+th, +tl} W64{+lh, +ll}:      +h = (tl + lh : U32)      W64.ok(Bool.and(U32.is_eq(th, 0), U32.is_le(lh, h)), W64{h, ll})# a * m, or None past 2^64 - 1def W64.mul(a: W64, +m: U32) -> Maybe<&2, W64>:  match a:    case W64{+ah, +al}:      W64.mul_fin(W64.mul32(ah, m), W64.mul32(al, m))def W64.then_add(r: Maybe<&2, W64>, +d: U32) -> Maybe<&2, W64>:  match r:    case None{}:      None{}    case Some{x}:      W64.add(x, W64.of(d))# a * 10 + d (a decimal digit), or None past 2^64 - 1def W64.digit(a: W64, +d: U32) -> Maybe<&2, W64>:  W64.then_add(W64.mul(a, 10), d)# ceil(a / 4), the 32-bit cells that hold a bytesdef W64.cells_fin(exact: Bool, +h: U32, +l: U32) -> W64:  match exact:    case True{}:      W64{h, l}    case False{}:      +l2 = (l + 1 : U32)      W64{(h + W64.carry(l, l2) : U32), l2}def W64.cells(a: W64) -> W64:  match a:    case W64{+h, +l}:      W64.cells_fin(U32.is_eq(U32.and(l, 3), 0), (h / 4 : U32), U32.or((U32.and(h, 3) * 1073741824 : U32), (l / 4 : U32)))def W64.hex(+x: U32) -> String:  Bool.pick(String, U32.is_eq(x, 0), "0", U32.hex(x))# as 0x<high>_<low>, in hexdef W64.show_go(+h: U32, +l: U32) -> String:  "0x" ++ W64.hex(h) ++ "_" ++ W64.hex(l)def W64.show(a: W64) -> String:  match a:    case W64{+h, +l}:      W64.show_go(h, l)