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

Time:04-30

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.

CodePudding user response:

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.)

CodePudding user response:

If rdx can be destroyed then you don't need to mask it like that because the left shift will clear the remaining bits anyway

shl rdx, 63
or rax, rdx

This assumes that the MSB of rax is cleared, as Jester said

CodePudding user response:

Rotate is as fast as shift on modern processors, but on older ones, it can be slow, so it's better to use shift if it can solve the problem.

If you can destroy rdx, I'm pretty sure that the following instruction sequence is the fastest on any x86_64 processor.

shl rdx, 63
or rax, rdx
  • Related