TorchLean API

NN.Floats.IEEEExec.Exec32.Dyadic

Dyadic helpers for executable IEEE32 arithmetic.

This file contains the integer/rational scaffolding used to describe binary significands, exponents, and exact dyadic values before rounding to IEEE32.

Exact dyadic value (-1)^sign * mant * 2^exp used as an intermediate for finite ops.

Instances For
    @[inline]

    2^k as a natural number.

    Instances For
      @[inline]

      Round n / 2^shift to nearest, ties-to-even.

      This is the primitive "shift + rounding" operation we use when shrinking a mantissa back down to a fixed bit width. It is the same tie-breaking policy as IEEE round-to-nearest-even.

      Instances For
        @[inline]

        Construct a raw binary32 bit-pattern from fields.

        mkBits sign exp frac places the low bits of each supplied field:

        • sign in bit 31,
        • the low 8 bits of exp in bits 30..23,
        • frac in bits 22..0 (masked to 23 bits).
        Instances For
          @[inline]

          Decode a finite binary32 into an exact dyadic value.

          Returns none for NaN/Inf.

          Instances For

            Convert an exact dyadic value to its exact rational value.

            Instances For

              Exact rational value of a finite binary32 value; returns none for NaN or infinity.

              Instances For

                If toDyadic? x = some d then x is not a NaN.

                Informal: toDyadic? only returns some _ for finite floats; NaNs map to none.

                If toDyadic? x = some d then x is not an infinity.

                Informal: toDyadic? only returns some _ for finite floats; infinities map to none.

                A successful dyadic decoding certifies that the source bit pattern is finite.

                The extracted binary32 fraction field fits in its 23-bit storage width.

                A finite binary32 value has a biased exponent field at most 254.

                Dyadic decoding preserves the sign bit of every finite executable value.

                @[simp]

                NaNs have no finite dyadic decoding.

                @[simp]

                Infinities have no finite dyadic decoding.

                A value that is neither NaN nor infinity has an exact dyadic decoding.

                Every finite binary32 value has an exact dyadic decoding.

                Dyadic decoding succeeds exactly on finite executable binary32 values.

                @[simp]

                Both signed zeros decode to the zero dyadic while retaining their sign bit.

                A decoded dyadic has zero mantissa only when the source bit pattern is a signed zero.

                Rounding back to binary32 #

                The general pattern for the finite ops in this file is:

                1. decode float32(s) to an exact intermediate representation (Dyadic for + - * fma sqrt, rationals for /),
                2. compute the exact result in that intermediate representation,
                3. round once to float32 using round-to-nearest, ties-to-even.
                @[inline]

                Round an exact dyadic value to binary32 (ties-to-even).

                This function implements:

                • overflow to ±Inf,
                • gradual underflow into subnormals (down to exponent -149),
                • underflow-to-zero below 2^-150 (half the minimum subnormal, where ties-to-even chooses 0),
                • mantissa rounding to the 24-bit precision of binary32 normal numbers.
                Instances For

                  Exact dyadic arithmetic (finite core) #

                  Dyadic is closed under + and * (at the exact level). We use these helpers before rounding back to float32.

                  @[inline]

                  Exact dyadic addition.

                  We align exponents by shifting the mantissa of the operand with the larger exponent, add signed integers, and then return an exact dyadic (no rounding yet).

                  Instances For

                    Exact rationals for division #

                    For division we compute an exact rational num/den (with den > 0) and then round it to binary32 using round-to-nearest, ties-to-even.

                    @[inline]

                    Round num/den to nearest, ties-to-even (assumes den > 0).

                    Instances For
                      @[inline]

                      Test whether num/den < 2^k without converting to reals.

                      Instances For
                        @[inline]

                        Test whether num/den ≥ 2^k without converting to reals.

                        Instances For
                          @[inline]

                          Compute ⌊log₂(num/den)⌋ as an Int (assumes num > 0 and den > 0).

                          We start from the initial estimate log2(num) - log2(den) and then adjust by checking against powers of two.

                          Instances For
                            @[inline]

                            Round an exact rational num/den to binary32 (ties-to-even).

                            This is the division analogue of roundDyadicToIEEE32: it uses the same exponent thresholds and the same final mantissa rounding policy.

                            Instances For