gpt4 book ai didi

c++ - gcc -Ofast - 完整的限制列表

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:10:31 34 4
gpt4 key购买 nike

我在我的程序中使用 -Ofast gcc 选项导致延迟要求。我写了一个简单的测试程序:

#include <iostream>
#include <math.h>

static double quiet_NaN = std::numeric_limits<double>::quiet_NaN();

int main()
{
double newValue = 130000;
double curValue = quiet_NaN;
printf("newValue = %f\n", newValue);
printf("curValue = %f\n", curValue);
printf("isnan(newValue) = %d\n", isnan(newValue));
printf("isnan(curValue) = %d\n", isnan(curValue));
printf("newValue == curValue %d\n", (newValue == curValue));
printf("newValue != curValue %d\n", (newValue != curValue));
}

我尝试使用默认标志和 -Ofast 运行它:

$ g++ TestPointer.cpp 
$./a.out
newValue = 130000.000000
curValue = nan
isnan(newValue) = 0
isnan(curValue) = 1
newValue == curValue 0
newValue != curValue 1

$ g++ -Ofast TestPointer.cpp
$ ./a.out
newValue = 130000.000000
curValue = nan
isnan(newValue) = 0
isnan(curValue) = 1
newValue == curValue 1
newValue != curValue 0

所以 !=== 的结果是不可信的。这是否意味着我应该 ==!= 仅当已知两个值都不是 nan 时,否则我应该在 isnan 之前进行测试?

是否保证 isnan-Ofast 一起正常工作?==!=-Ofast 的 double 如何正确工作?有人可以提供 -Ofast 添加的完整限制列表吗?

最佳答案

您正在观察 -ffast-math 的效果。

来自docs :

-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 and the Fortran-specific -fno-protect-parens and -fstack-arrays.

-ffast-math

Sets -fno-math-errno, -funsafe-math-optimizations, -fno-trapping-math, -ffinite-math-only, -fno-rounding-math, -fno-signaling-nans and fcx-limited-range.

-ffinite-math-only

Allow optimizations for floating-point arithmetic that assume that arguments and results are not NaNs or +-Infs.

对于这个标记为无效的,有几个 gcc 错误报告。

Problems with -ffast-math and isnan

此外,比较严格的 IEEE float 总是会导致错误。

Checking if a double (or float) is NaN in C++

这不一定适用于 -ffast-math 但它解释了您显示的内容。

gcc 没有描述 -ffast-math float 如何工作的正式标准,所以如果您必须且不假设 gcc 版本之间的一致性,您只需要凭经验计算出细节。更好的是,完全避免 NaN-ffast-math 的组合。

关于c++ - gcc -Ofast - 完整的限制列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27315941/

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