Speeduino
Loading...
Searching...
No Matches
Functions
Optimised bitwise shifts

As of AVR-GCC 13.2.0, the code produced for 32-bit shifts with a compile time shift distance is very poor. The templates here implement optimised shifting. Average 35% increase in shift performance on AtMega2560: see unit tests. More...

Functions

template<uint8_t b>
static uint32_t lshift (uint32_t a)
 Bitwise left shift - generic, unoptimized, case.
 
template<uint8_t b>
static uint32_t rshift (uint32_t a)
 Bitwise right shift - generic, unoptimized, case.
 
template<uint8_t b>
static uint32_t rshift_round (uint32_t a)
 Rounded arithmetic right shift.
 
template<>
uint32_t lshift< 4U > (uint32_t a)
 uint32_t bitwise left shift optimised for the specified shift distance

 
template<>
uint32_t lshift< 5U > (uint32_t a)
 uint32_t bitwise left shift optimised for the specified shift distance

 
template<>
uint32_t lshift< 6U > (uint32_t a)
 uint32_t bitwise left shift optimised for the specified shift distance

 
template<>
uint32_t lshift< 7U > (uint32_t a)
 uint32_t bitwise left shift optimised for the specified shift distance

 
template<>
uint32_t lshift< 9U > (uint32_t a)
 uint32_t bitwise left shift optimised for the specified shift distance

 
template<>
uint32_t lshift< 10U > (uint32_t a)
 uint32_t bitwise left shift optimised for the specified shift distance

 
template<>
uint32_t lshift< 11U > (uint32_t a)
 uint32_t bitwise left shift optimised for the specified shift distance

 
template<>
uint32_t lshift< 12U > (uint32_t a)
 uint32_t bitwise left shift optimised for the specified shift distance

 
template<>
uint32_t lshift< 13U > (uint32_t a)
 uint32_t bitwise left shift optimised for the specified shift distance

 
template<>
uint32_t lshift< 14U > (uint32_t a)
 uint32_t bitwise left shift optimised for the specified shift distance

 
template<>
uint32_t lshift< 15U > (uint32_t a)
 uint32_t bitwise left shift optimised for the specified shift distance

 
template<>
uint32_t rshift< 3U > (uint32_t a)
 uint32_t bitwise right shift optimised for the specified shift distance

 
template<>
uint32_t rshift< 4U > (uint32_t a)
 uint32_t bitwise right shift optimised for the specified shift distance

 
template<>
uint32_t rshift< 5U > (uint32_t a)
 uint32_t bitwise right shift optimised for the specified shift distance

 
template<>
uint32_t rshift< 6U > (uint32_t a)
 uint32_t bitwise right shift optimised for the specified shift distance

 
template<>
uint32_t rshift< 7U > (uint32_t a)
 uint32_t bitwise right shift optimised for the specified shift distance

 
template<>
uint32_t rshift< 9U > (uint32_t a)
 uint32_t bitwise right shift optimised for the specified shift distance

 
template<>
uint32_t rshift< 10U > (uint32_t a)
 uint32_t bitwise right shift optimised for the specified shift distance

 
template<>
uint32_t rshift< 11U > (uint32_t a)
 uint32_t bitwise right shift optimised for the specified shift distance

 
template<>
uint32_t rshift< 12U > (uint32_t a)
 uint32_t bitwise right shift optimised for the specified shift distance

 
template<>
uint32_t rshift< 13U > (uint32_t a)
 uint32_t bitwise right shift optimised for the specified shift distance

 
template<>
uint32_t rshift< 14U > (uint32_t a)
 uint32_t bitwise right shift optimised for the specified shift distance

 
template<>
uint32_t rshift< 15U > (uint32_t a)
 uint32_t bitwise right shift optimised for the specified shift distance

 

Detailed Description

As of AVR-GCC 13.2.0, the code produced for 32-bit shifts with a compile time shift distance is very poor. The templates here implement optimised shifting. Average 35% increase in shift performance on AtMega2560: see unit tests.

Usage:

static uint32_t rshift(uint32_t a)
Bitwise right shift - generic, unoptimized, case.
Definition bit_shifts.h:349

If there is no overload for a certain shift, that's because GCC produced decent ASM in that case.

Note
Code is usable on all architectures, but the optimization only applies to AVR-GCC. Other compilers will see a standard bitwise shift.

Function Documentation

◆ lshift()

template<uint8_t b>
static uint32_t lshift ( uint32_t  a)
inlinestatic

Bitwise left shift - generic, unoptimized, case.

Template Parameters
bnumber of bits to shift by
Parameters
avalue to shift
Returns
uint32_t a<<b

◆ lshift< 10U >()

template<>
uint32_t lshift< 10U > ( uint32_t  a)

uint32_t bitwise left shift optimised for the specified shift distance

Note
The assembler was generated using clang 17.0.1 cross compiling: -O3 –target=avr -mmcu=atmega2560. See https://godbolt.org/z/71cPnMYqs
Parameters
avalue to shift
Returns
uint32_t a<<b

◆ lshift< 11U >()

template<>
uint32_t lshift< 11U > ( uint32_t  a)

uint32_t bitwise left shift optimised for the specified shift distance

Note
The assembler was generated using clang 17.0.1 cross compiling: -O3 –target=avr -mmcu=atmega2560. See https://godbolt.org/z/71cPnMYqs
Parameters
avalue to shift
Returns
uint32_t a<<b

◆ lshift< 12U >()

template<>
uint32_t lshift< 12U > ( uint32_t  a)

uint32_t bitwise left shift optimised for the specified shift distance

Note
The assembler was generated using clang 17.0.1 cross compiling: -O3 –target=avr -mmcu=atmega2560. See https://godbolt.org/z/71cPnMYqs
Parameters
avalue to shift
Returns
uint32_t a<<b

◆ lshift< 13U >()

template<>
uint32_t lshift< 13U > ( uint32_t  a)

uint32_t bitwise left shift optimised for the specified shift distance

Note
The assembler was generated using clang 17.0.1 cross compiling: -O3 –target=avr -mmcu=atmega2560. See https://godbolt.org/z/71cPnMYqs
Parameters
avalue to shift
Returns
uint32_t a<<b

◆ lshift< 14U >()

template<>
uint32_t lshift< 14U > ( uint32_t  a)

uint32_t bitwise left shift optimised for the specified shift distance

Note
The assembler was generated using clang 17.0.1 cross compiling: -O3 –target=avr -mmcu=atmega2560. See https://godbolt.org/z/71cPnMYqs
Parameters
avalue to shift
Returns
uint32_t a<<b

◆ lshift< 15U >()

template<>
uint32_t lshift< 15U > ( uint32_t  a)

uint32_t bitwise left shift optimised for the specified shift distance

Note
The assembler was generated using clang 17.0.1 cross compiling: -O3 –target=avr -mmcu=atmega2560. See https://godbolt.org/z/71cPnMYqs
Parameters
avalue to shift
Returns
uint32_t a<<b

◆ lshift< 4U >()

template<>
uint32_t lshift< 4U > ( uint32_t  a)

uint32_t bitwise left shift optimised for the specified shift distance

Note
The assembler was generated using clang 17.0.1 cross compiling: -O3 –target=avr -mmcu=atmega2560. See https://godbolt.org/z/71cPnMYqs
Parameters
avalue to shift
Returns
uint32_t a<<b

◆ lshift< 5U >()

template<>
uint32_t lshift< 5U > ( uint32_t  a)

uint32_t bitwise left shift optimised for the specified shift distance

Note
The assembler was generated using clang 17.0.1 cross compiling: -O3 –target=avr -mmcu=atmega2560. See https://godbolt.org/z/71cPnMYqs
Parameters
avalue to shift
Returns
uint32_t a<<b
Here is the caller graph for this function:

◆ lshift< 6U >()

template<>
uint32_t lshift< 6U > ( uint32_t  a)

uint32_t bitwise left shift optimised for the specified shift distance

Note
The assembler was generated using clang 17.0.1 cross compiling: -O3 –target=avr -mmcu=atmega2560. See https://godbolt.org/z/71cPnMYqs
Parameters
avalue to shift
Returns
uint32_t a<<b
Here is the caller graph for this function:

◆ lshift< 7U >()

template<>
uint32_t lshift< 7U > ( uint32_t  a)

uint32_t bitwise left shift optimised for the specified shift distance

Note
The assembler was generated using clang 17.0.1 cross compiling: -O3 –target=avr -mmcu=atmega2560. See https://godbolt.org/z/71cPnMYqs
Parameters
avalue to shift
Returns
uint32_t a<<b
Here is the caller graph for this function:

◆ lshift< 9U >()

template<>
uint32_t lshift< 9U > ( uint32_t  a)

uint32_t bitwise left shift optimised for the specified shift distance

Note
The assembler was generated using clang 17.0.1 cross compiling: -O3 –target=avr -mmcu=atmega2560. See https://godbolt.org/z/71cPnMYqs
Parameters
avalue to shift
Returns
uint32_t a<<b

◆ rshift()

template<uint8_t b>
static uint32_t rshift ( uint32_t  a)
inlinestatic

Bitwise right shift - generic, unoptimized, case.

Template Parameters
bnumber of bits to shift by
Parameters
avalue to shift
Returns
uint32_t
Here is the caller graph for this function:

◆ rshift< 10U >()

template<>
uint32_t rshift< 10U > ( uint32_t  a)

uint32_t bitwise right shift optimised for the specified shift distance

Note
The assembler was generated using clang 17.0.1 cross compiling: -O3 –target=avr -mmcu=atmega2560. See https://godbolt.org/z/71cPnMYqs
Parameters
avalue to shift
Returns
uint32_t a>>b

◆ rshift< 11U >()

template<>
uint32_t rshift< 11U > ( uint32_t  a)

uint32_t bitwise right shift optimised for the specified shift distance

Note
The assembler was generated using clang 17.0.1 cross compiling: -O3 –target=avr -mmcu=atmega2560. See https://godbolt.org/z/71cPnMYqs
Parameters
avalue to shift
Returns
uint32_t a>>b

◆ rshift< 12U >()

template<>
uint32_t rshift< 12U > ( uint32_t  a)

uint32_t bitwise right shift optimised for the specified shift distance

Note
The assembler was generated using clang 17.0.1 cross compiling: -O3 –target=avr -mmcu=atmega2560. See https://godbolt.org/z/71cPnMYqs
Parameters
avalue to shift
Returns
uint32_t a>>b

◆ rshift< 13U >()

template<>
uint32_t rshift< 13U > ( uint32_t  a)

uint32_t bitwise right shift optimised for the specified shift distance

Note
The assembler was generated using clang 17.0.1 cross compiling: -O3 –target=avr -mmcu=atmega2560. See https://godbolt.org/z/71cPnMYqs
Parameters
avalue to shift
Returns
uint32_t a>>b

◆ rshift< 14U >()

template<>
uint32_t rshift< 14U > ( uint32_t  a)

uint32_t bitwise right shift optimised for the specified shift distance

Note
The assembler was generated using clang 17.0.1 cross compiling: -O3 –target=avr -mmcu=atmega2560. See https://godbolt.org/z/71cPnMYqs
Parameters
avalue to shift
Returns
uint32_t a>>b

◆ rshift< 15U >()

template<>
uint32_t rshift< 15U > ( uint32_t  a)

uint32_t bitwise right shift optimised for the specified shift distance

Note
The assembler was generated using clang 17.0.1 cross compiling: -O3 –target=avr -mmcu=atmega2560. See https://godbolt.org/z/71cPnMYqs
Parameters
avalue to shift
Returns
uint32_t a>>b

◆ rshift< 3U >()

template<>
uint32_t rshift< 3U > ( uint32_t  a)

uint32_t bitwise right shift optimised for the specified shift distance

Note
The assembler was generated using clang 17.0.1 cross compiling: -O3 –target=avr -mmcu=atmega2560. See https://godbolt.org/z/71cPnMYqs
Parameters
avalue to shift
Returns
uint32_t a>>b

◆ rshift< 4U >()

template<>
uint32_t rshift< 4U > ( uint32_t  a)

uint32_t bitwise right shift optimised for the specified shift distance

Note
The assembler was generated using clang 17.0.1 cross compiling: -O3 –target=avr -mmcu=atmega2560. See https://godbolt.org/z/71cPnMYqs
Parameters
avalue to shift
Returns
uint32_t a>>b

◆ rshift< 5U >()

template<>
uint32_t rshift< 5U > ( uint32_t  a)

uint32_t bitwise right shift optimised for the specified shift distance

Note
The assembler was generated using clang 17.0.1 cross compiling: -O3 –target=avr -mmcu=atmega2560. See https://godbolt.org/z/71cPnMYqs
Parameters
avalue to shift
Returns
uint32_t a>>b
Here is the caller graph for this function:

◆ rshift< 6U >()

template<>
uint32_t rshift< 6U > ( uint32_t  a)

uint32_t bitwise right shift optimised for the specified shift distance

Note
The assembler was generated using clang 17.0.1 cross compiling: -O3 –target=avr -mmcu=atmega2560. See https://godbolt.org/z/71cPnMYqs
Parameters
avalue to shift
Returns
uint32_t a>>b
Here is the caller graph for this function:

◆ rshift< 7U >()

template<>
uint32_t rshift< 7U > ( uint32_t  a)

uint32_t bitwise right shift optimised for the specified shift distance

Note
The assembler was generated using clang 17.0.1 cross compiling: -O3 –target=avr -mmcu=atmega2560. See https://godbolt.org/z/71cPnMYqs
Parameters
avalue to shift
Returns
uint32_t a>>b
Here is the caller graph for this function:

◆ rshift< 9U >()

template<>
uint32_t rshift< 9U > ( uint32_t  a)

uint32_t bitwise right shift optimised for the specified shift distance

Note
The assembler was generated using clang 17.0.1 cross compiling: -O3 –target=avr -mmcu=atmega2560. See https://godbolt.org/z/71cPnMYqs
Parameters
avalue to shift
Returns
uint32_t a>>b

◆ rshift_round()

template<uint8_t b>
static uint32_t rshift_round ( uint32_t  a)
inlinestatic

Rounded arithmetic right shift.

Right shifting throws away bits. When use for fixed point division, this effectively rounds down (towards zero). To round-to-the-nearest-integer when right-shifting by S, just add in 2 power b−1 (which is the fixed-point equivalent of 0.5) first

Template Parameters
bnumber of bits to shift by
Parameters
avalue to shift
Returns
uint32_t
Here is the call graph for this function: