我想编写一个计算范围a
中的字节数的c程序... c
使用以下代码:
char a[16], b[16], c[16];
int counter = 0;
for(i = 0; i < 16; i++)
{
if((a[i] < b[i]) && (b[i] < c[i]))
counter++;
}
return counter;
我打算做这样的事情
__m128i result1 = _mm_cmpgt_epi8 (b, a);
__m128i result2 = _mm_cmplt_epi8 (b, c);
unsigned short out1 = _mm_movemask_epi8(result1);
unsigned short out2 = _mm_movemask_epi8(result2);
unsigned short out3 = out1 & out2;
unsigned short out4 = _mm_popcnt_u32(out3);
我的方法正确吗?有没有更好的方法来做到这一点?
最佳答案
你的方法看起来很合理。我认为您可以通过在 SIMD 寄存器中执行 AND 来保存指令,如下所示:
__m128i result1 = _mm_cmpgt_epi8 (b, a);
__m128i result2 = _mm_cmplt_epi8 (b, c);
__m128i mask = _mm_and_si128(result1, result2);
int mask2 = _mm_movemask_epi8(mask);
int counter = _mm_popcnt_u32(mask2);
关于x86 - 如何使用 SSE 计算某个范围内的字节数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10609188/