I am implementing Fixed Point math on an embedded system using C.
For readability, I state the denominator as a power of 2:
#define Fixed_Point_Base 4096U
However, when I'm converting to and from the Fixed Point math, I need the shift quantity:
#define Fixed_Point_Bit_Position 12U
To make maintenance easier and the code more robust, I would like to have a #define
for the bit position (number of shifts) in terms of the Fixed_Point_Base number:
#define Fixed_Point_Bit_Position(x) {/*...*/}
The only method I know involves logarithms and division, and I really don't want to use logarithms or division in an embedded system:
bit count = ln(4096) / ln(2)
I'm looking for a preprocessor macro or compile time solution that returns the bit position of a power of 2.
My web search has returned examples in code, but not as compile-time / preprocessor solution.
FYI, I'm using IAR Embedded Workbench with an ARM7TDMI processor.
Edit 1: I'm using MISRA C 2004 guidelines with Parasoft Static Analysis and Coverity Static Analysis tools. Answers must pass these constraints.