Laws and correctness of shared fixed-carrier guard-and-sticky rounding #
LawfulTailCarrier and LawfulCandidateCarrier state the natural-number meaning of the carrier
operations that GuardStickyCarrier uses. Each law is as narrow as the shared kernel needs: the
arithmetic laws hold only under the bounds the kernel establishes, so wrapping machine operations
satisfy them. Concrete UInt64 and UInt128 records are proved lawful beside their kernels.
Given a lawful carrier of capacity bits and a format whose payload is shorter than capacity,
the theorems below show that the shared kernel is an execution refinement of the arbitrary-width
direct rounder: tailBit_eq_spec and tailHasNonzeroAfter_eq_spec for inspection,
lowerCandidateFromFields_toNat for packing, and roundNormalizedPositive_toNat_eq_direct for
the complete positive rounder against DirectDyadicPacking.roundPositiveCode.
Natural-number semantics of a finite-tail carrier.
This explicit record lives in Prop; the executable carrier contains only the operations.
Carrier bit reads agree with natural-number bit reads.
- hasLowBits_eq (value : α) (width : ℕ) : carrier.hasLowBits value width = (toNat value % 2 ^ width != 0)
Carrier suffix tests are natural-number remainder tests.
Instances For
Natural-number semantics of a candidate carrier of capacity bits.
Each law holds only under the bound the shared rounder establishes before applying the operation,
so the record can be instantiated by wrapping machine arithmetic. Like LawfulTailCarrier, it is
an explicit proof record rather than a typeclass.
- hasLowBits_eq (value : α) (width : ℕ) : carrier.hasLowBits value width = (toNat value % 2 ^ width != 0)
Every carrier value is below
2 ^ capacity.Embedding a machine word is exact.
- shiftLeft_toNat (value : α) (shift : ℕ) : shift < capacity → toNat value <<< shift < 2 ^ capacity → toNat (carrier.shiftLeft value shift) = toNat value <<< shift
A left shift below the carrier width whose result fits is exact.
- shiftRight_toNat (value : α) (shift : ℕ) : shift < capacity → toNat (carrier.shiftRight value shift) = toNat value >>> shift
A right shift below the carrier width is exact.
- fractionBelow_toNat (value : α) (leading : ℕ) : leading < capacity → 2 ^ leading ≤ toNat value → toNat value < 2 ^ (leading + 1) → toNat (carrier.fractionBelow value leading) = toNat value - 2 ^ leading
Removing the leading one of a normalized significand subtracts its power of two.
- add_toNat (left right : α) : toNat left + toNat right < 2 ^ capacity → toNat (carrier.add left right) = toNat left + toNat right
Addition is exact when the mathematical sum fits the carrier.
The low-ones mask below the carrier width has its mathematical value.
- increment_toNat (value : α) : toNat value + 1 < 2 ^ capacity → toNat (carrier.increment value) = toNat value + 1
The successor is exact when it fits the carrier.
The parity test reads the least significant bit.
The leading-bit index is the natural-number logarithm.
Instances For
Shared carrier bit inspection reads the representation-independent normalized tail stream.
Shared carrier suffix inspection is the representation-independent sticky-bit query.
The direct rounder's leading-bit underflow test is exact comparison against the smallest positive posit. This fact is independent of the native carrier used by a backend.
Storing the two-bit exponent field in a machine word is exact.
Packed field refinement #
The shared fraction-prefix kernel implements the representation-independent normalized prefix.
The shared exponent/fraction prefix is the representation-independent tail prefix.
Shared field packing implements the representation-independent lower-candidate constructor whenever the format's payload is shorter than the carrier.
Rounding #
The shared interior rounder implements the field-oriented nearest-even rule.
The theorem covers packed field construction, guard/sticky inspection, retained parity, and the increment, whose result fits the carrier.
The shared normalized rounder is the arbitrary-width direct positive rounder.
The hypotheses are the checks a caller performs before entering the carrier: the significand is
nonzero and the value is not below minPos. The payload bound is what makes every intermediate
fit the carrier.