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

C8 · Lesson 21 · Floating point on a processor

Float registers, what software floating point costs without them, and where the rounding bites

Lesson 20 did arithmetic on fractions with integers and a fixed binary point. Most programs use floating point instead, the IEEE 754 format of the digital logic course: a sign, an exponent and a 24-bit significand in a 32-bit float. On a processor with a floating-point unit, RISC-V's F extension, a fadd.s takes a few cycles from a separate bank of 32 float registers. On a small microcontroller without one, the compiler calls a library routine that unpacks, aligns, adds, normalizes and rounds in integer instructions, and the same line of C costs fifty times as much. Either way the results are rounded, and the rounding produces answers that surprise people who expect real arithmetic.

  • The spacing between adjacent float values, one unit in the last place (ULP), against the number's size. It doubles at every power of two: a float carries about seven significant digits wherever it is.
  • The dashed line is 1, the size of the smallest step an integer counter can take. Past 224 = 16 777 216 the floats are further apart than 1, so adding 1 does nothing.
  • Slide the value and read its ULP and how many floats lie between it and its double.

Predict

In C on any IEEE 754 machine, a float starts at 0 and has 0.1f added ten times. What does it hold?

Name it

Floating point in hardware

RISC-V's F extension adds 32 float registers f0 to f31 and instructions such as flw/fsw (load and store), fadd.s, fmul.s, fdiv.s, fsqrt.s, fused multiply-add fmadd.s (one rounding for a×b+c), and conversions fcvt.w.s (float to integer) and fcvt.s.w. The D extension adds double. Typical latencies are a few cycles for add and multiply, ten to twenty or more for divide and square root.

Floating point in software

Without an FPU, a + b on floats becomes a call to a routine such as __addsf3: extract the exponents, shift the smaller number's significand right to align, add or subtract, normalize, round to nearest even, and handle zero, infinity and NaN. That's tens of instructions on the common path, often 50 to 100 cycles, and multiplication and division cost more. On a microcontroller without an FPU, fixed point (lesson 20) is often ten times faster, which is why firmware that runs a control loop at tens of kilohertz uses it.

Where rounding shows

  • Spacing: the gap between neighbors near x is 2⌊log2⁡x⌋−23 for float, relative precision about 6×10−8, roughly 7 decimal digits.
  • Integers: every integer up to 224 = 16 777 216 is exact; beyond, odd integers aren't representable. A float counter stops counting at 224.
  • Absorption: adding a small number to a large one can do nothing at all.
  • Cancellation: subtracting nearly equal numbers leaves few correct digits.
  • Order matters: addition isn't associative, so summing small terms first is more accurate. Kahan summation carries the lost low-order bits forward in a second variable.

Worked example

Decode the float bit pattern 0x40490FDB.

Show the solution
  1. Split the fields. In binary: sign 0; exponent bits 1000 0000 = 128; significand bits 0x490FDB = 4 788 187.
  2. The exponent. 128−127=1
  3. The value. x=(1+4788187223)×21=1.57079637×2=3.14159274
  4. Compare with π. It's the float nearest π, which is 0.000 000 09 too large.
  5. Its neighbors. They are 21−23=2.4×10−7 away.

Answer 0x40490FDB is 3.141 592 74, the float nearest π.

Worked example

A microcontroller without an FPU runs a 1000-tap filter in float at 100 MHz. Each multiply-add costs 160 cycles in software. A version with an FPU takes 8 cycles per multiply-add. How many output samples per second can each produce?

Show the solution
  1. In software. 1000×160=160000cycles per sample, so 625 samples per second at 100 MHz.
  2. With the FPU. 1000×8=8000cycles per sample, 12 500 per second, twenty times as many.
  3. Fixed point. A fixed-point version with a single-cycle 32-bit multiplier and accumulator might take 2 to 3 cycles per tap, faster still, at the cost of managing the scaling by hand.

Answer 625 samples per second in software and 12 500 with the FPU.

Worked example

Sum 1/k for k = 1 to 100 000 in float, once forward and once backward, and compare with the exact 12.090 146.

Show the solution
  1. Forward. The running sum is already about 12 when the terms shrink to 10−5, and each addition rounds to a ULP of about 10−6, so many terms are partly lost.
  2. The forward result. It is 12.090 851, wrong in the fifth significant figure.
  3. Backward. The small terms accumulate among themselves first, and the result is 12.090 153, wrong in the seventh.
  4. Compare. The numbers and the operations are the same, and the order alone gives a hundred times less error.
  5. Kahan summation. It gets the correctly rounded float in either order.

Answer forward gives 12.090 851 and backward 12.090 153, against the exact 12.090 146.

Check

What is the spacing between adjacent single-precision floats near 100?

Check

A float has sign 0, stored exponent 3 + 127 and significand bits equal to 1048576. What value does it hold?

Check

A filter needs 1000 multiply-adds per output sample. Without an FPU each costs 80 cycles. At 200 MHz, how many samples per second can it produce?

Check

A float format has a 24-bit significand (counting the hidden 1). What's the largest integer below which every integer is exactly representable?

Check

In float, a program computes (a+d)−a with a = 3000000 and d = 0.01.

(a)

What is the spacing of floats near 3000000?

(b)

What does the expression return? (Round d to the nearest multiple of that spacing.)

Check

A firmware engineer counts milliseconds in a float, adding 1.0f each tick. After about 4.7 hours the count stops increasing. Why?

Check

Compare summation orders in single precision. Write to_f32(x) that rounds a Python float to the nearest float32 (use struct), and fsum32(values) that adds them one by one with a float32 accumulator. Also write kahan32(values), Kahan's compensated sum with every operation rounded to float32. For the harmonic series 1/k, k = 1 to n (20000), set fwd, bwd and kahan (forward order, backward order, Kahan forward), and exact from math.fsum in double.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

A control loop on a microcontroller without an FPU misses its deadlines after a developer changes its arithmetic from fixed point to float "for readability". Explain why, and when floating point would be the right choice anyway.

Practice

Keep practicing

8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.