How do you use __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?
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?
} ;
Then can I go:
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("");
}
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)
Edit 2:
I tried adding an __m256 register to my rather large project, and I got a whole bunch of
error C2719: 'value': formal parameter with __declspec(align('32')) won't be aligned
Errors, it leads me to believe that you can't keep __m256 registers inside a class, instead they should be declared as locals?