rlwimi
Rotate Left Word Immediate Then Mask Insert
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
Encoding
Operands
-
RA
Target/Dest -
RS
Source -
SH
Shift -
MB
Mask Begin -
ME
Mask End