Passing aligned types or structures with aligned types by value doesn't work with some implementations. This breaks STL containers, because some of the methods (such as resize) take their arguments by value.
I run some tests with Visual Studio 2008 and not entirely sure when and how pass by value fails. My main concern is function foo. It seems to work fine, but could it be a result of inlining or some other coincidence? What if I change its signature to void foo(const __m128&)?
Your input is greatly appreciated. Thank you.
struct A
{
__m128 x;
int n;
};
void foo(__m128);
void bar(A);
void f1()
{
// won't compile
// std::vector<A> vec1(3);
// compiles, but fails at runtime when elements are accessed
std::vector<__m128> vec2(3);
// this seems to work. WHY???
std::vector<__m128, some_16_byte_aligned_allocator<__m128> > vec3(3);
__m128 x;
A a;
// passed by value, is it OK?
foo(x);
// won't compile
//bar(a);
}
EDIT. STL fails even with aligned allocator, because pass by value problem remains.
Found this link pass __m128 by value
I think the only safe way to do this in general is to pass by reference. Some platforms (e.g. Xbox 360) support passing vector arguments in registers, but I don't think this is possible on x86.
For the std::vector
case, you'll need to make sure that the memory allocated is aligned to 16 bytes; otherwise you'll get crashes when you try to perform most operations on unaligned vectors.
If you're supporting multiple platforms, a safe plan is to use a typedef
e.g.
typedef const MyVector& MyVectorParameter;
You can then change the typedef on platforms that support vector pass-by-value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With