使用__m256d寄存器 [英] Using __m256d registers
问题描述
您如何使用__m256d
?
Say I want to use the Intel AVX instruction _mm256_add_pd
on a simple Vector3
class with 3-64 bit double
precision components (x
, y
, and z
). What is the correct way to use this?
由于x
,y
和z
是Vector3
类的成员,因此_我可以在union
中使用__m256d
变量声明它们吗?
Since x
, y
and z
are members of the Vector3
class, _can I declare them in union
with an __m256d
variable?
union Vector3
{
struct { double x,y,z ; } ;
__m256d _register ; // the Intel register?
} ;
那我可以去
Vector3 add( const Vector3& o )
{
Vector3 result;
result._register = _mm256_add_pd( _register, o._register ) ; // add 'em
return result;
}
这行得通吗?还是我需要宣布临时人员,
Is that going to work? Or do I need to declare temporaries,
Vector3 add( const Vector3& o )
{
__m256d d1 = *(__m256d*)(&x) ; // ? Cast to __m256d?
__m256d d2 = *(__m256d*)(&o.x) ; // ? Cast to __m256d?
__m256d result = _mm256_add_pd( d1, d2 ) ; // add 'em
return Vector3( result ) ; // make a ctor that accepts __m256d?
}
编辑
我想出了这个例子,
Edit
I came up with this example,
#include <stdio.h>
#include <intrin.h>
int main()
{
__m256d a, b, res;
for( int i = 0; i < sizeof(__m256d)/sizeof(double); i++ )
{
a.m256d_f64[i] = i ;
b.m256d_f64[i] = 2*i ;
}
// Perform __4__ adds.
res = _mm256_add_pd(a, b);
for( int i = 0; i < sizeof(__m256d)/sizeof(double); i++ )
{
printf("%f + %f = %f\n", a.m256d_f64[i], b.m256d_f64[i], res.m256d_f64[i]);
}
puts("");
}
我想现在的问题是,做 _mm256_add_pd
做 load 个操作自动,如果我不这样做会搞乱什么吗?要将我的__m256d
寄存器声明为接近使用它们的本地变量? (我担心酒店房间/办公桌抽屉类型问题)
I guess the question is now, does _mm256_add_pd
do load operations automatically, or will something get messed up if I don't declare my __m256d
registers as locals close to where they are used? (I'm afraid of a hotel room / deskdrawer type problem)
我尝试在我的大型项目中添加一个__m256
寄存器,然后得到了一大堆
I tried adding an __m256
register to my rather large project, and I got a whole bunch of
错误C2719:'值':带有__declspec(align('32'))的形式参数无法对齐
error C2719: 'value': formal parameter with __declspec(align('32')) won't be aligned
错误,这使我相信您不能将__m256
寄存器保留在一个类中,而是应将它们声明为本地变量?
Errors, it leads me to believe that you can't keep __m256
registers inside a class, instead they should be declared as locals?
推荐答案
首先,我想澄清一些混乱之处. __m256d
不是寄存器的类型,它是可以将 加载到AVX寄存器中的数据类型. __m256d
不再是寄存器,而int
只是寄存器.有几种方法可以将数据输入__m256d
(或任何其他向量类型):
First I'd like to clear up a little confusion. __m256d
isn't a type of register, it's a data type that can be loaded into an AVX register. A __m256d
is no more a register than an int
is a register. There are a few ways to get data in and out of an __m256d
(or any other vector type):
使用union
:是的,union
技巧可行.由于联合通常具有正确的对齐方式(尽管malloc
可能不正确,请使用posix_memalign
或_aligned_malloc
),因此效果很好.
Using a union
: Yes, the union
trick works. It works very well, since the union will generally have the correct alignment (although malloc
might not, use posix_memalign
or _aligned_malloc
).
class Vector3 {
public:
Vector3(double xx, double yy, double zz);
Vector3(__m256d vvec);
Vector3 operator+(const Vector3 &other) const
{
return Vector3(_mm256_add_pd(vec, other.vec));
}
union {
struct {
double x, y, z;
};
__m256d vec; // a data field, maybe a register, maybe not
};
};
使用内在函数:在函数内部,通常更容易使用内在函数来获取向量类型中的数据.
Using intrinsics: Inside a function, it's usually easier to use intrinsics to get data in and out of a vector type.
__m256d vec = ...;
double x, y, z;
vec = _mm256_add_pd(vec, _mm256_set_pd(x, y, z, 0.0));
使用指针强制转换:由于多种原因,强制转换指针是最后的选择.
Using pointer casts: Casting pointers is the last resort for a couple of reasons.
-
指针可能未正确对齐.
The pointer might not be aligned correctly.
投射指针有时可能会干扰编译器的别名分析.
Casting pointers can sometimes mess with the compiler's aliasing analysis.
指针转换绕过了许多安全保证.
Pointer casting bypasses a number of safety guarantees.
因此,我只使用指针转换来浏览大量数据.
So I'd only use pointer casting to plow through a big array of data.
这篇关于使用__m256d寄存器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!