Is it possible to bitshift using ARM native bitshift operators?

Status
Not open for further replies.

BLMinTenn

Well-known member
Looking in the user manual for the Teensy 3.x type processor uers guide, I have found 3 interesting commands that could possibly speed up bit shift operations for time critical code. The three specific operators for ARM assembler are

LSR = Logical Shift Right
ASR = "arithmetic" right shift
LSL = Logical Shift Left

excluding...
RORS
ROR
RRXS
RRX

The three are quite interesting, but I am alittle taken because all examples reference uint32_t & int32_t only. What about uint8_t and int8_t and using the "LSL" and LSR" commands specifically. Do any experts in the group have experience using these commands and how should / could I make assembler code compatable with the Arduino or C++ language? Theres "__asm__" but how do I create such a function that inserts a value determining how far to shift similar to 1<<3 & 1>>3.

Thanks
BLMinTenn
 
normally the compiler does that automatically.
and there are many more operations which can use shifts.
then, often 8 bit is slower than 32 bit.
so if you want fast code, use 32bit, uint32_t, not uint8_t.
 
When you use the << or >> operators in C/C++ code, the compiler will use those shift instructions anyway. For example, in some test code I used the statement "r = s << 3;" and the compiler executed the shift in one assembler instruction "lsls r3, r3, #3".
The 3.x Teensys use a Cortex M4 processor which has a barrel shifter. This means that all shift instructions execute in one clock cycle. I don't see that there's much room for optimization here.

What time critical code do you have in mind?

Pete
 
Last time I checked, the compiler does indeed detect this as a rotate and implement it with 1 instruction:

Code:
__attribute__((always_inline))
static inline uint32_t lrotate(uint32_t num, uint32_t r)
{
        return (num >> r) | (num << (32 - r));
}
 
Status
Not open for further replies.
Back
Top