AMD Athlon™ Processor x86 Code Optimization

22007E/0 — November 1999

AMD Athlon™ Processor Specific Code

Example 1 — Signed integer ABS function (X = labs(X)):

MOV

ECX, [X]

;load value

 

MOV

EBX, ECX

;save

value

 

NEG

ECX

;–value

 

CMOVS

ECX, EBX

;if –value

is negative, select value

MOV

[X], ECX

;save

labs

result

Example 2 — Unsigned integer min function (z = x < y ? x : y):

MOV

EAX, [X]

;load X

value

MOV

EBX, [Y]

;load

Y

value

CMP

EAX, EBX

;EBX<=EAX

? CF=0 : CF=1

CMOVNC

EAX,

EBX

;EAX=(EBX<=EAX) ? EBX:EAX

MOV

[Z],

EAX

;save

min

(X,Y)

Blended AMD-K6®and AMD Athlon™ Processor Code

Example 3 — Signed integer ABS function (X = labs(X)):

MOV

ECX, [X]

;load value

MOV

EBX, ECX

;save value

SAR

ECX, 31

;x < 0

? 0xffffffff : 0

XOR

EBX, ECX

;x < 0

? ~x : x

SUB

EBX, ECX

;x < 0

? (~x)+1 : x

MOV

[X], EBX

;x < 0

? -x : x

Example 4 — Unsigned integer min function (z = x < y ? x : y):

MOV

EAX, [x]

;load x

MOV

EBX, [y]

;load y

SUB

EAX, EBX

;x < y ? CF : NC ; x - y

SBB

ECX, ECX

;x < y ? 0xffffffff : 0

AND

ECX, EAX

;x < y ? x - y : 0

ADD

ECX, EBX

;x < y ? x - y + y : y

MOV

[z], ECX

;x < y ? x : y

Example 5 — Hexadecimal to ASCII conversion (y=x < 10 ? x + 0x30: x + 0x41):

MOV

AL, [X]

;load

X value

 

 

 

CMP

AL,

10

;if x

is

less

than 10, set carry flag

SBB

AL,

69h

;0..9

–>

96h,

Ah..Fh

–> A1h...A6h

 

DAS

 

 

;0..9: subtract 66h,

Ah..Fh: Sub.

60h

MOV

[Y],AL

;save

conversion in y

 

 

58

Avoid Branches Dependent on Random Data

Page 74
Image 74
AMD x86 manual AMD Athlon Processor Specific Code, Blended AMD-K6and AMD Athlon Processor Code