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

标签 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
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实际上安全吗?

最佳答案

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

是否-Ofast在特定情况下是否安全取决于算法,但通常大多数非科学应用程序都能很好地使用它。例如,大多数游戏引擎都是用 -ffast-math 构建的。 .

关于-O3 与 -Ofast 优化之间的 gcc 差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61232427/

相关文章:

gcc/ld : undefined reference to unused function

c - 如何让编译器处理这个函数呢?

json - jenkins Shell中解析json文件时出现jq错误

gcc - 从 GCC 获取优化报告

c - NASM 汇编数理逻辑

c++ - 打印到文件时退格字符不起作用

c - 如何正确解决 -Wcast-qual

c# - 编译时出现 MonoTouch 错误:System.Boolean System.Type::op_Equality(System.Type,System.Type)

c++ - 类引用成员指向同一类中的其他成员

c - GCC 优化标志的顺序