Intel x86 processors use 80-bit extended precision internally, whereas double
is normally 64-bit wide. Different optimization levels affect how often floating point values from CPU get saved into memory and thus rounded from 80-bit precision to 64-bit precision.
Use the -ffloat-store
gcc option to get the same floating point results with different optimization levels.
Alternatively, use the long double
type, which is normally 80-bit wide on gcc to avoid rounding from 80-bit to 64-bit precision.
man gcc
says it all:
-ffloat-store
Do not store floating point variables in registers, and inhibit
other options that might change whether a floating point value is
taken from a register or memory.
This option prevents undesirable excess precision on machines such
as the 68000 where the floating registers (of the 68881) keep more
precision than a "double" is supposed to have. Similarly for the
x86 architecture. For most programs, the excess precision does
only good, but a few programs rely on the precise definition of
IEEE floating point. Use -ffloat-store for such programs, after
modifying them to store all pertinent intermediate computations
into variables.
In x86_64 builds compilers use SSE registers for float
and double
by default, so that no extended precision is used and this issue doesn’t occur.
gcc
compiler option -mfpmath
controls that.