double 和 ffast-math 上的自动矢量化 [英] Auto vectorization on double and ffast-math

查看:53
本文介绍了double 和 ffast-math 上的自动矢量化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么必须使用 -ffast-math 和 g++ 来实现使用 doubles 的循环向量化?我不喜欢 -ffast-math 因为我不想失去精度.

Why is it mandatory to use -ffast-math with g++ to achieve the vectorization of loops using doubles? I don't like -ffast-math because I don't want to lose precision.

推荐答案

使用 -ffast-math 不一定会丢失精度.它只影响NaNInf等的处理以及操作执行的顺序.

You don’t necessarily lose precision with -ffast-math. It only affects the handling of NaN, Inf etc. and the order in which operations are performed.

如果您不希望 GCC 重新排序或简化计算的特定代码段,您可以使用 asm 语句将变量标记为正在使用.

If you have a specific piece of code where you do not want GCC to reorder or simplify computations, you can mark variables as being used using an asm statement.

例如,以下代码对 f 执行舍入操作.但是,两个 f += gf -= g 操作很可能会被 gcc 优化掉:

For instance, the following code performs a rounding operation on f. However, the two f += g and f -= g operations are likely to get optimised away by gcc:

static double moo(double f, double g)                                      
{                                                                          
    g *= 4503599627370496.0; // 2 ** 52                                    
    f += g;                                                                
    f -= g;                                                                
    return f;                                                            
}                                                                     

在 x86_64 上,您可以使用此 asm 语句来指示 GCC 不执行该优化:

On x86_64, you can use this asm statement to instruct GCC not to perform that optimisation:

static double moo(double f, double g)                                      
{                                                                          
    g *= 4503599627370496.0; // 2 ** 52                                    
    f += g;                                                                
    __asm__("" : "+x" (f));
    f -= g;
    return f;
}

不幸的是,您需要针对每种架构进行调整.在 PowerPC 上,使用 +f 而不是 +x.

You will need to adapt this for each architecture, unfortunately. On PowerPC, use +f instead of +x.

这篇关于double 和 ffast-math 上的自动矢量化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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