Correctness of configured bounded fixed-point operations #
Complete word conversions recover the stored code, and encoding preserves in-range words.
Arithmetic with (overflow := .wrap) decodes to centered modular arithmetic on signed
coefficients. The default .reject addition and subtraction are exact whenever they succeed, and
so is multiplication at a positive destination width. At positive widths,
(overflow := .saturate) decodes to the exact coefficient clamped to the signed range.
Unwrapping a freshly wrapped bounded fixed-point code returns the original code.
Rewrapping the code of a bounded fixed-point value returns the original value.
Reconstructing a value from its complete word preserves the value.
An in-range word is unchanged by bounded fixed-point encoding and decoding.
Constructing from an integer stores its two's-complement residue at the selected width.
Addition with .wrap decodes to the coefficient sum, centered modulo 2 ^ width.
Subtraction with .wrap decodes to the coefficient difference, centered modulo 2 ^ width.
Multiplication with .wrap decodes to the coefficient product, centered mod 2 ^ outWidth.
Addition succeeds with the wrapped sum when the exact coefficient sum fits.
Subtraction succeeds with the wrapped difference when the exact coefficient fits.
Multiplication succeeds with the encoded product when the exact coefficient fits.
Rejecting addition is exact: whenever it returns a value, that value decodes to the rational sum of the operands. No width hypothesis is needed because success already certifies the absence of signed overflow.
Rejecting subtraction is exact: whenever it returns a value, that value decodes to the rational difference of the operands.
Rejecting multiplication is exact: whenever it returns a value at a positive destination width, that value decodes to the rational product of the operands at the composed scale.
Addition with .saturate decodes to exact coefficient addition followed by clamping.
Subtraction with .saturate decodes to exact coefficient subtraction and clamping.
Multiplication with .saturate decodes to the clamped exact product at the composed scale.