Beware of fast-math

Date: 10/25/2023 · Tags: #til, #system-programming, #clang

fast-math is a set of compiler flags that enables a series of optimizations for floating-point arithmetic.

  • -ffast-math (and included by -Ofast) in GCC and Clang
  • -fp-model=fast (the default) in ICC
  • /fp:fast in MSVC
  • --math-mode=fast command line option or @fastmath macro in Julia.

But please notice that fast-math is not free lunch. Use it with cautions 😲.

Hence check this post 1 to see what innocuous performance speedup or unfortunate downstream effects fast-math would bring to you.

I mean, the whole point of fast-math is trading off speed with correctness. If fast-math was to give always the correct results, it wouldn’t be fast-math, it would be the standard way of doing math. -- Mosè Giordano 2

Footnotes

  1. Simon's notes - Beware of fast-math

  2. What’s going on with exp() and –math-mode=fast?