Skip to content

§ 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):

  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.
  2. 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.
  3. Align the smaller significand with an AlignShifter by the exponent difference, keeping two guard bits below it and a sticky bit.
  4. 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.
  5. Normalize: a carry out shifts right by one; otherwise a LeadingZeroCounter and a NormalizeShifter shift left by the leading zeros, but never below the smallest exponent (a subnormal result stays subnormal).
  6. 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-269 0x3C00 0x4000 0x0 0 0x00 0x4200
#11.5-plus-2.25103 0x3E00 0x4080 0x0 0 0x00 0x4380
#23-minus-5177 0x4200 0x4500 0x0 1 0x00 0xC000
#3tie-rne232 0x3C00 0x1000 0x0 0 0x01 0x3C00
#4tie-rmm247 0x3C00 0x1000 0x4 0 0x01 0x3C01
#5above-tie-rne285 0x3C00 0x1200 0x0 0 0x01 0x3C01
#6below-1-rtz344 0x3C00 0x8C00 0x1 0 0x01 0x3BFF
#7below-1-rdn367 0x3C00 0x8C00 0x2 0 0x01 0x3BFF
#8below-1-rup386 0x3C00 0x8C00 0x3 0 0x01 0x3C00
#9overflow-rne450 0x7BFF 0x7BFF 0x0 0 0x05 0x7C00
#10overflow-rtz464 0x7BFF 0x7BFF 0x1 0 0x05 0x7BFF
#11x-minus-x533 0x3D00 0x3D00 0x0 1 0x00 0x0000
#12x-minus-x-rdn544 0x3D00 0x3D00 0x2 1 0x00 0x8000
#13neg0-plus-neg0587 0x8000 0x8000 0x0 0 0x00 0x8000
#14pos0-plus-neg0627 0x0000 0x8000 0x0 0 0x00 0x0000
#15subnormals683 0x0001 0x0001 0x0 0 0x00 0x0002
#16normal-minus-subnormal763 0x0400 0x0001 0x0 1 0x00 0x03FF
#17inf-plus-1808 0x7C00 0x3C00 0x0 0 0x00 0x7C00
#18inf-minus-inf868 0x7C00 0x7C00 0x0 1 0x10 0x7E00
#19qnan-plus-1920 0x7E00 0x3C00 0x0 0 0x00 0x7E00
#20snan-plus-1938 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.

Open the full visualizer

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-271 0x3F800000 0x40000000 0x0 0 0x00 0x40400000
#11.5-plus-2.25109 0x3FC00000 0x40100000 0x0 0 0x00 0x40700000
#23-minus-5192 0x40400000 0x40A00000 0x0 1 0x00 0xC0000000
#3tie-rne260 0x3F800000 0x33800000 0x0 0 0x01 0x3F800000
#4tie-rmm275 0x3F800000 0x33800000 0x4 0 0x01 0x3F800001
#5above-tie-rne317 0x3F800000 0x33C00000 0x0 0 0x01 0x3F800001
#6below-1-rtz363 0x3F800000 0xB3000000 0x1 0 0x01 0x3F7FFFFF
#7below-1-rdn386 0x3F800000 0xB3000000 0x2 0 0x01 0x3F7FFFFF
#8below-1-rup405 0x3F800000 0xB3000000 0x3 0 0x01 0x3F800000
#9overflow-rne476 0x7F7FFFFF 0x7F7FFFFF 0x0 0 0x05 0x7F800000
#10overflow-rtz490 0x7F7FFFFF 0x7F7FFFFF 0x1 0 0x05 0x7F7FFFFF
#11x-minus-x558 0x3FA00000 0x3FA00000 0x0 1 0x00 0x00000000
#12x-minus-x-rdn569 0x3FA00000 0x3FA00000 0x2 1 0x00 0x80000000
#13neg0-plus-neg0614 0x80000000 0x80000000 0x0 0 0x00 0x80000000
#14pos0-plus-neg0657 0x00000000 0x80000000 0x0 0 0x00 0x00000000
#15subnormals720 0x00000001 0x00000001 0x0 0 0x00 0x00000002
#16normal-minus-subnormal813 0x00800000 0x00000001 0x0 1 0x00 0x007FFFFF
#17inf-plus-1863 0x7F800000 0x3F800000 0x0 0 0x00 0x7F800000
#18inf-minus-inf931 0x7F800000 0x7F800000 0x0 1 0x10 0x7FC00000
#19qnan-plus-1989 0x7FC00000 0x3F800000 0x0 0 0x00 0x7FC00000
#20snan-plus-11009 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.

Open FloatAdder<FP32>

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

  1. Unpack as the adder does; the sign is A's XOR B's.
  2. 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.
  3. Exponent: T = (ea + eb − bias), a signed number; the result's exponent field is T + 1 − (leading zeros of the product).
  4. 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).
  5. 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.
  6. 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-263 0x3E00 0x4000 0x0 0x00 0x4200
#1neg3-times-0.5115 0xC200 0x3800 0x0 0x00 0xBE00
#2rounded-rne171 0x3C01 0x3C01 0x0 0x01 0x3C02
#3rounded-rup187 0x3C01 0x3C01 0x3 0x01 0x3C03
#4overflow-rne255 0x7BFF 0x4000 0x0 0x05 0x7C00
#5overflow-rdn270 0x7BFF 0x4000 0x2 0x05 0x7BFF
#6to-subnormal309 0x0400 0x3400 0x0 0x00 0x0100
#7underflow-inexact344 0x0400 0x3555 0x0 0x03 0x0155
#8underflow-to-0381 0x0001 0x3400 0x0 0x03 0x0000
#9not-tiny-after-rounding441 0x07FF 0x3801 0x3 0x01 0x0401
#10subnormal-times-big504 0x0001 0x7000 0x0 0x00 0x1000
#11zero-times-neg536 0x0000 0xC500 0x0 0x00 0x8000
#12inf-times-2594 0x7C00 0x4000 0x0 0x00 0x7C00
#13inf-times-0634 0x7C00 0x0000 0x0 0x10 0x7E00
#14snan-times-1692 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.

Open the full visualizer

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-265 0x3FC00000 0x40000000 0x0 0x00 0x40400000
#1neg3-times-0.5118 0xC0400000 0x3F000000 0x0 0x00 0xBFC00000
#2rounded-rne175 0x3F800001 0x3F800001 0x0 0x01 0x3F800002
#3rounded-rup191 0x3F800001 0x3F800001 0x3 0x01 0x3F800003
#4overflow-rne269 0x7F7FFFFF 0x40000000 0x0 0x05 0x7F800000
#5overflow-rdn284 0x7F7FFFFF 0x40000000 0x2 0x05 0x7F7FFFFF
#6to-subnormal327 0x00800000 0x3E800000 0x0 0x00 0x00200000
#7underflow-inexact375 0x00800000 0x3EAAAAAB 0x0 0x03 0x002AAAAB
#8underflow-to-0417 0x00000001 0x3E800000 0x0 0x03 0x00000000
#9not-tiny-after-rounding477 0x00FFFFFF 0x3F000001 0x3 0x01 0x00800001
#10subnormal-times-big552 0x00000001 0x4C800000 0x0 0x00 0x02000000
#11zero-times-neg586 0x00000000 0xC0A00000 0x0 0x00 0x80000000
#12inf-times-2646 0x7F800000 0x40000000 0x0 0x00 0x7F800000
#13inf-times-0686 0x7F800000 0x00000000 0x0 0x10 0x7FC00000
#14snan-times-1745 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.

Open FloatMultiplier<FP32>

Source and tests

Units, 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