AMD Athlon Processor x86 Optimization Manual page 103

X86 code optimization
Table of Contents

Advertisement

22007E/0—November 1999
Efficient 64-Bit Integer Arithmetic
Example 4 (Left shift):
;shift operand in EDX:EAX left, shift count in ECX (count
;
applied modulo 64)
SHLD
EDX, EAX, CL
SHL
EAX, CL
TEST
ECX, 32
JZ
$lshift_done
MOV
EDX, EAX
XOR
EAX, EAX
$lshift_done:
Example 5 (Right shift):
SHRD
EAX, EDX, CL
SHR
EDX, CL
TEST
ECX, 32
JZ
$rshift_done
MOV
EAX, EDX
XOR
EDX, EDX
$rshift_done:
Example 6 (Multiplication):
;_llmul computes the low-order half of the product of its
;
arguments, two 64-bit integers
;
;INPUT:
[ESP+8]:[ESP+4]
;
[ESP+16]:[ESP+12] multiplier
;
;OUTPUT: EDX:EAX
;
;DESTROYS:
EAX,ECX,EDX,EFlags
_llmul PROC
MOV
EDX, [ESP+8]
MOV
ECX, [ESP+16]
OR
EDX, ECX
MOV
EDX, [ESP+12]
MOV
EAX, [ESP+4]
JNZ
$twomul
MUL
EDX
RET
$twomul:
IMUL
EDX, [ESP+8] ;p3_lo = multiplicand_hi*multiplier_lo
IMUL
ECX, EAX
ADD
ECX, EDX
MUL
DWORD PTR [ESP+12] ;p1=multiplicand_lo*multiplier_lo
ADD
EDX, ECX
RET
_llmul ENDP
AMD Athlon™ Processor x86 Code Optimization
;first apply shift count
; mod 32 to EDX:EAX
;need to shift by another 32?
;no, done
;left shift EDX:EAX
; by 32 bits
;first apply shift count
; mod 32 to EDX:EAX
;need to shift by another 32?
;no, done
;left shift EDX:EAX
; by 32 bits
multiplicand
(multiplicand * multiplier) % 2^64
;multiplicand_hi
;multiplier_hi
;one operand >= 2^32?
;multiplier_lo
;multiplicand_lo
;yes, need two multiplies
;multiplicand_lo * multiplier_lo
;done, return to caller
;p2_lo = multiplier_hi*multiplicand_lo
; p2_lo + p3_lo
;p1+p2lo+p3_lo = result in EDX:EAX
;done, return to caller
87

Advertisement

Table of Contents
loading

Table of Contents