6

是否存在允许将所有元素添加到通道中的内在函数?我正在使用 Neon 将 8 个数字相乘,我需要对结果求和。这是一些解释代码来显示我目前正在做的事情(这可能会被优化):

int16_t p[8], q[8], r[8];
int32_t sum;
int16x8_t pneon, qneon, result;

p[0] = some_number;
p[1] = some_other_number; 
//etc etc
pneon = vld1q_s16(p);

q[0] = some_other_other_number;
q[1] = some_other_other_other_number;
//etc etc
qneon = vld1q_s16(q);
result = vmulq_s16(p,q);
vst1q_s16(r,result);
sum = ((int32_t) r[0] + (int32_t) r[1] + ... //etc );

有一个更好的方法吗?

4

3 回答 3

5

如果您的目标是较新的 arm 64 位架构,那么 ADDV 正是适合您的指令。

这是您的代码将如何使用它。

qneon = vld1q_s16(q);
result = vmulq_s16(p,q);
sum = vaddvq_s16(result);

而已。只需一条指令即可汇总向量寄存器中的所有通道。

遗憾的是,这条指令在较旧的 32 位 arm 架构中不具备。

于 2015-07-10T05:05:42.780 回答
0

像这样的东西应该工作得非常好(注意:未经测试)

const int16x4_t result_low = vget_low_s16(result); // Extract low 4 elements
const int16x4_t result_high = vget_high_s16(result); // Extract high 4 elements
const int32x4_t twopartsum = vaddl_s16(result_low, result_high); // Extend to 32 bits and add (4 partial 32-bit sums are formed)
const int32x2_t twopartsum_low = vget_low_s32(twopartsum); // Extract 2 low 32-bit partial sums
const int32x2_t twopartsum_high = vget_high_s32(twopartsum); // Extract 2 high 32-bit partial sums
const int32x2_t fourpartsum = vadd_s32(twopartsum_low, twopartsum_high); // Add partial sums (2 partial 32-bit sum are formed)
const int32x2_t eightpartsum = vpadd_s32(fourpartsum, fourpartsum); // Final reduction
const int32_t sum = vget_lane_s32(eightpartsum, 0); // Move to general-purpose registers
于 2012-08-29T05:27:48.293 回答
0
temp = vadd_f32(vget_high_f32(variance_n), vget_low_f32(variance_n)); 
sum  = vget_lane_f32(vpadd_f32(variance_temp, variance_temp), 0);
于 2018-06-27T05:51:36.150 回答