22007E/0 — November 1999

AMD Athlon™ Processor x86 Code Optimization

 

Example 6 — Increment Ring Buffer Offset:

 

//C Code

 

char buf[BUFSIZE];

 

int a;

if (a < (BUFSIZE-1)) { a++;

}else { a = 0;

 

}

 

 

 

;-------------

 

 

 

;Assembly Code

 

 

 

MOV

EAX, [a]

; old

offset

 

CMP

EAX, (BUFSIZE-1)

; a <

(BUFSIZE-1)

? CF : NC

INC

EAX

; a++

 

 

SBB

EDX, EDX

; a <

(BUFSIZE-1)

? 0xffffffff :0

AND

EAX, EDX

; a <

(BUFSIZE-1)

? a++ : 0

MOV

[a], EAX

; store new offset

Example 7 — Integer Signum Function:

//C Code int a, s;

if (!a) {

s = 0;

}else if (a < 0) { s = -1;

}else {

s = 1;

}

 

 

 

;-------------

 

 

;Assembly Code

 

 

MOV

EAX, [a]

;load a

 

CDQ

 

;t = a < 0 ? 0xffffffff : 0

CMP

EDX, EAX

;a > 0

? CF : NC

ADC

EDX, 0

;a > 0

? t+1 : t

MOV

[s], EDX

;signum(x)

Always Pair CALL and RETURN

Wh en t h e 12 e n t ry re tur n a ddres s s t ack g et s out of synchronization, the latency of returns increase. The return address stack becomes out of sync when:

calls and returns do not match

the depth of the return stack is exceeded because of too many levels of nested functions calls

Always Pair CALL and RETURN

59

Page 75
Image 75
AMD x86 manual Always Pair Call and Return, Example 6 Increment Ring Buffer Offset, Example 7 Integer Signum Function