Try Embedded

Floating point on the Cortex-M33

The Pico 2's FPU and double coprocessor, float or fixed point, and rounding.

Floating point on the Raspberry Pi Pico 2

The Pico's RP2040 has two Cortex-M0+ cores and no floating-point unit: every float add or multiply is a call to a library routine of a few dozen instructions. The Pico 2's RP2350 has two Cortex-M33 cores, each with an FPU for single precision (float), and beside each core a double-precision coprocessor (DCP) that the Pico SDK uses for double. This lesson is about what that changes for firmware: where float is now the natural choice, what double costs, and the rounding that hardware does not take away.

What the FPU is

The Cortex-M33's FPU (FPv5, single precision) adds 32 floating-point registers, S0 to S31, and instructions that work on them: VADD.F32, VMUL.F32, VDIV.F32, VSQRT.F32, conversions (VCVT) and fused multiply-add (VFMA.F32). Most take one cycle; divide and square root take 14. The compiler uses them when the build says the FPU is there (-mfpu=fpv5-sp-d16), which the Arduino core for the Pico 2 and the Pico SDK both do.

Operation, single precisionPico (RP2040, software)Pico 2 (RP2350, FPU)
a + b, a * b40 to 80 cycles1 cycle
a / b100 to 150 cycles14 cycles
sqrtf(a)about 200 cycles14 cycles
int to floatabout 30 cycles1 cycle

(The RP2040's figures are for its ROM's optimised routines, which are already far faster than a generic C library's.)

The FPU is off at reset. The start-up code turns it on by giving coprocessors 10 and 11 full access in CPACR; an FPU instruction before that is a UsageFault (NOCP).

The registers in an interrupt

With the FPU on, an interrupt that arrives while the program uses floating point has more to save: S0 to S15 and FPSCR as well as the usual eight core registers. The Cortex-M33 does this lazily: it reserves room for them on the stack and saves them only if the handler itself uses the FPU. CONTROL.FPCA says whether the interrupted code was using floating point, and the EXC_RETURN value in LR says which size of frame to pop.

So floating point in a handler is allowed, but it costs about 17 more words of stack and some cycles the first time the handler touches the FPU. Many teams keep handlers integer-only and leave the maths to the main loop or a task.

double and the DCP

double has a 53-bit mantissa and the FPU cannot do it. On the RP2350, the SDK sends each double operation to the DCP, a coprocessor reached with the Armv8-M coprocessor instructions (MCRR, MRRC, CDP). It works on a pair of 32-bit registers per operand and does an add in a few steps. A double add or multiply costs roughly 10 to 20 cycles: much faster than software, several times slower than a float one.

The C rules make double easy to use by accident:

#include <stdio.h>

int main(void) {
    float level = 1000.0f;
    // 0.1 is a double constant: this whole line is worked out in double, then rounded back.
    float slow = level * 0.1;
    // 0.1f keeps it in float: one FPU multiply.
    float fast = level * 0.1f;
    printf("%f %f\n", slow, fast);
    // printf takes doubles: a float argument is converted, which is why %f works for both.
    return 0;
}

A literal without f, a call to sin() rather than sinf(), or a double variable turns float code into double code without a warning. -Wdouble-promotion makes GCC say where.

The rounding is still there

An FPU makes float fast; it does not make it exact. A float has 24 bits of mantissa, about 7 decimal digits. Each result is rounded to the nearest value it can hold, and the error grows with the size of the number:

#include <stdio.h>

int main(void) {
    float total = 0.0f;
    for (int i = 0; i < 10000; i++) total += 0.1f;
    printf("10000 x 0.1 = %f\n", total);

    float big = 16777216.0f;          // 2^24
    printf("2^24 + 1 = %.1f\n", big + 1.0f);
    return 0;
}

The sum drifts, because 0.1 has no exact binary form and each addition rounds; past 2^24, adding 1 does nothing at all. For a long-running total (energy, distance, a timer in seconds), keep the count in integers and convert at the end, or use compensated (Kahan) summation, which carries the rounded-away part in a second float.

Never compare floats for equality. Compare with a tolerance that suits the size of the values:

#include <stdio.h>
#include <math.h>
#include <stdbool.h>

bool close_enough(float a, float b) {
    float largest = fabsf(a) > fabsf(b) ? fabsf(a) : fabsf(b);
    return fabsf(a - b) <= 1e-5f * largest;
}

int main(void) {
    float a = 0.0f, b = 1.0f;
    for (int i = 0; i < 10; i++) a += 0.1f;     // ten tenths
    printf("a = %.8f, a == b: %d, close: %d\n", a, a == b, close_enough(a, b));
    return 0;
}

Fixed point or float?

On a core without an FPU, control loops and filters are written in fixed point: Q15 or Q16.16 integers, with the scaling done by shifts (the fixed-point lesson). On the Cortex-M33 the trade changes:

  • float is as fast as fixed point for adds and multiplies, has a far larger range, and needs no scaling or overflow checks.
  • Fixed point is still right where the result must be exact and the same on every build: counters, money, protocol values, and code shared with cores that have no FPU.
  • 16-bit fixed point (Q15) with the DSP instructions does two operations an instruction, so audio and filter code often stays in Q15 for speed (the DSP lesson).

The example project times one PID loop three ways. On a Pico 2, 20 000 steps take about 9 ms in float, 61 ms in double and 6 ms in Q16.16; on a Pico the same sketch takes about 136 ms in float.

Interview question: What does an FPU change about interrupt handling on a Cortex-M33?

Answer: The exception frame can grow: if the interrupted code was using the FPU (CONTROL.FPCA set), S0 to S15 and FPSCR must be preserved as well as the eight core registers. With lazy stacking the core reserves the space but writes the registers only if the handler itself uses the FPU, and EXC_RETURN tells the return which frame size to pop. Handlers that use float cost extra stack and latency, so many designs keep floating point out of interrupts.

Interview question: When would you still choose fixed point on a microcontroller that has an FPU?

Answer: When the result must be exact or bit-for-bit repeatable (counters, money, checksums, values in a protocol), when the same code must run on cores without an FPU, when 16-bit SIMD instructions give two operations an instruction (audio, filters), or when the values have a known small range and integer code is simpler to verify. For general control and sensor maths a single-precision FPU usually makes float the clearer and equally fast choice.

Going deeper

Interview question: Why does adding a small number to a large float sometimes do nothing?

Answer: A float holds 24 significant bits. When the large number is 2^24 or more, the gap between two neighbouring floats is 2 or more, so adding 1 rounds back to the same value. In general a sum loses the low bits of the smaller operand; long totals drift for the same reason. Integer totals, a double accumulator, or Kahan summation avoid it.

Interview question: How does the RP2350 speed up double precision without a double-precision FPU?

Answer: Each core has a double-precision coprocessor (DCP) reached through the Armv8-M coprocessor instructions. The SDK's double routines move the operands into it as register pairs with MCRR, issue operations with CDP, and read the result back with MRRC. It accelerates the add, multiply, divide and square-root steps, so a double operation takes tens of cycles rather than the hundreds a pure software routine would.

Explain it: In your own words, explain why float code on a Pico 2 can be as fast as fixed-point code, and why it was so much slower on a Pico.

Answer: On the Pico 2 each Cortex-M33 core has an FPU, so a float add or multiply is one instruction taking about one cycle, the same as an integer one, and no shifts or overflow checks are needed for scaling. On the Pico's Cortex-M0+ there is no FPU, so every float operation is a library routine of dozens of instructions that unpacks the exponent and mantissa, works on them in integers and packs the result, which is why fixed-point integer code was many times faster there.