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
floatvalues, 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 = 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?
1.000 000 12. 0.1 isn't representable in binary; the nearest float is 0.100 000 001 5, and each addition rounds again. The errors happen not to cancel. This is why programs compare floats with a tolerance, and why money is counted in integer cents.
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 ), 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 is for
float, relative precision about , roughly 7 decimal digits. - Integers: every integer up to = 16 777 216 is exact; beyond, odd integers aren't representable. A
floatcounter stops counting at . - 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
- Split the fields. In binary: sign 0; exponent bits 1000 0000 = 128; significand bits 0x490FDB = 4 788 187.
- The exponent.
- The value.
- Compare with π. It's the float nearest π, which is 0.000 000 09 too large.
- Its neighbors. They are 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
- In software. per sample, so 625 samples per second at 100 MHz.
- With the FPU. per sample, 12 500 per second, twenty times as many.
- 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 for = 1 to 100 000 in float, once forward and once backward, and compare with the exact 12.090 146.
Show the solution
- Forward. The running sum is already about 12 when the terms shrink to , and each addition rounds to a ULP of about , so many terms are partly lost.
- The forward result. It is 12.090 851, wrong in the fifth significant figure.
- Backward. The small terms accumulate among themselves first, and the result is 12.090 153, wrong in the seventh.
- Compare. The numbers and the operations are the same, and the order alone gives a hundred times less error.
- 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 with = 3000000 and = 0.01.
(a)
What is the spacing of floats near 3000000?
(b)
What does the expression return? (Round 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 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.
Without a floating-point unit, every float operation is a call to a software routine that unpacks the exponent and significand, aligns them, operates with integer instructions, normalizes and rounds. That costs tens to hundreds of cycles per operation against one or two for an integer multiply-add, so a loop that fit its time budget in fixed point can take ten or more times as long in float and miss its deadline. Floating point is the right choice when the processor has an FPU, where a float multiply-add takes a few cycles; when the values' range is too wide for one fixed scaling; or when the code runs rarely, where readability and freedom from overflow outweigh the cost.
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.