I want to clamp 32-bit unsigned ints to fixed value (0x10000) using only SSE2 instructions.
Basically, this C code:
if (c>0x10000) c=0x10000;
This code below works, but I'm wondering if it can be simplified, considering it's a specific constant (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
The value of c is in the range 0x00000000-0xFFFFFFFF, but code that assumes it is in the range 0x00000000-0x00FFFFFF or 0x00000000-0x00FF0000 may be acceptable.
If the range can be assumed to be
0x00000000to0x7fffffffor narrower, you can pretend the values are signed and simplify the sequence to:With SSE4.1, you can further simplify the code to just