Question

Can any one help in making me understand what does -ffast-math option does when compiled with gcc. I see a difference of 20 sec in my programs execution time when executed with -O3 and -ffast-math compared to only use of -O3

Was it helpful?

Solution

Why not read the gcc man page, it's your friend as well as mine. Here's what it told me:

Sets -fno-math-errno, -funsafe-math-optimizations, -ffinite-math-only, -fno-rounding-math, -fno-signaling-nans and -fcx-limited-range.

So it doesn't do anything interesting by itself, but is just a shorthand to several more interesting compiler options. What do the individual flags do?

  • fno-math-errno makes single-instruction math operations not set ERRNO
  • funsafe-math-optimizations allows math optimizations that assume valid arguments and can violate ANSI and IEEE standards (caution, not actually fun and safe)
  • ffinite-math-only, likewise, allows math optimizations that assume that any floating point values are neither infinite nor NaN
  • fno-rounding-math and fno-signaling-nans are actually on by default. Their opposites frounding-math and fsignaling-nans disable some potentially unsafe/unportable optimizations.
  • fcx-limited-range allows the compiler to not do certain complex number arithmetic checks. Not likely to impact your program unless you're actually working with complex numbers!

In short, it allows the compiler to optimize your program at the cost of losing standard compliance and some safety.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top