I'm trying to find the most way of performing 8 bit unsigned compares using SSE (up to SSE 4.2).
The most common case I'm working on is comparing for > 0U, e.g.
_mm_cmpgt_epu8(v, _mm_setzero_si128()) // #1
(which of course can also be considered to be a simple test for non-zero.)
But I'm also somewhat interested in the more general case, e.g.
_mm_cmpgt_epu8(v1, v2) // #2
The first case can be implemented with 2 instructions, using various different methods, e.g. compare with 0 and then invert the result. The second case typically requires 3 instructions, e.g. subtract 128 from both operands and perform signed compare. (See this question for various 3 instruction solutions.)
What I'm looking for ideally is a single instruction solution for #1, and a two instruction solution for #2. If neither of these are possible then I'm also interested in thoughts as to which of the various possible 2 or 3 instruction implementations is most efficient on modern Intel CPUs (Sandy Bridge, Ivy Bridge, Haswell).
Best implementations for case #2 so far:
- compare equal with unsigned max and invert result:
#define _mm_cmpgt_epu8(v0, v1) \ _mm_andnot_si128(_mm_cmpeq_epi8(_mm_max_epu8(v0, v1), v1), \ _mm_set1_epi8(-1))
Two arithmetic instructions + one bitwise = 1.33 throughput.
- invert sign bits for both arguments (== subtract 128) and use signed compare:
#define _mm_cmpgt_epu8(v0, v1) \ _mm_cmpgt_epi8(_mm_xor_si128(v0, _mm_set1_epi8(-128)), \ _mm_xor_si128(v1, _mm_set1_epi8(-128)))
One arithmetic instruction + two bitwise = 1.16 throughput.
Best implementations for case #1, derived from case #2 implementations above:
- 1.
#define _mm_cmpgtz_epu8(v0) \ _mm_andnot_si128(_mm_cmpeq_epi8(v0, _mm_set1_epi8(0)), \ _mm_set1_epi8(-1))
One arithmetic instruction + one bitwise = 0.83 throughput.
- 2.
#define _mm_cmpgtz_epu8(v0) \ _mm_cmpgt_epi8(_mm_xor_si128(v0, _mm_set1_epi8(-128)), \ _mm_set1_epi8(-128)))
One arithmetic instruction + one bitwise = 0.83 throughput.
I had an idea for doing
>=
in two instructions:It doesn't help for
>
, though.Also, it's pretty much equivalent to ErmIg's SIMD Library answer (max_epu8(a,b) -> cmpeq with a), but worse because it needs a zeroed register. This works for SSE2, instead of SSE4.1, though.
psubusb
runs on the same ports aspminusb
.A previous version of this answer had the mistaken idea that
b-a
has the sign bit set iffa>b
. But it's actually one bit to the left of that which needs testing: the carry flag / bit (which doesn't exist for packed-integer SIMD).See the edit history for some ideas about broadcasting the sign bit to the rest of the element with
pshufb
(negated result) orpblendvb
(which may be single-uop on Skylake for the non-VEX version only).