Posts: 43
Threads: 12
Joined: Jan 2020
Reputation:
1
I have another question about something I wanted to add to the PowerPC For Dummies guide.
I've been trying to learn how the rlwinm instruction works and oh man, let me tell you.
It's a lot harder than I thought it would be. I can't even begin to describe the headaches I got from trying to figure it out.
Yes, I got an actual headache from this.
I thought it was only one big instruction, but it turns out to be the substitute for 4 different ones.
I spent about 4 hours testing many things with it, I put together a very brief simplification of it.
Code: rlwinm = (Rotate Left Word Immediate With AND Mask) = An instruction that has the functions of the slwi, srwi, clrlwi, and clrrwi instructions.
rlwinm rA, rB, SH, MB, ME
# There isn't a definition I can give for any of these because their usage changes based on what operation is being done.
RA = Destination Register
RB = Source Register
SH = Shift Amount
MB = Mask Value
ME = End Mask
# clrlwi Operation
# You will get this if the "Shift Amount" is 0 and the Mask Value is 0
# The amount of bits cleared depends on what the End Mask Value is
# Assume the register holds the value 0x81234567
0 = 80000000
8 = 81000000
11 - 15 = 81200000
16 = 81230000
17 - 23 = 81234000
24 = 81234500
27 - 29 = 81234560
# clrrwi Operation
# You will get this if the "Shift Amount" is 0 and the End Mask is 31
# The amount of bits cleared depends on what the Mask Value is
# Assume the register holds the value 0x81234567
1 = 01234567
8 = 00234567
11 - 15 = 00034567
16 = 00004567
17 - 23 = 00000567
24 = 00000067
27 - 29 = 00000007
# slwi Operation
# You will get this if the Mask Value is 0 and the End Mask is the amount of bits you want to shift.
# If the End Mask is 31, the code will shift portions of the same value into the new space (I don't know any other way to word this).
# EXAMPLE :
# Assume the register holds the value 0x81234567
# rlwinm r18,r18,16,0,31
# If the End Mask was 24, the register would have the value 0x45678100
# But since it's 31, The value becomes 0x45678123
# The amount of bits shifted depends on what the Shift Amount is
# Assume the register holds the value 0x81234567
8 = 23456700
16 = 45670000
24 = 67000000
That's as far as I managed to get before I quit because of the headache I got.
My question is....
Is any part of this correct?
I want to know in advance before I add anything else to what I have.
Posts: 202
Threads: 12
Joined: Dec 2019
Reputation:
16
Personally I tend to ignore the macros and just use rlwinm itself, so I can't really comment on the others. Firstly, the operands of the fundamental rlwinm don't change their usage based on the operation, at the low level they still mean the same thing. I think it's worth specifying that it's specifically a *rotation* and not a shift for SH, since in a shift just discards bits that flow out of the end of the register, while rotation re-inserts them at the other end (and also specifically rotation left). 'Mask Value' doesn't really mean anything, it's the bit where the mask starts (indexed with 0 as the MSB and 31 as the LSB) like how ME is the bit where the mask ends. Probably worth mentioning too that the mask can 'loop around', so having it start at 31 and end at 29 would include every bit apart from bit 30. I'd guess you've already read Vega's thread on this, but in case you didn't know about it here's a link https://mkwii.com/showthread.php?tid=1262
Posts: 43
Threads: 12
Joined: Jan 2020
Reputation:
1
(06-26-2021, 11:40 AM)Seeky Wrote: Personally I tend to ignore the macros and just use rlwinm itself, so I can't really comment on the others. Firstly, the operands of the fundamental rlwinm don't change their usage based on the operation, at the low level they still mean the same thing. I think it's worth specifying that it's specifically a *rotation* and not a shift for SH, since in a shift just discards bits that flow out of the end of the register, while rotation re-inserts them at the other end (and also specifically rotation left). 'Mask Value' doesn't really mean anything, it's the bit where the mask starts (indexed with 0 as the MSB and 31 as the LSB) like how ME is the bit where the mask ends. Probably worth mentioning too that the mask can 'loop around', so having it start at 31 and end at 29 would include every bit apart from bit 30. I'd guess you've already read Vega's thread on this, but in case you didn't know about it here's a link https://mkwii.com/showthread.php?tid=1262
I'll be honest, I have no idea what most of this means. I think I have to start with the basics, what exactly does it mean to "rotate" a bit? shifting right/left multiplied and divided it when tested. Is "rotating" a bit putting it to "the power of" a value?
Posts: 4,292
Threads: 901
Joined: Feb 2018
Reputation:
98
06-26-2021, 05:34 PM
(This post was last modified: 06-26-2021, 05:36 PM by Vega.)
Not trying to sound rude but did you not read the thread Seeky linked? The thread has a portion of info which links a pic that visually shows rotating bits vs shifting bits.
Also included in the thread (at the very beginning) is a link to the Logical Operations thread (which includes the basics of bits/binary) tut which you NEED to know before diving into bit rotation/shifting/etc.
rlwinm deals with bit/binary shifting/rotating plus logical ANDing. Once you understand those two main points, it will make sense.
Hope this all helps as rotation is a bit confusing for beginner PPC coders.
Posts: 43
Threads: 12
Joined: Jan 2020
Reputation:
1
(06-26-2021, 05:34 PM)Vega Wrote: Not trying to sound rude but did you not read the thread Seeky linked? The thread has a portion of info which links a pic that visually shows rotating bits vs shifting bits.
Also included in the thread (at the very beginning) is a link to the Logical Operations thread (which includes the basics of bits/binary) tut which you NEED to know before diving into bit rotation/shifting/etc.
rlwinm deals with bit/binary shifting/rotating plus logical ANDing. Once you understand those two main points, it will make sense.
Hope this all helps as rotation is a bit confusing for beginner PPC coders.
Yeah, I did. That's one of the main things I refered to when writing that attempted simplification, I had a very hard time understanding it though, because I thought that rlwinm was just for bit rotation, but it turns out to be for many different purposes. Even with the examples I was still very confused.
Posts: 4,292
Threads: 901
Joined: Feb 2018
Reputation:
98
06-26-2021, 06:31 PM
(This post was last modified: 06-26-2021, 10:20 PM by Vega.)
rlwinm r5, r4, 3, 2, 30
r4 = source register ofc
r4's value is rotated to the left by 3 bits, (rotate each bit counter clockwise by the value of 3).
Bit 0 rotated to the left by value of 3 is now Bit 29
Bit 1 is now Bit 30
Bit 2 is now Bit 31
Bit 3 is now Bit 0
Bit 4 is now Bit 1
Bit 5 is now Bit 2
etc etc
Now you have a temporary rotated value
MB is the start of the ANDing mask, while ME is the end
2 is the start, 30 is the end
in binary that is a mask of 0011 1111 1111 1111 1111 1111 1111 1110
so in hex that's 0x3FFFFFFE
You take the temporary rotated value and AND it with 0x3FFFFFFE
w/e final result that is now goes into r5, and voila that's a rlwinm instruction
---
and as seeky mentioned earlier, the ME value can be lower than MB so the AND mask will 'loop' around clockwise
i.e. rlwinm r0, r31, 7, 29, 3 #This in binary is 1111 0000 0000 0000 0000 0000 0000 0111 (0xF0000007)
---
So the rotation is counter clockwise while the AND mask works in a clockwise fashion
Posts: 43
Threads: 12
Joined: Jan 2020
Reputation:
1
06-26-2021, 10:16 PM
(This post was last modified: 06-27-2021, 12:48 PM by Vega.
Edit Reason: Fixed typo in my quoted post - Vega
)
(06-26-2021, 06:31 PM)Vega Wrote: rlwinm r5, r4, 3, 2, 30
r4 = source register ofc
r4's value is rotated to the left by 3 bits, (rotate each bit counter clockwise by the value of 3).
Bit 0 rotated to the left by value of 3 is now Bit 29
Bit 1 is now Bit 30
Bit 2 is now Bit 31
Bit 3 is now Bit 0
Bit 4 is now Bit 1
Bit 5 is now Bit 2
etc etc
Now you have a temporary rotated value
MB is the start of the ANDing mask, while ME is the end
2 is the start, 30 is the end
in binary that is a mask of 0011 1111 1111 1111 1111 1111 1111 1110
so in hex that's 0x3FFFFFFE
You take the temporary rotated value and AND it with 0x3FFFFFFE
w/e final result that is now goes into r5, and voila that's a rlwinm instruction
---
and as seeky mentioned earlier, the ME value can be lower than MB so the AND mask will 'loop' around clockwise
i.e. rlwinm r0, r31, 7, 29, 3 #This in binary is 1111 0000 0000 0000 0000 0000 0000 0111 (0xF0000007)
---
So the rotation is counter clockwise while the AND mask works in a clockwise fashion
I'll be honest.... I still don't get it... I'm sorry...
Can you remake that example but with the source register having the value 0x80000004? I think that would help me understand this a lot better.
Posts: 4,292
Threads: 901
Joined: Feb 2018
Reputation:
98
Had a typo in my post, refresh. Im writing up another example atm using your value
Posts: 4,292
Threads: 901
Joined: Feb 2018
Reputation:
98
06-26-2021, 10:25 PM
(This post was last modified: 06-27-2021, 02:13 AM by Vega.)
rlwinm r3, r12, 5, 15, 16
r12 = 0x80000004 before the instruction is executed. result will go into r3 ofc.
--
1: Rotate 0x80000004 counter clockwise by 5 bits
Before rotation: 1000 0000 0000 0000 0000 0000 0000 0100
After rotation: 0000 0000 0000 0000 0000 0000 1001 0000
Temporary rotated value is 0x00000090
2. 15,16 = AND mask of 0000 0000 0000 0001 1000 0000 0000 0000
The MB and ME marks which bits are high in the ANDing mask
3. 0x00000090 AND'd with 0x00018000 = 0x00000000
4. r3 = 0
Apologies for any more typos, in a rush multitasking with other things atm
Posts: 43
Threads: 12
Joined: Jan 2020
Reputation:
1
(06-26-2021, 10:25 PM)Vega Wrote: rlwinm r3, r12, 5, 15, 16
r12 = 0x80000004 before the instruction is executed. result will go into r3 ofc.
--
1: Rotate 0x80000004 counter clockwise by 5 bits
Before rotation: 1000 0000 0000 0000 0000 0000 0000 0100
After rotation: 0000 0000 0000 0000 0000 0000 1001 0000
Temporary rotated value is 0x00000090
2. 15,16 = AND mask of 0000 0000 0000 0001 1000 0000 0000 0000
The MB and ME marks which bits are high in the ANDing mask
3. 0x00000090 AND'd with 0x00018000 = 0x00000000
4. r3 = 0
Apologize for any more typos, in a rush multitasking with other things atm
Thanks for trying to help me understand this, but unfortunately I still dont really get it. I think I need to do some more research and testing. This instruction turned out to be much harder than I thought it would be to understand. But thanks for trying to help!
|