srl
lyszt.net covers srl and other RISC-V instructions in more detail.
Encoding format
Description
srl moves bits to the right, the reverse of what sll does. The letters stand for shift right logical. This form takes the bits of rs1, moves them right by the count in rs2, and stores the result in rd.
srl rd, rs1, rs2
Any bits pushed past the right end are lost, and 0s fill the vacated spots on the left.
That zero fill explains the word logical. A right shift halves a value, so a shift of 1 divides by 2 and a shift of 3 divides by 8. The leftover is thrown away, matching integer division. srl divides an unsigned number by a power of two quickly.
Two right shift instructions exist, and the difference is what goes into the emptied high bits. srl puts 0s there. Its counterpart sra copies the sign bit into those positions, which preserves the sign of negative numbers. Choose srl for a raw bit pattern or a value that cannot be negative. On a signed value that may be negative, srl yields a large positive number, and sra is the correct instruction in that case.
According to the RISC-V manual, the shift count occupies the low 5 bits of register rs2. Those 5 bits span 0 through 31 places, so the remaining bits of rs2 are ignored. The manual calls srl a logical right shift, which inserts zeros at the top.
When the shift count is a constant, use the immediate form srli.
Example
li t0, -8 li t1, 1 srl t2, t0, t1 # t2 = 2147483644 (zero-filled)
