
Always Pair CALL and RETURN
59
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
W h e n t h e 1 2 e n t ry re t u r n a d d re s s s t a ck g e t s o u t o f
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
Summary of Contents for Athlon Processor x86
Page 1: ...AMD Athlon Processor x86 Code Optimization Guide TM...
Page 12: ...xii List of Figures AMD Athlon Processor x86 Code Optimization 22007E 0 November 1999...
Page 16: ...xvi Revision History AMD Athlon Processor x86 Code Optimization 22007E 0 November 1999...
Page 202: ...186 Page Attribute Table PAT AMD Athlon Processor x86 Code Optimization 22007E 0 November 1999...
Page 252: ...236 VectorPath Instructions AMD Athlon Processor x86 Code Optimization 22007E 0 November 1999...
Page 256: ...240 Index AMD Athlon Processor x86 Code Optimization 22007E 0 November 1999...