rlwimi

Rotate Left Word Immediate Then Mask Insert

rlwimi RA, RS, SH, MB, ME

Rotates a word left, then inserts bits into the target under a mask. Used for inserting bitfields.

Details

The rlwimi instruction rotates the contents of the low-order 32 bits of register RS left by SH bits. It then generates a mask with 1-bits from bit MB+32 through bit ME+32 and ANDs the rotated data with this mask. The result is inserted into the low-order 32 bits of register RA, preserving the high-order 32 bits of RA.

Pseudocode Operation

n ← SH
r ← ROTL32((RS)32:63, n)
m ← MASK(MB+32, ME+32)
RA ← (RA & ~MASK(0, 31)) | ((r & m) >> SH)

Programming Note

The rlwimi instruction is commonly used for bit manipulation tasks such as rotating bits and selectively inserting them into a register. Be cautious with the shift amount (SH), mask bits (MB and ME), and ensure they are within valid ranges to avoid unexpected results. This instruction operates at user privilege level and does not generate exceptions under normal conditions, but improper use can lead to data corruption.

Example

rlwimi r4, r3, 3, 0, 31

Encoding

Binary Layout
20
0
RS
6
RA
11
SH
16
MB
21
ME
26
 
Format M-form
Opcode 0x50000000
Extension Base

Operands

  • RA
    Target/Dest
  • RS
    Source
  • SH
    Shift
  • MB
    Mask Begin
  • ME
    Mask End