7

使用 SSE 内在函数,我得到了一个包含四个 32 位浮点数的向量,这些浮点数被限制在 0-255 的范围内并四舍五入到最接近的整数。我现在想把这四个写成字节。

有一个内在函数_mm_cvtps_pi8可以将 32 位转换为 8 位有符号整数,但问题是任何超过 127 的值都会被限制为 127。我找不到任何可以限制为无符号 8 位值的指令。

我有一种直觉,我可能想要做的是某种组合,_mm_cvtps_pi16然后_mm_shuffle_pi8是移动指令,以将我关心的四个字节放入内存。这是最好的方法吗?我要看看我是否能弄清楚如何对随机播放控制掩码进行编码。

更新:以下似乎完全符合我的要求。有没有更好的办法?

#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 lower byte of each into the first four bytes
    *(int*)out = _mm_cvtsi64_si32(y); // Store the lower 32 bits

    printf("%d\n", out[0]);
    printf("%d\n", out[1]);
    printf("%d\n", out[2]);
    printf("%d\n", out[3]);
    return 0;
}

UPDATE2:这是基于 Harold 回答的更好的解决方案:

#include <smmintrin.h>
#include <stdio.h>

unsigned char out[8];
float ins[4] = {10.4, 10.6, 120, 100000};

int main()
{   
    __m128 x = _mm_load_ps(ins);       // Load the floats
    __m128i y = _mm_cvtps_epi32(x);    // Convert them to 32-bit ints
    y = _mm_packus_epi32(y, y);        // Pack down to 16 bits
    y = _mm_packus_epi16(y, y);        // Pack down to 8 bits
    *(int*)out = _mm_cvtsi128_si32(y); // Store the lower 32 bits

    printf("%d\n", out[0]);
    printf("%d\n", out[1]);
    printf("%d\n", out[2]);
    printf("%d\n", out[3]);
    return 0;
}
4

2 回答 2

10

没有从浮点到字节的直接转换,_mm_cvtps_pi8是复合的。_mm_cvtps_pi16也是一个组合,在这种情况下,它只是在做一些你用 shuffle 撤销的无意义的东西。他们还返回烦人__m64的。

无论如何,我们可以转换为双字(有符号,但这没关系),然后将它们打包(无符号)或洗牌成字节。_mm_shuffle_(e)pi8生成一个pshufb,Core2 45nm 和 AMD 处理器不太喜欢它,你必须从某个地方得到一个掩码。

无论哪种方式,您都不必先四舍五入到最接近的整数,转换会这样做。至少,如果您没有弄乱舍入模式。

使用包 1:(未测试)- 可能没用,packusdw已经输出无符号字,但又packuswb想要有符号字。保留,因为它在其他地方被提及。

cvtps2dq xmm0, xmm0  
packusdw xmm0, xmm0     ; unsafe: saturates to a different range than packuswb accepts
packuswb xmm0, xmm0
movd somewhere, xmm0

使用不同的洗牌:

cvtps2dq xmm0, xmm0  
packssdw xmm0, xmm0     ; correct: signed saturation on first step to feed packuswb
packuswb xmm0, xmm0
movd somewhere, xmm0

使用随机播放:(未测试)

cvtps2dq xmm0, xmm0
pshufb xmm0, [shufmask]
movd somewhere, xmm0

shufmask: db 0, 4, 8, 12, 80h, 80h, 80h, 80h, 80h, 80h, 80h, 80h, 80h, 80h, 80h, 80h
于 2015-04-24T19:52:07.443 回答
7

我们可以通过使用有符号饱和进行打包的第一阶段来解决无符号钳位问题。 [0-255]适合带符号的 16 位 int,因此该范围内的值将保持不受限制。该范围之外的值将保持在它的同一侧。因此,signed16 -> unsigned8 步骤将正确地钳制它们。

;; SSE2: good for arrays of inputs
cvtps2dq xmm0, [rsi]      ; 4 floats
cvtps2dq xmm1, [rsi+16]   ; 4 more floats
packssdw xmm0, xmm1       ; 8 int16_t

cvtps2dq xmm1, [rsi+32]
cvtps2dq xmm2, [rsi+48]
packssdw xmm1, xmm2       ; 8 more int16_t
                          ; signed because that's how packuswb treats its input
packuswb xmm0, xmm1       ; 16 uint8_t
movdqa   [rdi], xmm0

这仅需要 SSE2,而不需要 SSE4.1 packusdw

我认为这就是 SSE2 仅包含从 dword 到 word 的签名包,但包含从 word 到 byte 的有符号和无符号包的原因。 packuswd仅当您的最终目标是uint16_t,而不是进一步打包时才有用。(从那时起,您需要在将其送入另一包之前屏蔽符号位)。

如果你确实使用了packusdw -> packuswb,当第一步饱和到uint16_t> 0x7fff 时,你会得到虚假的结果。 packuswb会将其解释为负数int16_t并将其饱和到 0。 packssdw会将此类输入饱和到0x7fff, max int16_t

(如果你的 32 位输入总是 <= 0x7fff,你可以使用任何一个,但是SSE4.1比packusdwSSE2packsswd占用更多的指令字节,并且永远不会运行得更快。)


如果您的源值不能为负,并且您只有一个 4 个浮点数的向量,而不是很多,您可以使用 harold 的pshufb想法。如果不是,您需要将负值限制为零,而不是通过将低字节改组到位来截断。

使用

;; SSE4.1, good for a single vector.  Use the PACK version above for arrays
cvtps2dq   xmm0, xmm0
pmaxsd     xmm0, zeroed-register
pshufb     xmm0, [mask]
movd       [somewhere], xmm0

可能比使用两条pack指令更有效,因为pmax可以在端口 1 或 5 (Intel Haswell) 上运行。 cvtps2dq仅端口 1,仅pshufb端口pack*5。

于 2015-12-03T21:44:43.873 回答