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

Rotates the contents of RS left by SH bit positions, creates a mask from bits MB through ME, and inserts the rotated value into RA under that mask, leaving other bits in RA unchanged. The condition register CR0 is updated if Rc=1.

Pseudocode Operation

n ← SH
mask ← MASK(MB, ME)
rotated ← ROTL32(RS, n)
RA ← (RA & ¬mask) | (rotated & mask)
if Rc = 1 then CR0 ← (RA < 0, RA > 0, RA = 0, SO)

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