gcc の -O3 と -Ofast の最適化の違い 質問する

gcc の -O3 と -Ofast の最適化の違い 質問する

-O3との違いを調べるために、gcc マニュアルを読んでいました-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

-オファスト

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

Ofastは、浮動小数点セマンティクスの C 標準の要件に違反する最適化を有効にします。特に、-Ofast(別名-ffast-math) では、浮動小数点計算を自由に並べ替えます (一般にa + (b + c) != (a + b) + c != (a + c) + b浮動小数点の場合、デフォルトでは禁止されています)。

特定のケースで安全かどうかは-Ofastアルゴリズムによって異なりますが、いつものほとんどの非科学的なアプリケーションはこれで問題なく動作します。たとえば、ほとんどのゲーム エンジンは で構築されています-ffast-math

編集:

2021年以降、-OfastGCCでも-fno-semantic-interposition詳細に説明されているように有効になりますここ

おすすめ記事