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.0001with afloat xtakes 225 clock cycles per step. With0.9999fand0.0001fit takes 13.- The accidental version is slower even than using
doubleon purpose (155 cycles), because it convertsfloat → double → floaton every step. - Arduino's
PI,TWO_PI,DEG_TO_RADand friends aredoubleconstants, soangle * DEG_TO_RADfalls into the same trap. - Fix: add
f, cast Arduino constants withfloat(...), and turn on-Wdouble-promotionand-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:
“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.
| Routine | What it does |
|---|---|
__extendsfdf2 | widens a float to a double |
__muldf3 | multiplies two double values |
__adddf3 | adds two double values |
__truncdfsf2 | narrows 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:
| Test | Code | Cycles / step |
|---|---|---|
| float | x * 0.9999f + 0.0001f | 13.1 |
| trap | x * 0.9999 + 0.0001, x is float | 225.4 (17×) |
| double on purpose | x * 0.9999 + 0.0001, x is double | 155.4 |
| Arduino constant trap | a * DEG_TO_RAD + 1.0f, a is float | 221.0 (18×) |
| fixed | a * float(DEG_TO_RAD) + 1.0f | 12.1 |
sinf | sinf(x) + 0.5f | 177.9 |
sin of a float | sin(x) + 0.5f, x is float | 177.9 |
sin of a double | sin(x) + 0.5, x is double | 1987.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"
-Wdouble-promotionwarns when afloatis implicitly promoted todouble, as inx * 0.9999.-Wfloat-conversionwarns when a value loses precision implicitly, for example when adoubleresult is assigned back to afloat.
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 warnings | Flag passed by the core | Warnings in our sketch |
|---|---|---|
| None (default) | -w | 0 |
| Default | — | 5 |
| More | -Wall | 5 |
| All | -Wall -Wextra | 5 |
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.
sin(x)with afloat xin a sketch. In C++,<cmath>provides an overloadfloat sin(float), andArduino.hincludes it. The measurement confirms it: 177.9 cycles, exactly the same assinf. In plain C (for example, a.cfile in an ESP-IDF project) there is no overload, andsin(x)really does run in double precision: 2036 cycles against 177 forsinf.pow(x, 2). The compiler turns it intox * xon the FPU (mul.s).- Constants that are exact in
float, like0.5. Forx * 0.5the compiler knows the result is the same in single precision and emitsmul.s. The trap triggers on “uneven” constants such as0.9999orPI.
Test setup and code
| Board | ESP32-S3 (QFN56) rev v0.2, 2 cores at 240 MHz, 16 MB flash, 8 MB PSRAM |
| Core | arduino-esp32 3.3.12, board esp32:esp32:esp32s3, default settings (-Os) |
| Toolchain | arduino-cli from Arduino IDE 2.3.8 |
| Method | chains of 100,000 dependent operations, CPU cycles via ESP.getCycleCount(), minimum of 5 runs |
| Cross-check | the same tests in plain C with ESP-IDF v5.5.6 (-Os): 13.0 / 224.5 / 154.8 cycles |
| Uno check | arduino: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.
#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
- 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.”
- cppreference: floating-point literal “(no suffix) defines double; f F defines float”
- 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”
- Xtensa Instruction Set Architecture Reference Manual, MADD.S “Using IEEE754 single-precision arithmetic, MADD.S multiplies … adds the product … with no intermediate round”
- GCC Internals: Soft float library routines
__muldf3,__adddf3,__extendsfdf2,__truncdfsf2 - arduino-esp32: cores/esp32/Arduino.h
#define PI 3.1415926535897932384626433832795and the other constants, without a suffix - Arduino Reference: double “On the UNO and other ATMEGA based boards, this occupies 4 bytes.”
- GCC Warning Options
-Wdouble-promotion: “Give a warning when a value of type float is implicitly promoted to double” ·-w: “Inhibit all warning messages” - arduino-ide issue #1630 “The default value of the "Compiler warnings" preference is "None"”
- Arduino Support: Compiler warnings being treated as errors and arduino-ide issue #2316 where the setting is and what the levels do
- 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.