Executable IEEE-754 binary32 (IEEE32Exec) #
TorchLean uses two complementary ways to talk about "float32":
NN/Floats/NeuralFloat/*andNN/Floats/FP32/*model rounding-on-ℝ. This is suited to proofs and for compositional "real computation + rounding error" arguments.IEEE32Exec(in this file) models bit-level IEEE-754 behavior. This is what you want when you care about corner cases like NaN/Inf payload propagation, signed zero, and exact tie-breaking.
We implement IEEE32Exec as raw UInt32 bits and provide:
- decoders/encoders for the binary32 layout,
nextUp/nextDown(adjacent representable floats),- basic arithmetic (
+ - * / fma) by decoding to an exact dyadic/rational intermediate and then rounding once (round-to-nearest, ties-to-even), - comparisons and
min/maxwith IEEE-754 NaN rules, sqrtvia integer arithmetic on the exact input value, rounded back to binary32.
We also provide a Context IEEE32Exec instance so the spec layer can run modules with an
executable scalar. That is why we import NN.Spec.Core.Context here.
About transcendentals #
IEEE-754 does not specify implementations for transcendental functions (exp, tanh, ...). In
practice those are provided by libm (or vendor math libraries) and vary across platforms.
We provide deterministic implementations for a few transcendentals in Lean so examples can
run without delegating to the host runtime. For the remaining ones, we may still delegate to Lean's
Float (binary64) and round back to binary32. These functions are executable and stable, but they
are not claimed to be correctly rounded or to match any particular hardware/libm.
References #
- IEEE Standard for Floating-Point Arithmetic, IEEE 754-2019.
- David Goldberg, “What Every Computer Scientist Should Know About Floating-Point Arithmetic”, ACM Computing Surveys (1991). DOI: 10.1145/103162.103163
- Jean-Michel Muller et al., Handbook of Floating-Point Arithmetic, 2nd ed. (2018).
- S. Boldo, G. Melquiond, “Flocq: a unified Coq library for proving floating-point algorithms correct” (ARITH 2011). DOI: 10.1109/ARITH.2011.40
Instances For
Instances For
ofBits (toBits x) = x.
Default inhabitant: all bits zero, i.e. +0.0.
Binary32 bit layout #
IEEE-754 binary32 is stored as:
- sign bit
sin bit 31, - exponent field
ein bits 30..23 (8 bits, bias 127), - fraction field
fin bits 22..0 (23 bits).
For NaNs, the "quiet" bit is the top fraction bit.
Mask selecting the sign bit (bit 31).
Instances For
Mask selecting the 8-bit exponent field (bits 30..23).
Instances For
Mask selecting the 23-bit fraction field (bits 22..0).
Instances For
The IEEE-754 "quiet NaN" indicator bit (top fraction bit).
Instances For
8-bit value 0xFF, used to test the “all ones” exponent field.
Instances For
True iff the sign bit (bit 31) is set.
Instances For
Extract the 8-bit exponent field (bits 30..23).
Instances For
Extract the 23-bit fraction field (bits 22..0).
Instances For
Predicate for NaN: exponent all ones and fraction nonzero.
Instances For
Predicate for quiet NaN (NaN with the quiet bit set).
Instances For
Predicate for signaling NaN (NaN with the quiet bit clear).
Instances For
Predicate for infinity: exponent all ones and fraction zero.
Instances For
Predicate for signed zero (both +0 and -0).
Instances For
Predicate for finiteness: exponent field is not all ones (excludes NaN/Inf).
Instances For
+0.0 as an executable binary32 constant.
Instances For
-0.0 as an executable binary32 constant.
Instances For
+1.0 as an IEEE-754 binary32 constant.
Instances For
-1.0 as an IEEE-754 binary32 constant.
Instances For
+∞ as an executable binary32 constant.
Instances For
-∞ as an executable binary32 constant.
Instances For
A canonical quiet NaN payload used by the executable kernel.
Instances For
NaN selection / payload propagation #
IEEE-754 leaves some freedom in how NaNs are "chosen" when multiple NaNs appear. For reproducibility (and nicer debugging), we make the choice deterministic (left-to-right) and we quiet signaling NaNs by setting the quiet bit.
Quiet a NaN by setting the quiet bit (and leave non-NaNs unchanged).
Instances For
If x is a NaN, return it (quieted).
Instances For
Choose a NaN from two operands.
This is the "NaN propagation" policy used by most binary ops in this file:
- if any operand is a signaling NaN, return that operand (quieted), left-to-right,
- otherwise if any operand is a quiet NaN, return that operand, left-to-right,
- otherwise return
none.
Instances For
Like chooseNaN2, but for ternary ops (used for fma).
Instances For
Smallest positive subnormal (bit pattern 0x00000001, value 2^-149).
Instances For
Smallest negative subnormal (bit pattern 0x80000001, value -2^-149).
Instances For
Largest finite positive float32 (bit pattern 0x7F7FFFFF).
Instances For
Largest finite negative float32 (bit pattern 0xFF7FFFFF).
Instances For
Flip the sign bit (works for finite/Inf/NaN, and distinguishes ±0).
Instances For
Clear the sign bit.