简体   繁体   English

__builtin_unreachable 与 GCC 中的浮点数

[英]__builtin_unreachable with floats in GCC

I have the following code, which I am compiling using gcc.我有以下代码,我正在使用 gcc 进行编译。

float add(float a, float b) {
    float sum = a + b;
    if (sum != 0) __builtin_unreachable();
    return sum;
}

When I am using -O3 I get the following assembly .当我使用-O3时,我得到以下程序集

add:
        addss   xmm0, xmm1
        ret

But with -Ofast I get the following .但是使用-Ofast我得到以下内容

add:
        pxor    xmm0, xmm0
        ret

Looks like compiler does understand assume that function is expected to return 0. What does prevent GCC to return 0 as in the second example without -Ofast ?看起来编译器确实理解假设 function 预计返回 0。是什么阻止 GCC 在没有-Ofast的第二个示例中返回 0?

From GCC documentation :来自GCC 文档

-fno-signed-zeros
Allow optimizations for floating-point arithmetic that ignore the signedness of zero.允许对忽略零符号的浮点算术进行优化。 IEEE arithmetic specifies the behavior of distinct +0.0 and -0.0 values, which then prohibits simplification of expressions such as x+0.0 or 0.0*x (even with -ffinite-math-only). IEEE 算术指定不同的 +0.0 和 -0.0 值的行为,然后禁止简化表达式,例如 x+0.0 或 0.0*x(即使使用 -ffinite-math-only)。 This option implies that the sign of a zero result isn't significant.此选项意味着零结果的符号不重要。

The default is -fsigned-zeros.默认值为 -fsigned-zeros。

-Ofast includes this optimization option, while -O3 doesn't. -Ofast包括这个优化选项,而-O3没有。

Without this option, the function is required to return the exact value of signed zero of the addition result, so the compiler simply perform the addition.如果没有这个选项,function 需要返回加法结果的有符号零的确切值,因此编译器只需执行加法。

If the 0 is changed to some other value such as 1 or 1.2f or 1.5 , then the compiler will optimize without -ffast-math .如果0更改为其他值,例如11.2f1.5 ,则编译器将在没有-ffast-math的情况下进行优化。

Remark: if the value is changed to 1.2 (so the comparison is the same as static_cast<double>(sum).=1.2 , as required by the standard) then the compiler will keep the addition, although the __builtin_unreachable() will always be executed because there's no float value exactly equal to 1.2 when converted to double.备注:如果将值更改为1.2 (因此比较与标准要求的static_cast<double>(sum).=1.2相同),则编译器将保留加法,尽管__builtin_unreachable()将始终为执行是因为在转换为双精度时没有完全等于1.2float值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM