c++ - 使用 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 - 也许整个代码片段都是错误的)
解决方案
正如彼得建议的那样,不要使用水平总和。使用垂直总和。
例如,在伪代码中,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
推荐阅读
- swift - InterfaceController 如何访问 ViewController 中创建的对象?
- android - 为什么不能将 FragmentActivity 转换为 androidx.fragment.app.FragmentActivity
- hadoop - NameNode 地址的 URI 无效,s3a 不是模式“hdfs”
- codenameone - 代号 一台服务器拒绝超过 20 000kb 的构建
- javascript - 根据两个先前选择的下拉选项填充第三个下拉列表
- reactjs - 完整的日历 Prev - React 中的 Next 按钮
- javascript - react-native 错误中的自定义后退按钮:params.handleThis 不是函数(在 params.handleThis() 中,params.handleThis 未定义)
- c# - 无法将 foreach 转换为 linq select
- php - 我想用 php 更新值 json
- ruby - 如何遍历文件夹中的文件并将它们编码为base64以存储在ruby中的数组中?