TorchLean API

NN.Floats.IEEEExec.Exec32.Core

Executable IEEE-754 binary32 (IEEE32Exec) #

TorchLean uses two complementary ways to talk about "float32":

We implement IEEE32Exec as raw UInt32 bits and provide:

TorchLean's tensor specifications provide their Context IEEE32Exec adapter separately. The executable kernel itself has no dependency on tensors, models, or runtime code.

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.

The transcendental operations in this namespace use deterministic, pure Lean algorithms so examples do not delegate to the host math library. Their formulas, range reduction, and rounding path are fixed by the source. They are not claimed to be correctly rounded or to match a particular hardware or libm; each proved or tested accuracy statement remains an explicit contract.

References #

Executable IEEE-754 binary32 value, stored as raw bits.

Instances For
    @[inline]

    Wrap raw binary32 bits as an IEEE32Exec.

    Instances For
      @[inline]

      Extract the raw binary32 bits of an IEEE32Exec.

      Instances For
        @[implicit_reducible]

        Default inhabitant: all bits zero, i.e. +0.0.

        Binary32 bit layout #

        IEEE-754 binary32 is stored as:

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

                  True iff the sign bit (bit 31) is set.

                  Instances For
                    @[inline]

                    Extract the 8-bit exponent field (bits 30..23).

                    Instances For
                      @[inline]

                      Extract the 23-bit fraction field (bits 22..0).

                      Instances For
                        @[inline]

                        Predicate for NaN: exponent all ones and fraction nonzero.

                        Instances For
                          @[inline]

                          Predicate for quiet NaN (NaN with the quiet bit set).

                          Instances For
                            @[inline]

                            Predicate for signaling NaN (NaN with the quiet bit clear).

                            Instances For
                              @[inline]

                              Predicate for infinity: exponent all ones and fraction zero.

                              Instances For
                                @[inline]

                                Predicate for signed zero (both +0 and -0).

                                Instances For
                                  @[inline]

                                  Predicate for finiteness: exponent field is not all ones (excludes NaN/Inf).

                                  Instances For
                                    @[inline]

                                    +0.0 as an executable binary32 constant.

                                    Instances For
                                      @[inline]

                                      -0.0 as an executable binary32 constant.

                                      Instances For
                                        @[inline]

                                        +1.0 as an IEEE-754 binary32 constant.

                                        Instances For
                                          @[inline]

                                          -1.0 as an IEEE-754 binary32 constant.

                                          Instances For
                                            @[inline]

                                            +∞ as an executable binary32 constant.

                                            Instances For
                                              @[inline]

                                              -∞ as an executable binary32 constant.

                                              Instances For
                                                @[inline]

                                                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.

                                                  @[inline]

                                                  Quiet a NaN by setting the quiet bit (and leave non-NaNs unchanged).

                                                  Instances For
                                                    @[inline]

                                                    If x is a NaN, return it (quieted).

                                                    Instances For
                                                      @[inline]

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

                                                        Like chooseNaN2, but for ternary ops (used for fma).

                                                        Instances For

                                                          Adjacent floats (nextUp/nextDown) #

                                                          @[inline]

                                                          Smallest positive subnormal (bit pattern 0x00000001, value 2^-149).

                                                          Instances For
                                                            @[inline]

                                                            Smallest negative subnormal (bit pattern 0x80000001, value -2^-149).

                                                            Instances For
                                                              @[inline]

                                                              Largest finite positive float32 (bit pattern 0x7F7FFFFF).

                                                              Instances For
                                                                @[inline]

                                                                Largest finite negative float32 (bit pattern 0xFF7FFFFF).

                                                                Instances For
                                                                  @[inline]

                                                                  nextUp x is the next representable float32 strictly greater than x.

                                                                  IEEE-754 special cases:

                                                                  • NaN propagates (quieted).
                                                                  • nextUp (+∞) = +∞.
                                                                  • nextUp (-0) = +minSubnormal (since +0 is not strictly greater than -0).
                                                                  Instances For
                                                                    @[inline]

                                                                    nextDown x is the next representable float32 strictly less than x.

                                                                    IEEE-754 special cases:

                                                                    • NaN propagates (quieted).
                                                                    • nextDown (-∞) = -∞.
                                                                    • nextDown (+0) = -minSubnormal (since -0 is not strictly less than +0).
                                                                    Instances For
                                                                      @[inline]

                                                                      Flip only the sign bit.

                                                                      IEEE sign-bit operations do not quiet signaling NaNs: their exponent, payload, and quiet/signaling classification are preserved. Arithmetic operations quiet an sNaN when they consume it.

                                                                      Instances For
                                                                        @[inline]

                                                                        Clear only the sign bit, preserving every NaN payload bit and its quiet/signaling class.

                                                                        Instances For