使用 intel Intrinsics 赋值 - 水平添加 [英] assignment with intel Intrinsics - horizontal add

查看:42
本文介绍了使用 intel Intrinsics 赋值 - 水平添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want sum up all elements of a big vector ary. My idea was to do it with a horizontal sum.

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 - 可能整个代码片段都错了)

I looked for a method, with which i can assign the resulting vector directly to the quadfloat a4 directly like _mm_store_ps(a4, _mm_hadd_ps(a4,b4)) Is there such a Intel method? (It is my first time to work with SSE -maybe the whole code snippet is wrong)

推荐答案

正如 Peter 建议的那样,不要使用水平总和.使用垂直总和.

As Peter suggested, do not use horizontal sums. Use vertical sums.

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

For example, in pseudo-code, with 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

这篇关于使用 intel Intrinsics 赋值 - 水平添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆