S310 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 −2m−1 to 2m−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.

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?

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 2−n:

  • resolution 2−n;
  • range −2m−1 to 2m−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 ⌈log2⁡K⌉ 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×1.f×2e−127. Binary64 has 11 exponent bits (bias 1023) and 52 fraction bits. The spacing between adjacent numbers, one ULP, is 2e−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.

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
  1. Each product. Q1.15 times Q1.15 is Q2.30, 32 bits.
  2. Guard bits for the sum. Summing 64 of them needs log2⁡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.

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
  1. Exponent. 130−127=3
  2. Significand. The fraction 0x200000 is 221223=0.25 so the significand is 1.25.
  3. Value. 1.25×23=10.0
  4. ULP. ULP=23−23=2−20≈9.5×10−7 Numbers near 10 are spaced about a millionth apart.

Answer the number is 10.0, and its ULP is 2−20≈9.5×10−7.

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
  1. Scale and round. 0.3×215=9830.4 which rounds to 9830 (0x2666).
  2. Value represented. 983032768=0.2999878
  3. Error. 1.22×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×10−5 (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°.

Explain why

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

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.