c - -Ofast 的 GCC 问题?

标签 c gcc

我有一个关于最新 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/

相关文章:

c - Freetype Opengl Glyph 未呈现

c - arm-none-eabi-gcc 是否支持#pragma pack(n)

gcc:如何不编译警告

c++ - 理解 GCC 中的 std::pow 实现

windows - 在 Windows 上使用为 Linux 构建的库

c++ - Clang 错误,没有可行的转换

c - toascii 有什么作用?

c - 将 int 值传递给函数时发生变化

在C处编译错误netbeans,cygwin

c - 如何从 C 中的字符串中提取 str + 数字的出现