TorchLean API

FloatLib.Floats.Formats.Posit.Arithmetic.Shared.GuardSticky.Runtime

Shared fixed-carrier guard-and-sticky Posit rounding #

One-word and two-limb posit kernels round the same normalized exponent/fraction stream. They differ only in the carrier that holds the significand and the packed code, so everything from tail inspection through candidate packing to the round-to-nearest-even decision is written once here and instantiated by each carrier.

Both records are passed explicitly to always-inlined functions. The UInt64 and UInt128 instances are transparent constants, allowing specialization to their primitive operations. The sibling proof module states the natural-number meaning of every operation (LawfulTailCarrier, LawfulCandidateCarrier) and proves the shared kernel against the arbitrary-width rounder DirectDyadicPacking.roundPositiveCode.

The two exponent bits are always stored in UInt64, so this module reads them through neutral fixed-word primitives and depends on neither posit storage backend. Underflow detection, the zero test, and sign restoration stay in the specialized kernels: they use carrier-specific comparison and complement primitives with their own contracts.

Operations needed to inspect the finite tail of a packed significand.

The structure contains executable data only. LawfulTailCarrier in the sibling proof module states how these operations represent natural-number bit streams.

  • bitAt : αBool

    Read one bit using least-significant-bit numbering.

  • hasLowBits : αBool

    Test whether any of the low width bits is set.

Instances For

    Operations needed to pack and round a positive normalized posit candidate inside one fixed-width carrier.

    Every operation is total. Its natural-number meaning, stated by LawfulCandidateCarrier, is guaranteed only under the bounds the shared rounder establishes: add and increment are exact when the mathematical result fits the carrier, shiftLeft when the shifted value fits, lowOnes below the carrier width, and fractionBelow on a significand whose leading one sits at the given index. The structure contains executable data only.

    • bitAt : αBool
    • hasLowBits : αBool
    • ofWord : UInt64α

      Embed a machine word; used for the two-bit exponent field and the constants zero and one.

    • shiftLeft : αα

      Shift left, returning zero once the shift reaches the carrier width.

    • shiftRight : αα

      Shift right, returning zero once the shift reaches the carrier width.

    • fractionBelow : αα

      Remove the leading one, at the given bit index, from a normalized significand.

    • add : ααα

      Addition; exact whenever the mathematical sum fits the carrier.

    • lowOnes : α

      A mask whose low width bits are one.

    • increment : αα

      Successor; exact whenever it fits the carrier.

    • isOdd : αBool

      Whether the least significant bit is set.

    • log2 : α

      Index of the leading one of a nonzero value.

    Instances For
      @[inline]
      def FloatLib.Floats.Formats.Posit.Model.GuardStickyCarrier.tailBit {α : Type} (carrier : TailCarrier α) (exponentField : UInt64) (significand : α) (leading index : ) :

      Read one bit of the two-bit exponent followed by the finite fraction.

      Instances For
        @[inline]
        def FloatLib.Floats.Formats.Posit.Model.GuardStickyCarrier.tailHasNonzeroAfter {α : Type} (carrier : TailCarrier α) (exponentField : UInt64) (significand : α) (leading consumed : ) :

        Test whether the exact exponent/fraction suffix after consumed contains a one.

        Instances For
          @[inline]
          def FloatLib.Floats.Formats.Posit.Model.GuardStickyCarrier.fractionPrefix {α : Type} (carrier : CandidateCarrier α) (significand : α) (leading count : ) :
          α

          First count normalized fraction bits after the leading one at index leading.

          Instances For
            @[inline]
            def FloatLib.Floats.Formats.Posit.Model.GuardStickyCarrier.tailPrefix {α : Type} (carrier : CandidateCarrier α) (exponentField : UInt64) (significand : α) (leading count : ) :
            α

            Prefix of the standard two-bit exponent followed by the normalized fraction.

            Instances For
              @[inline]
              def FloatLib.Floats.Formats.Posit.Model.GuardStickyCarrier.lowerCandidateFromFields {α : Type} (carrier : CandidateCarrier α) (format : Format) (regime : ) (exponentField : UInt64) (significand : α) (leading : ) :
              α

              Pack normalized regime, exponent, and significand fields into the unsigned lower posit candidate.

              Regimes that consume the whole payload saturate to maxPos or truncate to zero. Interior regimes place the run, its terminator, and the retained tail prefix.

              Instances For
                @[inline]
                def FloatLib.Floats.Formats.Posit.Model.GuardStickyCarrier.roundInterior {α : Type} (carrier : CandidateCarrier α) (format : Format) (regime : ) (exponentField : UInt64) (significand : α) (leading regimeFieldBits : ) :
                α

                Round an interior normalized target to nearest even from its retained prefix, guard bit, and sticky suffix.

                Instances For
                  @[inline]
                  def FloatLib.Floats.Formats.Posit.Model.GuardStickyCarrier.roundNormalizedPositive {α : Type} (carrier : CandidateCarrier α) (format : Format) (significand : α) (exponent : ) :
                  α

                  Round the nonzero positive value significand * 2 ^ exponent, known not to underflow, to its unsigned posit code.

                  Regimes that consume the payload map directly to maxPos or minPos; interior values use the field-oriented guard/sticky rule. For a lawful carrier whose capacity is strictly greater than the format's payload width, the result is the carrier image of DirectDyadicPacking.roundPositiveCode (roundNormalizedPositive_toNat_eq_direct).

                  Instances For