SSE Compare Packed Unsigned Bytes

I am trying to use SSE instructions to filter images. The image I'm using has bytes per pixel (255 shades of gray), and I need to compare unsigned packed bytes using a comparison other than comparison. I looked into the intel manual and a comparison exists, but only for signed bytes (PCMPGTB). How can I do this comparison for unsigned bytes? thanks in advance

+4
source share
4 answers

It is impossible to do more than compare in packed unsigned bytes, I unpacked the bytes into words (since they were likely like converting from unsigned to signature and expanding from byte to word) and compared them using PCMPGTB.

+1
source

It is not possible to directly perform unsigned comparisons.

But you can add -128 to each value (either subtract 128, or XOR 0x80, or the like). This will turn 0 into -128, 255 to 127 and other values ​​into values ​​between them; as a result, you get the correct results from the comparison.

Extending it to words should work too, but it sounds a little slower, since you get half the work done for each instruction.

(Better late answer than never, I think.)

+9
source

An unsigned comparison (a> = b) is identical to maxu (a, b) == a, so you can use

_mm_cmpeq_epi8( a, _mm_max_epu8(a,b)) --> a >= b "cmpge_epu8(a,b)" 

If you need a comparison < or > , you need to invert the result, after which the Alcaro approach can be just as good (although this method requires a register to transfer the constant for inversion). But for comparison, >= or <= is definitely better (since there is no _mm_cmple_epi8 or _mm_cmpge_epi8 there, even after converting unsigned to a signed range).

+4
source

Suggestion of a small but important improvement for @greggos solution:

 maxu( a, b ) == a 

has a drawback as you need to backup "a" before comparing maxu, which will result in an extra operation, something like this:

 movq mmc, mma pmaxu mma, mmb pcmpeq mma, mmc 

 minu( a, b ) == b 

gives exactly the same effect, but retains operators for checking equality:

 pminu mma, mmb pcmpeq mma, mmb 

The increase is significant: only 2 operations instead of 3.

+1
source

All Articles