TorchLean API

FloatLib.Floats.Formats.BinaryInterchange.Dyadic.Core

Exact dyadic decoding primitives #

Finite binary interchange values decode exactly to a sign, natural significand, and integral power-of-two exponent. This module defines that bridge and the exact comparison used by arithmetic, conversions, and rounding.

The decoding layer depends on no arithmetic operation, so addition, multiplication, FMA, conversion, and directed rounding share the same exact domain and each prove their own final packing step.

@[inline]

Exact negation with the zero convention selected by the destination format.

Instances For

    Exact dyadic value represented by a finite value in Lean's logical float model.

    Instances For
      @[inline]

      2^k as a Nat.

      Instances For

        The executable shift definition of a power of two agrees with natural exponentiation.

        Exact comparison of two dyadics by aligning their exponents and comparing signed mantissas.

        The alignment shift is the full exponent difference, so this form is only cheap when the exponents are close. cmpDyadic decides every other case first and uses this form only when the leading bit positions coincide.

        Instances For

          Exact comparison of two dyadics.

          Zero operands and opposite signs are decided directly. For nonzero operands of one sign, the leading bit positions log2 significand + exponent decide the magnitude order unless they coincide, and only then are the significands aligned. The alignment shift is therefore bounded by the significand widths, not by the distance between the exponents.

          This lives with the decoding primitives because finite-value comparison does not depend on any rounding operation.

          Instances For

            The leading-position comparison agrees with exponent alignment for every operand pair.

            Exchanging the operands of the exact comparison reverses its result.

            Both operands are aligned to the same smaller exponent whichever comes first, so the aligned form reduces this to Int.compare_swap.

            @[inline]

            Decode an IEEE bit pattern into an exact dyadic.

            • NaN / Inf → none
            • ±0 → mant = 0, exp = 0 (sign preserved)
            • subnormal → mant = frac, exp = ieeeMinSubnormalExponent fmt
            • normal → mant = 2^fracWidth + frac, exp = e_biased - (bias + fracWidth)

            Every threshold is derived from fmt, and no fixed-width constants are used.

            Instances For
              @[inline]

              Compiled IEEE finite decoder.

              The storage word is converted to Nat once, then all three fields are extracted from that shared value. The exponent field alone determines whether an IEEE encoding is exceptional, so the decoder also avoids the repeated masks and shifts performed by IEEE.isNaN and IEEE.isInf.

              Instances For
                @[csimp]

                The compiler uses the single-pass field decoder while proofs keep ieeeToDyadic?.