Performantly reverse the order of 16-bit quantities within a 64-bit word

158 views Asked by At

I need to do a lexicographic comparison of a small number of small unsigned integers. If there are (for example) 8 8-bit integers, the obvious approach is to byteswap them and do an ordinary integer compare in a GPR. If there are 2 32-bit integers, a 32-bit rotate and an ordinary compare will do the trick. What if there are 4 16-bit integers? Obviously with a vector register it is easy to shuffle them, but is there an efficient approach—either to reversing their order, or to doing the compare without reversing order—using only GPR?

1

There are 1 answers

2
Nate Eldredge On

For the reverse alone, here's my attempt:

wswap2:
        ;;  rdi = ABCD (words)
        mov rax, rdi            
        ror edi, 16             ; rdi = 00DC
        shl rdi, 32             ; rdi = DC00
        shr rax, 32             ; rax = 00AB
        ror eax, 16             ; rax = 00BA
        or rax, rdi             ; rax = DCBA
        ret

It's convenient to be able to use a 32-bit rotate to swap two adjacent words.

We've got two parallel dependency chains of two uops each, followed by one more uop to merge them.