gpt4 book ai didi

c - 数字的安全 gcc 优化选项

转载 作者:太空狗 更新时间:2023-10-29 16:02:24 25 4
gpt4 key购买 nike

哪些 gcc 编译器选项可以安全地用于数值编程?

打开 gcc 优化的简单方法是将 -0# 添加到编译器选项。说 -O3 很诱人。但是我知道 -O3 包括非保存优化,因为一旦包含此选项,数值计算的结果可能会有所不同。如果算法稳定,结果的微小变化可能无关紧要。另一方面,对于某些数学运算,精度可能是一个问题,因此数学优化可能会产生重大影响。

我发现在调试过程中考虑编译器相关问题很不方便。 IE。我不想怀疑代码中的微小变化是否会导致截然不同的行为,因为编译器在内部改变了它的优化。

如果我想在我的代码中实现确定性的——因此是可控的——行为,添加哪些选项是安全的?哪些几乎是安全的,也就是说,与性能优势相比,哪些选项只会带来较小的不确定性?

我想到了像这样的选项:-finline -finline-limit=2000 内联函数,即使它们很长。

最佳答案

-O3 包含数值上不安全的优化是不正确的。根据the manual , -O3-O2 相比包括以下优化过程:

-finline-functions, -funswitch-loops, -fpredictive-commoning, -fgcse-after-reload, -ftree-vectorize and -fipa-cp-clone

您可能指的是 -ffast-math,默认情况下使用 -Ofast 打开,但不使用 -O3:

-ffast-math Sets -fno-math-errno, -funsafe-math-optimizations, -ffinite-math-only, -fno-rounding-math, -fno-signaling-nans and -fcx-limited-range. This option causes the preprocessor macro __FAST_MATH__ to be defined.

This option is not turned on by any -O option besides -Ofast since it can result in incorrect output for programs that depend on an exact implementation of IEEE or ISO rules/specifications for math functions. It may, however, yield faster code for programs that do not require the guarantees of these specifications.

换句话说,-O-O2-O3 对于数值编程都是安全的。

关于c - 数字的安全 gcc 优化选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13518759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com