Reputation: 83
I've looked at similar questions here and tried to use similar code, but am going wrong somewhere.. This is just a learning exercise by the way. Here is the code:
struct alignas(16) Data
{
union
{
int field1;
int field2;
int field3;
int field4;
int field5;
int field6;
int field7;
int field8;
__m128 v[2];
};
};
void copy((__m128* __restrict b, const __m128* __restrict a)
{
*b++ = *a++;
*b++ = *a++;
}
int main(int, char**)
{
Data dst={0};
Data src={0};
src.field1=1;
src.field2=500;
src.field3=200;
src.field4=393;
src.field5=29383;
src.field6=3838;
src.field7=128484;
src.field8=111;
copy(dst.v,src.v);
std::cout<<" before copy dst.field1=" << dst.field1 <<" dst.field2=" << dst.field2 << std::endl;
return 0;
}
It's showing 0 for both field1 and field2 before the copy, but both are 111 after the copy? I'm fairly new to c++ so it appears it's copying the last 32 bits of the struct over 64 bits of the dst struct, but not sure why?
Upvotes: 0
Views: 63
Reputation: 36488
Your union is incorrect, it has 1 integer or your array of __m128
. field1
, field2
etc all point to the same memory. All your assignments apart from your last one to field8
are being overwritten.
You could use this instead:
struct alignas(16) Data
{
union
{
int fields[8];
__m128 v[2];
};
};
Upvotes: 4