Where does the SRAI shift amount come from?
For immediate shifts, the amount comes from the shamt field; in RV32I it is 5 bits.
Shift rs1 right arithmetically by shamt bits (sign-bit into MSBs), result in rd
SRAI uses opcode 0010011 (0x13), funct3 101, funct7 0100000. The rs1 field selects the source register, the 12-bit immediate provides the second operand, and rd selects the destination.
SRAI (I-type variant, opcode=0010011, funct3=101, funct7=0100000) shifts rs1 right arithmetically by shamt, filling upper bits with the original sign bit (bit31) to preserve sign. Result in rd. funct7 bit30=1 (encoding 0100000), which distinguishes it from SRLI. Used for signed integer division by power of 2.
SRAI performs a immediate arithmetic right shift, fills high bits with the original sign bit, and writes rd. The shift amount comes from the shamt immediate field.
Understand this scenario with real code like «srai x5, x6, 2 # x5 = x6 >> 2 (signed, sign-extended)».
Understand this scenario with real code like «srai x5, x6, 2 # x5 = x6 >> 2 (signed, sign-extended)».
Understand this scenario with real code like «srai x5, x6, 2 # x5 = x6 >> 2 (signed, sign-extended)».
For immediate shifts, the amount comes from the shamt field; in RV32I it is 5 bits.
Logical right shift fills high bits with zeros. Arithmetic right shift copies the original sign bit, which is usually used to preserve signedness.