I came across code similar to this in the ARM CMSIS-NN library:
int32_t val, shift;
// code that initializes val and shift
int32_t result = val * (1 << shift);
which aims to multiply val
by some power of two value x
, with shift
being the exponent, i.e. x=pow(2,shift)
. Why are they not simply shifting? Like so
int32_t result = val << shift;
Is there something special that I am missing? Can the compiler optimize the former operation in some special way?
EDIT: What confuses me is that they are using "simple shifts" throughout the code. Additionally, the code should be highly optimized. But I guess modern compilers will figure out by themselves, that shifting is the way to go (instead of multiplying)?
CodePudding user response:
It is always sign correct and forces the use of the proper FPU instructions and works with any type of data.