Binary interchange format definitions #
FloatFormat completely describes a binary storage format: its sign/exponent/fraction layout,
exponent bias, and exceptional-value encoding. There is no global precision ceiling. IEEE,
finite-only, and FNUZ formats are values of the same structure, and so is a custom format, which
pairs one of the four exceptional-value encodings with a validated exponent bias.
Lean's generic Float.Model.Format represents the IEEE interpretation of a layout. The
toModel conversion below therefore exposes that logical view only for the layout fields; the
policy-aware executable semantics use exponentBias and encoding directly.
References:
- IEEE Standard for Floating-Point Arithmetic, IEEE 754-2019, https://doi.org/10.1109/IEEESTD.2019.8766229.
Assignment of exceptional values within a sign/exponent/fraction binary format.
- ieee : Encoding
All-ones exponent: zero fraction is infinity, nonzero fraction is NaN.
- finiteMaxNaN : Encoding
No infinity; the all-ones exponent and all-ones fraction pattern is NaN.
- finiteUnsignedZero : Encoding
No infinity or negative zero; the negative-zero bit pattern is the sole NaN.
- finite : Encoding
Every nonzero bit pattern is finite; both signs of zero remain representable.
Instances For
Executable equality of encodings by case split.
The explicit inlined test lets the compiler reduce comparisons of known constructors. The
DecidableEq instance uses this test so descriptor eligibility checks can specialize with it.
Instances For
Encoding.beq decides propositional equality of encodings.
Equality of encodings is decidable; the instance is inlined so literal comparisons fold.
Largest encoded exponent that is finite at fraction zero.
Instances For
Complete binary storage, exponent-bias, and exceptional-value format descriptor.
- expWidth : Nat
Width of the biased exponent field.
At least two exponent bits are needed to separate zero/subnormal encodings, finite normal exponents, and the all-ones infinity/NaN class.
- fracWidth : Nat
Width of the fraction (significand without the implicit integer bit).
An interchange format has at least one explicit fraction bit.
- exponentBias : Nat
Bias subtracted from every nonzero encoded exponent.
- encoding : Encoding
Interpretation of infinity, NaN, and zero bit patterns.
A positive bias makes
exponentBiasthe normal encoding of one.The normal encoding of one is finite and fits in the exponent field.
Instances For
Instances For
Instances For
Format construction #
The conventional IEEE exponent bias determined by the exponent width.
Instances For
Default exponent bias for a configured encoding.
IEEE, finite-max-NaN, and signed-zero finite formats use the conventional IEEE bias. The
finite-unsigned-zero encoding shifts the bias by one, matching its reuse of negative zero as NaN.
Callers can still supply any other validated bias through FloatFormat.custom.
Instances For
The conventional IEEE bias fits below the largest finite IEEE exponent encoding.
The conventional IEEE bias fits below the largest finite-only exponent encoding.
The shifted FNUZ bias fits below the largest finite exponent encoding.
Construct a validated custom binary format.
For literal widths and biases, Lean discharges the four side conditions with by decide.
Instances For
Give an arbitrary layout its ordinary IEEE bias and exceptional-value interpretation.
Instances For
OCP E4M3-style format with no infinity and one maximum-magnitude NaN per sign.
Instances For
FNUZ format with bias one above the IEEE bias and negative zero reused as NaN.
Instances For
Finite-only binary format in which no bit pattern denotes infinity or NaN.
Instances For
Total storage width: sign + exponent + fraction.
Instances For
The corresponding format used by Lean's logical floating-point model.
Instances For
The declared exponent bias fits in the stored exponent field.
Recover an executable IEEE format descriptor from Lean's logical format descriptor.
Both descriptors require at least two exponent bits; fmt.he supplies the explicit width proof.
The conversion assigns the conventional IEEE bias and exceptional-value interpretation.
Instances For
Converting a Lean logical format to an executable descriptor and back is exact.
Converting through Lean's logical model retains the widths and produces the corresponding conventional IEEE descriptor.
Lean's logical model and the executable descriptor assign the same total storage width.
Raw storage has precisely the width prescribed by the format.