S3 · Lesson 11 · Floating-point units
Alignment, normalization, rounding and fused multiply-add in hardware
An integer adder is one carry chain. A floating-point adder is a small pipeline: compare the exponents, shift the smaller significand into alignment, add or subtract, find where the leading one landed, shift it back into place, and round, all while tracking the bits that fell off the end so the rounding is exactly what IEEE 754 prescribes. That's why a floating-point add takes three to five cycles where an integer add takes one, and why every one of those steps is a hardware design problem.
- The sawtooth is the spacing between adjacent floating-point numbers divided by the number itself, in units of the machine epsilon , for a significand of p bits (24 for single, 53 for double, 11 for half).
- It stays between half an epsilon and one epsilon at every magnitude, to : each power of two starts a new binade with the same number of values. The bits set the size of the unit (see the readout); the shape never changes.
- That bounded relative spacing is what floating point buys; rounding to nearest makes each result's relative error at most .
Predict
A floating-point adder adds 12 = 1.5 × 2³ and 1.25 = 1.25 × 2⁰. What does the alignment step do, and what significand and exponent come out?
The exponents differ by 3, so the smaller operand's significand is shifted right 3 places: . Adding gives , already between 1 and 2, so no normalization shift is needed. Had the sum reached 2 or more, a one-place right shift and an exponent increment would fix it.
Name it
The adder's steps
- Compare and swap: subtract the exponents; the difference d is the alignment shift, and the larger operand goes on top.
- Align: shift the smaller significand right by d with a barrel shifter, keeping three extra bits: guard, round and sticky (the OR of every bit shifted out below the round bit).
- Add or subtract the significands.
- Normalize: an addition can carry out (shift right one); a subtraction of close values can cancel many leading bits (shift left by the leading-zero count, which a leading-zero anticipator predicts in parallel with the subtraction).
- Round to nearest, ties to even: round up if the bits below the kept significand exceed half an ULP, or equal half and the kept LSB is 1.
Costs
- A barrel shifter for shifts up to S on n bits takes levels of n multiplexers.
- A multiplier for p-bit significands forms partial-product bits (576 for single, 2809 for double).
- Fused multiply-add computes with one rounding instead of two: more accurate, and the core of every dot product and matrix unit.
Rounding error
With p significand bits, rounding to nearest has a relative error of at most . A number in has an ULP of .
Worked example
In single precision, what are 1 + 2⁻²⁴ and 1 + 1.5 × 2⁻²⁴?
Show the solution
- Spacing near 1. Single-precision numbers near 1 are spaced apart.
- The first sum. sits exactly halfway between 1 and : guard 1, round and sticky 0, a tie. Since 1's last bit is 0, it rounds to 1.0.
- The second sum. is past halfway (guard and round both 1), so it rounds up:
Answer rounds to 1.0, and rounds to .
Worked example
Subtract 1.0 from the single-precision number nearest 1.0000001. How far must the result be normalized?
Show the solution
- The stored number. The nearest single to 1.0000001 is .
- Subtract. exactly.
- Normalize. The subtraction cancels 23 leading bits, so the normalizer shifts left 23 places and subtracts 23 from the exponent.
- What the result holds. It is exact, but it carries only the one significant bit the inputs had: cancellation exposes earlier rounding.
Answer the result, , must be shifted left 23 places.
Worked example
How large is a single-precision FMA's multiplier array, and how wide is the exact product it adds to c?
Show the solution
- The array. Two 24-bit significands make partial-product bits, and a 48-bit exact product.
- The addition. The FMA aligns c against all 48 bits before one final rounding, so its adder and shifter are about three times as wide as a plain adder's. That is part of why an FMA takes 4 or more cycles.
Answer a 576-bit partial-product array, and a 48-bit exact product.
Check
A floating-point adder adds 1.5 × 2^5 and 1.25 × 2^4.
(a)
How far is the smaller significand shifted?
(b)
What significand comes out after normalization?
(c)
And what exponent?
Check
A barrel shifter moves a 27-bit significand right by any amount from 0 to 27.
(a)
How many multiplexer levels does it need?
(b)
How many 2-to-1 multiplexers?
Check
A floating-point format has a 24-bit significand (hidden bit included).
(a)
How many partial-product bits does its multiplier form?
(b)
What is its unit roundoff?
Check
Why does IEEE 754 round ties to even rather than always up?
Check
Match each part of a floating-point adder to its job.
- Athe OR of every bit shifted out below the round bit
- Bpredicts the normalization shift while the subtraction runs
- Ccarry-save adder tree
- Daligns the smaller significand to the larger exponent
- Ethe first bit below the kept significand
Check
A binary floating-point format has p significand bits; x lies in .
Step 1
What is the ULP at x?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What is the largest rounding error at x, rounding to nearest?
Step 3
What is that relative to the smallest x in the binade?
Check
Write fadd(a, b): add two positive, normal single-precision numbers given as 32-bit integer bit patterns, returning the bit pattern of the correctly rounded sum (round to nearest, ties to even). Keep guard, round and sticky bits during alignment.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does a floating-point addition take several cycles when an integer addition of the same width takes one?
An integer adder only adds. A floating-point adder must first compare the exponents and shift the smaller significand into alignment, which needs a subtractor and a barrel shifter before the addition can start. After adding, the result may have carried out or, when nearly equal numbers are subtracted, lost many leading bits, so it must find the leading one and shift again. Finally it must round, which may carry all the way up the significand and force one more adjustment. Each step is a full-width operation in series, so the path is several times longer than one carry chain, and designs pipeline it over three to five cycles.
Practice
Keep practicing
6 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.