Last active
April 26, 2025 19:43
-
-
Save gsauthof/c2dbb214803015c07d71fd0a7d4e0d74 to your computer and use it in GitHub Desktop.
demonstrate left shifting on risc-v - cf. https://stackoverflow.com/questions/57949406/rotating-bits-in-risc-v/58018098?noredirect=1#comment106365159_58018098
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Code / Registers: x5 x6 x7 x29 x30 | |
addi x30, x0, 0x123 ? ? ? ? 0x123 | |
andi x29, x30, 0x00f 0x3 | |
slli x6, x29, 60 0x3000000000000000 | |
srli x7, x30, 4 0x12 | |
add x5, x6, x7 0x3000000000000012 | |
when eliminating the andi instruction, it yields exactly the same result in x6 and of course in x5: | |
Code / Registers: x5 x6 x7 x29 x30 | |
addi x30, x0, 0x123 ? ? ? ? 0x123 | |
slli x6, x30, 60 0x3000000000000000 | |
srli x7, x30, 4 0x12 | |
add x5, x6, x7 0x3000000000000012 | |
That means the masking step is superfluous, because the left shift would shift out all the masked bits anyways. | |
Q.E.D. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment