§ 7.2. Building Blocks¶
A floating-point unit spends most of its gates moving significands around: lining one up under another before adding, sliding a result back so its leading 1 is on top, and deciding whether the bits that fall off push the last kept bit up. These four blocks do exactly that, and every unit of the chapter is built from them, the prefix adders of § 2.1 and the multiplier tree of § 6.1.1.
| Component | Description | Structural | Behavioral |
|---|---|---|---|
LeadingZeroCounter<W> |
How many zeros stand above the first 1 | Yes | Yes (compiled gate list) |
AlignShifter<W> |
Right shift that remembers whether a 1 fell off (sticky) | Yes | Yes (compiled gate list) |
NormalizeShifter<W> |
Left shift | Yes | Yes (compiled gate list) |
Rounder<P> |
Adds one at the last kept place when the rounding mode says so | Yes | Yes (compiled gate list) |
Catalog: float.leading-zeros.W, float.align-shift.W,
float.normalize-shift.W, float.round.P, at the sizes the FP16 and FP32
units use (for example W = 14 for the FP16 adder's sum, 76 and 77 for the
FP32 fused multiply-add's window and sum; P = 11, 24, and 33 for the
float-to-integer conversion). Each block is one builder generated over its
width; the examples show the FP16 sizes. A pin carries at most 64 bits, so
a word wider than that is two pins: IN holds its low 64 bits and
IN_HIGH the rest (likewise OUT and OUT_HIGH).
7.2.1. LeadingZeroCounter<W>¶
IO pins¶
| Pin | Direction | Width | Meaning |
|---|---|---|---|
IN |
Input | W (at most 64) | The word |
IN_HIGH |
Input | W − 64 | Its bits above 64, when W > 64 |
COUNT |
Output | bits to hold W | Zeros above the highest 1 (W when there is none) |
ALL_ZERO |
Output | 1 | IN is 0 |
Behavior¶
Combinational. COUNT is the number of 0 bits from the top of IN down
to its highest 1. A normalizer shifts left by COUNT to bring that 1 to
the top.
Unknown inputs and timing¶
Four-state gates: an unknown bit above the highest known 1 makes the count unknown from that level of the tree up. Settling grows with log₂ W.
Implementation¶
Structural implementation¶
A tree. The input, padded at the bottom to a power of two with one 1 (so an all-zero input counts W, not more) and zeros, is cut into single bits; each leaf says "zero?" and has an empty count. A node joins two halves of 2^k bits each:
- zero = both halves zero;
- count = (high half zero) followed by (high half zero ? low count : high count).
So the top bit of the count is "the upper half was empty", and each level
adds one bit. Every node of a level does the same thing, so a level is
built as a few word gates (below): one ANDGate
word for all its nodes' zero bits, and one AND/AND/OR set of words for all
its nodes' count multiplexers, each node's select spread over its count
bits; the leaves' inverters are one NOTGate word. The tree has log₂ W
levels.
Behavioral implementation¶
The compact form runs the same gates as one compiled gate list: exact for every input, X and Z included, timed from those gates.
Examples¶
All 6 recorded rows of LeadingZeroCounterScenarioTest, 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 | INInput · 14 bit |
ALL_ZEROExpected output · 1 bit |
COUNTExpected output · 4 bit |
|---|---|---|---|---|
#0in-0x2000 | 8 | 0x2000 |
0 |
0x0 |
#1in-0x1000 | 16 | 0x1000 |
0 |
0x1 |
#2in-0x123 | 26 | 0x0123 |
0 |
0x5 |
#3in-0x1 | 35 | 0x0001 |
0 |
0xD |
#4in-0x0 | 45 | 0x0000 |
1 |
0xE |
#5in-0x3fff | 52 | 0x3FFF |
0 |
0x0 |
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.2.2. AlignShifter<W>¶
IO pins¶
| Pin | Direction | Width | Meaning |
|---|---|---|---|
IN |
Input | W (at most 64) | The significand to shift |
IN_HIGH |
Input | W − 64 | Its bits above 64, when W > 64 |
SHIFT |
Input | bits to hold W | Places to shift right |
OUT |
Output | W (at most 64) | IN >> SHIFT, zeros entering at the top |
OUT_HIGH |
Output | W − 64 | Its bits above 64, when W > 64 |
STICKY |
Output | 1 | A 1 was shifted out |
Behavior¶
Combinational. A shift of W or more gives OUT = 0 and STICKY = (IN ≠
0). Before two numbers are added, the smaller one is shifted right by the
difference of their exponents; the bits that fall off cannot be kept, but
whether any of them was 1 is exactly what rounding needs to know (is the
true value just above the last kept bits, or exactly on them?). That one
bit is the sticky bit.
Unknown inputs and timing¶
Four-state gates. Settling: one multiplexer level per SHIFT bit, plus the
OR tree of the sticky bits.
Implementation¶
Structural implementation¶
A barrel shifter: one stage per SHIFT bit s, moving the word 2^s places
right when that bit is 1. Each stage ORs the 2^s bits it drops into its
own sticky bit (gated by its shift bit), and STICKY is the OR of the
stages' bits. A stage whose distance is W or more drops the whole word.
Each stage is a part of its own, STAGE_s of type AlignStage<W,D> with
D = 2^s:
| Pin | Direction | Width | Meaning |
|---|---|---|---|
IN |
Input | W | The word from the stage before |
SHIFT |
Input | 1 | SHIFT bit s |
OUT |
Output | W | IN >> D when SHIFT is 1, else IN |
STICKY |
Output | 1 | SHIFT and a 1 among the D bits dropped |
Inside, the multiplexer is word gates (two ANDGate<W> and an ORGate<W>,
or one Mux<2,W> at 4, 8, 16 and 32 bits) and the OR of the dropped bits a
tree of ORGate words.
Behavioral implementation¶
The same gates as a compiled gate list.
Examples¶
All 6 recorded rows of AlignShifterScenarioTest, 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 | INInput · 13 bit |
SHIFTInput · 4 bit |
OUTExpected output · 13 bit |
STICKYExpected output · 1 bit |
|---|---|---|---|---|---|
#00x1800-right-0 | 10 | 0x1800 |
0x0 |
0x1800 |
0 |
#10x1800-right-3 | 20 | 0x1800 |
0x3 |
0x0300 |
0 |
#20x1801-right-1 | 28 | 0x1801 |
0x1 |
0x0C00 |
1 |
#30x1801-right-12 | 42 | 0x1801 |
0xC |
0x0001 |
1 |
#40x1801-right-13 | 52 | 0x1801 |
0xD |
0x0000 |
1 |
#50x4-right-15 | 64 | 0x0004 |
0xF |
0x0000 |
1 |
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.2.3. NormalizeShifter<W>¶
IO pins¶
| Pin | Direction | Width | Meaning |
|---|---|---|---|
IN |
Input | W (at most 64) | The word |
IN_HIGH |
Input | W − 64 | Its bits above 64, when W > 64 |
SHIFT |
Input | bits to hold W | Places to shift left |
OUT |
Output | W (at most 64) | IN << SHIFT within W bits, zeros entering at the bottom |
OUT_HIGH |
Output | W − 64 | Its bits above 64, when W > 64 |
Behavior¶
Combinational; a shift of W or more gives 0. Used after a subtraction or a
multiplication, by the count of a LeadingZeroCounter, to bring the leading
1 to the top.
Implementation¶
The barrel shifter of AlignShifter mirrored, without sticky bits: stages
STAGE_s of type NormalizeStage<W,D> with pins IN, SHIFT and OUT
as above (OUT = IN << D within W bits when SHIFT is 1). The compact
form is its compiled gate list.
Examples¶
All 5 recorded rows of NormalizeShifterScenarioTest, 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 | INInput · 14 bit |
SHIFTInput · 4 bit |
OUTExpected output · 14 bit |
|---|---|---|---|---|
#00x123-left-0 | 9 | 0x0123 |
0x0 |
0x0123 |
#10x123-left-5 | 19 | 0x0123 |
0x5 |
0x2460 |
#20x1-left-13 | 27 | 0x0001 |
0xD |
0x2000 |
#30x3-left-13 | 35 | 0x0003 |
0xD |
0x2000 |
#40x3fff-left-14 | 44 | 0x3FFF |
0xE |
0x0000 |
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.2.4. Rounder<P>¶
IO pins¶
| Pin | Direction | Width | Meaning |
|---|---|---|---|
SIG |
Input | P | The kept significand |
GUARD |
Input | 1 | The first bit below it |
STICKY |
Input | 1 | The OR of all the bits below GUARD |
SIGN |
Input | 1 | The sign of the value (directed modes round by it) |
RM |
Input | 3 | Rounding mode, RISC-V encoding (7.1) |
OUT |
Output | P | SIG, or SIG + 1 when rounding up |
CARRY |
Output | 1 | SIG + 1 overflowed P bits (OUT is then 0: the value became a power of two) |
INEXACT |
Output | 1 | GUARD or STICKY: bits were lost |
Behavior¶
Combinational. Whether to add one:
| Mode | Rounds up when |
|---|---|
| RNE (0) | GUARD and (STICKY or the last bit of SIG): above half, or exactly half and odd |
| RTZ (1) | never |
| RDN (2) | the value is negative and inexact |
| RUP (3) | the value is positive and inexact |
| RMM (4) | GUARD: half or more |
| 5–7 (reserved) | never (as RTZ) |
The units handle the exponent change a CARRY brings, overflow, and the
special values; the rounder only decides.
Implementation¶
Structural implementation¶
A decoder for the five modes, the table above as AND/OR gates, and one
incrementer: a prefix adder of SIG and zero with the round-up bit as its
carry in.
Behavioral implementation¶
The same gates as a compiled gate list.
Examples¶
Ties to even (down and up), a tie away from zero, the directed modes on both signs, and a carry out of the top bit.
All 10 recorded rows of RounderScenarioTest, 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 | GUARDInput · 1 bit |
RMInput · 3 bit |
SIGInput · 11 bit |
SIGNInput · 1 bit |
STICKYInput · 1 bit |
CARRYExpected output · 1 bit |
INEXACTExpected output · 1 bit |
OUTExpected output · 11 bit |
|---|---|---|---|---|---|---|---|---|---|
#0exact | 8 | 0 |
0x0 |
0x401 |
0 |
0 |
0 |
0 |
0x401 |
#1tie-to-even-down | 11 | 1 |
0x0 |
0x400 |
0 |
0 |
0 |
1 |
0x400 |
#2tie-to-even-up | 19 | 1 |
0x0 |
0x401 |
0 |
0 |
0 |
1 |
0x402 |
#3tie-away | 26 | 1 |
0x4 |
0x400 |
0 |
0 |
0 |
1 |
0x401 |
#4above-tie | 35 | 1 |
0x0 |
0x400 |
0 |
1 |
0 |
1 |
0x401 |
#5toward-zero | 45 | 1 |
0x1 |
0x4FF |
0 |
1 |
0 |
1 |
0x4FF |
#6down-negative | 53 | 0 |
0x2 |
0x400 |
1 |
1 |
0 |
1 |
0x401 |
#7down-positive | 59 | 0 |
0x2 |
0x400 |
0 |
1 |
0 |
1 |
0x400 |
#8up-positive | 66 | 0 |
0x3 |
0x400 |
0 |
1 |
0 |
1 |
0x401 |
#9carry-out | 75 | 1 |
0x3 |
0x7FF |
0 |
1 |
1 |
1 |
0x000 |
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.
Word gates and steps¶
The units of this chapter are built from three kinds of parts, so a reader can open a unit step by step instead of meeting thousands of loose gates.
- Word gates. A select, an inversion, an XOR with one bit or an OR of
many bits applies one gate to every bit of a word. These are the word
gates of § 1 (
ANDGate<W>,ORGate<W>,XORGate<W>,NOTGate<W>for W = 2 to 32, andMux<2,W>at 4, 8, 16 and 32 bits), 32-bit pieces for wider words. A word gate holds the same one-bit gates between zero-delay wiring, so the gates, depth and delays are those of a bit-by-bit circuit. A select at an odd width is twoANDGate<W>and anORGate<W>around one shared select inverter; an OR of n bits is a tree of words that halves the word at each level (n − 1 gates). - Blocks of this section, the prefix adders of § 2 and the significand multiplier (below).
- Steps. Each unit's algorithm steps are parts of their own, named for
what they do (
UNPACK_A,ORDER,SUM,NORMALIZE,RESULT, …). A step is not a catalog part: the unit's compact form is the unit's whole gate list, steps included. The steps every unit shares are below; each unit's page lists its own.
7.2.5. Shared steps¶
| Part | Type | Inputs | Outputs | What it does |
|---|---|---|---|---|
MODE |
FloatRoundingMode |
RM (3) |
NEAREST_EVEN, NEAREST_AWAY, DOWN, UP (1 each) |
Decodes the rounding mode once for the unit (codes 5–7: none of the lines, rounding toward zero) |
UNPACK_A, UNPACK_B, … |
FloatUnpack<FMT> |
X (E + F + 1) |
SIGN, EXPONENT (E), FRACTION (F), SIGNIFICAND (P, the hidden bit on top), EFFECTIVE (E: the exponent, 1 for a subnormal), ZERO, INFINITY, NAN, SIGNALLING |
Takes one operand apart |
PRODUCT, MULTIPLY |
SignificandMultiplier<P> |
A, B (P each) |
PRODUCT (2P) |
The exact product of two significands, by the multiplier tree of § 6.1.1 |
NORMALIZE |
ResultNormalizer<W,T,E> |
VALUE (W), T (signed) |
BITS (W), LOST, FIELD (E + 2) |
Shifts VALUE left by its leading zeros while the exponent field T + 1 − lz stays at least 1, left by T for a subnormal, or right by −T (with LOST for the bits shifted out) when T is negative; FIELD is the exponent field of the top bit. Inside: a LeadingZeroCounter, a NormalizeShifter and an AlignShifter |
ROUND_BITS |
RoundingBits<W,P> |
BITS (W), and LOST, the mode and SIGN when the unit can lose bits or underflow |
KEPT (the top P bits), GUARD, STICKY, TINY |
The rounder's inputs: the next bit, the OR of the rest (and LOST), and tininess after rounding |
…_ZERO |
ZeroTest<W> |
VALUE (W) |
ANY, ZERO |
Whether any bit is set |
RESULT |
FloatResult<FMT> |
RM, the mode, SIGN, FIELD, SIGNIFICAND (P), GUARD, STICKY, TINY, NAN, INVALID, INFINITY, INFINITY_SIGN, ZERO, ZERO_SIGN, and PASS, PASS_VALUE for the FMA |
Y, FLAGS (5) |
Rounds with a Rounder<P>, adds a rounding carry to the exponent, turns an overflow into ∞ or the largest finite value (by the mode), then puts the special values in front: NaN over ∞ over the pass-through value over zero; raises NX, UF, OF and NV. Its type names the optional pins (FloatResult<FP32,tiny,pass>) |
Source and tests¶
Blocks, shared construction helpers, tests.
| Test | Coverage |
|---|---|
LeadingZeroCounterTest |
Every registered width: 0, all ones, each single bit, each bit with random bits below it, random words; gates and compact form |
AlignShifterTest |
Every registered width and every shift value from 0 to the largest the pin holds, on all-ones, one and random words: OUT and STICKY |
NormalizeShifterTest |
Every width and shift value on all-ones and random words |
RounderTest |
P = 11, 24, 33: every mode (0–7), every guard/sticky pair and sign, on edge and random significands |
…ScenarioTest |
The recorded rows above, on the gates |