TorchLean API

FloatLib.Floats.Formats.BinaryInterchange.Conversion.Cast.Proof

Correctness of binary format casts #

A finite cast decodes the source exactly and applies one nearest-even rounding step in the destination format. The real-valued rounding bridge assumes conventional IEEE descriptors and a finite result, and it does not assign a real value to an infinity or NaN.

References #

The directed cast kernel computes cast in every rounding direction.

The status cast kernel is cast with status := true.

@[simp]
theorem FloatLib.Floats.Formats.BinaryInterchange.Model.value_cast (src dst : FloatFormat) (x : Model src) (mode : IEEERoundingMode) :
(cast src dst x mode true).value = cast src dst x mode

A cast with status := true delivers the value of the cast in the same direction.

@[simp]

Casting a finite value to its own format is the identity, signed zero included.

NaN propagation #

Setting the quiet bit of a word sets bit fracWidth - 1 of its fraction field.

Setting the quiet bit of a word leaves its exponent field unchanged.

Setting the quiet bit of a word leaves its sign bit unchanged.

The quiet bit of a word is clear exactly when bit fracWidth - 1 of its fraction is clear.

Rebuilding a NaN from its sign and IEEE payload gives the quieted NaN.

This is the same-descriptor case of IEEE 754-2019 §6.2.3, so cast could use propagatedNaN on every NaN. It keeps quietNaN on the same descriptor because that path avoids field packing.

A propagated NaN is a NaN in every destination that has a NaN encoding.

On a NaN source, cast in any rounding direction is propagatedNaN on the source sign and IEEE payload, for equal and unequal descriptors alike (IEEE 754-2019 §6.2.3).

Casting a NaN into a destination with no NaN encoding raises invalid in every rounding mode.

Such a destination has no NaN to deliver, so cast returns positive zero and reports the lost NaN through the invalid flag.

theorem FloatLib.Floats.Formats.BinaryInterchange.Model.signBit_widenExact {src dst : FloatFormat} (x : Model src) (hexp : src.expWidth = dst.expWidth) (hbias : src.exponentBias = dst.exponentBias) (hencoding : src.encoding = dst.encoding) (hfrac : src.fracWidth ≤ dst.fracWidth) :
(x.widenExact hexp hbias hencoding hfrac).signBit = x.signBit

Exact widening preserves the sign bit.

theorem FloatLib.Floats.Formats.BinaryInterchange.Model.expField_widenExact {src dst : FloatFormat} (x : Model src) (hexp : src.expWidth = dst.expWidth) (hbias : src.exponentBias = dst.exponentBias) (hencoding : src.encoding = dst.encoding) (hfrac : src.fracWidth ≤ dst.fracWidth) :
(x.widenExact hexp hbias hencoding hfrac).expField = x.expField

Exact widening preserves the biased exponent field.

theorem FloatLib.Floats.Formats.BinaryInterchange.Model.fracField_widenExact {src dst : FloatFormat} (x : Model src) (hexp : src.expWidth = dst.expWidth) (hbias : src.exponentBias = dst.exponentBias) (hencoding : src.encoding = dst.encoding) (hfrac : src.fracWidth ≤ dst.fracWidth) :
(x.widenExact hexp hbias hencoding hfrac).fracField = x.fracField.shiftLeft (dst.fracWidth - src.fracWidth)

Exact widening shifts the stored fraction into the high end of the destination field.

theorem FloatLib.Floats.Formats.BinaryInterchange.Model.isFinite_widenExact {src dst : FloatFormat} (x : Model src) (hexp : src.expWidth = dst.expWidth) (hbias : src.exponentBias = dst.exponentBias) (hencoding : src.encoding = dst.encoding) (hfrac : src.fracWidth ≤ dst.fracWidth) (hx : x.isFinite = true) :
(x.widenExact hexp hbias hencoding hfrac).isFinite = true

A finite value stays finite under exact widening.

theorem FloatLib.Floats.Formats.BinaryInterchange.Model.toReal_widenExact {src dst : FloatFormat} (x : Model src) (hexp : src.expWidth = dst.expWidth) (hbias : src.exponentBias = dst.exponentBias) (hencoding : src.encoding = dst.encoding) (hfrac : src.fracWidth ≤ dst.fracWidth) (hx : x.isFinite = true) :
(x.widenExact hexp hbias hencoding hfrac).toReal = x.toReal

Exact same-exponent widening preserves the decoded real value.

For a nonzero finite value, widening left-shifts the integer significand by dst.fracWidth - src.fracWidth and the destination scale loses the same number of powers of two, so the represented real number is unchanged.

A destination with at least as many fraction bits and no larger minimum subnormal exponent has a finer dyadic grid. This grid has no upper exponent bound.

Equal biases and increased precision preserve the entire rounded-real grid.

theorem FloatLib.Floats.Formats.BinaryInterchange.Model.cast_eq_widenExact_of_compatibleWidening {src dst : FloatFormat} (x : Model src) (hexp : src.expWidth = dst.expWidth) (hbias : src.exponentBias = dst.exponentBias) (hencoding : src.encoding = dst.encoding) (hfrac : src.fracWidth ≤ dst.fracWidth) (hx : x.isFinite = true) (mode : IEEERoundingMode := IEEERoundingMode.nearest) :
cast src dst x mode = x.widenExact hexp hbias hencoding hfrac

Compatible finite widening preserves the complete field encoding, including signed zero, in every rounding mode.

theorem FloatLib.Floats.Formats.BinaryInterchange.Model.cast_exact_of_compatibleWidening {src dst : FloatFormat} (x : Model src) (hexp : src.expWidth = dst.expWidth) (hbias : src.exponentBias = dst.exponentBias) (hencoding : src.encoding = dst.encoding) (hfrac : src.fracWidth ≤ dst.fracWidth) (hx : x.isFinite = true) :
toReal (cast src dst x) = x.toReal

Casting across compatible exponent semantics with at least as much precision is exact.

theorem FloatLib.Floats.Formats.BinaryInterchange.Model.cast_eq_roundAt {src dst : FloatFormat} (hsrc : src.isIEEE = true) (hdst : dst.isIEEE = true) (x : Model src) (hx : x.isFinite = true) (hxy : isFinite (cast src dst x) = true) :
toReal (cast src dst x) = roundAt dst x.toReal

For conventional IEEE source and destination formats, executable casting refines one nearest-even rounding step in the destination's real-valued model.

Casting any finite binary descriptor to an IEEE destination performs one nearest-even real rounding, provided the delivered result is finite. The source may use a custom bias or a finite-only encoding.

theorem FloatLib.Floats.Formats.BinaryInterchange.Model.cast_exact_of_gridExtension {src dst : FloatFormat} (x : Model src) (hsrc : src.isIEEE = true) (hdst : dst.isIEEE = true) (hfrac : src.fracWidth ≤ dst.fracWidth) (hmin : dst.minSubnormalExponent ≤ src.minSubnormalExponent) (hx : x.isFinite = true) (hxy : isFinite (cast src dst x) = true) :
toReal (cast src dst x) = x.toReal

Conventional IEEE casting into a finer dyadic grid is exact whenever the executable result is finite. This permits a wider exponent field and covers the usual exact standard-format widening conversions.

A finer destination grid preserves the real value of every finite source descriptor, including finite-only encodings. It needs a finite result because grid inclusion does not bound the destination's upper exponent range.