GCC中 -O3 优化与 -Ofast 优化的区别

20

我刚刚在阅读gcc手册,想了解-O3-Ofast之间的区别。

对于-O3

-O3

Optimize yet more. -O3 turns on all optimizations specified by -O2 and also turns on the following optimization flags:

-fgcse-after-reload 
-fipa-cp-clone
-floop-interchange 
-floop-unroll-and-jam 
-fpeel-loops 
-fpredictive-commoning 
-fsplit-paths 
-ftree-loop-distribute-patterns 
-ftree-loop-distribution 
-ftree-loop-vectorize 
-ftree-partial-pre 
-ftree-slp-vectorize 
-funswitch-loops 
-fvect-cost-model 
-fversion-loops-for-strides

是GCC编译器的一个选项,它结合了其他优化选项以最大化代码执行速度,但其副作用是牺牲了代码大小和标准的符合性。使用此选项需要谨慎,因为某些代码可能会出现意外行为。

Disregard strict standards compliance. -Ofast enables all -O3 optimizations. It also enables optimizations that are not valid for
all standard-compliant programs. It turns on -ffast-math,
-fallow-store-data-races and the Fortran-specific -fstack-arrays, unless -fmax-stack-var-size is specified, and -fno-protect-parens
因此,我想知道-Ofast是否比-O3不安全,所以大多数情况下我应该坚持使用-O3。您能澄清使用它们时的“实际差异”吗? -Ofast是否真的安全?
1个回答

32
Ofast 能够开启违反 C 标准浮点数语义要求的优化。特别是在 -Ofast(即-ffast-math)下,它会自由地重新排列浮点运算(默认情况下被禁止,因为通常来说浮点运算不满足交换律:a + (b + c) != (a + b) + c != a + (c + b))。
是否在特定情况下使用 -Ofast 是安全的,取决于算法,但通常大多数非科学应用都可以正常工作。例如,大多数游戏引擎都是使用 -ffast-math 来构建的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接