在double和ffast-math上自动矢量化 [英] Auto vectorization on double and ffast-math
问题描述
为什么在g ++中使用 -ffast-math
来强制使用 double
s来实现循环的向量化?我不喜欢 -ffast-math
因为我不想失去精确度。
NaN
, Inf
等的处理以及执行操作的顺序。 如果您有一段代码不希望GCC重新排序或简化计算,则可以使用 asm $>标记变量c $ c>语句。
例如,以下代码对 f
执行舍入操作。然而,两个 f + = g
和 f - = g
操作可能会被gcc优化掉:
static double moo(double f,double g)
{
g * = 4503599627370496.0; // 2 ** 52
f + = g;
f - = g;
返回f;
$ / code>
在x86_64上,您可以使用 asm
语句来指示GCC不执行该优化:
$ p $ static double moo(double f,double g)
{
g * = 4503599627370496.0; // 2 ** 52
f + = g;
__asm __(:+ x(f));
f - = g;
返回f;
}
不幸的是,您需要对每种架构进行调整。在PowerPC上,使用 + f
代替 + x
。
Why is it mandatory to use -ffast-math
with g++ to achieve the vectorization of loops using double
s? I don't like -ffast-math
because I don't want to lose precision.
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.
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.
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;
}
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;
}
You will need to adapt this for each architecture, unfortunately. On PowerPC, use +f
instead of +x
.
这篇关于在double和ffast-math上自动矢量化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!