User's Manual

78 Replace Divides with Multiplies
AMD Athlon Processor x86 Code Optimization
22007E/0November 1999
Signed Division
Utility
In the opt_utilities directory of the AMD documentation
CDROM, run sdiv.exe in a DOS window to find the fastest code
for signed division by a constant. The utility displays the code
after the user enters a signed constant divisor. Type sdiv >
example.out to output the code to a file.
Unsigned Division
Utility
In the opt_utilities directory of the AMD documentation
CDROM, run udiv.exe in a DOS window to find the fastest code
for unsigned division by a constant. The utility displays the code
after the user enters an unsigned constant divisor. Type udiv >
example.out to output the code to a file.
Unsigned Division by Multiplication of Constant
Algorithm: Divisors
1 <= d < 2
31
, Odd d
The following code shows an unsigned division using a constant
value multiplier.
;In: d = divisor, 1 <= d < 2^31, odd d
;Out: a = algorithm
; m = multiplier
; s = shift factor
;algorithm 0
MOV EDX, dividend
MOV EAX, m
MUL EDX
SHR EDX, s ;EDX=quotient
;algorithm 1
MOV EDX, dividend
MOV EAX, m
MUL EDX
ADD EAX, m
ADC EDX, 0
SHR EDX, s ;EDX=quotient
Derivation of a, m, s The derivation for the algorithm (a), multiplier (m), and shift
count (s), is found in the section Unsigned Derivation for
Algorithm, Multiplier, and Shift Factor on page 93.
Algorithm: Divisors
2
31
<= d < 2
32
For divisors 2
31
<= d < 2
32
, the possible quotient values are
either 0 or 1. This makes it easy to establish the quotient by
simple comparison of the dividend and divisor. In cases where
the dividend needs to be preserved, example 1 below is
recommended.