brestel.dean.001

Reduce flash usage by replacing POSITION_VAL when compiling with GNU

Discussion created by brestel.dean.001 on Nov 22, 2017
Latest reply on Feb 19, 2018 by Andrew Neil

The stm32fXxx.h file has a macro for POSITION_VAL that determines which index the lowest bit is in.  This is accomplished by reversing the bit order and counting the leading zeros.  Because it is using asm instructions, this requires that the value is loaded into memory for the instructions to act upon.

 

Most of the usages of POSITION_VAL act upon constant values where the result could be determined at compile time.  GNU C has __builtin_ctz which accomplishes the same thing but can deduce the value at compile time if appropriate.

 

For me, modifying the macro in stm32f1xx.h from

 

# define POSITION_VAL(VAL) (__CLZ(__RBIT(VAL)))

 

to

 

#if __GNUC__
# define POSITION_VAL(VAL) (__builtin_ctz(VAL))
#else
# define POSITION_VAL(VAL) (__CLZ(__RBIT(VAL)))
#endif

 

reduced my code size from 105KB to 95KB.

Outcomes