AMD Athlon Processor x86 Optimization Manual page 90

X86 code optimization
Table of Contents

Advertisement

AMD Athlon™ Processor x86 Code Optimization
74
Example 1 (Avoid):
int a[MAXSIZE], b[MAXSIZE], c[MAXSIZE], i;
for (i=0; i < MAXSIZE; i++) {
c [i] = a[i] + b[i];
}
MOV
ECX, MAXSIZE
XOR
ESI, ESI
XOR
EDI, EDI
XOR
EBX, EBX
$add_loop:
MOV
EAX, [ESI + a] ;get element a
MOV
EDX, [EDI + b] ;get element b
ADD
EAX, EDX
MOV
[EBX + c], EAX ;write result to c
ADD
ESI, 4
ADD
EDI, 4
ADD
EBX, 4
DEC
ECX
JNZ
$add_loop
Example 2 (Preferred):
int a[MAXSIZE], b[MAXSIZE], c[MAXSIZE], i;
for (i=0; i < MAXSIZE; i++) {
c [i] = a[i] + b[i];
}
MOV ECX, MAXSIZE-1
$add_loop:
MOV EAX, [ECX*4 + a]
MOV EDX, [ECX*4 + b]
ADD EAX, EDX
MOV [ECX*4 + c], EAX
DEC ECX
JNS $add_loop
Note that the code in example 2 traverses the arrays in a
downward direction (i.e., from higher addresses to lower
addresses), whereas the original code in example 1 traverses
the arrays in an upward direction. Such a change in the
direction of the traversal is possible if each loop iteration is
completely independent of all other loop iterations, as is the
case here.
In code where the direction of the array traversal can't be
switched, it is still possible to minimize pointer arithmetic by
appropriately biasing base addresses and using an index
;initialize loop counter
;initialize offset into array a
;initialize offset into array b
;initialize offset into array c
;a[i] + b[i]
;increment offset into a
;increment offset into b
;increment offset into c
;decrement loop count
;until loop count 0
;initialize loop counter
;get element a
;get element b
;a[i] + b[i]
;write result to c
;decrement index
;until index negative
Minimize Pointer Arithmetic in Loops
22007E/0—November 1999

Advertisement

Table of Contents
loading

Table of Contents