gcc浮点运算的一个编译选项
一些浮点运算可能出现一些意外状况,譬如同事使用的ceil:
g++ -O3 结果总是会有些出入,原因如下:
-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.
-ffloat-store
Might help a Fortran program that depends on exact IEEE conformance on some machines, but might slow down a program that doesn't.
This option is effective when the floating-point unit is set to work in IEEE 854 `extended precision'--as it typically is on x86 and m68k GNU systems--rather than IEEE 754 double precision. -ffloat-store tries to remove the extra precision by spilling data from floating-point registers into memory and this typically involves a big performance hit. However, it doesn't affect intermediate results, so that it is only partially effective. `Excess precision' is avoided in code like:
a = b + cbut not in code like:
d = a * e
d = (b + c) * e
For another, potentially better, way of controlling the precision, see Floating-point precision.
但即便使用该参数也有可能会出问题:
Floating-point precision
If your program depends on exact IEEE 754 floating-point handling it may help on some systems--specifically x86 or m68k hardware--to use the -ffloat-store option or to reset the precision flag on the floating-point unit. See Optimize Options.
However, it might be better simply to put the FPU into double precision mode and not take the performance hit of -ffloat-store. On x86 and m68k GNU systems you can do this with a technique similar to that for turning on floating-point exceptions (see Floating-point Exception Handling). The control word could be set to double precision by some code like this one:
#include <fpu_control.h>(It is not clear whether this has any effect on the operation of the GNU maths library, but we have no evidence of it causing trouble.)
{
fpu_control_t cw = (_FPU_DEFAULT & ~_FPU_EXTENDED) | _FPU_DOUBLE;
_FPU_SETCW(cw);
}
RelatedEntries:
ubuntu8.10[Intrepid Ibex]mac风格美化 - 11 18, 2008
ubuntu 下vpn - 09 05, 2008
accept Serialization - multiple sockets - 04 29, 2008
gcc -fPIC/-fpic - 08 24, 2007
关于hash_map的一个例子 - 04 26, 2007