将8个16位SSE寄存器转换为8位数据 [英] Convert 8 16 bit SSE register to 8bit data
本文介绍了将8个16位SSE寄存器转换为8位数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑到我有一个16位数据的SSE阵列:
Consider I have an SSE array with 16 bit data:
{1,2,3,4,5,6,7,8}
现在,我需要通过在前8个字节中仅存储16位数据的低字节来将此SSE数组转换为8位数据,例如:
Now I need to convert this SSE array into 8 bit data by storing only the lower byte of 16 bit data in the first 8 bytes like:
{1,2,3,4,5,6,7,8,0,0,0,0,0,0,0,0}.
是否有任何SSE指令来执行此操作?
Is there any SSE instruction to perform this operation?
推荐答案
如 @harold 所述,您可以使用 pshufb
aka轻松完成此操作_mm_shuffle_epi8
,例如
As @harold says in the comments above, you can do this quite easily with pshufb
aka _mm_shuffle_epi8
, e.g.
#include <stdio.h>
#include <tmmintrin.h>
static __m128i pack_16_to_8(const __m128i v)
{
const __m128i vperm = _mm_setr_epi8(0, 2, 4, 6, 8, 10, 12, 14, -1, -1, -1, -1, -1, -1, -1, -1);
return _mm_shuffle_epi8(v, vperm);
}
int main(void)
{
const __m128i v = _mm_setr_epi16(1, 2, 3, 4, 5, 6, 7, 8);
printf("%vhd -> %vd\n", v, pack_16_to_8(v));
return 0;
}
编译并运行:
$ gcc -Wall -mssse3 pack_16_to_8.c && ./a.out
1 2 3 4 5 6 7 8 -> 1 2 3 4 5 6 7 8 0 0 0 0 0 0 0 0
这篇关于将8个16位SSE寄存器转换为8位数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文