Unsigned Division By Multiplication Of Constant - AMD Athlon Processor x86 Optimization Manual

X86 code optimization
Table of Contents

Advertisement

AMD Athlon™ Processor x86 Code Optimization
Signed Division
Utility
Unsigned Division
Utility

Unsigned Division by Multiplication of Constant

Algorithm: Divisors
31
1 <= d < 2
, Odd d
Derivation of a, m, s
Algorithm: Divisors
31
32
2
<= d < 2
78
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.
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.
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
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.
31
For divisors 2
<= d < 2
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.
32
, the possible quotient values are
Replace Divides with Multiplies
22007E/0—November 1999

Advertisement

Table of Contents
loading

Table of Contents