Search code examples
powerpc

Clueless About insrwi Instruction


I have looked it up and nothing explains it well. It says that rlwimi can be used to be equivalent to it, but I don't know that instruction either.

Code with it in there:

 andi.     r0, r6, 3     # while(r6 != 3)
 bdnzf     eq, loc_90014730 # if(CTR != 0) loc_90014730();
 insrwi    r4, r4, 8,16  # ????
 srwi.     r0, r5, 4     # r0 = r5 >> 4;
 insrwi    r4, r4, 16,0

(r4 == 0)

I've been stuck on this instruction for a while. Please, don't just give me the result, please give me a detailed explanation.


Solution

  • I think you need to do some experiments with rlwimi to fully explain it to yourself, but here is what I find helpful.

    There is a programming note in Book 1 of the Power PC Programming Manual for rlwimi that provides a little more detail on inslwi and insrwi:

    rlwimi can be used to insert an n-bit field that is left-justified in the low-order 32 bits of register RS, into RAL starting at bit position b, by setting SH=32-b, MB=b, and ME=(b+n)-1. It can be used to insert an n-bit field that is right-justified in the low-order 32 bits of register RS, into RAL starting at bit position b, by setting SH=32-(b+n), MB=b, and ME=(b+n)-1.

    It also helps to compare the results of insrwi and inslwi. Here are two examples tracing through the rlwimi procedure, where r4=0x12345678.

    insrwi r4,r4,8,16 is equivalent to rlwimi r4,r4,8,16,23

    1. Rotate left 8 bits and notice it puts the last 8 bits of the original r4 in those positions that match the generated mask: 0x34567812
    2. Generate the mask: 0x0000FF00
    3. Insert the last 8 bits, which were those 8 bits that were right justified in r4, under the control of the generated mask: 0x12347878

    So insrwi takes n bits from the right side (starting at bit 32) and inserts them into the destination register starting at bit b.

    inslwi r4,r4,8,16 is equivalent to rlwimi r4,r4,16,16,23

    1. Rotate left 16 bits and notice it puts the first 8 bits of the original r4 in those positions that match the generated mask: 0x56781234
    2. Generate the mask: 0x0000FF00
    3. Insert the first 8 bits, which were those 8 bits that were left justified in r4, under the control of the generated mask: 0x12341278

    So inslwi takes n bits from the left side (starting at bit 0) and inserts them into the destination register starting at bit b.