~/bend-docscommunity

cells64.bend source

cells64.bend on the hub · documented module

# bend-ndarray's float64 elements, computed with bend-float64 (IEEE 754 binary64 in pure Bend, x86-64's bits). A# float64 element is two cells in the tree, its high word then its low one (tree.bend). Arithmetic and comparisons# are IEEE's; floor_divide and remainder follow NumPy's divmod (Python's // and %); the math functions are# bend-float64's, correctly rounded.import Baseimport ./shape.bend as Simport ./cells.bend as Cimport bend-float64@0.1.8.0/wide.bend as Wimport bend-float64@0.1.8.0/float64.bend as Fimport bend-float64@0.1.8.0/math.bend as Mimport bend-float64@0.1.8.0/text.bend as Tdef is_f64(dt: S.DType) -> Bool:  match dt:    case S.DF64{}:      True{}    case _:      False{}def of(+h: U32, +l: U32) -> F.F64:  F.F64{h, l}def zero() -> F.F64:  F.F64{0, 0}def one() -> F.F64:  F.F64{1072693248, 0}# ---- fmod and divmod ----# m (nonzero) shifted until its top bit is bit 52, and its exponent lowered to matchtype NM is Data:  NM{e: U32, m: W.W64}def norm(+e: U32, +m: W.W64) -> NM:  +sh = (W.clz(m) - 11 : U32)  NM{(e - sh : U32), W.shl(m, sh)}def nm_of(c: F.Cls) -> NM:  match c:    case F.FFin{s, +e, +m}:      norm(e, m)    case _:      NM{0, W.w(0)}# r 2^k mod my, k steps of doubling, r < mydef mod_steps(k: Nat, +r: W.W64, +my: W.W64) -> W.W64:  match k:    case 0n:      r    case 1n+j:      +r2 = W.shl(r, 1)      mod_steps(j, Bool.pick(W.W64, W.le(my, r2), W.sub(r2, my), r2), my)# r 2^(e - 1075) with x's sign, exactly (r < 2^53; e from -51 up, below 1 for a subnormal y): one product by a power# of two from e = 53 up, else by 2^(e - 965) and then 2^-110, every step exact because the result is a float64def scaled(+neg: Bool, +r: W.W64, +e: U32) -> F.F64:  +v = Bool.pick(F.F64, C.is_neg((e - 53 : U32)), F.mul(F.mul(F.from_u64(r), F.of_bits(U32.shln((e + 58 : U32), 20n), 0)), F.of_bits(957349888, 0)),    F.mul(F.from_u64(r), F.of_bits(U32.shln((e - 52 : U32), 20n), 0)))  Bool.pick(F.F64, neg, F.neg(v), v)def fmod_nm(a: NM, b: NM, +neg: Bool) -> F.F64:  match a b:    case NM{+ea, +ma} NM{+eb, +mb}:      +r0 = Bool.pick(W.W64, W.le(mb, ma), W.sub(ma, mb), ma)      scaled(neg, mod_steps(U32.to_nat((ea - eb : U32)), r0, mb), eb)# |x| >= |y|, both finite and nonzerodef fmod_big(+x: F.F64, +y: F.F64) -> F.F64:  fmod_nm(nm_of(F.cls(x)), nm_of(F.cls(y)), F.signbit(x))# which way fmod goes: invalid (a NaN, an infinite x, a zero y), x itself (|x| < |y|, or y infinite), or the exact looptype FK is Data:  FBothNan{}  FInvalid{}  FKeep{}  FLoop{}# of two NaNs, quieted, the one with the larger significand, the positive one of two equal (glibc's fmod on x86-64,# whose NaN NumPy's remainder returns)def nan_pick(+x: F.F64, +y: F.F64) -> F.F64:  +qx = F.quiet(x)  +qy = F.quiet(y)  +mx: W.W64 = W.W64{U32.and(F.hi_bits(qx), 1048575), F.lo_bits(qx)}  +my: W.W64 = W.W64{U32.and(F.hi_bits(qy), 1048575), F.lo_bits(qy)}  Bool.pick(F.F64, W.lt(mx, my), qy, Bool.pick(F.F64, W.lt(my, mx), qx, Bool.pick(F.F64, F.signbit(qx), qy, qx)))def fmod_k(k: FK, +x: F.F64, +y: F.F64) -> F.F64:  match k:    case FBothNan{}:      nan_pick(x, y)    case FInvalid{}:      F.div(F.mul(x, y), F.mul(x, y))    case FKeep{}:      x    case FLoop{}:      fmod_big(x, y)# C's fmod: x - trunc(x / y) y, exactly, with x's sign; a NaN, an infinite x or a zero y gives (x y) / (x y), as glibc# (and of two NaNs, nan_pick's)def fmod(+x: F.F64, +y: F.F64) -> F.F64:  fmod_k(Bool.pick(FK, Bool.and(F.is_nan(x), F.is_nan(y)), FBothNan{}, Bool.pick(FK, Bool.or(Bool.or(F.is_nan(x), F.is_nan(y)), Bool.or(F.is_inf(x), F.eq(y, zero()))), FInvalid{},    Bool.pick(FK, Bool.or(F.is_inf(y), F.lt(F.abs(x), F.abs(y))), FKeep{}, FLoop{}))), x, y)type DM is Data:  DM{div: F.F64, mod: F.F64}def dm_floor(+div: F.F64, +mod: F.F64, +x: F.F64, +y: F.F64) -> DM:  +fl = F.floor(div)  DM{Bool.pick(F.F64, F.ne(div, zero()), Bool.pick(F.F64, F.gt(F.sub(div, fl), F.F64{1071644672, 0}), F.add(fl, one()), fl), F.copysign(zero(), F.div(x, y))), mod}def dm_adjust(+mod: F.F64, +div: F.F64, +x: F.F64, +y: F.F64) -> DM:  +flip = Bool.and(F.ne(mod, zero()), Bool.xor(F.lt(y, zero()), F.lt(mod, zero())))  dm_floor(Bool.pick(F.F64, flip, F.sub(div, one()), div),    Bool.pick(F.F64, F.ne(mod, zero()), Bool.pick(F.F64, flip, F.add(mod, y), mod), F.copysign(zero(), y)), x, y)def dm_go(by0: Bool, +mod: F.F64, +x: F.F64, +y: F.F64) -> DM:  match by0:    case True{}:      DM{F.div(x, y), mod}    case False{}:      dm_adjust(mod, F.div(F.sub(x, mod), y), x, y)# NumPy's divmod for floats (npy_divmod): Python's // and %, the quotient snapped to an integerdef divmod(+x: F.F64, +y: F.F64) -> DM:  dm_go(F.eq(y, zero()), fmod(x, y), x, y)def dm_div(d: DM) -> F.F64:  match d:    case DM{a, b}:      adef dm_mod(d: DM) -> F.F64:  match d:    case DM{a, b}:      b# ---- binary operations ----# an arithmetic operation (not a comparison)def f2(op: C.B2, +x: F.F64, +y: F.F64) -> F.F64:  match op:    case C.BAdd{}:      F.add(x, y)    case C.BSub{}:      F.sub(x, y)    case C.BMul{}:      F.mul(x, y)    case C.BDiv{}:      F.div(x, y)    case C.BFloorDiv{}:      dm_div(divmod(x, y))    case C.BRem{}:      dm_mod(divmod(x, y))    case C.BMin{}:      F.minimum(x, y)    case C.BMax{}:      F.maximum(x, y)    case C.BPow{}:      M.power(x, y)    case C.BAtan2{}:      M.arctan2(x, y)    case C.BHypot{}:      M.hypot(x, y)    case _:      zero()# a comparison, as a bool celldef cmp2(op: C.B2, +x: F.F64, +y: F.F64) -> U32:  match op:    case C.BEq{}:      C.b2u(F.eq(x, y))    case C.BNe{}:      C.b2u(F.ne(x, y))    case C.BLt{}:      C.b2u(F.lt(x, y))    case C.BLe{}:      C.b2u(F.le(x, y))    case C.BGt{}:      C.b2u(F.gt(x, y))    case C.BGe{}:      C.b2u(F.ge(x, y))    case _:      0# ---- one argument ----def math(f: C.MF, +x: F.F64) -> F.F64:  match f:    case C.MExp{}:      M.exp(x)    case C.MLog{}:      M.log(x)    case C.MLog2{}:      M.log2(x)    case C.MLog10{}:      M.log10(x)    case C.MSqrt{}:      F.sqrt(x)    case C.MSin{}:      M.sin(x)    case C.MCos{}:      M.cos(x)    case C.MTan{}:      M.tan(x)    case C.MAsin{}:      M.arcsin(x)    case C.MAcos{}:      M.arccos(x)    case C.MAtan{}:      M.arctan(x)    case C.MSinh{}:      M.sinh(x)    case C.MCosh{}:      M.cosh(x)    case C.MTanh{}:      M.tanh(x)    case C.MFloor{}:      F.floor(x)    case C.MCeil{}:      F.ceil(x)    case C.MTrunc{}:      F.trunc(x)    case C.MRint{}:      F.rint(x)    case C.MRecip{}:      F.div(one(), x)# -1, 0 or 1 (nan stays nan; a zero of either sign gives +0, as NumPy)def sign(+x: F.F64) -> F.F64:  Bool.pick(F.F64, F.is_nan(x), x, Bool.pick(F.F64, F.gt(x, zero()), one(), Bool.pick(F.F64, F.lt(x, zero()), F.F64{3220176896, 0}, zero())))# a float64 to another float64 (every operation but a conversion to another dtype)def cell1(op: C.U1, +x: F.F64) -> F.F64:  match op:    case C.UNeg{}:      F.neg(x)    case C.UAbs{}:      F.abs(x)    case C.UDivN{n}:      F.div(x, F.from_u32(n))    case C.UMath{f}:      math(f, x)    case C.USquare{}:      F.mul(x, x)    case C.USign{}:      sign(x)    case _:      x# ---- conversions ----# a float64 to another dtype's cell: float32 rounded to nearest; int32 toward zero (nan and out of range: -2^31, as# x86); uint32 through int64, as NumPy on x86-64; uint8 the low byte of the int32; bool: not zerodef to_cell(to: S.DType, +x: F.F64) -> U32:  match to:    case S.DF32{}:      F.to_f32_bits(x)    case S.DI32{}:      F.to_i32(x)    case S.DU32{}:      F.to_u32(x)    case S.DU8{}:      U32.and(F.to_i32(x), 255)    case S.DBool{}:      C.b2u(F.ne(x, zero()))    case S.DF64{}:      F.hi_bits(x)# another dtype's cell to a float64, exactlydef of_cell(from: S.DType, +x: U32) -> F.F64:  match from:    case S.DF32{}:      F.from_f32_bits(x)    case S.DI32{}:      F.from_i32(x)    case _:      F.from_u32(x)# an element as NumPy's repr writes it (Python's repr of the float)def show(+x: F.F64) -> String:  T.show(x)