TorchLean API

FloatLib.Floats.Formats.P3109.Projection.Runtime

P3109 projection #

P3109 projection has three steps:

  1. round finite dyadics to the descriptor's precision, preserve infinities, and map exceptions to NaN;
  2. apply the selected saturation rule at the descriptor's finite endpoints;
  3. encode the resulting datum.

This module follows that order. All integer widths, exponent bounds, and special codes come from Format, with no named-width branches or host floating-point conversions.

The stochastic modes receive a caller-supplied BitVec, whose width enforces the random-word range.

Reference #

Exact random bits supplied to one P3109 stochastic-rounding decision.

  • width : ℕ

    Number of random bits (N in the report).

  • bits : BitVec self.width

    Unsigned random integer R, with 0 ≤ R < 2^N by its type.

Instances For
    @[inline]

    Construct a supplied random word from the low width bits of a natural number.

    Instances For
      @[inline]

      Read the supplied random bits as an unsigned natural number.

      Instances For

        Rounding modes defined by P3109 Section 4.7.4.

        The five modes shared with IEEE 754 use the names of the binary IEEERoundingMode. The report's names are TowardZero, TowardPositive, TowardNegative, NearestTiesToAway, and NearestTiesToEven.

        • towardZero : RoundingMode

          TowardZero: the neighbour of smaller magnitude.

        • up : RoundingMode

          TowardPositive: the neighbour toward positive infinity.

        • down : RoundingMode

          TowardNegative: the neighbour toward negative infinity.

        • nearestAway : RoundingMode

          NearestTiesToAway: the nearer neighbour, ties to the larger magnitude.

        • nearest : RoundingMode

          NearestTiesToEven: the nearer neighbour, ties to the candidate with an even code.

        • toOdd : RoundingMode

          ToOdd: an inexact value takes the neighbour with an odd code.

        • stochasticA (random : RandomBits) : RoundingMode

          StochasticA: round away when the scaled fraction plus random reaches 2^N.

        • stochasticB (random : RandomBits) : RoundingMode

          StochasticB: as stochasticA with one more fraction bit and an offset of one half.

        • stochasticC (random : RandomBits) : RoundingMode

          StochasticC: as stochasticA with the scaled fraction rounded to nearest-even.

        Instances For

          Saturation modes defined by P3109 Section 4.7.5.

          • finite : SaturationMode

            SatFinite: clamp every out-of-range result, including infinities, to a finite endpoint.

          • propagate : SaturationMode

            SatPropagate: clamp finite overflow but keep an infinity the format can represent.

          • none : SaturationMode

            SatNone: overflow to infinity, or to NaN where the format has no infinity. A directed mode that rounds toward the finite range still returns its endpoint.

          Instances For

            Complete policy of one P3109 projection.

            The default {} is nearest-even rounding with no saturation request. Executable operations take the two fields as their mode and saturation options. This record is the conversion context of P3109 destinations and the parameter of the specification functions such as projectValue.

            • rounding : RoundingMode

              Rule used when precision rounding discards nonzero low bits.

            • saturation : SaturationMode

              Rule used after rounding produces a value outside the target datum set.

            Instances For
              @[inline]

              Largest finite positive code point for this descriptor.

              Instances For
                @[inline]

                Exact largest finite value in this descriptor.

                Instances For
                  @[inline]

                  Exact smallest finite value in this descriptor.

                  Instances For
                    @[inline]
                    def FloatLib.Floats.Formats.P3109.Format.lowerCodeIsEven (format : Format) (quantumExponent : ℤ) (lowerSignificand : ℕ) :

                    Whether the lower candidate has an even P3109 code at a rounding boundary.

                    For P > 1, code parity is ordinary significand parity. The P = 1 branch is the special rule from Section 4.7.4: zero is even, and a nonzero code is even exactly when Q + B is even.

                    Instances For
                      @[inline]

                      Floor of (remainder / 2^discardedBits) * 2^outputBits.

                      Instances For
                        @[inline]

                        Nearest-even integer to (remainder / 2^discardedBits) * 2^outputBits.

                        Instances For
                          @[inline]
                          def FloatLib.Floats.Formats.P3109.Format.Internal.roundAway (format : Format) (mode : RoundingMode) (negative : Bool) (quantumExponent : ℤ) (lower remainder discardedBits : ℕ) :

                          Decide whether precision rounding selects the integer above the floor candidate.

                          Instances For
                            @[inline]

                            Round one exact finite dyadic to P3109 precision before saturation.

                            The quantum is max(floor(log2 |X|), 1 - B) - P + 1, so the same implementation handles normals, subnormals, P = 1, and arbitrary descriptor widths.

                            Instances For
                              @[inline]

                              Apply P3109 precision rounding while preserving infinities and mapping exceptions to NaN.

                              Instances For
                                @[inline]

                                Saturate a rounded value below the finite range.

                                Instances For
                                  @[inline]

                                  Saturate a rounded value above the finite range.

                                  Instances For
                                    @[inline]

                                    Apply P3109 Section 4.7.5 to a value that has already been rounded to precision.

                                    Instances For
                                      @[inline]

                                      Mathematical result of P3109 projection before encoding: precision rounding, then saturation.

                                      Instances For
                                        @[inline]

                                        Encode a positive finite datum.

                                        The caller supplies a value already rounded to precision and inside the finite range. This is the integer form of Section 4.7.6 and does not enumerate the codebook.

                                        Instances For
                                          @[inline]

                                          Encode a value known to be in this descriptor's datum set.

                                          Instances For

                                            P3109 equality of datum meanings.

                                            Finite values are equal numerically, infinity signs must match, and every NaN payload denotes the format's single NaN datum. Other exceptional categories are not P3109 datums.

                                            Instances For
                                              @[inline]

                                              Executable equality of P3109 datum meanings.

                                              Finite dyadics are compared numerically, so harmless differences in significand normalization do not make a representable value fail the checked encoder. P3109 has one NaN, so its optional payload is not semantically observable here.

                                              Instances For
                                                @[inline]

                                                Encode a value only when it belongs to this descriptor's datum set.

                                                There is no rounding or saturation. The operation computes the P3109 code, decodes it, and accepts the code only when the decoded datum has the same P3109 numerical meaning as value (SameDatum).

                                                Instances For
                                                  @[inline]

                                                  Project one exact closed value and return its descriptor-width code.

                                                  Instances For
                                                    @[inline]

                                                    Project an exact finite, infinite, or NaN value into a P3109 descriptor.

                                                    The operation rounds once to P3109 precision in mode, applies saturation, and encodes the result, never going through IEEE 754 or a host float.

                                                    Instances For
                                                      @[inline]

                                                      Project one exact finite dyadic into a P3109 descriptor, with the options of project.

                                                      Instances For
                                                        @[inline]

                                                        Encode an already representable P3109 datum without rounding it.

                                                        Instances For