Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 801
stht55 Avatar asked Oct 21 '25 17:10

stht55


1 Answers

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

like image 71
Aki Suihkonen Avatar answered Oct 23 '25 13:10

Aki Suihkonen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!