使用英特尔内部技术分配-水平添加

时间:2018-10-22 19:16:43

标签: c++ sse simd

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

enter image description here

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

}

我正在寻找一种方法,通过该方法,我可以像a4一样直接将结果向量直接分配给四方浮点_mm_store_ps(a4, _mm_hadd_ps(a4,b4)) 有这种英特尔方法吗? (这是我第一次使用SSE,也许整个代码段都是错误的)

1 个答案:

答案 0 :(得分:2)

如Peter所建议的,不要使用水平和。使用垂直和。

例如,伪代码中simd宽度= 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