-O3与-Ofast优化之间的gcc差异

时间:2020-04-15 15:23:14

标签: gcc compilation compiler-optimization

我只是在阅读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

-Ofast

-Ofast

Disregard strict standards compliance. -Ofast enables all -O3 optimizations. It also enables optimizations that are not valid for

所有符合标准的程序。它打开-ffast-math, -fallow-store-data-races和特定于Fortran的-fstack-arrays,除非指定了-fmax-stack-var-size和-fno-protect-parens

因此,我想知道,也许-Ofast出于某种原因不如-O3安全,因此那时候我应该坚持使用-O3

使用它们时,您能否澄清“实际差异”?如果-Ofast实际上是安全的?

1 个答案:

答案 0 :(得分:0)

Ofast启用了违反C标准对浮点语义的要求的优化。特别是在-Ofast(又名-ffast-math)下,它将自由地对浮点计算进行重新排序(默认情况下是禁止使用的,因为通常对浮点使用a + (b + c) != (a + b) + c != a + (c + b))。

在特定情况下,-Ofast是否安全取决于算法,但是通常大多数非科学应用都可以使用该算法。例如,大多数游戏引擎都是使用-ffast-math构建的。