sse unsigned long long数组

时间:2014-03-16 17:52:46

标签: c++ sum sse simd uint64

基于SSE reduction of float vector我尝试将 unsigned long long 的数组求和,但不幸的是没有成功。

uint64_t vsum_uint64 (uint64_t *a, int n)
{
    uint64_t sum;    // lets say sum fits
    __m128 vsum = _mm_set1_ps(0);

    for (int i = 0; i < n; i += 2) {    // 2 unit64 in single __m128
        __m128 v = _mm_loadl_epi64(&a[i]);
        vsum = _mm_add_epi64(vsum, v);
    }

    _mm_store_ss(&sum, vsum);
    uint64_t *p = &vsum;
    sum+=*(p+1);

    // vsum = _mm_hadd_ps(vsum, vsum);
    // vsum = _mm_hadd_ps(vsum, vsum);
    return sum;
}

这应该是正确的,但是gcc仍然无法编译它。我搜索了答案,但没有找到答案。

这就是gcc所说的:

main.cpp: In function ‘uint64_t vsum_uint64(const uint64_t*, int)’:
main.cpp:73:35: error: cannot convert ‘const uint64_t* {aka const long unsigned int*}’ to ‘const __m128i* {aka const __vector(2) long long int*}’ for argument ‘1’ to ‘__m128i _mm_loadl_epi64(const __m128i*)’
main.cpp:74:31: error: cannot convert ‘__m128 {aka __vector(4) float}’ to ‘__m128i {aka __vector(2) long long int}’ for argument ‘1’ to ‘__m128i _mm_add_epi64(__m128i, __m128i)’
main.cpp:77:25: error: cannot convert ‘uint64_t* {aka long unsigned int*}’ to ‘float*’ for argument ‘1’ to ‘void _mm_store_ss(float*, __m128)’
main.cpp:78:17: error: cannot convert ‘__m128* {aka __vector(4) float*}’ to ‘uint64_t* {aka long unsigned int*}’ in initialization
你能帮帮我吗?我真的很感激

由于

1 个答案:

答案 0 :(得分:4)

以下是一些事项:

  1. 使用__m128i代替__m128

  2. 您可以使用vsum__m128i vsum = _mm_setzero_si128()进行零初始化;

  3. 对于数据加载,强制转换为正确的__m128i类型并使用压缩加载版本(_mm_loadl_epi64仅加载一个64位整数)。所以,要么

    for (int i = 0; i < n; i += 2) {    // 2 uint64 in single __m128i
        __m128i v = _mm_loadu_si128(reinterpret_cast<__m128i*>(&a[i]));
    

    __m128i* pa = reinterpret_cast<__m128i*>(a);
    for (int i = 0; i < n; i += 2) {    // 2 uint64 in single __m128i
        __m128i v = _mm_loadu_si128(pa);
        pa++;
    
  4. 最后你可以使用sum = vsum.m128i_u64[0] + vsum.m128i_u64[1];分配总和,如果有一个为它定义的联合(在windows / Visual-Studio下,但你使用的是不同的环境)。