PowerPC - Trying to understand the rlwinm instruction - Printable Version +- Mario Kart Wii Gecko Codes, Cheats, & Hacks (https://mariokartwii.com) +-- Forum: Hacks/Modding (https://mariokartwii.com/forumdisplay.php?fid=14) +--- Forum: Code Support / Help / Requests (https://mariokartwii.com/forumdisplay.php?fid=61) +--- Thread: PowerPC - Trying to understand the rlwinm instruction (/showthread.php?tid=1848) |
PowerPC - Trying to understand the rlwinm instruction - JimmyKazakhstan - 06-26-2021 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. 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. RE: PowerPC - Trying to understand the rlwinm instruction - Seeky - 06-26-2021 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 RE: PowerPC - Trying to understand the rlwinm instruction - JimmyKazakhstan - 06-26-2021 (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? RE: PowerPC - Trying to understand the rlwinm instruction - Vega - 06-26-2021 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. RE: PowerPC - Trying to understand the rlwinm instruction - JimmyKazakhstan - 06-26-2021 (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. 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. RE: PowerPC - Trying to understand the rlwinm instruction - Vega - 06-26-2021 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 RE: PowerPC - Trying to understand the rlwinm instruction - JimmyKazakhstan - 06-26-2021 (06-26-2021, 06:31 PM)Vega Wrote: rlwinm r5, r4, 3, 2, 30 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. RE: PowerPC - Trying to understand the rlwinm instruction - Vega - 06-26-2021 Had a typo in my post, refresh. Im writing up another example atm using your value RE: PowerPC - Trying to understand the rlwinm instruction - Vega - 06-26-2021 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 RE: PowerPC - Trying to understand the rlwinm instruction - JimmyKazakhstan - 06-26-2021 (06-26-2021, 10:25 PM)Vega Wrote: rlwinm r3, r12, 5, 15, 16 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! |