Integer Division by a Constant
The integer divide instructions (
SDIV
and
UDIV
) on the ARM Cortex-M4 are
more than 10 times slower than most other instructions. However, when the
divisor is a constant, the divide instruction can be replaced by a faster
sequence of other instructions.
A positive or unsigned integer may be divided by 2
N by a logical
right shift of N bits. Similarly, a negative signed integer can be divided
by 2
N by first adding 2
N-1 and then shifting right
arithmetically by N bits.
Division by other constants is a bit more complicated, but can be
accomplished with a sequence that is still faster than the divide instruction.
(Click anywhere to remove this message.)