POOL32A 000000 |
rt |
rs |
WSBH 0111101100 |
POOL32AXf 111100 |
6 |
5 |
5 |
10 |
6 |
WSBH rt, rs |
microMIPS |
Word Swap Bytes Within Halfwords |
Word Swap Bytes Within Halfwords
To swap the bytes within each halfword of GPR rs and store the value into GPR rt.
GPR[rt] = SwapBytesWithinHalfwords(GPR[rs])
Within each halfword of the lower word of GPR rs the bytes are swapped, the result is sign-extended, and stored in
GPR rt.
In implementations prior to Release 2 of the architecture, this instruction resulted in a Reserved Instruction exception.
If GPR rs does not contain a sign-extended 32-bit value (bits 63..31 equal), then the result of the operation is
UNPREDICTABLE.
if NotWordValue(GPR[rt]) then UNPREDICTABLE endif GPR[rt] = sign_extend(GPR[rs]23..16 || GPR[rs]31..24 || GPR[rs]7..0 || GPR[rs]15..8)
Reserved Instruction
The WSBH instruction can be used to convert halfword and word data of one endianness to another endianness. The endianness of a word value can be converted using the following sequence:
lw t0, 0(a1) /* Read word value */ wsbh t0, t0 /* Convert endiannes of the halfwords */ rotr t0, t0, 16 /* Swap the halfwords within the words */
Combined with SEH and SRA, two contiguous halfwords can be loaded from memory, have their endianness converted, and be sign-extended into two word values in four instructions. For example:
lw t0, 0(a1) /* Read two contiguous halfwords */ wsbh t0, t0 /* Convert endiannes of the halfwords */ seh t1, t0 /* t1 = lower halfword sign-extended to word */ sra t0, t0, 16 /* t0 = upper halfword sign-extended to word */
Zero-extended words can be created by changing the SEH and SRA instructions to ANDI and SRL instructions, respectively.
.