Analyzing results of comparision in AVX2

Viewed 215

I'm trying to optimize code with AVX2 assembly. At one point I need to compare the result of computation with the threshold and write 0 or 255 byte to output. I compare with

VCMPPD ymm2,ymm1 (values here),ymm4 (thresholds here),1

Then, ymm2 holds 4 QUADWORDS 0 and 0xFF. Ideally would be to shrink it all to 4 byte in EAX. But now, I'm doing 4 VPTEST operations and several conditional jumps to form the output. This slows down the performance significantly.

Question: how to get and use the result of comparison with AVX2 effectively?

1 Answers

You're probably looking for vmovmskpd eax, ymm2 (manual entry) to get a 4-bit bitmap which you can then analyze with integer instructions like test eax,eax. or cmp al, 0xf to check if all elements were true, or even as an index for a jump table like jmp [table + rax*8] if you need finer detail of which elements were true.

You could of course use vpmovmskb if you actually want 8 identical bits from each vector element, one from each byte.

If you didn't already know about movmskps/pd and so on, I'd suggest Agner Fog's optimization guide: he has a chapter about SIMD. https://agner.org/optimize/. vector -> bitmap is one of x86's best features.

Related