Understanding the PowerPC rlwinm instruction
So, I finally convinced myself to try and learn PowerPC (PPC). Everything is going well and most of the information has been found on the internet. However, while looking at some examples, I came across this:
rlwinm r3, r3, 0,1,1
How do I do this in C? I tried doing some research but couldn't find anything that would help me. Thanks in advance!
source to share
rlwinm
means "Rotate left word immediate, then aNd with mask and that's the correct usage
rlwinm RA, RS, SH, MB, ME
According to the description page :
- RA Indicates the target universal register that stores the result of the operation.
- RS Specifies the source general register to operate on.
- SH Indicates the shift value for the operation.
- MB Specifies the initial mask value for the operation.
- ME Sets the final mask value for the job.
- BM Specifies the 32-bit mask value.
and
- If the MB value is less than the ME + 1 value, then the mask bit is between the start point and the end point, including those. All other bits are zeros.
- If the MB value is the same as ME + 1, then all 32 mask bits are set to one.
- If the MB value is greater than the ME + 1 value, then all mask bits between the enable value and the MD value +1 and the MB -1 value are set to zero. All other bits are set to ones.
So in your example, the source and target are the same. The amount of the shift 0
, so there is no shift. And MB=ME=1
, therefore, it applies the first case, so that the mask becomes all zeroes with the number 1 as a bit 1
, and the numbering of MSB=0
: 0x40000000
.
In C, we can write it as simple as
a &= 0x40000000;
Assuming a
is a 32 bit variable.
source to share
rlwinm
rotates the value of the register left by the specified number, executes AND
and stores the result in the register.
Example: rlwinm r3, r4, 5, 0, 31
r4
is the source register that is rotated by 5
, and before the rotated result is placed in r3
, it is also AND
ed with a bit mask of only 1 s, since the spacing between 0
and 31
is all 32-bit value.
An example from here .
For the implementation, C
you can take a look at how to rotate left and howAND
, which should now be trivial. Something like the following should work:
int rotateLeft(int input, int shift) {
return (input << shift) | ((input >> (32 - shift)) & ~(-1 << shift));
}
int rlwinm(int input, int shift, int mask) {
return rotateLeft(input, shift) & mask;
}
source to share