AMD Athlon Processor x86 Optimization Manual page 96

X86 code optimization
Table of Contents

Advertisement

AMD Athlon™ Processor x86 Code Optimization
Derivation for a, m, s
Signed Division By 2
n
Signed Division By 2
Signed Division By –2
Signed Division By
n
–(2
)
Remainder of Signed
Integer 2 or –2
80
;algorithm 1
MOV
EAX, m
MOV
EDX, dividend
MOV
ECX, EDX
IMUL
EDX
ADD
EDX, ECX
SHR
ECX, 31
SAR
EDX, s
ADD
EDX, ECX
The derivation for the algorithm (a), multiplier (m), and shift
count (s), is found in the section "Signed Derivation for
Algorithm, Multiplier, and Shift Factor" on page 95.
;IN: EAX = dividend
;OUT:EAX = quotient
CMP
EAX, 800000000h
SBB
EAX, –1
SAR
EAX, 1
;IN:EAX = dividend
;OUT:EAX = quotient
CDQ
AND
EDX, (2^n–1)
ADD
EAX, EDX
SAR
EAX, (n)
;IN:EAX = dividend
;OUT:EAX = quotient
CMP
EAX, 800000000h
SBB
EAX, –1
SAR
EAX, 1
NEG
EAX
;IN:EAX = dividend
;OUT:EAX = quotient
CDQ
AND
EDX, (2^n–1)
ADD
EAX, EDX
SAR
EAX, (n)
NEG
EAX
;IN:EAX = dividend
;OUT:EAX = remainder
CDQ
AND
EDX, 1
XOR
EAX, EDX
SUB
EAX, EDX
MOV
[remainder], EAX
;quotient in EDX
;CY = 1, if dividend >=0
;Increment dividend if it is < 0
;Perform a right shift
;Sign extend into EDX
;Mask correction (use divisor –1)
;Apply correction if necessary
;Perform right shift by
; log2 (divisor)
;CY = 1, if dividend >= 0
;Increment dividend if it is < 0
;Perform right shift
;Use (x/–2) == –(x/2)
;Sign extend into EDX
;Mask correction (–divisor –1)
;Apply correction if necessary
;Right shift by log2(–divisor)
;Use (x/–(2^n)) == (–(x/2^n))
;Sign extend into EDX
;Compute remainder
;Negate remainder if
;Dividend was < 0
Replace Divides with Multiplies
22007E/0—November 1999

Advertisement

Table of Contents
loading

Table of Contents