What does GCC's -Ofast enable beyond -O3, and why can it silently break a correct floating-point program?
answer
- -O3 plus non-conforming flags
- -ffast-math is a bundle
- no NaNs, no infinities assumed
- reassociation changes sums
- denormals flushed process-wide
basics
~20 s-Ofast is -O3 plus -ffast-math and -fallow-store-data-races. Fast-math assumes no NaNs or infinities, allows reordering floating-point operations and skips errno, so NaN checks can vanish, sums change, and on x86 linked startup code flushes denormals to zero.
solid answer
~40 sGCC's manual describes `-Ofast` as disregarding strict standards compliance: it enables all of `-O3` and turns on `-ffast-math`, `-fallow-store-data-races` and, for Fortran, `-fstack-arrays`, while turning off `-fsemantic-interposition`. `-ffast-math` is a bundle: `-fno-math-errno`, `-funsafe-math-optimizations` (which brings `-fassociative-math`, `-freciprocal-math`, `-fno-signed-zeros`, `-fno-trapping-math`), `-ffinite-math-only`, `-fno-rounding-math`, `-fno-signaling-nans`, `-fcx-limited-range` and `-fexcess-precision=fast`. Consequences: `std::isnan` checks may be folded to `false`; reassociation reorders sums, so compensated summation can be optimized away; and when an executable is linked with these flags on x86, startup code sets flush-to-zero and denormals-are-zero for the whole process. `-fallow-store-data-races` may add stores that race in multithreaded code. Prefer `-O3` with individually chosen flags, scoped to the files that need them.
code
cpp · 16 lines#include <cmath>
#include <cstdio>
#include <limits>
// Returns how many samples are NaN.
int count_nan(const double* v, int n) {
int bad = 0;
for (int i = 0; i < n; ++i)
if (std::isnan(v[i])) ++bad;
return bad;
}
int main() {
double samples[] = {1.0, std::numeric_limits<double>::quiet_NaN(), 3.0};
std::printf("NaN samples: %d\n", count_nan(samples, 3));
}go deeper
Know that -Ofast goes beyond -O3 and may change floating-point results.
List what -ffast-math bundles and give a concrete casualty, such as a folded NaN check.
Explain reassociation, process-wide denormal flushing from the link step and the store-data-race flag, and scope relaxed math to specific files.
Set the policy for relaxed floating point: which components may use it, how results are validated, and that it never reaches the executable's link by accident.
## What -Ofast adds `-Ofast` is the only `-O` level that is allowed to change the meaning of correct programs. In GCC 16 the manual says it enables all `-O3` optimizations plus optimizations that are not valid for all standard-compliant programs: - `-ffast-math`; - `-fallow-store-data-races`; - `-fstack-arrays` for Fortran (unless `-fmax-stack-var-size` is given) and `-fno-protect-parens`; - and it turns **off** `-fsemantic-interposition`. For `-ffast-math` and several of its components the manual repeats that they are **not turned on by any `-O` option besides `-Ofast`**, because it can give incorrect output for programs that depend on an exact implementation of IEEE or ISO rules. ## The -ffast-math bundle | Flag | Assumption or permission | What can break | |---|---|---| | `-fno-math-errno` | math functions need not set `errno` | code that checks `errno` after `sqrt` | | `-fassociative-math` | `(a + b) + c` may become `a + (b + c)` | compensated summation, order-dependent results | | `-freciprocal-math` | `x / y` may become `x * (1 / y)` | last-bit accuracy | | `-fno-signed-zeros` | the sign of zero does not matter | `1 / -0.0`, branch cuts | | `-fno-trapping-math` | operations do not trap | code relying on floating-point exceptions | | `-ffinite-math-only` | no NaNs or infinities | `std::isnan`, `std::isinf`, NaN propagation | | `-fcx-limited-range` | simple complex division is fine | complex results near overflow | `-ffast-math` also defines the macro `__FAST_MATH__`, so headers and code can detect it. ## Three ways it silently changes a correct program 1. **NaN checks disappear.** Under `-ffinite-math-only` the compiler may assume a value is never NaN, so `if (std::isnan(x))` can be folded to `false`. A pipeline that filters bad sensor readings stops filtering them, and the NaNs flow on. 2. **Reassociation rewrites arithmetic.** Kahan summation exists to preserve the rounding error of each step; algebraically the error term is zero, so with `-fassociative-math` the compiler may remove it. Vectorized reductions also add elements in a different order from the source loop. 3. **Denormals vanish process-wide.** The x86 manual entry for `-mdaz-ftz` explains that `-Ofast`, `-ffast-math` and `-funsafe-math-optimizations` normally link in startup code that sets the flush-to-zero and denormals-are-zero flags, except when building a shared library. Linking the **executable** with these flags therefore changes denormal handling for every library in the process, including code compiled carefully without fast-math. ## The data-race flag `-fallow-store-data-races` lets the optimizer introduce stores to global data on paths that did not store before, for example writing back an unchanged value after if-conversion. The manual says that is safe only if the global data is not accessed by multiple threads. In a multithreaded program it can overwrite another thread's update, a bug that looks like random corruption. ## Seeing reassociation remove a correction Compensated summation keeps the rounding error of each addition in a separate variable: ```cpp double kahan_sum(const double* v, int n) { double sum = 0.0, c = 0.0; for (int i = 0; i < n; ++i) { double y = v[i] - c; double t = sum + y; c = (t - sum) - y; // algebraically zero; numerically the lost low bits sum = t; } return sum; } ``` In real arithmetic `(t - sum) - y` is zero, and `-fassociative-math` allows the compiler to reason exactly that way, so it may simplify `c` to zero and turn the loop back into a naive sum. The code still compiles and runs; it just loses the accuracy it was written for. Summing `1e16` followed by a million `1.0` values with this function, a Clang build (Clang shares the flag) printed `10000000001000000.0` at `-O2` and `10000000000875000.0` with `-ffast-math` added. Some numerical code that relies on such tricks checks `__FAST_MATH__` and refuses to build when it is defined. ## Using fast math deliberately - Prefer `-O3` and add only the flags you have reasoned about, for example `-fno-math-errno`, which is the default on Darwin anyway because its math library never sets `errno`. - Scope relaxed math to the translation units that need it, such as a rendering or DSP kernel, and keep it off the link line of the executable if denormal flushing is not wanted; `-mno-daz-ftz` controls that explicitly on x86. - Test with the same flags you ship, with tolerances rather than exact comparisons. Clang takes a firmer line on the shared flag: since Clang 19 `-Ofast` is deprecated there, with `-O3 -ffast-math` suggested instead. GCC 16 documents `-Ofast` without a deprecation.
- Why can linking an executable with -Ofast change results in a library compiled without it?On x86, `-Ofast`, `-ffast-math` and `-funsafe-math-optimizations` normally link startup code that sets flush-to-zero and denormals-are-zero in the MXCSR register. That register is part of the thread's floating-point state, which threads created later inherit, so the library's carefully compiled code also sees denormals flushed. Shared-library links skip this, and `-mno-daz-ftz` disables it.
- Which single fast-math flag is usually safe to enable on its own?`-fno-math-errno` is the common choice: it lets `sqrt` and similar functions compile to single instructions without setting `errno`, while keeping IEEE arithmetic intact. It only breaks code that checks `errno` after math calls, which is rare and easy to audit.
- -Ofast is just a faster -O3 with no change in meaning.
- -ffast-math only affects speed, never results.
- std::isnan always works regardless of compiler flags.
- Fast-math settings in one translation unit cannot affect code compiled without them.
- -Ofast is safe for multithreaded code because it only touches floating point.
Assumes GCC 16; the -Ofast and -ffast-math flag lists and the x86 -mdaz-ftz startup behaviour are read from its manual.