使用SSE内在函数,我得到了一个四个32位浮点数的向量,它被钳位到0-255范围并四舍五入到最接近的整数.我现在想把这四个写成字节.
有一个内部函数_mm_cvtps_pi8会将32位转换为8位有符号整数,但问题是任何超过127的值都会被钳位到127.我找不到任何会压缩无符号8位值的指令.
我有一种直觉,我可能想要做的是移动指令的一些组合_mm_cvtps_pi16和_mm_shuffle_pi8后面的操作,以获得我关心的四个字节到内存中.这是最好的方法吗?我将看看我是否可以弄清楚如何编码shuffle控制掩码.
更新:以下似乎完全符合我的要求.有没有更好的办法?
#include <tmmintrin.h>
#include <stdio.h>
unsigned char out[8];
unsigned char shuf[8] = { 0, 2, 4, 6, 128, 128, 128, 128 };
float ins[4] = {500, 0, 120, 240};
int main()
{
__m128 x = _mm_load_ps(ins); // Load the floats
__m64 y = _mm_cvtps_pi16(x); // Convert them to 16-bit ints
__m64 sh = *(__m64*)shuf; // Get the shuffle mask into a register
y = _mm_shuffle_pi8(y, sh); // Shuffle the …Run Code Online (Sandbox Code Playgroud)