我们尝试做一些SSE操作,但是,在add_sse函数结束时,我们试图读回刚刚计算的值,它会给我们一个seg错误。但是,如果我们只是在for循环中打印该值,结果就可以了。 也可以只读取每个数组中的元素0。读取元素1及其后将导致seg错误。
任何人都可以帮我们确定问题吗?我们尝试了一切,但仍然不明白我们会有一个段错误。谢谢
void main()
{
ResultCounter *c_sse=(ResultCounter *)memalign(16,sizeof(ResultCounter)*4);
resetCounter (c_sse); //initial struct to all 0
add_sse (1,2, 3,4, c_sse);
}
void add_sse (unsigned int first, unsigned int second, unsigned int third, unsigned int fourth, ResultCounter *c)
{
__attribute__((align(16))) int m_intarray[4] = {first, second, third,fourth};
__attribute__((align(16))) int m_Larray[4] = {c[0].L, c[1].L, c[2].L,c[3].L};
__attribute__((align(16))) int m_Marray[4] = {c[0].M, c[1].M, c[2].M,c[3].M};
__attribute__((align(16))) int m_Harray[4] = {c[0].H, c[1].H, c[2].H,c[3].H};
__m128i N = _mm_load_si128(&m_intarray[0]);
__m128i L = _mm_load_si128(&m_Larray[0]);
__m128i M = _mm_load_si128(&m_Marray[0]);
__m128i H = _mm_load_si128(&m_Harray[0]);
__m128i Lcarry = _mm_and_si128 (L, N);
L = _mm_xor_si128 (L, N);
__m128i Mcarry = _mm_and_si128 (M, Lcarry);
M = _mm_xor_si128 (M, Lcarry);
H = _mm_or_si128 (H,Mcarry);
_mm_store_si128(&m_Larray[0], L);
_mm_store_si128(&m_Marray[0], M);
_mm_store_si128(&m_Harray[0], H);
for(i = 0; i < 4; i++) {
//printf ("L:%d,addr=%u,M:%u,addr=%u,H:%u,addr=%u\n",m_Larray[i],&m_Larray[i],m_Marray[i],&m_Marray[i],m_Harray[i],&m_Harray[i]);
c[i].L=m_Larray[i];
c[i].M=m_Marray[i];
c[i].H=m_Harray[i];
}
}
//The struct used in main function.
typedef struct
{
unsigned int L;
unsigned int M;
unsigned int H;
} ResultCounter;
答案 0 :(得分:3)
问题是ResultCounter
结构的大小是12个字节,所以尽管数组的第一个元素c[0]
是16字节对齐的,但第二个元素c[1]
是不。现在最快/最简单的修复方法是向此结构添加4个字节的填充,例如另外一个未使用的int:
typedef struct
{
unsigned int L;
unsigned int M;
unsigned int H;
unsigned int unused;
} ResultCounter;
答案 1 :(得分:-2)
_mm_load_si128需要16字节对齐的数据。