首页 > 解决方案 > 使用 intel Intrinsics 分配 - 水平添加

问题描述

我想总结一个大向量的所有元素ary。我的想法是用水平总和来做。

在此处输入图像描述

const int simd_width = 16/sizeof(float); 
float helper[simd_width];

//take the first 4 elements
const __m128 a4 = _mm_load_ps(ary);

for(int i=0; i<N-simd_width; i+=simd_width){
     const __m128 b4 = _mm_load_ps(ary+i+simd_width);
     //save temporary result in helper array
     _mm_store_ps(helper, _mm_hadd_ps(a4,b4)); //C
     const __m128 a4 = _mm_load_ps(helper);

}

我寻找了一种方法,我可以使用它直接将结果向量直接分配给 quadfloata4就像_mm_store_ps(a4, _mm_hadd_ps(a4,b4)) 有这样的英特尔方法吗?(这是我第一次使用 SSE - 也许整个代码片段都是错误的)

标签: c++ssesimd

解决方案


正如彼得建议的那样,不要使用水平总和。使用垂直总和。

例如,在伪代码中,simd width = 2

SIMD sum = {0,0}; // we use 2 accumulators
for (int i = 0; i + 1 < n; i += 2)
    sum = simd_add(sum, simd_load(x+i));
float s = horizzontal_add(sum);
if (n & 1)  // n was not a multiple of 2?
   s += x[n-1]; // deal with last element

推荐阅读