---
title: Fixed point and floating point in hardware
heading: Q formats, bit growth, rounding, saturation and IEEE 754
eyebrow: S3 · Lesson 10 · Fixed point and floating point in hardware
xp: 50
---

::: hook
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.
:::

::: play
- A signed fixed-point format Qm.n (m integer bits including the sign, n fractional bits): its range runs from $-2^{m-1}$ to $2^{m-1} - 2^{-n}$ in steps of $2^{-n}$.
- 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.
:::
:::

```sim plot
view: graph
x: t = 0..1
y: v = -2.2..2.2
slider: m = 1..3 step 1 = 2
slider: n = 1..10 step 1 = 3
slider: g = 0.5..3 = 1
plot: g*0.8*sin(2*pi*t) | dashed | color: ch2 | label: ideal signal
plot: (((floor(g*0.8*sin(2*pi*t)*2^n + 0.5)/2^n) + (2^(m - 1) - 2^(-n)) - abs((floor(g*0.8*sin(2*pi*t)*2^n + 0.5)/2^n) - (2^(m - 1) - 2^(-n))))/2 - 2^(m - 1) + abs(((floor(g*0.8*sin(2*pi*t)*2^n + 0.5)/2^n) + (2^(m - 1) - 2^(-n)) - abs((floor(g*0.8*sin(2*pi*t)*2^n + 0.5)/2^n) - (2^(m - 1) - 2^(-n))))/2 + 2^(m - 1)))/2 | samples: 1000 | label: Qm.n value, rounded and saturated
readout: resolution = 2^(-n)
readout: largest value = 2^(m - 1) - 2^(-n)
readout: SQNR for a full-scale sine, dB = 6.02*(m + n) + 1.76
```

::: 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?

- [ ] 16 bits, Q1.15
- [x] 32 bits, Q2.30
- [ ] 31 bits, Q1.30
- [ ] 17 bits, Q2.15

::: reveal
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.
:::
:::

::: nameit
## 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 $2^{-n}$:

- resolution $2^{-n}$;
- range $-2^{m-1}$ to $2^{m-1} - 2^{-n}$;
- quantization SQNR for a full-scale sine about $6.02(m + n) + 1.76$ dB.

## Bit growth

- **Addition:** adding two Qm.n numbers can carry into one more integer bit: Q(m+1).n. Summing K terms needs $\lceil\log_2K\rceil$ 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 $(-1)^s \times 1.f \times 2^{e - 127}$. Binary64 has 11 exponent bits (bias 1023) and 52 fraction bits. The spacing between adjacent numbers, one **ULP**, is $2^{e - 127 - 23}$: relative precision about $2^{-24}$ ≈ 6 × 10⁻⁸ for single, $2^{-53}$ ≈ 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.
:::

::: 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?

::: solution
1. **Each product.** Q1.15 times Q1.15 is Q2.30, 32 bits.
2. **Guard bits for the sum.** Summing 64 of them needs $\log_2 64 = 6$ guard bits, giving Q8.30, 38 bits. DSP blocks provide 48-bit accumulators for this reason.
3. **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.
:::
:::

::: example
## What is the IEEE 754 single-precision number with sign 0, exponent field 130 and fraction field 0x200000? And what's its ULP?

::: solution
1. **Exponent.**
   $$130 - 127 = 3$$
2. **Significand.** The fraction 0x200000 is
   $$\frac{2^{21}}{2^{23}} = 0.25$$
   so the significand is 1.25.
3. **Value.**
   $$1.25 \times 2^3 = 10.0$$
4. **ULP.**
   $$\begin{aligned} \text{ULP} &= 2^{3 - 23} \\ &= 2^{-20} \\ &\approx 9.5 \times 10^{-7} \end{aligned}$$
   Numbers near 10 are spaced about a millionth apart.

**Answer:** the number is 10.0, and its ULP is $2^{-20} \approx 9.5 \times 10^{-7}$.
:::
:::

::: example
## Store 0.3 in Q1.15 with rounding. What integer is stored, what value does it represent, and how big is the error?

::: solution
1. **Scale and round.**
   $$0.3 \times 2^{15} = 9830.4$$
   which rounds to 9830 (0x2666).
2. **Value represented.**
   $$\frac{9830}{32768} = 0.2999878$$
3. **Error.** $1.22 \times 10^{-5}$, which is 0.4 LSB: within the half LSB that rounding guarantees.

**Answer:** 9830 (0x2666) is stored, representing 0.2999878, an error of $1.22 \times 10^{-5}$ (0.4 LSB).
:::
:::

::: check
given: m = 1 | 4 | 8; n = 7 | 12 | 15
answer: 2^(m - 1) - 2^(-n)
tolerance: 0.0001%

## What's the largest value a signed Q{m}.{n} number can hold?

::: right
Right. $2^{m-1} - 2^{-n}$ = {answer}.
:::

::: misses
- [2^m - 2^(-n)] The sign takes one of the integer bits.
- [any] Not yet. The top bit is the sign.
:::
:::

::: check
given: a = 1 | 4; b = 7 | 15; c = 1 | 2; d = 15 | 23
answer: a + b + c + d
tolerance: 0

## A Q{a}.{b} number is multiplied by a Q{c}.{d} number. How many bits does the exact product need?

::: right
Right. Q({a}+{c}).({b}+{d}): {answer} bits.
:::

::: misses
- [any] Not yet. Integer bits add, and so do fractional bits.
:::
:::

::: check
given: K = 10 | 100 | 1000
answer: ceil(log(K)/log(2))
tolerance: 0

## How many guard bits does an accumulator need to add {K} full-scale terms without overflow?

::: right
Right. $\lceil\log_2K\rceil$ = {answer}.
:::

::: misses
- [K] Each doubling of the count needs one more bit, not each term.
- [any] Not yet. The sum can be K times a single term.
:::
:::

::: check
given: e = 125 | 130 | 140; f = 0 | 0.5 | 0.75
answer: (1 + f)*2^(e - 127)
tolerance: 0.0001%

## An IEEE 754 single has sign 0, exponent field {e} and fraction value {f} (as a binary fraction). What number is it?

::: right
Right. $(1 + f) \times 2^{e - 127}$ = {answer}.
:::

::: misses
- [f*2^(e - 127)] Include the implied leading 1.
- [(1 + f)*2^e] Subtract the bias, 127.
- [any] Not yet. Unbias the exponent and add the hidden bit.
:::
:::

::: check
given: e = 120 | 127 | 150
answer: 2^(e - 127 - 23)
tolerance: 0.0001%

## What's one ULP for single-precision numbers with exponent field {e}?

::: right
Right. $2^{e - 127 - 23}$ = {answer}.
:::

::: misses
- [2^(e - 127 - 52)] That's double precision's 52-bit fraction.
- [any] Not yet. The last fraction bit's weight.
:::
:::

::: check
given: m = 1 | 2; n = 7 | 11 | 15
answer: 6.02*(m + n) + 1.76
unit: dB
tolerance: 0.5%

## What SQNR does a full-scale sine get in signed Q{m}.{n}?

::: right
Right. $6.02(m + n) + 1.76$ = {answer} dB.
:::

::: misses
- [6.02*n + 1.76] Count every bit, integer ones included, for a full-scale signal.
- [any] Not yet. Total bits set the quantization SNR.
:::
:::

::: check
kind: parts
given: m = 1 | 4 | 8; n = 7 | 15 | 23; K = 10 | 64 | 100

## A signed number is held in Q{m}.{n} format, and an accumulator sums {K} of them.

::: part
answer: 2^(-n)
tolerance: 0.5%

### What is its resolution?

::: misses
- [any] $2^{-n}$.
:::
:::

::: part
answer: -2^(m - 1)
tolerance: 0

### What is the most negative value it holds?

::: misses
- [any] Two's complement reaches one LSB further below zero than above: $-2^{m-1}$.
:::
:::

::: part
answer: ceil(log(K)/log(2))
unit: bits
tolerance: 0

### How many guard bits does the accumulator need to never overflow?

::: misses
- [any] $\lceil\log_2K\rceil$.
:::
:::
:::

::: check
kind: derivation
vars: e = 1..254, f = 0..0.999

## 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
answer: (1 + f)*2^(e - 127)

### What value does it represent (sign positive)?
:::

::: step
answer: 2^(e - 150)

### What is its ULP?
:::

::: step
answer: 2^(-23)/(1 + f)

### What is the ULP relative to the value?
:::

::: right
Right. The relative spacing stays between $2^{-24}$ and $2^{-23}$ across the whole range: that's what floating point buys.
:::
:::

::: explain
## Why do signal-processing datapaths usually saturate on overflow instead of wrapping?

::: model
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

::: check
given: m = 2 | 6; n = 10 | 14
answer: 2^(m - 1) - 2^(-n)
tolerance: 0.0001%

## Largest Q{m}.{n} value?

::: misses
- [any] $2^{m-1} - 2^{-n}$.
:::
:::

::: check
given: K = 10 | 30 | 100 | 255 | 500 | 1000 | 3000
answer: ceil(log(K)/log(2))
tolerance: 0

## Guard bits for {K} terms?

::: misses
- [any] $\lceil\log_2K\rceil$.
:::
:::

::: check
given: e = 128 | 135; f = 0.25 | 0.625
answer: (1 + f)*2^(e - 127)
tolerance: 0.0001%

## Single: exponent field {e}, fraction {f}?

::: misses
- [any] $(1 + f)2^{e - 127}$.
:::
:::
:::
