使用SSE内部函数复制少量数据时出现问题

Issue copying small amount of data using SSE intrinsics

本文关键字:问题 数据 SSE 内部函数 复制 使用      更新时间:2023-10-16

我在这里看到了类似的问题,并尝试使用类似的代码,但在某些地方出错了。。顺便说一下,这只是一个学习练习。这是代码:

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;
}

在复制之前,字段1和字段2都显示0,但复制之后都是111?我对c++还很陌生,所以它似乎是在复制结构体的最后32位而不是dst结构体的64位,但不确定为什么?

您的并集不正确,它有1个整数或__m128数组。CCD_ 2、CCD_。除上次分配给field8外,您的所有作业都将被覆盖。

你可以用这个代替:

struct alignas(16) Data
{
union
{
int fields[8];
__m128 v[2];
};
};