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 2N by a logical right shift of N bits. Similarly, a negative signed integer can be divided by 2N by first adding 2N-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.)

Integer Divide by Constant
Revised: Thursday, 23-Jun-2022 20:38:12 PDT
Operand Data Type: uint32_t int32_t
Operands Decimal Hex ARM Assembly
A (32-bit dividend):
B (32-bit constant divisor):
User
Entry
Required
for Quotient
Final 32-Bit
Quotient
Reference: Warren, Hacker's Delight, 2nd ed., Addison Wesley, 2012