One letter makes your ESP32-S3 math 17× slower

A float literal without the f suffix is a double. The ESP32-S3 FPU only handles single precision, so one missing letter moves your math from hardware into software routines.

In short

  • x * 0.9999 + 0.0001 with a float x takes 225 clock cycles per step. With 0.9999f and 0.0001f it takes 13.
  • The accidental version is slower even than using double on purpose (155 cycles), because it converts float → double → float on every step.
  • Arduino's PI, TWO_PI, DEG_TO_RAD and friends are double constants, so angle * DEG_TO_RAD falls into the same trap.
  • Fix: add f, cast Arduino constants with float(...), and turn on -Wdouble-promotion and -Wfloat-conversion. In Arduino IDE also set Compiler warnings to anything but None, or the warnings stay silent.

Two lines, one letter

These two lines look almost identical:

x = x * 0.9999f + 0.0001f;   // 13 cycles per step
x = x * 0.9999  + 0.0001;    // 225 cycles per step

In both, x is a float. The only difference is the f after each constant. On an ESP32-S3 at 240 MHz, the second line is about 17 times slower.

The FPU only does single precision

The ESP32 and the ESP32-S3 have a hardware floating-point unit, but it only supports single precision (32-bit float). Espressif says this directly in the ESP-IDF speed optimization guide:

ESP-IDF: Speed Optimization

“Avoid using double precision floating point arithmetic double. These calculations are emulated in software and are very slow.”

So float math runs on the FPU, and double math becomes calls into a software library that does the arithmetic with integer instructions.

Why 0.9999 is a double

In C and C++, a floating-point literal without a suffix has type double. Only the f (or F) suffix makes it a float:

0.9999     // double
0.9999f    // float
0.9999L    // long double

When an arithmetic expression mixes a float and a double, the usual arithmetic conversions convert the float operand to double. So x * 0.9999 + 0.0001 really means:

x = (float)((double)x * 0.9999 + 0.0001);

Four steps, every iteration: widen x to double, multiply in software, add in software, narrow the result back to float for the assignment.

What the compiler generates

The disassembly of the Arduino build (arduino-esp32 3.3.12, -Os) shows the difference clearly. With the f, the body of the loop is a single FPU instruction: madd.s multiplies and adds in one step, with no intermediate rounding.

With f

madd.s  f1, f0, f2

Without f

callx8  a8   # __extendsfdf2
callx8  a8   # __muldf3
callx8  a8   # __adddf3
callx8  a8   # __truncdfsf2

Without the f, every iteration makes four calls into software floating-point routines. The calls are indirect: the address of each function is loaded into register a8 with l32r, and callx8 a8 jumps there. The function names above are added as comments.

RoutineWhat it does
__extendsfdf2widens a float to a double
__muldf3multiplies two double values
__adddf3adds two double values
__truncdfsf2narrows a double back to a float

These are the standard GCC soft-float library routines. On the ESP32-S3 they live in the chip's ROM.

Measured: 13 vs 225 cycles

Each test runs a chain of 100,000 dependent operations and counts CPU clock cycles. The result is the minimum of five runs, divided by the number of iterations.

The accidental double is slower than a deliberate double: on top of the software multiply and add, it pays for two conversions per step. You get the precision of a float and worse speed than a double.

The full set of Arduino measurements:

TestCodeCycles / step
floatx * 0.9999f + 0.0001f13.1
trapx * 0.9999 + 0.0001, x is float225.4 (17×)
double on purposex * 0.9999 + 0.0001, x is double155.4
Arduino constant trapa * DEG_TO_RAD + 1.0f, a is float221.0 (18×)
fixeda * float(DEG_TO_RAD) + 1.0f12.1
sinfsinf(x) + 0.5f177.9
sin of a floatsin(x) + 0.5f, x is float177.9
sin of a doublesin(x) + 0.5, x is double1987.6

At 240 MHz, 13 cycles is about 54 ns and 225 cycles is about 0.94 µs per step. In a loop that runs thousands of times per second, that difference adds up quickly.

The Arduino constants trap

Arduino makes this easy to fall into. The math constants in the core's Arduino.h are plain literals without a suffix, so they are double:

// arduino-esp32, cores/esp32/Arduino.h
#define PI         3.1415926535897932384626433832795
#define HALF_PI    1.5707963267948966192313216916398
#define TWO_PI     6.283185307179586476925286766559
#define DEG_TO_RAD 0.017453292519943295769236907684886
#define RAD_TO_DEG 57.295779513082320876798154814105

So this very common line runs in software, even though angle and rad are both float:

float rad = angle * DEG_TO_RAD;   // double math!

In the benchmark this costs 221 cycles per step, against 12 for the fixed version.

Why Arduino Uno users never noticed

On the Arduino Uno and other AVR boards, double is the same 4-byte type as float. The Arduino reference says so: “On the UNO and other ATMEGA based boards, this occupies 4 bytes.” Writing 0.9999 instead of 0.9999f there costs nothing.

Arduino Uno (AVR)

sizeof(double) == 4

ESP32-S3

sizeof(double) == 8

On the ESP32 family double is a real 64-bit type, and code written with AVR habits silently becomes software math.

How to fix it

1. Add f to constants, cast Arduino constants

x = x * 0.9999f + 0.0001f;
float rad = angle * float(DEG_TO_RAD);

The cast is evaluated at compile time, so float(DEG_TO_RAD) costs nothing at run time.

2. Let the compiler find the rest

GCC has two warnings for this. Neither is part of -Wall or -Wextra, so they have to be enabled explicitly. In a sketch, put these two lines at the top:

#pragma GCC diagnostic warning "-Wdouble-promotion"
#pragma GCC diagnostic warning "-Wfloat-conversion"

Enable both. In our tests -Wdouble-promotion pointed at x * 0.9999, but in plain C it did not flag sin(x) with a float argument; -Wfloat-conversion caught both. The old line now produces:

warning: implicit conversion from 'float' to 'double' to match other operand of binary expression [-Wdouble-promotion]

3. Turn on compiler warnings in Arduino IDE

This step is easy to miss. By default, Arduino IDE sets Compiler warnings to None, and with that setting the ESP32 core passes -w to the compiler, which suppresses all warnings, including the ones the pragmas just enabled.

Change it in File → Preferences → Compiler warnings (on macOS: Arduino IDE → Settings):

Compiler warningsFlag passed by the coreWarnings in our sketch
None (default)-w0
Default—5
More-Wall5
All-Wall -Wextra5

Default is enough. Arduino support notes that on More and All some platforms turn warnings into errors. With arduino-esp32 3.3.12 our sketch still built at those levels.

What is not a trap

A few similar-looking cases turned out to be fine. They are worth knowing so you don't “fix” code that is already fast.

Test setup and code

BoardESP32-S3 (QFN56) rev v0.2, 2 cores at 240 MHz, 16 MB flash, 8 MB PSRAM
Corearduino-esp32 3.3.12, board esp32:esp32:esp32s3, default settings (-Os)
Toolchainarduino-cli from Arduino IDE 2.3.8
Methodchains of 100,000 dependent operations, CPU cycles via ESP.getCycleCount(), minimum of 5 runs
Cross-checkthe same tests in plain C with ESP-IDF v5.5.6 (-Os): 13.0 / 224.5 / 154.8 cycles
Uno checkarduino:avr:uno (core 1.8.8), compile-time static_assert

The measurements apply to the ESP32-S3. The classic ESP32 has the same kind of single-precision FPU, but the exact cycle counts were not measured on it.

float_trap.ino (shortened) · full file
#pragma GCC diagnostic warning "-Wdouble-promotion"
#pragma GCC diagnostic warning "-Wfloat-conversion"

#define N    100000
#define RUNS 5

volatile float  vf = 0.999f;  // volatile: the compiler cannot know these values
volatile double vd = 0.999;
volatile float  sink_f;
volatile double sink_d;

__attribute__((noinline)) void mul_float() {
  float x = vf;
  for (int i = 0; i < N; i++) x = x * 0.9999f + 0.0001f;
  sink_f = x;
}
__attribute__((noinline)) void mul_float_trap() {
  float x = vf;
  for (int i = 0; i < N; i++) x = x * 0.9999 + 0.0001;
  sink_f = x;
}

// ... mul_double, deg_trap, deg_fixed and the sin() tests: see the full file

void loop() {
  uint32_t best = UINT32_MAX;
  for (int r = 0; r < RUNS; r++) {
    uint32_t c0 = ESP.getCycleCount();
    mul_float_trap();
    uint32_t dt = ESP.getCycleCount() - c0;
    if (dt < best) best = dt;
  }
  Serial.printf("%.1f cycles/iter\n", (double)best / N);
}

Also: avr_double_size.ino, the compile-time check for the Uno.

Sources

  1. ESP-IDF Speed Optimization (ESP32-S3) and (ESP32) “Even though ESP32 has a single precision hardware floating point unit…” · “These calculations are emulated in software and are very slow.”
  2. cppreference: floating-point literal “(no suffix) defines double; f F defines float”
  3. cppreference: usual arithmetic conversions “the operand of the type with the lesser floating-point conversion rank is converted to the type of the other operand”
  4. Xtensa Instruction Set Architecture Reference Manual, MADD.S “Using IEEE754 single-precision arithmetic, MADD.S multiplies … adds the product … with no intermediate round”
  5. GCC Internals: Soft float library routines __muldf3, __adddf3, __extendsfdf2, __truncdfsf2
  6. arduino-esp32: cores/esp32/Arduino.h #define PI 3.1415926535897932384626433832795 and the other constants, without a suffix
  7. Arduino Reference: double “On the UNO and other ATMEGA based boards, this occupies 4 bytes.”
  8. GCC Warning Options -Wdouble-promotion: “Give a warning when a value of type float is implicitly promoted to double” · -w: “Inhibit all warning messages”
  9. arduino-ide issue #1630 “The default value of the "Compiler warnings" preference is "None"”
  10. Arduino Support: Compiler warnings being treated as errors and arduino-ide issue #2316 where the setting is and what the levels do
  11. cppreference: std::sin float sin(float num);

Video

A short video on the same topic is on the ESPSSS YouTube channel (2:08, 2026-11-16). This page goes into more detail.

Also as a YouTube Short.