Unsigned comparisons in AVX2/SSE: a quick note
outerproduct.net
Unsigned comparisons in AVX2/SSE: a quick note
1–10 of 21 posts
Re: Unsigned comparisons in AVX2/SSE: a quick note
#2Re: Unsigned comparisons in AVX2/SSE: a quick note
#3I would like to read more about superoptimizers targeting AVX2, since previously I've complained that gcc will not make transformations between various equivalent instruction sequences for moving bytes around within and between registers, and some of these are a lot slower than others.
Re: Unsigned comparisons in AVX2/SSE: a quick note
#4(and, for completeness, a regular C loop[1], with some massaging to make it more readable)
Re: Unsigned comparisons in AVX2/SSE: a quick note
#5What gcc/clang does?
Re: Unsigned comparisons in AVX2/SSE: a quick note
#6For what it's worth, clang[0] compiles to the vpminu[bwd] + vpcmpeq[bwd] version even from manually written intrinsics of the addition-based version, and has since clang 7. Though, interestingly enough, it fails to cancel out a following xor. (and, for completeness, a regular C loop[1], with some massaging to make it more readable) [0]: https://godbolt.org/z/e7TE5P73Y [1]: https://godbolt.org/z/xhj3WTnxv
Re: Unsigned comparisons in AVX2/SSE: a quick note
#7On many CPUs, bitwise XOR is slightly more efficient than addition. But you still need the magic number.
> and you are in a hostile environment, you will have to figure out how to load up a vector of 128s, which costs cycles
That particular vector can be generated with 2 instructions without RAM access, pcmpeqd to generate a vector with all bits set, and psllw/pslld for shifts.
Modern compilers support LTCG/LTO which optimizes code across translation units. If you have a loop comparing these vectors, the magic vector is likely to be created outside, and kept in a register.
> To avoid this, use min
Yeah, but if you need to compare for a > if anyone working on superoptimisation has caught these?
Page #17 there: http://const.me/articles/simd/simd.pdf
Re: Unsigned comparisons in AVX2/SSE: a quick note
#8> simply add 128 (or however much—depending on the sizes at hand) to each of your inputs before comparing On many CPUs, bitwise XOR is slightly more efficient than addition. But you still need the magic number. > and you are in a hostile environment, you will have to figure out how to load up a vector of 128s, which costs cycles That particular vector can be generated with 2 instructions without RAM access, pcmpeqd t…
That's pretty interesting, any examples of CPUs (or microcontrollers) where this happens?
Re: Unsigned comparisons in AVX2/SSE: a quick note
#9> simply add 128 (or however much—depending on the sizes at hand) to each of your inputs before comparing On many CPUs, bitwise XOR is slightly more efficient than addition. But you still need the magic number. > and you are in a hostile environment, you will have to figure out how to load up a vector of 128s, which costs cycles That particular vector can be generated with 2 instructions without RAM access, pcmpeqd t…
On a more global scale, all assembly "optimisations"/tricks are hidden deep into compilers, which are reasonably "transparent" to their devs only, that due to their abysmal complexity and size.
We would need some sort of online library for those assembly (boolean/branchless calculus...) tricks.
A job for wikipedia? Maybe linked to the maths/boolean calculus?
Re: Unsigned comparisons in AVX2/SSE: a quick note
#10> simply add 128 (or however much—depending on the sizes at hand) to each of your inputs before comparing On many CPUs, bitwise XOR is slightly more efficient than addition. But you still need the magic number. > and you are in a hostile environment, you will have to figure out how to load up a vector of 128s, which costs cycles That particular vector can be generated with 2 instructions without RAM access, pcmpeqd t…
> On many CPUs, bitwise XOR is slightly more efficient than addition. That's pretty interesting, any examples of CPUs (or microcontrollers) where this happens?
BTW, Zen 2 CPUs are used in both Xbox S/X, and PS5.