我正在尝试使用内在函数为NEON实现校验和计算代码(2的补码加法)。当前的校验和计算正在ARM上进行。
我的实现从存储器一次取128位到NEON寄存器并进行SIMD(加法),结果从128位数字折叠成16位数。
一切看起来都很好,但是我的NEON实现消耗的时间比ARM版本多。
ARM版本需要: 0.860000 s NEON版本需要: 1.260000 s
注意:
其他详情:
问题:
为什么NEON版本比ARM版本花费更多时间? (虽然我已经注意到使用批次中具有最小循环的固有物质)
如何实现我想要达到的目标? (与NEON的效率)
有人可以指点我或分享一些使用ARM-NEON的互操作的示例实现(伪代码/算法/代码,而不是理论实现论文或会谈)吗?
< / LI> 醇>非常感谢任何帮助。
这是我的代码:
uint16_t do_csum(const unsigned char * buff, int len)
{
int odd, count, i;
uint32x4_t result = veorq_u32( result, result), sum = veorq_u32( sum, sum);
uint16x4_t data, data_hi, data_low, data8;
uint16x8_t dataq;
uint16_t result16, disp[20] = {0,0,0,0,0,0,0,0,0,0};
if (len <= 0)
goto out;
odd = 1 & (unsigned long) buff;
if (odd) {
uint8x8_t data1 = veor_u8( data1, data1);
data1 = (uint16x4_t)vld1_lane_u8((uint8_t *)buff, data1, 0); //result = *buff << 8;
data1 = (uint16x4_t)vshl_n_u16( data1, 8);
len--;
buff++;
result = vaddw_u16(result, data1);
}
count = len >> 1; /* nr of 16-bit words.. */
if (count) {
if (2 & (unsigned long) buff) {
uint16x4_t data2 = veor_u16( data2, data2);
data2 = (uint16x4_t) vld1_lane_u16((uint16_t *)buff, data2, 0); //result += *(unsigned short *) buff;
count--;
len -= 2;
buff += 2;
result = vaddw_u16( result, data2);
}
count >>= 1; /* nr of 32-bit words.. */
if (count) {
if (4 & (unsigned long) buff) {
uint32x2_t data4 = (uint16x4_t) vld1_lane_u32((uint32_t *) buff, data4, 0);
count--;
len -= 4;
buff += 4;
result = vaddw_u16( result, data4);
}
count >>= 1; /* nr of 64-bit words.. */
if (count) {
if (8 & (unsigned long) buff) {
uint64x1_t data8 = vld1_u64((uint64_t *) buff);
count--;
len -= 8;
buff += 8;
result = vaddw_u16( result,(uint16x4_t)data8);
}
count >>= 1; /* nr of 128-bit words.. */
if (count) {
do {
dataq = (uint16x8_t)vld1q_u64((uint64_t *) buff); // VLD1.64 {d0, d1}, [r0]
count--;
buff += 16;
sum = vpaddlq_u16(dataq);
vst1q_u16( disp, dataq); // VST1.16 {d0, d1}, [r0]
result = vaddq_u32( sum, result);
} while (count);
}
if (len & 8) {
uint64x1_t data8 = vld1_u64((uint64_t *) buff);
buff += 8;
result = vaddw_u16( result, (uint16x4_t)data8);
}
}
if (len & 4) {
uint32x2_t data4 = veor_u32( data4, data4);
data4 = (uint16x4_t)vld1_lane_u32((uint32_t *) buff, data4, 0);//result += *(unsigned int *) buff;
buff += 4;
result = vaddw_u16( result,(uint16x4_t) data4);
}
}
if (len & 2) {
uint16x4_t data2 = veor_u16( data2, data2);
data2 = (uint16x4_t) vld1_lane_u16((uint16_t *)buff, data2, 0); //result += *(unsigned short *) buff;
buff += 2;
result = vaddw_u16( result, data2);
}
}
if (len & 1){
uint8x8_t data1 = veor_u8( data1, data1);
data1 = (uint16x4_t) vld1_lane_u8((uint8_t *)buff, data1, 0); //result = *buff << 8;
result = vaddw_u8( result, data1);
}
result16 = from128to16(result);
if (odd)
result16 = ((result16 >> 8) & 0xff) | ((result16 & 0xff) << 8);
out:
return result16;
}
答案 0 :(得分:6)
您可以改进的一些事项:
disp
- 这看起来像调试代码留下了?gcc -O3 ...
从编译器优化中获得最大收益goto
! (不影响表现,但却是邪恶的。)