TorchLean API

FloatLib.Numerics.Representations.FixedInt.Semantics.Basic

Representation semantics of fixed-width signed integers #

The executable two's-complement carrier has a NumericalSystem interpretation. The representation laws characterize the signed interval, encoding and decoding, and the clamp used by saturating arithmetic.

Keeping this adapter separate from arithmetic is useful: generic operation contracts only need to know what a code denotes, while the implementation remains free to use BitVec overflow instructions and fixed-width storage.

Fixed-width integers interpreted as their signed two's-complement values.

Instances For
    @[reducible, inline]

    A fixed-width word with an erased proof of its signed value.

    Instances For
      @[simp]
      theorem FloatLib.Numerics.Representations.FixedInt.numericalSystem_represents_iff {width : } (code : FixedInt width) (value : ) :
      (numericalSystem width).Represents code value code.toInt = value

      Representation by a fixed-width integer is equality with its signed two's-complement value.

      @[simp]
      theorem FloatLib.Numerics.Representations.FixedInt.toInt_ofInt {width : } (value : ) :
      (ofInt value).toInt = value.bmod (2 ^ width)

      Converting an integer to fixed width denotes centered reduction modulo 2 ^ width.

      @[simp]
      theorem FloatLib.Numerics.Representations.FixedInt.ofInt_toInt {width : } (value : FixedInt width) :
      ofInt value.toInt = value

      Re-encoding the signed value of a fixed-width integer recovers its original bits.

      @[simp]

      The minimum fixed-width code denotes the signed lower bound.

      @[simp]

      The maximum fixed-width code denotes the signed upper bound.

      theorem FloatLib.Numerics.Representations.FixedInt.minValue_eq {width : } (hwidth : 0 < width) :
      minValue width = -2 ^ (width - 1)

      At positive width, the signed lower bound is -(2 ^ (width - 1)).

      The signed upper bound is 2 ^ (width - 1) - 1.

      At positive width, the signed storage interval is nonempty.

      theorem FloatLib.Numerics.Representations.FixedInt.clamp_inRange {width : } (hwidth : 0 < width) (value : ) :
      InRange width (clamp width value)

      Clamping at positive width always produces an in-range integer.

      theorem FloatLib.Numerics.Representations.FixedInt.clamp_eq_self {width : } {value : } (hvalue : InRange width value) :
      clamp width value = value

      Clamping fixes every integer already in the signed storage interval.

      theorem FloatLib.Numerics.Representations.FixedInt.toInt_ofInt_eq_self {width : } (hwidth : 0 < width) {value : } (hvalue : InRange width value) :
      (ofInt value).toInt = value

      Encoding an in-range integer at positive width preserves its exact signed value.

      @[simp]
      theorem FloatLib.Numerics.Representations.FixedInt.toInt_ofIntSaturating {width : } (hwidth : 0 < width) (value : ) :
      (ofIntSaturating value).toInt = clamp width value

      Saturating conversion denotes exact clamping to the signed storage interval.