July 2002
Intermediate to advanced
320 pages
8h 15m
English
For a compiler to change division by a constant into a multiplication, it must compute the magic number M and the shift amount s, given a divisor d. The straightforward computation is to evaluate (6) or (18) for p = W, W + 1, … until it is satisfied. Then, m is calculated from (5) or (17). M is simply a reinterpretation of m as a signed integer, and s = p − W.
The scheme described below handles positive and negative d with only a little extra code, and it avoids doubleword arithmetic.
Recall that nc is given by
![]()
Hence |nc| can be computed from
The remainder must be evaluated using unsigned division, because ...
Read now
Unlock full access