我只是阅读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
实际上是安全的?
Ofast
启用了违反C标准对浮点语义的要求的优化。特别是在-Ofast
(又名-ffast-math
)下,它将自由地对浮点计算进行重新排序(默认情况下是禁止使用的,因为通常对浮点使用a + (b + c) != (a + b) + c != a + (c + b)
)。
-Ofast
是否安全在特定情况下取决于算法,但通常大多数非科学应用程序都可以使用它。例如,大多数游戏引擎都是使用-ffast-math
构建的。