§ 7.4. Fused Multiply-Add¶
A fused multiply-add computes (A × B) + C with one rounding at the end.
That is more accurate than a multiply followed by an add, and it is the one
operation a GPU lane is built around: a Volta core's FFMA does
multiply, add and multiply-add alike. The RV32F core of 7.7 runs
its adds, subtracts and multiplies on it too.
| Component | Description | Structural | Behavioral |
|---|---|---|---|
FloatFma<FMT> |
±(A × B) ± C, one rounding, combinational | Yes | Yes (compiled gate list) |
FloatFmaPipelined<FMT> |
The same in pipeline stages: 4 edges (FP32), 6 (FP16), as Volta | Yes | Yes (compiled gate list) |
Catalog: float.fma.fp16, float.fma.fp32, float.fma-pipelined.fp16,
float.fma-pipelined.fp32.
7.4.1. FloatFma<FMT>¶
IO pins¶
| Pin | Direction | Width | Meaning |
|---|---|---|---|
A, B, C |
Input | 16 or 32 each | Operands |
NEG_PRODUCT |
Input | 1 | Negate A × B |
NEG_ADDEND |
Input | 1 | Negate C |
RM |
Input | 3 | Rounding mode |
Y |
Output | 16 or 32 | Result |
FLAGS |
Output | 5 | NX, UF, OF, DZ (never), NV |
The two negations give RISC-V's four instructions: FMADD (0, 0),
FMSUB (0, 1), FNMSUB (1, 0), FNMADD (1, 1).
Behavior¶
Y = ±(A × B) ± C, the exact value rounded once by RM. ∞ × 0 is invalid,
even when C is a quiet NaN (RISC-V; x86 differs); ∞ − ∞ between the
product and C is invalid. A zero product leaves C exact (with its sign);
an exact zero sum is +0, or −0 when rounding down, unless both terms are
zeros of the same sign.
C++ reference¶
#include "fp/FloatReference.hpp"
using namespace circuit::fp;
// FP32: (1 + 2^-23)(1 - 2^-23) - 1 = -2^-46 exactly; a multiply then an add gives 0.
const uint64_t a = 0x3f800001, b = 0x3f7fffff, c = 0xbf800000;
Result fused = fusedMultiplyAdd(FP32, a, b, c); // -0x1p-46, exact
Result twice = add(FP32, multiply(FP32, a, b).bits, c); // +0
Unknown inputs and timing¶
Four-state gates; the compact form is the same gate list. This is the deepest unit of the chapter: a multiplier tree, a wide shifter, a wide adder, a second leading-zero count and shifter, and a rounder in series.
Implementation¶
Structural implementation¶
- Unpack A, B, C; the product's sign includes
NEG_PRODUCT, the addend'sNEG_ADDEND. - Exact product: the P × P tree (as the multiplier, 7.3.2), then normalized at once by its leading zeros, so its leading 1 is always on top of its 2P bits; its exponent EP follows.
- Window. The product sits in a window of 3P + 4 bits: two bits below
it, the product at 2 … 2P + 1, room above. The addend's significand
starts at the top of the window and is shifted right by
P + 2 + EP − EC (an
AlignShifter); anything shifted out becomes a sticky bit. An addend so far above that the shift would be negative is left at the top: the product is then below a quarter of the addend's last place, where it rounds the same way however far below it really is. - Sum. One adder (3P + 5 bits) adds or subtracts; a negative difference is negated, and the sign follows it.
- Normalize and round with the multiplier's path: leading zeros, a left shift not below the smallest exponent (or a right shift with sticky when the whole sum is below it), tininess after rounding, the rounder, overflow, special values. A zero product passes C through unchanged.
| Part | Type | Inputs | Outputs | What it does |
|---|---|---|---|---|
UNPACK_A, UNPACK_B, UNPACK_C |
shared (§ 7.2.5) | Step 1 | ||
SPECIALS |
FloatFmaSpecials |
the three signs, NEG_PRODUCT, NEG_ADDEND, the operands' ZERO, INFINITY, NAN, SIGNALLING |
PRODUCT_SIGN, ADDEND_SIGN, NAN, INVALID, INFINITY, INFINITY_SIGN, PRODUCT_ZERO |
The signs of step 1, and the special cases from the operands alone: ∞ × 0 (invalid even with a quiet NaN addend), an infinite product meeting an opposite ∞ |
PRODUCT |
FloatFmaProduct<FMT> |
A_/B_SIGNIFICAND (P), A_/B_EXPONENT (E) |
NORMALIZED (2P), EP (E + 3) |
Step 2: a SignificandMultiplier<P> (MULTIPLY), a LeadingZeroCounter<2P> and a NormalizeShifter<2P>, and the product's exponent |
SUM |
FloatFmaSum<FMT> |
NORMALIZED, EP, EC, C_SIGNIFICAND, PRODUCT_SIGN, ADDEND_SIGN |
MAGNITUDE (3P + 5), T, SIGN, SUBTRACTING |
Steps 3–4: the addend's shift, an AlignShifter<3P+4>, the conditional inversion, the adder, the negation of a negative difference, and T for the normalizer |
MODE, SUM_ZERO, NORMALIZE, ROUND_BITS |
shared | Step 5's normalization, whether the sum is 0, the rounder's inputs and tininess | ||
ZERO_RULES |
FloatFmaZeroRules |
ALL_ZERO, NAN, INFINITY, PRODUCT_ZERO, C_ZERO, SUBTRACTING, PRODUCT_SIGN, DOWN |
ZERO, ZERO_SIGN, PASS |
An exact zero and its sign, and whether a zero product passes C through |
RESULT |
FloatResult<FMT,tiny,pass> |
Y, FLAGS |
Step 5's rounding and special values (shared) |
At FP32 the window (76 bits) and the sum (77 bits) are wider than a pin,
so those blocks take the word as two pins (IN, IN_HIGH; see
§ 7.2).
Behavioral implementation¶
The compact form is the FMA's compiled gate list.
Fused against separate¶
The FMA gives the exactly rounded ±A × B ± C; a multiply then an add rounds twice. The cost of fusing, measured in 7.9, is one wider adder and shifter against a whole second unit.
Examples¶
FP16: the four sign variants; the one-rounding case above (exact, where a multiply and an add give 0); cancellation to +0 and, rounding down, −0; a tiny addend under round-toward-zero and round-up; a large addend; overflow; a zero product keeping the addend; ∞ × 0 + quiet NaN (invalid); ∞ − ∞.
All 14 recorded rows of FloatFmaFp16ScenarioTest, 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 |
CInput · 16 bit |
NEG_ADDENDInput · 1 bit |
NEG_PRODUCTInput · 1 bit |
RMInput · 3 bit |
FLAGSExpected output · 5 bit |
YExpected output · 16 bit |
|---|---|---|---|---|---|---|---|---|---|
#02x3-plus-1 | 124 | 0x4000 |
0x4200 |
0x3C00 |
0 |
0 |
0x0 |
0x00 |
0x4700 |
#1fmsub-2x3-minus-1 | 190 | 0x4000 |
0x4200 |
0x3C00 |
1 |
0 |
0x0 |
0x00 |
0x4500 |
#2fnmsub-2x3-plus-1 | 204 | 0x4000 |
0x4200 |
0x3C00 |
0 |
1 |
0x0 |
0x00 |
0xC500 |
#3fnmadd-2x3-minus-1 | 267 | 0x4000 |
0x4200 |
0x3C00 |
1 |
1 |
0x0 |
0x00 |
0xC700 |
#4one-rounding | 384 | 0x3C01 |
0x3BFE |
0xBC00 |
0 |
0 |
0x0 |
0x00 |
0x8010 |
#5cancel-to-zero | 472 | 0x3E00 |
0x4000 |
0xC200 |
0 |
0 |
0x0 |
0x00 |
0x0000 |
#6cancel-to-zero-rdn | 485 | 0x3E00 |
0x4000 |
0xC200 |
0 |
0 |
0x2 |
0x00 |
0x8000 |
#7tiny-addend-rtz | 592 | 0x3C00 |
0x3C00 |
0x0001 |
0 |
0 |
0x1 |
0x01 |
0x3C00 |
#8tiny-addend-rup | 610 | 0x3C00 |
0x3C00 |
0x0001 |
0 |
0 |
0x3 |
0x01 |
0x3C01 |
#9big-addend | 709 | 0x1400 |
0x1400 |
0x3C00 |
0 |
0 |
0x0 |
0x01 |
0x3C00 |
#10overflow | 792 | 0x7BFF |
0x4000 |
0x0000 |
0 |
0 |
0x0 |
0x05 |
0x7C00 |
#11zero-product-keeps-addend | 893 | 0x0000 |
0x4500 |
0xC700 |
0 |
0 |
0x0 |
0x00 |
0xC700 |
#12inf-times-0-plus-qnan | 1010 | 0x7C00 |
0x0000 |
0x7E00 |
0 |
0 |
0x0 |
0x10 |
0x7E00 |
#13inf-minus-inf | 1117 | 0x7C00 |
0x3C00 |
0x7C00 |
1 |
0 |
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 14 recorded rows of FloatFmaFp32ScenarioTest, 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 |
CInput · 32 bit |
NEG_ADDENDInput · 1 bit |
NEG_PRODUCTInput · 1 bit |
RMInput · 3 bit |
FLAGSExpected output · 5 bit |
YExpected output · 32 bit |
|---|---|---|---|---|---|---|---|---|---|
#02x3-plus-1 | 125 | 0x40000000 |
0x40400000 |
0x3F800000 |
0 |
0 |
0x0 |
0x00 |
0x40E00000 |
#1fmsub-2x3-minus-1 | 194 | 0x40000000 |
0x40400000 |
0x3F800000 |
1 |
0 |
0x0 |
0x00 |
0x40A00000 |
#2fnmsub-2x3-plus-1 | 208 | 0x40000000 |
0x40400000 |
0x3F800000 |
0 |
1 |
0x0 |
0x00 |
0xC0A00000 |
#3fnmadd-2x3-minus-1 | 272 | 0x40000000 |
0x40400000 |
0x3F800000 |
1 |
1 |
0x0 |
0x00 |
0xC0E00000 |
#4one-rounding | 392 | 0x3F800001 |
0x3F7FFFFE |
0xBF800000 |
0 |
0 |
0x0 |
0x00 |
0xA8800000 |
#5cancel-to-zero | 483 | 0x3FC00000 |
0x40000000 |
0xC0400000 |
0 |
0 |
0x0 |
0x00 |
0x00000000 |
#6cancel-to-zero-rdn | 496 | 0x3FC00000 |
0x40000000 |
0xC0400000 |
0 |
0 |
0x2 |
0x00 |
0x80000000 |
#7tiny-addend-rtz | 584 | 0x3F800000 |
0x3F800000 |
0x26800000 |
0 |
0 |
0x1 |
0x01 |
0x3F800000 |
#8tiny-addend-rup | 602 | 0x3F800000 |
0x3F800000 |
0x26800000 |
0 |
0 |
0x3 |
0x01 |
0x3F800001 |
#9big-addend | 708 | 0x34000000 |
0x34000000 |
0x3F800000 |
0 |
0 |
0x0 |
0x01 |
0x3F800000 |
#10overflow | 800 | 0x7F7FFFFF |
0x40000000 |
0x00000000 |
0 |
0 |
0x0 |
0x05 |
0x7F800000 |
#11zero-product-keeps-addend | 914 | 0x00000000 |
0x40A00000 |
0xC0E00000 |
0 |
0 |
0x0 |
0x00 |
0xC0E00000 |
#12inf-times-0-plus-qnan | 1035 | 0x7F800000 |
0x00000000 |
0x7FC00000 |
0 |
0 |
0x0 |
0x10 |
0x7FC00000 |
#13inf-minus-inf | 1150 | 0x7F800000 |
0x3F800000 |
0x7F800000 |
1 |
0 |
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.
7.4.2. FloatFmaPipelined<FMT>¶
IO pins¶
FloatFma's pins, plus:
| Pin | Direction | Width | Meaning |
|---|---|---|---|
CLK, RST |
Input | 1 each | Clock; reset clears every stage register |
VALID_IN |
Input | 1 | The operands at this edge are an operation |
VALID_OUT |
Output | 1 | Y and FLAGS are an operation's result |
Behavior¶
Operands taken at a rising edge give Y and FLAGS 4 edges later for
FP32 and 6 for FP16, with VALID_OUT; a new operation can start every
edge. These are the dependent-issue latencies Jia et al. measured on a
V100: FFMA 4 cycles, HFMA2 6 ("Dissecting the NVIDIA Volta GPU
architecture via microbenchmarking", 2018, Table 4.1).
Implementation¶
The combinational FMA's builder with three cut points that become
registers: after the exact product (and the operand decoding), after the
sum, after normalization; a fourth register holds the result. Every signal
alive at a cut goes through the register (packed into 32-bit register
words). The FP16 unit, whose datapath is shorter, adds two delay registers
at the output to reach Volta's six. VALID travels beside the operands
through the same number of registers. The cuts fall between the steps of
7.4.1: after SPECIALS and PRODUCT, after SUM, and
after NORMALIZE and ROUND_BITS; the registers (STAGE1_0, …) sit in the
unit's top level between them, and a second rounding-mode step,
MODE_FINAL, decodes the registered RM for ZERO_RULES and RESULT.
The compact form is the pipelined unit's compiled gate list (its flip-flops included).
Examples¶
Reset, then three operations on consecutive edges; each result comes out four (FP32) or six (FP16) edges after its operands, while the next ones are still in the pipe.
All 7 recorded rows of FloatFmaPipelinedFp32ScenarioTest, 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 |
CInput · 32 bit |
CLKInput · 1 bit |
NEG_ADDENDInput · 1 bit |
NEG_PRODUCTInput · 1 bit |
RMInput · 3 bit |
RSTInput · 1 bit |
VALID_INInput · 1 bit |
FLAGSExpected output · 5 bit |
VALID_OUTExpected output · 1 bit |
YExpected output · 32 bit |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
#0edge-1 | 141 | 0x40000000 |
0x40400000 |
0x3F800000 |
1 |
0 |
0 |
0x0 |
0 |
1 |
not checked | 0 |
not checked |
#1edge-2 | 226 | 0x3FC00000 |
0xC0800000 |
0x3E800000 |
1 |
0 |
0 |
0x0 |
0 |
1 |
not checked | 0 |
not checked |
#2edge-3 | 336 | 0x3F800001 |
0x3F7FFFFE |
0xBF800000 |
1 |
0 |
0 |
0x0 |
0 |
1 |
not checked | 0 |
not checked |
#3edge-4 | 428 | 0x00000000 |
0x00000000 |
0x00000000 |
1 |
0 |
0 |
0x0 |
0 |
0 |
0x00 |
1 |
0x40E00000 |
#4edge-5 | 465 | 0x00000000 |
0x00000000 |
0x00000000 |
1 |
0 |
0 |
0x0 |
0 |
0 |
0x00 |
1 |
0xC0B80000 |
#5edge-6 | 493 | 0x00000000 |
0x00000000 |
0x00000000 |
1 |
0 |
0 |
0x0 |
0 |
0 |
0x00 |
1 |
0xA8800000 |
#6edge-7 | 508 | 0x00000000 |
0x00000000 |
0x00000000 |
1 |
0 |
0 |
0x0 |
0 |
0 |
not checked | 0 |
not checked |
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 9 recorded rows of FloatFmaPipelinedFp16ScenarioTest, 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 |
CInput · 16 bit |
CLKInput · 1 bit |
NEG_ADDENDInput · 1 bit |
NEG_PRODUCTInput · 1 bit |
RMInput · 3 bit |
RSTInput · 1 bit |
VALID_INInput · 1 bit |
FLAGSExpected output · 5 bit |
VALID_OUTExpected output · 1 bit |
YExpected output · 16 bit |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
#0edge-1 | 130 | 0x4000 |
0x4200 |
0x3C00 |
1 |
0 |
0 |
0x0 |
0 |
1 |
not checked | 0 |
not checked |
#1edge-2 | 219 | 0x3E00 |
0xC400 |
0x3400 |
1 |
0 |
0 |
0x0 |
0 |
1 |
not checked | 0 |
not checked |
#2edge-3 | 320 | 0x3C01 |
0x3BFE |
0xBC00 |
1 |
0 |
0 |
0x0 |
0 |
1 |
not checked | 0 |
not checked |
#3edge-4 | 406 | 0x0000 |
0x0000 |
0x0000 |
1 |
0 |
0 |
0x0 |
0 |
0 |
not checked | 0 |
not checked |
#4edge-5 | 444 | 0x0000 |
0x0000 |
0x0000 |
1 |
0 |
0 |
0x0 |
0 |
0 |
not checked | 0 |
not checked |
#5edge-6 | 472 | 0x0000 |
0x0000 |
0x0000 |
1 |
0 |
0 |
0x0 |
0 |
0 |
0x00 |
1 |
0x4700 |
#6edge-7 | 490 | 0x0000 |
0x0000 |
0x0000 |
1 |
0 |
0 |
0x0 |
0 |
0 |
0x00 |
1 |
0xC5C0 |
#7edge-8 | 508 | 0x0000 |
0x0000 |
0x0000 |
1 |
0 |
0 |
0x0 |
0 |
0 |
0x00 |
1 |
0x8010 |
#8edge-9 | 523 | 0x0000 |
0x0000 |
0x0000 |
1 |
0 |
0 |
0x0 |
0 |
0 |
not checked | 0 |
not checked |
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 |
|---|---|
FloatFmaTest |
FP16 and FP32: triples of special values in random modes and negations, then random triples, a third of them with C near −A × B (cancellation), all eight RM codes; gates and compact form against the reference |
FloatFmaPipelinedTest |
FP16 and FP32, gates and compact form: 300 edges of random operations (a quarter of the edges idle); every result and VALID_OUT exactly at the latency, against the reference |
…ScenarioTest |
The recorded rows above |