User's Manual

74 Minimize Pointer Arithmetic in Loops
AMD Athlon Processor x86 Code Optimization
22007E/0November 1999
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 ;initialize loop counter
XOR ESI, ESI ;initialize offset into array a
XOR EDI, EDI ;initialize offset into array b
XOR EBX, EBX ;initialize offset into array c
$add_loop:
MOV EAX, [ESI + a] ;get element a
MOV EDX, [EDI + b] ;get element b
ADD EAX, EDX ;a[i] + b[i]
MOV [EBX + c], EAX ;write result to c
ADD ESI, 4 ;increment offset into a
ADD EDI, 4 ;increment offset into b
ADD EBX, 4 ;increment offset into c
DEC ECX ;decrement loop count
JNZ $add_loop ;until loop count 0
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 ;initialize loop counter
$add_loop:
MOV EAX, [ECX*4 + a] ;get element a
MOV EDX, [ECX*4 + b] ;get element b
ADD EAX, EDX ;a[i] + b[i]
MOV [ECX*4 + c], EAX ;write result to c
DEC ECX ;decrement index
JNS $add_loop ;until index negative
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 cant be
switched, it is still possible to minimize pointer arithmetic by
appropriately biasing base addresses and using an index