gcc -Ofast-限制的完整列表 [英] gcc -Ofast - complete list of limitations

查看:159
本文介绍了gcc -Ofast-限制的完整列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using -Ofast gcc option in my program cause latency requirements. I wrote simple test program:

#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来运行它:

I've tried to run it with default flags and with -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进行测试?

So the result of != and == can not be trusted. Does it mean that I should == and != only when both values are known to be not nan, otherwise I should test with isnan before?

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

Is it guaranteed that isnan works correctly with -Ofast? How correctly == and != works for double with -Ofast? Can someone provide complete list of limitations added by -Ofast?

推荐答案

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

文档:

-Ofast

-Ofast

忽略严格的标准合规性. -Ofast启用所有-O3优化.它也是 启用对所有符合标准的程序均无效的优化.它打开 -ffast-math和特定于Fortran的-fno-protect-parens和-fstack-arrays.

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

设置-fno-math-errno,-funsafe-math优化, -fno-trapping-math,-fno-only-math,-fno-rounding-math,-fno-signaling-nans和fcx-limited-range.

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

允许对假定参数和的浮点算法进行优化 结果不是NaN或+ -Infs.

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

有几个针对此标记为无效的gcc错误报告.

There are several gcc bug reports for this marked invalid.

-ffast-math和isnan问题

此外,严格的IEEE浮点数比较始终会导致错误.

Additionally, comparison of strict IEEE floating points always results in false.

检查是否为double(或float) C ++中的NaN

这不一定适用于-ffast-math,但可以解释您显示的内容.

This doesn't necessarily apply for -ffast-math but it explains what you show.

gcc并未描述有关-ffast-math浮点数工作方式的正式标准,因此,如果必须且不假定gcc版本之间的一致性,则只需凭经验确定细节.更好的是,完全避免使用NaN-ffast-math的组合.

gcc does not describe a formal standard for how -ffast-math floats work so you'll just have to work out the details empirically if you must and not assume consistency between versions of gcc. Better yet, completely avoid the combination of NaN and -ffast-math.

这篇关于gcc -Ofast-限制的完整列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆