S3 · Lesson 10 · Fixed point and floating point in hardware
Q formats, bit growth, rounding, saturation and IEEE 754
Hardware arithmetic works on integers, and real-valued signals are squeezed into them by agreeing where the binary point sits. That's fixed point: cheap, fast, and the default for signal processing in FPGAs and ASICs, but every addition and multiplication grows the number of bits, and the designer decides where to round and where to clamp. Floating point moves the binary point automatically with an exponent, trading area and latency for range. This lesson does the bookkeeping: the range and resolution of a format, how many bits a sum or product needs, what rounding and saturation cost, and what an IEEE 754 number actually stores.
- A signed fixed-point format Qm.n (m integer bits including the sign, n fractional bits): its range runs from to in steps of .
- The curve is a test signal, 0.8 sin, after quantizing to n fractional bits, with saturation at the format's limits. Drop n and the staircase coarsens; drop m to 1 and a gain of 2 drives the signal into clipping.
- The readouts give the resolution and the signal-to-quantization-noise ratio the format allows for a full-scale sine.
Predict
Two Q1.15 numbers (16 bits, values from −1 to just under 1) are multiplied. How many bits does the exact product need, and in what format?
32 bits as Q2.30: integer bits add (1 + 1) and fractional bits add (15 + 15). The extra integer bit is needed for exactly one case, −1 × −1 = +1, which doesn't fit in Q1.30. DSP hardware usually saturates that case and keeps Q1.31, or rounds back to Q1.15 for the next stage, choosing where to drop precision.
Name it
Q formats
A signed Qm.n number has m integer bits (including the sign) and n fractional bits, m + n in all. It represents integers scaled by :
- resolution ;
- range to ;
- quantization SQNR for a full-scale sine about dB.
Bit growth
- Addition: adding two Qm.n numbers can carry into one more integer bit: Q(m+1).n. Summing K terms needs extra integer bits (guard bits) to never overflow.
- Multiplication: Qa.b × Qc.d = Q(a+c).(b+d), exactly.
Rounding and saturation
Dropping fractional bits can truncate (cheap, but biased by half an LSB on average) or round (add half an LSB first; unbiased except at exact halves, where convergent rounding to even removes the remaining bias). Overflowing the integer bits can wrap (free, but a large positive becomes a large negative: violent in a filter) or saturate (clamp to the largest magnitude: a comparator and a multiplexer, and far gentler).
IEEE 754 floating point
A binary32 (single) number has 1 sign bit, an 8-bit exponent with bias 127, and a 23-bit fraction with an implied leading 1: value . Binary64 has 11 exponent bits (bias 1023) and 52 fraction bits. The spacing between adjacent numbers, one ULP, is : relative precision about ≈ 6 × 10⁻⁸ for single, ≈ 1.1 × 10⁻¹⁶ for double. A floating-point adder must align exponents, add, normalize and round: several pipeline stages and several times a fixed-point adder's area.
Worked example
A filter sums 64 products of two Q1.15 numbers. How wide must the accumulator be to never overflow, and how many fractional bits does it carry?
Show the solution
- Each product. Q1.15 times Q1.15 is Q2.30, 32 bits.
- Guard bits for the sum. Summing 64 of them needs guard bits, giving Q8.30, 38 bits. DSP blocks provide 48-bit accumulators for this reason.
- Back to the output format. At the end the result is rounded and saturated back to the output format, say Q1.15, at one place in the datapath instead of after every operation.
Answer a 38-bit accumulator, Q8.30, carrying 30 fractional bits.
Worked example
What is the IEEE 754 single-precision number with sign 0, exponent field 130 and fraction field 0x200000? And what's its ULP?
Show the solution
- Exponent.
- Significand. The fraction 0x200000 is so the significand is 1.25.
- Value.
- ULP. Numbers near 10 are spaced about a millionth apart.
Answer the number is 10.0, and its ULP is .
Worked example
Store 0.3 in Q1.15 with rounding. What integer is stored, what value does it represent, and how big is the error?
Show the solution
- Scale and round. which rounds to 9830 (0x2666).
- Value represented.
- Error. , which is 0.4 LSB: within the half LSB that rounding guarantees.
Answer 9830 (0x2666) is stored, representing 0.2999878, an error of (0.4 LSB).
Check
What's the largest value a signed Q4.7 number can hold?
Check
A Q1.7 number is multiplied by a Q2.23 number. How many bits does the exact product need?
Check
How many guard bits does an accumulator need to add 100 full-scale terms without overflow?
Check
An IEEE 754 single has sign 0, exponent field 130 and fraction value 0 (as a binary fraction). What number is it?
Check
What's one ULP for single-precision numbers with exponent field 127?
Check
What SQNR does a full-scale sine get in signed Q1.7?
Check
A signed number is held in Q4.7 format, and an accumulator sums 100 of them.
(a)
What is its resolution?
(b)
What is the most negative value it holds?
(c)
How many guard bits does the accumulator need to never overflow?
Check
A normal IEEE 754 single has exponent field e and fraction value f (the fraction bits read as a number from 0 to 1).
Step 1
What value does it represent (sign positive)?
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 its ULP?
Step 3
What is the ULP relative to the value?
Explain why
Why do signal-processing datapaths usually saturate on overflow instead of wrapping?
Two's complement arithmetic wraps naturally: one past the largest positive number is the most negative. In a running filter or controller, a wrapped value is catastrophic, because a signal that should have been slightly too large suddenly becomes a huge value of the opposite sign, which then feeds back through the filter and can ring or oscillate. Saturating instead clamps the result to the largest representable value of the correct sign: still wrong, but by as little as possible, and with the correct sign, so the system behaves like an analog circuit hitting its rails. The cost is a little logic to detect overflow and select the limit.
Practice
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.