Search code examples
assemblyoptimizationbit-manipulationx86-64micro-optimization

Fastest way to set highest order bit of rax register to lowest order bit in rdx register


This is my approach:

and rdx, 0x1
ror rdx, 1
or rax, rdx

But I think this is not very efficient. And I don't know if shift operations would be more cost efficient.


Solution

  • Try

     add rax,rax
     shld rdx, rax, 63
    

    Msb of rax is first removed, then the concatenated sequence only contains the lsb of rdx and the 63 bits of rax, which are to be shifted left to rdx.

    or

     add rax, rax
     shrd rax, rdx, 1
    

    (This answer assumes, that the explanation in the question is right and the code is wrong -- since 'copying' a cleared bit over a set bit is not possible with that code.)