SSE4: grater/less or equal relations for unsigned bytes/words
Relation greater or equal can be expressed with min function:
text
(x <= y) = (min(x, y) = x)Likewise relation less or equal can be expressed with max function:
text
(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]:
text
; 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)Proof
In the proof we consider three cases: x < y, x = y and x > y.
The second and the last column (i.e. left and right side of equivalence) are the same in both cases. QED
Greater or equal
| case | x <= y | min(x, y) | min(x, y) = x |
|---|---|---|---|
| x < y | true | x | true |
| x = y | true | x | true |
| x > y | false | y | false |
Less or equal
| case | x >= y | max(x, y) | max(x, y) = x |
|---|---|---|---|
| x < y | false | y | false |
| x = y | true | x | true |
| x > y | true | x | true |