# bend-float64: IEEE 754 binary64 ("double", NumPy's float64) in pure Bend, as two U32 words and integer arithmetic. # Every operation is correctly rounded (to nearest, ties to even; the _m versions take any of IEEE 754's five rounding # modes) and gives the bits x86-64 gives, NaNs included: # a NaN operand passes through quieted (for two, the first; fma takes b, then a, then c), and an invalid operation # (inf - inf, 0 * inf, 0 / 0, inf / inf, the square root of a negative number) gives the default NaN # 0xFFF8000000000000. minimum and maximum follow NumPy's np.minimum and np.maximum; the conversions to integers # follow NumPy's astype on x86-64. import Base import ./wide.bend as W type F64 is Data: F64{hi: U32, lo: U32} # ---- bits ---- # the float64 with these words (hi holds the sign, the exponent and the top 20 bits of the fraction) def of_bits(+hi: U32, +lo: U32) -> F64: F64{hi, lo} def hi_bits(x: F64) -> U32: match x: case F64{h, l}: h def lo_bits(x: F64) -> U32: match x: case F64{h, l}: l def sign_of(+h: U32) -> U32: U32.and(h, 2147483648) def exp_of(+h: U32) -> U32: U32.and(U32.shrn(h, 20n), 2047) def frac_hi(+h: U32) -> U32: U32.and(h, 1048575) def zero(+s: U32) -> F64: F64{s, 0} def inf(+s: U32) -> F64: F64{U32.or(s, 2146435072), 0} # x86's default NaN, the answer to an invalid operation def dnan() -> F64: F64{4294443008, 0} # a NaN with its quiet bit set def quiet(x: F64) -> F64: match x: case F64{h, l}: F64{U32.or(h, 524288), l} def neg(x: F64) -> F64: match x: case F64{h, l}: F64{U32.xor(h, 2147483648), l} def abs(x: F64) -> F64: match x: case F64{h, l}: F64{U32.and(h, 2147483647), l} def nan_bits(+h: U32, +l: U32) -> Bool: Bool.or(U32.is_lt(2146435072, h), Bool.and(U32.is_eq(h, 2146435072), U32.is_ne(l, 0))) def is_nan(x: F64) -> Bool: match x: case F64{h, l}: nan_bits(U32.and(h, 2147483647), l) def is_inf(x: F64) -> Bool: match x: case F64{h, l}: Bool.and(U32.is_eq(U32.and(h, 2147483647), 2146435072), U32.is_eq(l, 0)) def is_finite(x: F64) -> Bool: match x: case F64{h, l}: U32.is_lt(U32.and(h, 2147483647), 2146435072) # the sign bit (True for -0.0 and negative NaNs too) def signbit(x: F64) -> Bool: match x: case F64{h, l}: U32.is_ne(sign_of(h), 0) def with_sign(+s: U32, x: F64) -> F64: match x: case F64{h, l}: F64{U32.or(s, U32.and(h, 2147483647)), l} # x's magnitude with y's sign bit def copysign(x: F64, y: F64) -> F64: match y: case F64{h, l}: with_sign(sign_of(h), x) # a < b for exponents held as signed 32-bit words def s_lt(+a: U32, +b: U32) -> Bool: U32.is_lt(U32.xor(a, 2147483648), U32.xor(b, 2147483648)) # ---- classes ---- # a finite nonzero x is m * 2^(e - 1075): m < 2^53, e >= 1 (a subnormal has e = 1 and m < 2^52) type Cls is Data: FNan{} FInf{s: U32} FZero{s: U32} FFin{s: U32, e: U32, m: W.W64} def cls_fin(+s: U32, +e: U32, +fh: U32, +lo: U32) -> Cls: Bool.pick(Cls, U32.is_eq(e, 0), FFin{s, 1, W.W64{fh, lo}}, FFin{s, e, W.W64{U32.or(fh, 1048576), lo}}) def cls_parts(+s: U32, +e: U32, +fh: U32, +lo: U32) -> Cls: +fz = U32.is_eq(U32.or(fh, lo), 0) Bool.pick(Cls, U32.is_eq(e, 2047), Bool.pick(Cls, fz, FInf{s}, FNan{}), Bool.pick(Cls, Bool.and(U32.is_eq(e, 0), fz), FZero{s}, cls_fin(s, e, fh, lo))) def cls(x: F64) -> Cls: match x: case F64{+h, +l}: cls_parts(sign_of(h), exp_of(h), frac_hi(h), l) # m * 2^(e - 1075) with m's top bit at bit 52 type NM is Data: NM{e: U32, m: W.W64} def norm(+e: U32, +m: W.W64) -> NM: +k = (W.clz(m) - 11 : U32) NM{(e - k : U32), W.shl(m, k)} # ---- rounding: s * S * 2^(e - 1084), e a signed word, S a nonzero 64-bit integer, to a float64 by a mode ---- # IEEE 754's five rounding modes: to nearest with ties to even (the default everywhere), to nearest with ties away # from zero, toward zero, toward +infinity, toward -infinity type Rnd is Data: RNearEven{} RNearAway{} RZero{} RUp{} RDown{} def of_w(x: W.W64) -> F64: match x: case W.W64{h, l}: F64{h, l} def rp_bits(+s: U32, +e: U32, m: W.W64) -> F64: of_w(W.add(W.W64{U32.or(s, U32.shln((e - 1 : U32), 20n)), 0}, m)) # whether to round the kept bits up, given the 9 bits below them (rb, the lowest one sticky), the sign, and whether # the kept bits are odd def rnd_up(m: Rnd, +s: U32, +rb: U32, +odd: Bool) -> Bool: match m: case RNearEven{}: Bool.or(U32.is_lt(256, rb), Bool.and(U32.is_eq(rb, 256), odd)) case RNearAway{}: U32.is_le(256, rb) case RZero{}: False{} case RUp{}: Bool.and(U32.is_ne(rb, 0), U32.is_eq(s, 0)) case RDown{}: Bool.and(U32.is_ne(rb, 0), U32.is_ne(s, 0)) # S's top bit at bit 61, e >= 1: keep 53 bits, round on the 9 below as the mode says, pack; a carry out of the # significand moves into the exponent, and past the largest exponent gives infinity def rp_round(+rm: Rnd, +s: U32, +e: U32, +sig: W.W64) -> F64: +q = W.shr(sig, 9) rp_bits(s, e, W.add(q, W.w(Bool.to_u32(rnd_up(rm, s, U32.and(W.low(sig), 511), U32.is_eq(U32.and(W.low(q), 1), 1)))))) # past the largest float64: infinity, or the largest finite number when the mode rounds toward zero on that side def overflow(m: Rnd, +s: U32) -> F64: match m: case RNearEven{}: inf(s) case RNearAway{}: inf(s) case RZero{}: F64{U32.or(s, 2146435071), 4294967295} case RUp{}: Bool.pick(F64, U32.is_eq(s, 0), inf(0), F64{4293918719, 4294967295}) case RDown{}: Bool.pick(F64, U32.is_eq(s, 0), F64{2146435071, 4294967295}, inf(2147483648)) # a nonzero value too small for any float64 (below half the smallest subnormal): zero, or the smallest subnormal # when the mode rounds away from zero on that side def underflow(m: Rnd, +s: U32) -> F64: match m: case RUp{}: Bool.pick(F64, U32.is_eq(s, 0), F64{0, 1}, zero(s)) case RDown{}: Bool.pick(F64, U32.is_eq(s, 0), zero(0), F64{2147483648, 1}) case _: zero(s) # the sign of an exact zero sum of opposite operands: -0 rounding down, +0 otherwise def zero_sum(m: Rnd) -> U32: match m: case RDown{}: 2147483648 case _: 0 def rp_range(+rm: Rnd, over: Bool, tiny: Bool, +s: U32, +e: U32, +sig: W.W64) -> F64: match over tiny: case True{} _: overflow(rm, s) case False{} True{}: rp_round(rm, s, 1, W.shr_jam(sig, (1 - e : U32))) case False{} False{}: rp_round(rm, s, e, sig) def rp_at(+rm: Rnd, +s: U32, +e: U32, +sig: W.W64) -> F64: rp_range(rm, Bool.and(U32.is_lt(e, 2147483648), U32.is_le(2047, e)), s_lt(e, 1), s, e, sig) def rp_norm(+rm: Rnd, small: Bool, +s: U32, +e: U32, +sig: W.W64, +n: U32) -> F64: match small: case True{}: rp_at(rm, s, (e + (2 - n) : U32), W.shr_jam(sig, (2 - n : U32))) case False{}: rp_at(rm, s, (e - (n - 2) : U32), W.shl(sig, (n - 2 : U32))) def round_pack_m(+rm: Rnd, +s: U32, +e: U32, +sig: W.W64) -> F64: +n = W.clz(sig) rp_norm(rm, U32.is_lt(n, 2), s, e, sig, n) def round_or_zero(+rm: Rnd, z: Bool, +s: U32, +e: U32, +sig: W.W64) -> F64: match z: case True{}: zero(zero_sum(rm)) case False{}: round_pack_m(rm, s, e, sig) # ---- addition ---- def add_sum(+rm: Rnd, same: Bool, +s: U32, +e: U32, +x: W.W64, +y: W.W64) -> F64: match same: case True{}: round_pack_m(rm, s, e, W.add(x, y)) case False{}: round_or_zero(rm, W.eq(x, y), s, e, W.sub(x, y)) # |x| >= |y|: x's exponent ex, the exponents' difference d def add_mag(+rm: Rnd, +sx: U32, +ex: U32, +mx: W.W64, +sy: U32, +d: U32, +my: W.W64) -> F64: add_sum(rm, U32.is_eq(sx, sy), sx, ex, W.shl(mx, 9), W.shr_jam(W.shl(my, 9), d)) def add_ord(+rm: Rnd, a_big: Bool, +sa: U32, +ea: U32, +ma: W.W64, +sb: U32, +eb: U32, +mb: W.W64) -> F64: match a_big: case True{}: add_mag(rm, sa, ea, ma, sb, (ea - eb : U32), mb) case False{}: add_mag(rm, sb, eb, mb, sa, (eb - ea : U32), ma) def add_fin(+rm: Rnd, +sa: U32, +ea: U32, +ma: W.W64, +sb: U32, +eb: U32, +mb: W.W64) -> F64: add_ord(rm, Bool.or(U32.is_lt(eb, ea), Bool.and(U32.is_eq(ea, eb), W.le(mb, ma))), sa, ea, ma, sb, eb, mb) def add_c(+rm: Rnd, ca: Cls, cb: Cls, +a: F64, +b: F64) -> F64: match ca cb: case FInf{+sa} FInf{sb}: Bool.pick(F64, U32.is_eq(sa, sb), inf(sa), dnan()) case FInf{sa} _: a case _ FInf{sb}: b case FZero{+sa} FZero{+sb}: zero(Bool.pick(U32, U32.is_eq(zero_sum(rm), 0), U32.and(sa, sb), U32.or(sa, sb))) case FZero{sa} _: b case _ FZero{sb}: a case FFin{sa, ea, ma} FFin{sb, eb, mb}: add_fin(rm, sa, ea, ma, sb, eb, mb) case _ _: dnan() def add_n(+rm: Rnd, na: Bool, nb: Bool, +a: F64, +b: F64) -> F64: match na nb: case True{} _: quiet(a) case False{} True{}: quiet(b) case False{} False{}: add_c(rm, cls(a), cls(b), a, b) # a + b, rounded as m says def add_m(+rm: Rnd, +a: F64, +b: F64) -> F64: add_n(rm, is_nan(a), is_nan(b), a, b) def sub_n(+rm: Rnd, na: Bool, nb: Bool, +a: F64, +b: F64) -> F64: match na nb: case True{} _: quiet(a) case False{} True{}: quiet(b) case False{} False{}: add_c(rm, cls(a), cls(neg(b)), a, neg(b)) # a - b, rounded as m says (a NaN b passes through with its own sign) def sub_m(+rm: Rnd, +a: F64, +b: F64) -> F64: sub_n(rm, is_nan(a), is_nan(b), a, b) # ---- multiplication ---- def mul_hi(p: W.W128) -> W.W64: match p: case W.W128{h, l}: W.jam_or(h, Bool.not(W.is_zero(l))) def mul_nm(+rm: Rnd, +s: U32, a: NM, b: NM) -> F64: match a b: case NM{ea, ma} NM{eb, mb}: round_pack_m(rm, s, (ea + eb - 1023 : U32), mul_hi(W.mul64(W.shl(ma, 10), W.shl(mb, 11)))) def mul_fin(+rm: Rnd, +s: U32, +ea: U32, +ma: W.W64, +eb: U32, +mb: W.W64) -> F64: mul_nm(rm, s, norm(ea, ma), norm(eb, mb)) def mul_c(+rm: Rnd, ca: Cls, cb: Cls) -> F64: match ca cb: case FInf{sa} FInf{sb}: inf(U32.xor(sa, sb)) case FInf{sa} FZero{sb}: dnan() case FInf{sa} FFin{sb, eb, mb}: inf(U32.xor(sa, sb)) case FZero{sa} FInf{sb}: dnan() case FZero{sa} FZero{sb}: zero(U32.xor(sa, sb)) case FZero{sa} FFin{sb, eb, mb}: zero(U32.xor(sa, sb)) case FFin{sa, ea, ma} FInf{sb}: inf(U32.xor(sa, sb)) case FFin{sa, ea, ma} FZero{sb}: zero(U32.xor(sa, sb)) case FFin{sa, ea, ma} FFin{sb, eb, mb}: mul_fin(rm, U32.xor(sa, sb), ea, ma, eb, mb) case _ _: dnan() def mul_n(+rm: Rnd, na: Bool, nb: Bool, +a: F64, +b: F64) -> F64: match na nb: case True{} _: quiet(a) case False{} True{}: quiet(b) case False{} False{}: mul_c(rm, cls(a), cls(b)) # a * b, rounded as m says def mul_m(+rm: Rnd, +a: F64, +b: F64) -> F64: mul_n(rm, is_nan(a), is_nan(b), a, b) # ---- division ---- type QR is Data: QR{q: W.W64, r: W.W64} # n steps of long division: q gains one bit a step def div_go(n: Nat, +rem: W.W64, +d: W.W64, +q: W.W64) -> QR: match n: case 0n: QR{q, rem} case 1n+k: +ge = W.le(d, rem) div_go(k, W.shl(Bool.pick(W.W64, ge, W.sub(rem, d), rem), 1), d, W.add(W.shl(q, 1), W.w(Bool.to_u32(ge)))) def qr_sticky(r: QR) -> W.W64: match r: case QR{q, rem}: W.jam_or(q, Bool.not(W.is_zero(rem))) # q = floor(ma * 2^62 / mb), between 2^61 and 2^63 def div_nm(+rm: Rnd, +s: U32, a: NM, b: NM) -> F64: match a b: case NM{ea, ma} NM{eb, mb}: round_pack_m(rm, s, (ea - eb + 1022 : U32), qr_sticky(div_go(63n, ma, mb, W.w(0)))) def div_fin(+rm: Rnd, +s: U32, +ea: U32, +ma: W.W64, +eb: U32, +mb: W.W64) -> F64: div_nm(rm, s, norm(ea, ma), norm(eb, mb)) def div_c(+rm: Rnd, ca: Cls, cb: Cls) -> F64: match ca cb: case FInf{sa} FInf{sb}: dnan() case FInf{sa} FZero{sb}: inf(U32.xor(sa, sb)) case FInf{sa} FFin{sb, eb, mb}: inf(U32.xor(sa, sb)) case FZero{sa} FInf{sb}: zero(U32.xor(sa, sb)) case FZero{sa} FZero{sb}: dnan() case FZero{sa} FFin{sb, eb, mb}: zero(U32.xor(sa, sb)) case FFin{sa, ea, ma} FInf{sb}: zero(U32.xor(sa, sb)) case FFin{sa, ea, ma} FZero{sb}: inf(U32.xor(sa, sb)) case FFin{sa, ea, ma} FFin{sb, eb, mb}: div_fin(rm, U32.xor(sa, sb), ea, ma, eb, mb) case _ _: dnan() def div_n(+rm: Rnd, na: Bool, nb: Bool, +a: F64, +b: F64) -> F64: match na nb: case True{} _: quiet(a) case False{} True{}: quiet(b) case False{} False{}: div_c(rm, cls(a), cls(b)) # a / b, rounded as m says def div_m(+rm: Rnd, +a: F64, +b: F64) -> F64: div_n(rm, is_nan(a), is_nan(b), a, b) # ---- square root ---- # n steps of the digit-by-digit square root: two bits of x in, one bit of q out def sqrt_go(n: Nat, +x: W.W64, +rem: W.W64, +q: W.W64) -> QR: match n: case 0n: QR{q, rem} case 1n+k: +r2 = W.add(W.shl(rem, 2), W.shr(x, 62)) +t = W.add(W.shl(q, 2), W.w(1)) +ge = W.le(t, r2) sqrt_go(k, W.shl(x, 2), Bool.pick(W.W64, ge, W.sub(r2, t), r2), W.add(W.shl(q, 1), W.w(Bool.to_u32(ge)))) # m * 2^(e - 1075) with an even power of two: q = floor(sqrt(m' * 2^70)), between 2^61 and 2^62 def sqrt_nm(+rm: Rnd, a: NM) -> F64: match a: case NM{+e, +m}: +odd = U32.is_eq(U32.and(e, 1), 1) round_pack_m(rm, 0, U32.div(Bool.pick(U32, odd, (e + 1023 : U32), (e + 1022 : U32)), 2), qr_sticky(sqrt_go(62n, W.shl(Bool.pick(W.W64, odd, m, W.shl(m, 1)), 10), W.w(0), W.w(0)))) def sqrt_c(+rm: Rnd, c: Cls, +a: F64) -> F64: match c: case FNan{}: quiet(a) case FInf{s}: Bool.pick(F64, U32.is_eq(s, 0), a, dnan()) case FZero{s}: a case FFin{s, e, m}: Bool.pick(F64, U32.is_eq(s, 0), sqrt_nm(rm, norm(e, m)), dnan()) def sqrt_n(+rm: Rnd, na: Bool, +a: F64) -> F64: match na: case True{}: quiet(a) case False{}: sqrt_c(rm, cls(a), a) # the square root, rounded as m says (of -0.0 is -0.0) def sqrt_m(+rm: Rnd, +a: F64) -> F64: sqrt_n(rm, is_nan(a), a) # ---- fused multiply-add: a * b + c rounded once ---- type Prod is Data: PBad{} PInf{s: U32} PZero{s: U32} PFin{s: U32, ea: U32, ma: W.W64, eb: U32, mb: W.W64} def prod_of(ca: Cls, cb: Cls) -> Prod: match ca cb: case FInf{sa} FInf{sb}: PInf{U32.xor(sa, sb)} case FInf{sa} FZero{sb}: PBad{} case FInf{sa} FFin{sb, eb, mb}: PInf{U32.xor(sa, sb)} case FZero{sa} FInf{sb}: PBad{} case FZero{sa} FZero{sb}: PZero{U32.xor(sa, sb)} case FZero{sa} FFin{sb, eb, mb}: PZero{U32.xor(sa, sb)} case FFin{sa, ea, ma} FInf{sb}: PInf{U32.xor(sa, sb)} case FFin{sa, ea, ma} FZero{sb}: PZero{U32.xor(sa, sb)} case FFin{sa, ea, ma} FFin{sb, eb, mb}: PFin{U32.xor(sa, sb), ea, ma, eb, mb} case _ _: PBad{} # R nonzero: R * 2^sc as s * S * 2^(e - 1084), S of at most 62 bits with the bits below jammed def fma_pack(+rm: Rnd, +s: U32, +sc: U32, +r: W.W128) -> F64: +lead = (127 - W.clz128(r) : U32) Bool.pick(F64, U32.is_lt(61, lead), round_pack_m(rm, s, (sc + (lead - 61) + 1084 : U32), W.low128(W.shr_jam128(r, (lead - 61 : U32)))), round_pack_m(rm, s, (sc + 1084 : U32), W.low128(r))) def fma_zero(+rm: Rnd, z: Bool, +s: U32, +sc: U32, +r: W.W128) -> F64: match z: case True{}: zero(zero_sum(rm)) case False{}: fma_pack(rm, s, sc, r) def fma_diff(+rm: Rnd, x_small: Bool, +sx: U32, +sy: U32, +sc: U32, +x: W.W128, +y: W.W128) -> F64: match x_small: case True{}: fma_pack(rm, sy, sc, W.sub128(y, x)) case False{}: fma_zero(rm, W.is_zero128(W.sub128(x, y)), sx, sc, W.sub128(x, y)) # x the operand at the larger scale sc, y aligned to it def fma_sum(+rm: Rnd, same: Bool, +sx: U32, +sy: U32, +sc: U32, +x: W.W128, +y: W.W128) -> F64: match same: case True{}: fma_pack(rm, sx, sc, W.add128(x, y)) case False{}: fma_diff(rm, W.lt128(x, y), sx, sy, sc, x, y) def fma_ord(+rm: Rnd, p_big: Bool, +sp: U32, +scp: U32, +p: W.W128, +sc: U32, +scc: U32, +c: W.W128) -> F64: match p_big: case True{}: fma_sum(rm, U32.is_eq(sp, sc), sp, sc, scp, p, W.shr_jam128(c, (scp - scc : U32))) case False{}: fma_sum(rm, U32.is_eq(sp, sc), sc, sp, scc, c, W.shr_jam128(p, (scc - scp : U32))) # the product P = ma * mb (below 2^106) as P * 2^20 at scale ea + eb - 2170, c's mc * 2^72 at scale ec - 1147 def fma_nm(+rm: Rnd, +sp: U32, a: NM, b: NM, +sc: U32, c: NM) -> F64: match a b c: case NM{ea, ma} NM{eb, mb} NM{ec, mc}: +scp = (ea + eb - 2170 : U32) +scc = (ec - 1147 : U32) fma_ord(rm, Bool.not(s_lt(scp, scc)), sp, scp, W.shl128(W.mul64(ma, mb), 20), sc, scc, W.W128{W.shl(mc, 8), W.w(0)}) def fma_c(+rm: Rnd, p: Prod, cc: Cls, +c: F64) -> F64: match p cc: case PBad{} _: dnan() case PInf{+sp} FInf{sc}: Bool.pick(F64, U32.is_eq(sp, sc), inf(sp), dnan()) case PInf{sp} _: inf(sp) case _ FInf{sc}: c case PZero{+sp} FZero{+sc}: zero(Bool.pick(U32, U32.is_eq(zero_sum(rm), 0), U32.and(sp, sc), U32.or(sp, sc))) case PZero{sp} _: c case PFin{s, ea, ma, eb, mb} FZero{sc}: mul_fin(rm, s, ea, ma, eb, mb) case PFin{s, ea, ma, eb, mb} FFin{sc, ec, mc}: fma_nm(rm, s, norm(ea, ma), norm(eb, mb), sc, norm(ec, mc)) case _ _: dnan() def fma_n(+rm: Rnd, nb: Bool, na: Bool, nc: Bool, +a: F64, +b: F64, +c: F64) -> F64: match nb na nc: case True{} _ _: quiet(b) case False{} True{} _: quiet(a) case False{} False{} True{}: quiet(c) case False{} False{} False{}: fma_c(rm, prod_of(cls(a), cls(b)), cls(c), c) # a * b + c with one rounding, as m says def fma_m(+rm: Rnd, +a: F64, +b: F64, +c: F64) -> F64: fma_n(rm, is_nan(b), is_nan(a), is_nan(c), a, b, c) # ---- the default mode: to nearest, ties to even ---- def round_pack(+s: U32, +e: U32, +sig: W.W64) -> F64: round_pack_m(RNearEven{}, s, e, sig) # a + b def add(+a: F64, +b: F64) -> F64: add_m(RNearEven{}, a, b) # a - b (a NaN b passes through with its own sign) def sub(+a: F64, +b: F64) -> F64: sub_m(RNearEven{}, a, b) # a * b def mul(+a: F64, +b: F64) -> F64: mul_m(RNearEven{}, a, b) # a / b def div(+a: F64, +b: F64) -> F64: div_m(RNearEven{}, a, b) # the square root (of -0.0 is -0.0) def sqrt(+a: F64) -> F64: sqrt_m(RNearEven{}, a) # a * b + c with one rounding def fma(+a: F64, +b: F64, +c: F64) -> F64: fma_m(RNearEven{}, a, b, c) # ---- comparisons (IEEE: NaN is unordered, -0.0 equals +0.0) ---- def both_zero(+ah: U32, +al: U32, +bh: U32, +bl: U32) -> Bool: U32.is_eq(U32.or(U32.and(U32.or(ah, bh), 2147483647), U32.or(al, bl)), 0) def eq_bits(a: F64, b: F64) -> Bool: match a b: case F64{+ah, +al} F64{+bh, +bl}: Bool.or(both_zero(ah, al, bh, bl), Bool.and(U32.is_eq(ah, bh), U32.is_eq(al, bl))) def lt_sign(na: Bool, nb: Bool, +ah: U32, +al: U32, +bh: U32, +bl: U32) -> Bool: match na nb: case False{} False{}: W.lt(W.W64{ah, al}, W.W64{bh, bl}) case True{} True{}: W.lt(W.W64{bh, bl}, W.W64{ah, al}) case True{} False{}: True{} case False{} True{}: False{} def lt_bits(a: F64, b: F64) -> Bool: match a b: case F64{+ah, +al} F64{+bh, +bl}: Bool.and(Bool.not(both_zero(ah, al, bh, bl)), lt_sign(U32.is_ne(sign_of(ah), 0), U32.is_ne(sign_of(bh), 0), ah, al, bh, bl)) def ordered(na: Bool, nb: Bool, r: Bool) -> Bool: match na nb: case False{} False{}: r case _ _: False{} # a == b def eq(+a: F64, +b: F64) -> Bool: ordered(is_nan(a), is_nan(b), eq_bits(a, b)) # a != b (True when either is NaN) def ne(+a: F64, +b: F64) -> Bool: Bool.not(eq(a, b)) # a < b def lt(+a: F64, +b: F64) -> Bool: ordered(is_nan(a), is_nan(b), lt_bits(a, b)) # a <= b def le(+a: F64, +b: F64) -> Bool: ordered(is_nan(a), is_nan(b), Bool.or(lt_bits(a, b), eq_bits(a, b))) def gt(+a: F64, +b: F64) -> Bool: lt(b, a) def ge(+a: F64, +b: F64) -> Bool: le(b, a) def pick_nan(na: Bool, nb: Bool, +a: F64, +b: F64, r: F64) -> F64: match na nb: case True{} _: a case False{} True{}: b case False{} False{}: r # NumPy's np.minimum: a NaN (the first if both) passes through as it is; otherwise a if a < b, else b def minimum(+a: F64, +b: F64) -> F64: pick_nan(is_nan(a), is_nan(b), a, b, Bool.pick(F64, lt_bits(a, b), a, b)) # NumPy's np.maximum: a NaN (the first if both) passes through as it is; otherwise a if a > b, else b def maximum(+a: F64, +b: F64) -> F64: pick_nan(is_nan(a), is_nan(b), a, b, Bool.pick(F64, lt_bits(b, a), a, b)) # ---- rounding to an integer ---- type RMode is Data: RTrunc{} RFloor{} RCeil{} REven{} def one_hi() -> U32: 1072693248 # |x| < 1, x not zero def ri_small(m: RMode, +s: U32, +e: U32, +f: W.W64) -> F64: match m: case RTrunc{}: zero(s) case RFloor{}: Bool.pick(F64, U32.is_eq(s, 0), zero(0), F64{U32.or(s, one_hi()), 0}) case RCeil{}: Bool.pick(F64, U32.is_eq(s, 0), F64{one_hi(), 0}, zero(s)) case REven{}: Bool.pick(F64, Bool.and(U32.is_eq(e, 1022), Bool.not(W.is_zero(f))), F64{U32.or(s, one_hi()), 0}, zero(s)) def ri_up(m: RMode, +s: U32, +fr: W.W64, +half: W.W64, +odd: Bool) -> Bool: match m: case RTrunc{}: False{} case RFloor{}: Bool.and(U32.is_ne(s, 0), Bool.not(W.is_zero(fr))) case RCeil{}: Bool.and(U32.is_eq(s, 0), Bool.not(W.is_zero(fr))) case REven{}: Bool.or(W.lt(half, fr), Bool.and(W.eq(half, fr), odd)) # 1 <= |x| < 2^52: the low u = 1075 - e bits of x's bits are the fraction def ri_mid(m: RMode, +s: U32, +u: U32, +bits: W.W64) -> F64: +unit = W.shl(W.w(1), u) +mask = W.sub(unit, W.w(1)) +int = W.and(bits, W.not(mask)) of_w(Bool.pick(W.W64, ri_up(m, s, W.and(bits, mask), W.shl(W.w(1), (u - 1 : U32)), Bool.not(W.is_zero(W.and(int, unit)))), W.add(int, unit), int)) # x not NaN def ri_parts(+m: RMode, +s: U32, +e: U32, +h: U32, +l: U32) -> F64: Bool.pick(F64, U32.is_le(1075, e), F64{h, l}, Bool.pick(F64, U32.is_lt(e, 1023), Bool.pick(F64, both_zero(h, l, 0, 0), F64{h, l}, ri_small(m, s, e, W.W64{frac_hi(h), l})), ri_mid(m, s, (1075 - e : U32), W.W64{h, l}))) def ri_bits(m: RMode, x: F64) -> F64: match x: case F64{+h, +l}: ri_parts(m, sign_of(h), exp_of(h), h, l) def ri_n(na: Bool, m: RMode, +x: F64) -> F64: match na: case True{}: quiet(x) case False{}: ri_bits(m, x) # x rounded to an integer in mode m (a NaN passes through quieted) def round_int(m: RMode, +x: F64) -> F64: ri_n(is_nan(x), m, x) # toward zero (np.trunc) def trunc(x: F64) -> F64: round_int(RTrunc{}, x) # toward minus infinity (np.floor) def floor(x: F64) -> F64: round_int(RFloor{}, x) # toward plus infinity (np.ceil) def ceil(x: F64) -> F64: round_int(RCeil{}, x) # to the nearest integer, ties to even (np.rint, np.round) def rint(x: F64) -> F64: round_int(REven{}, x) # ---- conversions ---- def from_mag(+s: U32, +x: U32) -> F64: Bool.pick(F64, U32.is_eq(x, 0), zero(0), round_pack(s, 1084, W.w(x))) # a signed 32-bit integer, exactly def from_i32(+x: U32) -> F64: +s = sign_of(x) from_mag(s, Bool.pick(U32, U32.is_eq(s, 0), x, (0 - x : U32))) # an unsigned 32-bit integer, exactly def from_u32(+x: U32) -> F64: from_mag(0, x) def apply_sign(+s: U32, +x: U32) -> U32: Bool.pick(U32, U32.is_eq(s, 0), x, (0 - x : U32)) def sig_of(+h: U32, +l: U32) -> W.W64: W.W64{U32.or(frac_hi(h), 1048576), l} # toward zero, as a signed 32-bit integer; NaN and values out of range give -2^31 (x86's "integer indefinite") def to_i32(x: F64) -> U32: match x: case F64{+h, +l}: +e = exp_of(h) Bool.pick(U32, U32.is_lt(e, 1023), 0, Bool.pick(U32, U32.is_le(1054, e), 2147483648, apply_sign(sign_of(h), W.low(W.shr(sig_of(h, l), (1075 - e : U32)))))) # toward zero, as NumPy's astype(uint32) on x86-64 does it: through a signed 64-bit integer, keeping its low 32 # bits (so -1.0 gives 4294967295), and 0 for NaN and values outside the 64-bit range def to_u32(x: F64) -> U32: match x: case F64{+h, +l}: +e = exp_of(h) Bool.pick(U32, Bool.or(U32.is_lt(e, 1023), U32.is_le(1086, e)), 0, apply_sign(sign_of(h), W.low(Bool.pick(W.W64, U32.is_le(e, 1075), W.shr(sig_of(h, l), (1075 - e : U32)), W.shl(sig_of(h, l), (e - 1075 : U32)))))) def f32_bits(x: F32) -> U32: match x: case F32{w}: U32{w} def bits_f32(x: U32) -> F32: match x: case U32{w}: F32{w} # a float32's bits to the float64 of the same value, exactly (a NaN keeps its payload, quieted) def from_f32_bits(+u: U32) -> F64: +s = sign_of(u) +e = U32.and(U32.shrn(u, 23n), 255) +f = U32.and(u, 8388607) Bool.pick(F64, U32.is_eq(e, 255), Bool.pick(F64, U32.is_eq(f, 0), inf(s), F64{U32.or(U32.or(s, 2146959360), U32.shrn(f, 3n)), U32.shln(f, 29n)}), Bool.pick(F64, U32.is_eq(f, 0), Bool.pick(F64, U32.is_eq(e, 0), zero(s), round_pack(s, (e + 934 : U32), W.w(U32.or(f, 8388608)))), Bool.pick(F64, U32.is_eq(e, 0), round_pack(s, 935, W.w(f)), round_pack(s, (e + 934 : U32), W.w(U32.or(f, 8388608)))))) # a float32, exactly def from_f32(x: F32) -> F64: from_f32_bits(f32_bits(x)) # whether to round up, from how the bits below compare with half (above, exactly half), whether any is set, the sign # and whether the kept bits are odd def rnd_up_g(m: Rnd, +s: U32, +above: Bool, +half: Bool, +any: Bool, +odd: Bool) -> Bool: match m: case RNearEven{}: Bool.or(above, Bool.and(half, odd)) case RNearAway{}: Bool.or(above, half) case RZero{}: False{} case RUp{}: Bool.and(any, U32.is_eq(s, 0)) case RDown{}: Bool.and(any, U32.is_ne(s, 0)) # S's top bit at bit 61, the value S * 2^(e32 - 188), e32 >= 1: round to 24 bits on the 38 below, as rm says def r32_round(+rm: Rnd, +s: U32, +e32: U32, sig: W.W64) -> U32: match sig: case W.W64{+h, +l}: +m0 = U32.shrn(h, 6n) +r = U32.and(h, 63) +up = rnd_up_g(rm, s, Bool.or(U32.is_lt(32, r), Bool.and(U32.is_eq(r, 32), U32.is_ne(l, 0))), Bool.and(U32.is_eq(r, 32), U32.is_eq(l, 0)), Bool.or(U32.is_ne(r, 0), U32.is_ne(l, 0)), U32.is_eq(U32.and(m0, 1), 1)) (U32.or(s, U32.shln((e32 - 1 : U32), 23n)) + m0 + Bool.to_u32(up) : U32) # past the largest float32: infinity, or the largest finite float32 when the mode rounds toward zero on that side def overflow32(m: Rnd, +s: U32) -> U32: match m: case RNearEven{}: U32.or(s, 2139095040) case RNearAway{}: U32.or(s, 2139095040) case RZero{}: U32.or(s, 2139095039) case RUp{}: Bool.pick(U32, U32.is_eq(s, 0), 2139095040, 4286578687) case RDown{}: Bool.pick(U32, U32.is_eq(s, 0), 2139095039, 4286578688) def r32_range(+rm: Rnd, over: Bool, tiny: Bool, +s: U32, +e32: U32, +sig: W.W64) -> U32: match over tiny: case True{} _: overflow32(rm, s) case False{} True{}: r32_round(rm, s, 1, W.shr_jam(sig, (1 - e32 : U32))) case False{} False{}: r32_round(rm, s, e32, sig) def to_f32_nm(+rm: Rnd, +s: U32, n: NM) -> U32: match n: case NM{e, m}: +e32 = (e - 896 : U32) r32_range(rm, Bool.and(U32.is_lt(e32, 2147483648), U32.is_le(255, e32)), s_lt(e32, 1), s, e32, W.shl(m, 9)) def to_f32_c(+rm: Rnd, c: Cls, +h: U32, +l: U32) -> U32: match c: case FNan{}: U32.or(U32.or(sign_of(h), 2143289344), U32.or(U32.shln(frac_hi(h), 3n), U32.shrn(l, 29n))) case FInf{s}: U32.or(s, 2139095040) case FZero{s}: s case FFin{s, e, m}: to_f32_nm(rm, s, norm(e, m)) # the float32 bits of x rounded as rm says (a NaN keeps the top of its payload, quieted) def to_f32_bits_m(+rm: Rnd, x: F64) -> U32: match x: case F64{+h, +l}: to_f32_c(rm, cls(F64{h, l}), h, l) # the float32 bits nearest x (ties to even; a NaN keeps the top of its payload, quieted) def to_f32_bits(x: F64) -> U32: to_f32_bits_m(RNearEven{}, x) # x rounded to a float32 as rm says def to_f32_m(+rm: Rnd, x: F64) -> F32: bits_f32(to_f32_bits_m(rm, x)) # the float32 nearest x def to_f32(x: F64) -> F32: bits_f32(to_f32_bits(x)) # ---- 64-bit integers (W.W64{hi, lo}; signed ones in two's complement) ---- def w_neg(+x: W.W64) -> W.W64: W.sub(W.w(0), x) # an unsigned 64-bit integer, rounded as rm says def from_u64_m(+rm: Rnd, +x: W.W64) -> F64: Bool.pick(F64, W.is_zero(x), zero(0), round_pack_m(rm, 0, 1084, x)) # a signed 64-bit integer, rounded as rm says def from_i64_m(+rm: Rnd, +x: W.W64) -> F64: +s = sign_of(W.high(x)) Bool.pick(F64, W.is_zero(x), zero(0), round_pack_m(rm, s, 1084, Bool.pick(W.W64, U32.is_eq(s, 0), x, w_neg(x)))) # an unsigned 64-bit integer, rounded to nearest (ties to even) def from_u64(+x: W.W64) -> F64: from_u64_m(RNearEven{}, x) # a signed 64-bit integer, rounded to nearest (ties to even) def from_i64(+x: W.W64) -> F64: from_i64_m(RNearEven{}, x) def i64_mag(+h: U32, +l: U32, +e: U32) -> W.W64: Bool.pick(W.W64, U32.is_le(e, 1075), W.shr(sig_of(h, l), (1075 - e : U32)), W.shl(sig_of(h, l), (e - 1075 : U32))) # toward zero, as a signed 64-bit integer; NaN and values outside [-2^63, 2^63) give -2^63 (x86's "integer indefinite") def to_i64(x: F64) -> W.W64: match x: case F64{+h, +l}: +e = exp_of(h) Bool.pick(W.W64, U32.is_lt(e, 1023), W.w(0), Bool.pick(W.W64, U32.is_le(1086, e), W.W64{2147483648, 0}, Bool.pick(W.W64, U32.is_eq(sign_of(h), 0), i64_mag(h, l, e), w_neg(i64_mag(h, l, e))))) def flip_top(x: W.W64) -> W.W64: match x: case W.W64{h, l}: W.W64{U32.xor(h, 2147483648), l} # toward zero, as NumPy's astype(uint64) on x86-64 does it: below 2^63 through a signed 64-bit integer (so -1.0 # gives 2^64 - 1, and NaN and values below -2^63 give 2^63); from 2^63 as x - 2^63 with the top bit set (so values # from 2^64 up, and inf, give 0) def to_u64(+x: F64) -> W.W64: Bool.pick(W.W64, le(F64{1138753536, 0}, x), flip_top(to_i64(sub(x, F64{1138753536, 0}))), to_i64(x))