【发布时间】:2018-05-02 23:01:44
【问题描述】:
给定一个浮点数组,以及另一个将值索引到应该求和的数组中的排序数组——有没有办法比这和自动矢量化代码做得更好?任何适用的内在函数?
#include "stdint.h"
void IndexedSum(float buf[], uint32_t index[], int len, float *res) {
float acc = 0;
for(int i = 0; i < len; i++) {
acc += buf[index[i]];
}
*res = acc;
}
目前正在使用clang 6.0 和-O3、-ffast-math 和-mllvm -force-vector-width=8 进行编译:
【问题讨论】:
-
我唯一能想到的就是对索引进行(预)排序。
-
应该提到它们已经排序了:)
-
假设生成的汇编器已经有一些向量化?我会考虑将 const 放在 const 的东西上。也许发布汇编程序,我们可以看到它已经非常接近理想了。
-
根据长度,您可能会通过穿线并在完成时添加来获得良好的回报。
-
另外,如果你知道长度足够长,那么一点点循环展开也可能会有所帮助。不过,您需要对此进行概要分析。