- Byte swapping: 8 bytes at a time (By Dick Sites)
This one is useful to swap large regions of memory (arrays), and was originally
written with the 21064 in mind.
Input: 0x1122334455667788 (in memory)
| Instruction | Value |
| LDG f1, &mem | f1: 0x7788556633441122 |
| STT f1, &tmp_area |
| LDQ r1, &tmp_area | f1: 0x7788556633441122 |
| ZAP r1, #0xaa, r2 | r2: 0x0088006600440022 |
| ZAP r1, #0x55, r3 | r3: 0x7700550033001100 |
| SLL r2, 0x8, r2 | r2: 0x8800660044002200 |
| SRL r3, 0x8, r3 | r3: 0x0077005500330011 |
| OR r2, r3, r0 | r0: 0x8877665544332211 |
By using the FTOIT instruction in the 21264, the sequence can be:
| Instruction | Value |
| LDG f1, &mem | f1: 0x7788556633441122 |
| FTOIT f1, r1 | f1: 0x7788556633441122 |
| ZAP r1, #0xaa, r2 | r2: 0x0088006600440022 |
| ZAP r1, #0x55, r3 | r3: 0x7700550033001100 |
| SLL r2, 0x8, r2 | r2: 0x8800660044002200 |
| SRL r3, 0x8, r3 | r3: 0x0077005500330011 |
| OR r2, r3, r0 | r0: 0x8877665544332211 |
- Byte swapping: signed 32-bit item (by Rick Gorton & Anton Chernoff)
Input: 0xAABBCCDD (in a register)
| Instruction | Value |
| EXTWL src, 1, tmp1 | tmp1: 0x0000BBCC |
| EXTBL src, 3, tmp2 | tmp2: 0x000000AA |
| INSBL src, 3, tmp3 | tmp3: 0xDD000000 |
| ANDNOT tmp1, 0xff, tgt | tgt: 0x0000BB00 |
| INSBL tmp1, 2, tmp1 | tmp1: 0x00CC0000 |
| ADDL tmp2, tmp3, tmp2 | tmp2: 0xDD0000AA |
| ADDL tmp1, tmp2, tmp1 | tmp1: 0xDDCC00AA |
| ADDL tgt, tmp1, tgt | tmp1: 0xDDCCBBAA |
For an unsigned 32-bit version, simply replace the last ADDL with an
OR
- Byte swapping: signed 32-bit item (by Mike Burrows)
Input: 0xXXXXXXXXAABBCCDD (X meaning irrelevant)
| Instruction | Value |
| INSLH a0, 7, v0 | v0: 0000000000AABBCC |
| INSWL a0, 3, a0 | a0: 000000CCDD000000 |
| BIS a0, v0, a0 | v0: 000000CCDDAABBCC |
| SRL a0, 16, v0 | v0: 0000000000CCDDAA |
| ZAPNOT a0, 0xa, a0 | a0: 00000000DD00BB00 |
| ZAP v0, 0xa, v0 | v0: 0000000000CC00AA |
| ADDL v0, a0, v0 | v0: 00000000DDCCBBAA |
For an unsigned 32-bit version, simply replace the last ADDL with an
OR
- Byte swapping: signed 32-bit item assuming 21164PC or 21264 (by Mike
Burrows)
Input: 0xXXXXXXXXAABBCCDD (X meaning irrelevant)
| Instruction | Value |
| UNPKBW a0, v0 | v0: 00AA00BB00CC00DD |
| SLL v0, 24, a0 | a0: BB00CC00DD000000 |
| BIS v0, a0, a0 | a0: BBAACCBBDDCC00DD |
| EXTWL a0, 6, v0 | v0: 000000000000BBAA |
| ZAP a0, 0xf3, a0 | a0: 00000000DDCC0000 |
| ADDL a0, v0, v0 | v0: 00000000DDCCBBAA |
For an unsigned 32-bit version, simply replace the last ADDL with an
OR
- Byte swapping using memory: unsigned 32-bit item assuming 21264 (by
Mike Burrows)
Input: 0xXXXXXXXXAABBCCDD (X meaning irrelevant)
| Instruction | Value |
| UNPKBW a0, v0 | v0: 00AA00BB00CC00DD |
| STQ v0, 0(sp) | |
| LDG f0, 0(sp) | f0: 00DD00CC00BB00AA |
| FTOIT f0, v0 | v0: 00DD00CC00BB00AA |
| PKWB v0, v0 | a0: 00000000DDCCBBAA |
For a signed 32-bit version, add 'ADDL v0, 0, v0' to the sequence