§ 7.3. Adder and Multiplier¶
The two basic operations, each rounded once by any of the five modes and raising the five flags. Both are generated from the format (E, F); the catalog holds FP16 and FP32.
| Component | Description | Structural | Behavioral |
|---|---|---|---|
FloatAdder<FMT> |
A + B or A − B | Yes | Yes (compiled gate list) |
FloatMultiplier<FMT> |
A × B | Yes | Yes (compiled gate list) |
Catalog: float.add.fp16, float.add.fp32, float.multiply.fp16,
float.multiply.fp32.
Common pins¶
| Pin | Direction | Width | Meaning |
|---|---|---|---|
A, B |
Input | 16 or 32 each | Operands |
SUB |
Input | 1 | Adder only: subtract B |
RM |
Input | 3 | Rounding mode (7.1) |
Y |
Output | 16 or 32 | Result |
FLAGS |
Output | 5 | NX, UF, OF, DZ, NV (bit 0 first); DZ is never raised here |
Both follow the reference's add, subtract and multiply exactly, special values and flags included (7.1).
7.3.1. FloatAdder<FMT>¶
Behavior¶
Y = A ± B rounded once. ∞ − ∞ is invalid (canonical NaN); any NaN gives the canonical NaN, invalid only for a signalling one. An exact zero sum is +0, or −0 when rounding down; (−0) + (−0) is −0. The sum of two floats is exact whenever it is tiny, so the adder never raises underflow.
C++ reference¶
#include "fp/FloatReference.hpp"
using namespace circuit::fp;
// 1.5 + 2.25 in FP16, to nearest even: 3.75, no flags.
Result r = add(FP16, 0x3e00, 0x4080, Rounding::NearestEven); // r.bits == 0x4380, r.flags == 0
Unknown inputs and timing¶
Four-state gates throughout; an unknown operand bit spreads through the comparison and the shifters, so most of the result goes unknown. The compact form is the same gate list and gives the same unknowns.
Implementation¶
Structural implementation¶
A single-path adder, in the order the data flows (P = F + 1):
- Unpack both operands: sign, exponent, fraction; the hidden bit is 1 unless the exponent field is 0 (a subnormal, whose exponent counts as 1); ±0, ±∞ and NaN are decoded.
- Larger magnitude first. One subtractor compares the two operands without their signs (exponent and fraction together, an integer compare); multiplexers put the larger one on the left.
- Align the smaller significand with an
AlignShifterby the exponent difference, keeping two guard bits below it and a sticky bit. - Add or subtract (signs equal or not) in one prefix adder, P + 4 bits wide: the sticky bit enters as the lowest bit, so a subtraction borrows correctly from below.
- Normalize: a carry out shifts right by one; otherwise a
LeadingZeroCounterand aNormalizeShiftershift left by the leading zeros, but never below the smallest exponent (a subnormal result stays subnormal). - Round with a
Rounder<P>and finish: the exponent field after a rounding carry, overflow to ∞ or to the largest finite value (by the mode), then the special values.
Massive cancellation (two nearly equal values) only happens when the exponents differ by at most one, so the sticky bit is 0 then and the left shift loses nothing; that is why two guard bits and one sticky bit are enough for correct rounding in every mode.
The parts at its top level, in that order (the shared steps are in
§ 7.2.5); the only loose gate is the XOR that
gives B the sign SUB asks for:
| Part | Type | Inputs | Outputs | What it does |
|---|---|---|---|---|
MODE, UNPACK_A, UNPACK_B |
shared | Rounding mode and step 1 | ||
ORDER |
FloatAdderOrder<FMT> |
A_MAGNITUDE, B_MAGNITUDE (E + F), the signs, A_EXPONENT, B_EXPONENT (E), A_SIGNIFICAND, B_SIGNIFICAND (P) |
LARGE_… and SMALL_… sign, exponent and significand |
Step 2: one subtractor on the magnitudes, then word selects |
SUM |
FloatAdderSum<FMT> |
the large and small sign, exponent and significand | SUM (P + 4), SUBTRACTING |
Steps 3–4: the exponent difference, an AlignShifter<P+2>, the conditional inversion (an XORGate word) and the prefix adder |
NORMALIZE |
FloatAdderNormalize<FMT> |
SUM, SUBTRACTING, LARGE_EXPONENT |
KEPT (P), GUARD, STICKY, FIELD (E + 2), ZERO_SUM |
Step 5: a LeadingZeroCounter<P+3>, the shift limit, a NormalizeShifter<P+3> or the right shift on a carry, and the exponent field |
SPECIALS |
FloatAdderSpecials |
the signs, A_/B_INFINITY, NAN, SIGNALLING, DOWN |
NAN, INVALID, INFINITY, INFINITY_SIGN, ZERO_SIGN |
NaN operands, ∞ − ∞ (invalid), the sign of ∞ and of an exact zero (−0 only when rounding down) |
RESULT |
FloatResult<FMT> |
Y, FLAGS |
Step 6 (shared) |
Behavioral implementation¶
The compact form is the adder's own compiled gate list.
Examples¶
FP16: an exact sum; ties to even and away; the three directed modes on a value just below 1; overflow to ∞ (RNE) and to the largest finite value (RTZ); x − x in RNE and RDN; signed zeros; subnormals; the special values.
All 21 recorded rows of FloatAdderFp16ScenarioTest, in checkpoint order. Outputs are test expectations verified against the simulation. Time is the absolute settled checkpoint time in simulator units, not milliseconds or CPU cycles.
Default scenario profile: fidelity-gate-v1 / structural (ed03f70afe360dba). Changing fidelity or the circuit can change these times.
| Checkpointzero-based number · timeline label | Timesimulator units | AInput · 16 bit |
BInput · 16 bit |
RMInput · 3 bit |
SUBInput · 1 bit |
FLAGSExpected output · 5 bit |
YExpected output · 16 bit |
|---|---|---|---|---|---|---|---|
#01-plus-2 | 69 | 0x3C00 |
0x4000 |
0x0 |
0 |
0x00 |
0x4200 |
#11.5-plus-2.25 | 103 | 0x3E00 |
0x4080 |
0x0 |
0 |
0x00 |
0x4380 |
#23-minus-5 | 177 | 0x4200 |
0x4500 |
0x0 |
1 |
0x00 |
0xC000 |
#3tie-rne | 232 | 0x3C00 |
0x1000 |
0x0 |
0 |
0x01 |
0x3C00 |
#4tie-rmm | 247 | 0x3C00 |
0x1000 |
0x4 |
0 |
0x01 |
0x3C01 |
#5above-tie-rne | 285 | 0x3C00 |
0x1200 |
0x0 |
0 |
0x01 |
0x3C01 |
#6below-1-rtz | 344 | 0x3C00 |
0x8C00 |
0x1 |
0 |
0x01 |
0x3BFF |
#7below-1-rdn | 367 | 0x3C00 |
0x8C00 |
0x2 |
0 |
0x01 |
0x3BFF |
#8below-1-rup | 386 | 0x3C00 |
0x8C00 |
0x3 |
0 |
0x01 |
0x3C00 |
#9overflow-rne | 450 | 0x7BFF |
0x7BFF |
0x0 |
0 |
0x05 |
0x7C00 |
#10overflow-rtz | 464 | 0x7BFF |
0x7BFF |
0x1 |
0 |
0x05 |
0x7BFF |
#11x-minus-x | 533 | 0x3D00 |
0x3D00 |
0x0 |
1 |
0x00 |
0x0000 |
#12x-minus-x-rdn | 544 | 0x3D00 |
0x3D00 |
0x2 |
1 |
0x00 |
0x8000 |
#13neg0-plus-neg0 | 587 | 0x8000 |
0x8000 |
0x0 |
0 |
0x00 |
0x8000 |
#14pos0-plus-neg0 | 627 | 0x0000 |
0x8000 |
0x0 |
0 |
0x00 |
0x0000 |
#15subnormals | 683 | 0x0001 |
0x0001 |
0x0 |
0 |
0x00 |
0x0002 |
#16normal-minus-subnormal | 763 | 0x0400 |
0x0001 |
0x0 |
1 |
0x00 |
0x03FF |
#17inf-plus-1 | 808 | 0x7C00 |
0x3C00 |
0x0 |
0 |
0x00 |
0x7C00 |
#18inf-minus-inf | 868 | 0x7C00 |
0x7C00 |
0x0 |
1 |
0x10 |
0x7E00 |
#19qnan-plus-1 | 920 | 0x7E00 |
0x3C00 |
0x0 |
0 |
0x00 |
0x7E00 |
#20snan-plus-1 | 938 | 0x7C01 |
0x3C00 |
0x0 |
0 |
0x10 |
0x7E00 |
Scroll within the table for all rows and pins. Buses use hexadecimal; values containing X/Z use MSB-first binary. Inputs show the declared values in effect at each checkpoint, including retained inputs. Sequential tests also replay preparation changes between checkpoints, such as lowering the clock. Use the matching checkpoint label in the simulation below.
The same rows at FP32:
All 21 recorded rows of FloatAdderFp32ScenarioTest, in checkpoint order. Outputs are test expectations verified against the simulation. Time is the absolute settled checkpoint time in simulator units, not milliseconds or CPU cycles.
Default scenario profile: fidelity-gate-v1 / structural (ed03f70afe360dba). Changing fidelity or the circuit can change these times.
| Checkpointzero-based number · timeline label | Timesimulator units | AInput · 32 bit |
BInput · 32 bit |
RMInput · 3 bit |
SUBInput · 1 bit |
FLAGSExpected output · 5 bit |
YExpected output · 32 bit |
|---|---|---|---|---|---|---|---|
#01-plus-2 | 71 | 0x3F800000 |
0x40000000 |
0x0 |
0 |
0x00 |
0x40400000 |
#11.5-plus-2.25 | 109 | 0x3FC00000 |
0x40100000 |
0x0 |
0 |
0x00 |
0x40700000 |
#23-minus-5 | 192 | 0x40400000 |
0x40A00000 |
0x0 |
1 |
0x00 |
0xC0000000 |
#3tie-rne | 260 | 0x3F800000 |
0x33800000 |
0x0 |
0 |
0x01 |
0x3F800000 |
#4tie-rmm | 275 | 0x3F800000 |
0x33800000 |
0x4 |
0 |
0x01 |
0x3F800001 |
#5above-tie-rne | 317 | 0x3F800000 |
0x33C00000 |
0x0 |
0 |
0x01 |
0x3F800001 |
#6below-1-rtz | 363 | 0x3F800000 |
0xB3000000 |
0x1 |
0 |
0x01 |
0x3F7FFFFF |
#7below-1-rdn | 386 | 0x3F800000 |
0xB3000000 |
0x2 |
0 |
0x01 |
0x3F7FFFFF |
#8below-1-rup | 405 | 0x3F800000 |
0xB3000000 |
0x3 |
0 |
0x01 |
0x3F800000 |
#9overflow-rne | 476 | 0x7F7FFFFF |
0x7F7FFFFF |
0x0 |
0 |
0x05 |
0x7F800000 |
#10overflow-rtz | 490 | 0x7F7FFFFF |
0x7F7FFFFF |
0x1 |
0 |
0x05 |
0x7F7FFFFF |
#11x-minus-x | 558 | 0x3FA00000 |
0x3FA00000 |
0x0 |
1 |
0x00 |
0x00000000 |
#12x-minus-x-rdn | 569 | 0x3FA00000 |
0x3FA00000 |
0x2 |
1 |
0x00 |
0x80000000 |
#13neg0-plus-neg0 | 614 | 0x80000000 |
0x80000000 |
0x0 |
0 |
0x00 |
0x80000000 |
#14pos0-plus-neg0 | 657 | 0x00000000 |
0x80000000 |
0x0 |
0 |
0x00 |
0x00000000 |
#15subnormals | 720 | 0x00000001 |
0x00000001 |
0x0 |
0 |
0x00 |
0x00000002 |
#16normal-minus-subnormal | 813 | 0x00800000 |
0x00000001 |
0x0 |
1 |
0x00 |
0x007FFFFF |
#17inf-plus-1 | 863 | 0x7F800000 |
0x3F800000 |
0x0 |
0 |
0x00 |
0x7F800000 |
#18inf-minus-inf | 931 | 0x7F800000 |
0x7F800000 |
0x0 |
1 |
0x10 |
0x7FC00000 |
#19qnan-plus-1 | 989 | 0x7FC00000 |
0x3F800000 |
0x0 |
0 |
0x00 |
0x7FC00000 |
#20snan-plus-1 | 1009 | 0x7F800001 |
0x3F800000 |
0x0 |
0 |
0x10 |
0x7FC00000 |
Scroll within the table for all rows and pins. Buses use hexadecimal; values containing X/Z use MSB-first binary. Inputs show the declared values in effect at each checkpoint, including retained inputs. Sequential tests also replay preparation changes between checkpoints, such as lowering the clock. Use the matching checkpoint label in the simulation below.
7.3.2. FloatMultiplier<FMT>¶
Behavior¶
Y = A × B rounded once. ∞ × 0 is invalid. Underflow is detected after rounding: a product just below the smallest normal that rounds up to it is not tiny (7.1).
Implementation¶
Structural implementation¶
- Unpack as the adder does; the sign is A's XOR B's.
- Significand product: the P × P product of the hidden-bit significands by the multiplier tree of § 6.1.1 (AND partial products, a Dadda tree of full and half adders, prefix adders), 2P bits.
- Exponent: T = (ea + eb − bias), a signed number; the result's exponent field is T + 1 − (leading zeros of the product).
- Normalize: left by the product's leading zeros when that keeps the exponent field at least 1; left by T when T ≥ 0 (the result is subnormal); right by −T with a sticky bit when T < 0 (far below the smallest exponent).
- Tininess after rounding: a second rounding decision, one place lower, tells whether a value just below the smallest normal would round up to it with an unlimited exponent range; if so, it is not tiny.
- Round and finish as the adder.
| Part | Type | Inputs | Outputs | What it does |
|---|---|---|---|---|
MODE, UNPACK_A, UNPACK_B |
shared | Rounding mode and step 1; the product's sign is one loose XOR | ||
PRODUCT |
SignificandMultiplier<P> |
A, B (P) |
PRODUCT (2P) |
Step 2 (§ 7.2.5) |
EXPONENT |
FloatMultiplierExponent<FMT> |
A_EXPONENT, B_EXPONENT (E) |
T (E + 2, signed) |
Step 3: T = ea + eb − bias |
NORMALIZE, ROUND_BITS |
shared | Steps 4–5 | ||
SPECIALS |
FloatMultiplierSpecials |
A_/B_ZERO, INFINITY, NAN, SIGNALLING |
NAN, INVALID, INFINITY, ZERO |
NaN operands, ∞ × 0 (invalid), an infinite or zero product |
RESULT |
FloatResult<FMT,tiny> |
Y, FLAGS |
Step 6 (shared) |
Behavioral implementation¶
The compact form is the multiplier's compiled gate list.
Examples¶
FP16: exact products, rounding in two modes, overflow (RNE to ∞, RDN to the largest finite value), a product that becomes subnormal, one that underflows with inexact, one that underflows to 0, a product that is not tiny after rounding, a subnormal times a large number, signed zero, ∞ × 2, ∞ × 0 and a signalling NaN.
All 15 recorded rows of FloatMultiplierFp16ScenarioTest, in checkpoint order. Outputs are test expectations verified against the simulation. Time is the absolute settled checkpoint time in simulator units, not milliseconds or CPU cycles.
Default scenario profile: fidelity-gate-v1 / structural (ed03f70afe360dba). Changing fidelity or the circuit can change these times.
| Checkpointzero-based number · timeline label | Timesimulator units | AInput · 16 bit |
BInput · 16 bit |
RMInput · 3 bit |
FLAGSExpected output · 5 bit |
YExpected output · 16 bit |
|---|---|---|---|---|---|---|
#01.5-times-2 | 63 | 0x3E00 |
0x4000 |
0x0 |
0x00 |
0x4200 |
#1neg3-times-0.5 | 115 | 0xC200 |
0x3800 |
0x0 |
0x00 |
0xBE00 |
#2rounded-rne | 171 | 0x3C01 |
0x3C01 |
0x0 |
0x01 |
0x3C02 |
#3rounded-rup | 187 | 0x3C01 |
0x3C01 |
0x3 |
0x01 |
0x3C03 |
#4overflow-rne | 255 | 0x7BFF |
0x4000 |
0x0 |
0x05 |
0x7C00 |
#5overflow-rdn | 270 | 0x7BFF |
0x4000 |
0x2 |
0x05 |
0x7BFF |
#6to-subnormal | 309 | 0x0400 |
0x3400 |
0x0 |
0x00 |
0x0100 |
#7underflow-inexact | 344 | 0x0400 |
0x3555 |
0x0 |
0x03 |
0x0155 |
#8underflow-to-0 | 381 | 0x0001 |
0x3400 |
0x0 |
0x03 |
0x0000 |
#9not-tiny-after-rounding | 441 | 0x07FF |
0x3801 |
0x3 |
0x01 |
0x0401 |
#10subnormal-times-big | 504 | 0x0001 |
0x7000 |
0x0 |
0x00 |
0x1000 |
#11zero-times-neg | 536 | 0x0000 |
0xC500 |
0x0 |
0x00 |
0x8000 |
#12inf-times-2 | 594 | 0x7C00 |
0x4000 |
0x0 |
0x00 |
0x7C00 |
#13inf-times-0 | 634 | 0x7C00 |
0x0000 |
0x0 |
0x10 |
0x7E00 |
#14snan-times-1 | 692 | 0x7C01 |
0x3C00 |
0x0 |
0x10 |
0x7E00 |
Scroll within the table for all rows and pins. Buses use hexadecimal; values containing X/Z use MSB-first binary. Inputs show the declared values in effect at each checkpoint, including retained inputs. Sequential tests also replay preparation changes between checkpoints, such as lowering the clock. Use the matching checkpoint label in the simulation below.
All 15 recorded rows of FloatMultiplierFp32ScenarioTest, in checkpoint order. Outputs are test expectations verified against the simulation. Time is the absolute settled checkpoint time in simulator units, not milliseconds or CPU cycles.
Default scenario profile: fidelity-gate-v1 / structural (ed03f70afe360dba). Changing fidelity or the circuit can change these times.
| Checkpointzero-based number · timeline label | Timesimulator units | AInput · 32 bit |
BInput · 32 bit |
RMInput · 3 bit |
FLAGSExpected output · 5 bit |
YExpected output · 32 bit |
|---|---|---|---|---|---|---|
#01.5-times-2 | 65 | 0x3FC00000 |
0x40000000 |
0x0 |
0x00 |
0x40400000 |
#1neg3-times-0.5 | 118 | 0xC0400000 |
0x3F000000 |
0x0 |
0x00 |
0xBFC00000 |
#2rounded-rne | 175 | 0x3F800001 |
0x3F800001 |
0x0 |
0x01 |
0x3F800002 |
#3rounded-rup | 191 | 0x3F800001 |
0x3F800001 |
0x3 |
0x01 |
0x3F800003 |
#4overflow-rne | 269 | 0x7F7FFFFF |
0x40000000 |
0x0 |
0x05 |
0x7F800000 |
#5overflow-rdn | 284 | 0x7F7FFFFF |
0x40000000 |
0x2 |
0x05 |
0x7F7FFFFF |
#6to-subnormal | 327 | 0x00800000 |
0x3E800000 |
0x0 |
0x00 |
0x00200000 |
#7underflow-inexact | 375 | 0x00800000 |
0x3EAAAAAB |
0x0 |
0x03 |
0x002AAAAB |
#8underflow-to-0 | 417 | 0x00000001 |
0x3E800000 |
0x0 |
0x03 |
0x00000000 |
#9not-tiny-after-rounding | 477 | 0x00FFFFFF |
0x3F000001 |
0x3 |
0x01 |
0x00800001 |
#10subnormal-times-big | 552 | 0x00000001 |
0x4C800000 |
0x0 |
0x00 |
0x02000000 |
#11zero-times-neg | 586 | 0x00000000 |
0xC0A00000 |
0x0 |
0x00 |
0x80000000 |
#12inf-times-2 | 646 | 0x7F800000 |
0x40000000 |
0x0 |
0x00 |
0x7F800000 |
#13inf-times-0 | 686 | 0x7F800000 |
0x00000000 |
0x0 |
0x10 |
0x7FC00000 |
#14snan-times-1 | 745 | 0x7F800001 |
0x3F800000 |
0x0 |
0x10 |
0x7FC00000 |
Scroll within the table for all rows and pins. Buses use hexadecimal; values containing X/Z use MSB-first binary. Inputs show the declared values in effect at each checkpoint, including retained inputs. Sequential tests also replay preparation changes between checkpoints, such as lowering the clock. Use the matching checkpoint label in the simulation below.
Source and tests¶
| Test | Coverage |
|---|---|
FloatAdderTest |
FP16 and FP32: every pair of special values (zeros, subnormals, normal boundaries, ±∞, quiet and signalling NaN, values around 1) in a random mode and with a random SUB, then random patterns biased toward cancellation, overflow and underflow in all eight RM codes; gates and compact form against the reference |
FloatMultiplierTest |
The same, with products near the smallest normal (tininess) |
…ScenarioTest |
The recorded rows above |