SSE4: grater/less or equal relations for unsigned bytes/words

Author:Wojciech Muła
Added on:2.06.2008

Relation greater or equal can be expressed with min function:

(x <= y) = (min(x, y) = x)

Likewise relation less or equal can be expressed with max function:

(x >= y) = (max(x, y) = x)

SSE4.1 introduced instructions PMAXUB, PMAXUW, PMINUB and PMINUW that operate on unsigned bytes and words. With help of these instructions we can compare unsigned words using presented equivalences.

Here is a sample code that test if all bytes from xmm0 lie in range [lo .. hi]:

; xmm0 - vector
;   lo - lower bound
;   hi - higher bound

movdqa  %xmm0, %xmm1            ; clone vector

pminub     lo, %xmm0
pmaxub     hi, %xmm1
pcmpeqb    lo, %xmm0            ; xmm0 = (vector >= lo)
pcmpeqb    hi, %xmm1            ; xmm1 = (vector <= hi)

pand    %xmm1, %xmm0            ; xmm1 = (vector >= lo) and (vector <= hi)