我想使用 only SSE2 指令将 32 位无符号整数钳制为固定值(0x10000)。
基本上,这个 C 代码:
if (c>0x10000) rc=0x10000;
下面的代码有效,但我想知道是否可以简化它,考虑到它是一个特定的常量(0xFFFF + 0x0001)
movdqa xmm3, xmm0 <-- xmm0 contains 4 dword unsigned values
movdqa xmm4, xmm5 <-- xmm5: four dword 0x10000 values
pxor xmm3, xmm5
pcmpgtd xmm4, XMM0
psrad xmm3, 31
pxor xmm4, xmm3
pand xmm0, xmm4
pandn xmm4, xmm5
por xmm0, xmm4
您可以稍微简化顺序:
; xmm0 contains 4 dword unsigned values (input)
; xmm5 contains [0x10000, 0x10000, 0x10000, 0x10000]
movdqa xmm1, xmm5
pcmpgtd xmm1, xmm0 ; input < 0x10000
pand xmm0, xmm1 ; input < 0x10000 ? input : 0
pandn xmm1, xmm5 ; input < 0x10000 ? 0 : 0x10000
por xmm0, xmm1 ; input < 0x10000 ? input : 0x10000