rlwinm (Rotate Left Word Immediate then aNd with Mask)
rlwinm is a powerful PPC instruction. But it can also be confusing.
args: dest-register, source-register, rotate-left, mask-left, mask-right
This instruction rotates source to the left, then it ANDs the result with a mask that is all ones from bit mask-left to bit mask-right. All arguments are in the range 0..31. mask-left and mask-right are bit numbers (where the left-most (most significant) bit is zero, and the rightmost bit is 31. The range is inclusive.
Example: If you wanted to take the 8 highest bits from r3 and put them in r4 (but shifted down 8 bits) you would do:
rlwinm r4, r3, 24, 8, 15
To see how, pretend that r3 contains 0x12345678. r4 will get clobbered, so it can contain anything, initially. The first thing that happens is that the r3 value gets left-shifted 24 bits, not-sign-extended, to 64 bits. r3, internally, now looks like this:
0x0012345678000000
Since this is a rotate operation, not a shift, and a 32-bit result, nonetheless, we must preserve the bits that fell off the left side of the 32-bit r3. Those fallen bits get resurrected at the bottom of the working word. The working 32-bit word is now:
0x78123456
Next, the mask is applied. The most-significant bit is bit-zero, so masking bits 8 through 15 equates to this mask:
0x00ff0000
Applying the mask, the value is:
0x00120000
Which is exactly the top 8 bits from r3, shifted right 8 bits. This is what gets jammed into r4.
下面是pem32b.pdf上的解释
rlwinm rA,rS,SH,MB,ME (Rc = 0)
rlwinm. rA,rS,SH,MB,ME (Rc = 1) // 影响条件寄存器CR0
n <-- SH // 左旋位n(=SH)
r <-- ROTL(rS, n) // rS左旋n位,赋给r
m <-- MASK(MB, ME) // m的MB ~ ME位均为1,其余为0
rA <-- r & m // r 和 m 逻辑与的结果
The contents of rS[32-63] are rotated left the number of bits specified by operand SH. A mask is generated having 1 bits from bit MB through bit ME and 0 bits elsewhere. The rotated data is ANDed with the generated mask and the result is placed into rA.
1. Extract the sign bit (bit 0) of rS and place the result right-justified into rA.
extrwi rA,rS,1,0 (equivalent to rlwinm rA,rS,1,31,31)
2. Insert the bit extracted in (1) into the sign bit (bit 0) of rB.
insrwi rB,rA,1,0 (equivalent to rlwimi rB,rA,31,0,0)
3. Shift the contents of rA left 8 bits.
slwi rA,rA,8 (equivalent to rlwinm rA,rA,8,0,23)
4. Clear the high-order 16 bits of rS and place the result into rA.
clrlwi rA,rS,16 (equivalent to rlwinm rA,rS,0,16,31)
阅读(1794) | 评论(0) | 转发(0) |