ARM > Introduction to ARM > Barrel Shifter
Examples of Barrel Shifting
MOV r0, r0, LSL #1
- Multiply R0 by two.
MOV r1, r1, LSR #2
- Divide R1 by four (unsigned).
MOV r2, r2, ASR #2
- Divide R2 by four (signed).
MOV r3, r3, ROR #16
- Swap the top and bottom halves of R3.
ADD r4, r4, r4, LSL #4
- Multiply R4 by 17. (N = N + N * 16)
RSB r5, r5, r5, LSL #5
- Multiply R5 by 31. (N = N * 32 - N)
Remarks
At the start I mentioned that certain C operations don’t map onto available CPU operations. ROR
and RRX
are two of those operations.
Certain ARM instructions such as MUL
, CLZ
and QADD
cannot use the barrel shifter.
Note that right shifting negative signed quantities is strictly implementation defined behaviour in C. The compiler is allowed to choose whether it performs a logical or an arithmetic shift. (That said, it’s always implemented as an arithmetic shift in all of the ARM toolchains the author has used.)