我有一个关于最新 GCC 编译器(版本 >= 5)的问题,代码如下:
#include <math.h>
void test_nan (
const float * const __restrict__ in,
const int n,
char * const __restrict__ out )
{
for (int i = 0; i < n; ++i)
out[i] = isnan(in[i]);
}
来自 GCC 的程序集列表:
test_nan:
movq %rdx, %rdi
testl %esi, %esi
jle .L1
movslq %esi, %rdx
xorl %esi, %esi
jmp memset
.L1:
ret
这看起来像
memset(out, 0, n)
.为什么 GCC 假设没有条目可以是 NaN 与 -Ofast ?
使用相同的编译选项,ICC 不会显示此问题。
使用 GCC,问题会随着“-O3”消失。
请注意,使用“-O3”时,此查询
gcc -c -Q -O3 --help=optimizers | egrep -i nan
给 -fsignaling-nans [disabled]
.我在本地和 godbolt 上都对此进行了验证,带有附加选项“-std=c99”。
编辑:按照以下有用的答案,我可以确认
-Ofast -std=c99 -fno-finite-math-only
正确解决这个问题。
最佳答案
来自海湾合作委员会 Options That Control Optimizations文档。-Ofast
除了 -O3
之外,还启用以下优化:
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.
-ffast-math
启用以下功能:-fno-math-errno, -funsafe-math-optimizations, -ffinite-math-only, -fno-rounding-math, -fno-signaling-nans, -fcx-limited-range and -fexcess-precision=fast.
-ffinite-math-only
执行以下操作:Allow optimizations for floating-point arithmetic that assume that arguments and results are not NaNs or +-Infs.
这允许它假设
isnan()
总是返回 0
.
关于c - -Ofast 的 GCC 问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62431365/