TorchLean API

FloatLib.Floats.Formats.BinaryInterchange.Format.Definition

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:

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
    @[inline]

    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.

      @[instance_reducible, inline]

      Equality of encodings is decidable; the instance is inlined so literal comparisons fold.

      @[inline]

      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.

        • expWidth_ge_two : 2 self.expWidth

          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).

        • fracWidth_pos : 0 < self.fracWidth

          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.

        • exponentBias_pos : 0 < self.exponentBias

          A positive bias makes exponentBias the normal encoding of one.

        • exponentBias_le_maxFinite : self.exponentBias self.encoding.maxFiniteExponent self.expWidth

          The normal encoding of one is finite and fits in the exponent field.

        Instances For

          Format construction #

          @[inline]

          The conventional IEEE exponent bias determined by the exponent width.

          Instances For
            @[inline]

            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
              theorem FloatLib.Floats.Formats.BinaryInterchange.FloatFormat.ieeeBias_pos (expWidth : Nat) (h : 2 expWidth) :
              0 < ieeeBias expWidth

              The conventional IEEE bias is positive when the exponent field has at least two bits.

              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.

              @[inline]
              def FloatLib.Floats.Formats.BinaryInterchange.FloatFormat.custom (expWidth fracWidth exponentBias : Nat) (encoding : Encoding) (expWidth_ge_two : 2 expWidth := by decide) (fracWidth_pos : 0 < fracWidth := by decide) (exponentBias_pos : 0 < exponentBias := by decide) (exponentBias_le_maxFinite : exponentBias encoding.maxFiniteExponent expWidth := by decide) :

              Construct a validated custom binary format.

              For literal widths and biases, Lean discharges the four side conditions with by decide.

              Instances For
                @[inline]
                def FloatLib.Floats.Formats.BinaryInterchange.FloatFormat.ieee (expWidth fracWidth : Nat) (expWidth_ge_two : 2 expWidth := by decide) (fracWidth_pos : 0 < fracWidth := by decide) :

                Give an arbitrary layout its ordinary IEEE bias and exceptional-value interpretation.

                Instances For
                  @[inline]
                  def FloatLib.Floats.Formats.BinaryInterchange.FloatFormat.finiteMaxNaN (expWidth fracWidth : Nat) (expWidth_ge_two : 2 expWidth := by decide) (fracWidth_pos : 0 < fracWidth := by decide) :

                  OCP E4M3-style format with no infinity and one maximum-magnitude NaN per sign.

                  Instances For
                    @[inline]
                    def FloatLib.Floats.Formats.BinaryInterchange.FloatFormat.finiteUnsignedZero (expWidth fracWidth : Nat) (expWidth_ge_two : 2 expWidth := by decide) (fracWidth_pos : 0 < fracWidth := by decide) :

                    FNUZ format with bias one above the IEEE bias and negative zero reused as NaN.

                    Instances For
                      @[inline]
                      def FloatLib.Floats.Formats.BinaryInterchange.FloatFormat.finite (expWidth fracWidth : Nat) (expWidth_ge_two : 2 expWidth := by decide) (fracWidth_pos : 0 < fracWidth := by decide) :

                      Finite-only binary format in which no bit pattern denotes infinity or NaN.

                      Instances For
                        @[inline]

                        Total storage width: sign + exponent + fraction.

                        Instances For
                          @[inline]

                          The corresponding format used by Lean's logical floating-point model.

                          Instances For

                            The declared exponent bias fits in the stored exponent field.

                            @[inline]

                            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
                              @[simp]

                              Converting a Lean logical format to an executable descriptor and back is exact.

                              @[simp]

                              Converting through Lean's logical model retains the widths and produces the corresponding conventional IEEE descriptor.

                              @[simp]

                              Lean's logical model and the executable descriptor assign the same total storage width.

                              @[reducible, inline]

                              Raw storage has precisely the width prescribed by the format.

                              Instances For