0

我们试图做一些 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;
4

2 回答 2

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;
于 2012-12-05T08:17:49.483 回答
-2

_mm_load_si128 需要 16 字节对齐的数据。

于 2012-12-04T23:40:25.987 回答