# bend-float64 IEEE 754 binary64 (`double`, NumPy's `float64`) for Bend 2, in pure Bend. Bend has no 64-bit types, so a float64 is two `U32` words and every operation is integer arithmetic on them. Each result is **correctly rounded** (to nearest, ties to even) and has **the bits x86-64 gives**, NaNs included. 32.5 million random cases were checked bit for bit against NumPy and glibc on x86-64, and the NaN rules are proved for every input. ```bend import bend-float64@0.1.0.0/float64.bend as F def tenth() -> F.F64: F.div(F.from_i32(1), F.from_i32(10)) # 0.1, correctly rounded def main() -> IO(Unit): # 0.1 + 0.2 = 0.30000000000000004: hi 1070805811, lo 858993460 IO.print(U32.show(F.hi_bits(F.add(tenth(), F.div(F.from_i32(1), F.from_i32(5)))))) ``` ## What it has | | | | --- | --- | | type | `F64{hi, lo}`: `hi` holds the sign, the 11-bit exponent and the top 20 fraction bits, `lo` the low 32; `of_bits(hi, lo)`, `hi_bits`, `lo_bits` | | arithmetic | `add`, `sub`, `mul`, `div`, `sqrt`, `fma(a, b, c)` (a * b + c, rounded once) | | sign | `neg`, `abs`, `copysign(x, y)`, `signbit` | | compare | `eq`, `ne`, `lt`, `le`, `gt`, `ge` (IEEE: NaN is unordered, -0.0 == +0.0), `minimum`, `maximum` (NumPy's `np.minimum`, `np.maximum`) | | classify | `is_nan`, `is_inf`, `is_finite` | | to an integer value | `trunc`, `floor`, `ceil`, `rint` (ties to even, as `np.round`), or `round_int(mode, x)` with `RTrunc{}`, `RFloor{}`, `RCeil{}`, `REven{}` | | conversions | `from_i32`, `from_u32` (a `U32` holding the integer), `from_f32` / `from_f32_bits` (exact), `to_f32` / `to_f32_bits` (rounded), `to_i32`, `to_u32` (toward zero) | | constants | `zero(s)`, `inf(s)` with `s` = 0 or 2147483648 (the sign bit), `dnan()` (the default NaN), `quiet(x)` | ## The exact rules - **Rounding.** Every operation rounds to nearest, ties to even, and handles subnormals in full (no flush to zero). NumPy uses no other mode, and no other mode is provided. - **NaN results** are x86's: - a NaN operand passes through with its quiet bit set; - of two NaNs, the first operand's is returned, as `addsd`, NumPy's scalar path, `np.subtract` and `np.divide` do; - `fma` takes b's NaN, then a's, then c's, as glibc's `fma` does on x86-64, even for `0 * inf + NaN`; - an invalid operation gives the default NaN `0xFFF8000000000000`: `inf - inf`, `0 * inf`, `0 / 0`, `inf / inf`, and the square root of a number below zero; - `sqrt(-0.0)` is `-0.0`; - `neg`, `abs` and `copysign` change only the sign bit, NaN or not. - **NumPy's own `add` and `multiply` are not consistent.** For arrays of 9 or more elements their vector loop swaps the operands, so of two NaNs they return the second. bend-float64 always returns the first. - **`minimum`/`maximum`** pass a NaN through unchanged (not quieted), the first if both are NaN. Otherwise they return the second of two equal values, so `minimum(+0.0, -0.0)` is `-0.0`. These are NumPy's answers. - **`to_i32`** truncates, and NaN and values out of range give -2^31 (x86's "integer indefinite"). - **`to_u32`** follows NumPy's `astype(np.uint32)` on x86-64. It truncates to a signed 64-bit integer and keeps the low 32 bits, so `-1.0` gives 4294967295; NaN and values beyond the 64-bit range give 0. - **`to_f32`** rounds to nearest, ties to even. A NaN keeps the top 22 bits of its payload, quieted. ## How it is checked - `LAWS.bend` / `PROOF.bend` hold 20 laws, gated by `./bend-check PROOF.bend --verdict`. - The NaN rules above are proved for **every** input: first and second operand for add, sub, mul and div; the order b, a, c for fma; sqrt and the four roundings; all six comparisons; minimum and maximum. - Correct rounding is pinned at about 90 literal inputs, each chosen to exercise a rounding path: ties, overflow thresholds, gradual underflow, exact cancellation, the sign of zero, fma's single rounding, and conversion edges. Every expected value comes from the hardware. - `mutants.sh` plants 27 single bugs in the core (ties rounding up, a lost sticky bit, a lost carry, a NaN that is not quieted, and others). The laws catch all 27. - `test/check.py` runs `test/cases`, a Bend program that prints random cases of all 31 operations. It recomputes each case with NumPy and glibc's `fma` on the machine and compares the bits. - The inputs mix special values, subnormals, both ends of the exponent range, sparse and dense significands, exact ties, and pairs close enough to cancel. - `python3 check.py 20` runs 2^20 cases per operation, 32.5 million in all; every one matched on x86-64. - `test/run.sh` runs everything. ## Speed It is software floating point, so expect it to be about 100 times slower than the hardware. On a 32-thread CPU, summing op(i/7, (i+1)/3) over 2^20 values of i in parallel took 0.32 s for add, 0.33 s for mul, 0.47 s for div, 0.49 s for sqrt and 0.51 s for fma. That includes making the inputs, which takes 0.17 s on its own. NumPy does one of these operations over 2^20 elements in about 1.5 ms on one thread (`test/bench.bend`). ## Not yet Decimal parsing and printing, `exp`/`log`/`sin`/`cos`/`pow`, 64-bit integer conversions, and the other rounding modes.