Software Optimization Guide for AMD64 Processors

25112 Rev. 3.06 September 2005

7.6Pointer Arithmetic in Loops

Optimization

Minimize pointer arithmetic in loops, especially if the loop bodies are small. Take advantage of scaled-index addressing modes to utilize the loop counter as an index into memory arrays.

Application

This optimization applies to:

32-bit software

64-bit software

Rationale

In small loops, pointer arithmetic causes significant overhead. Using scaled-index addressing modes has no negative impact on execution speed, but the reduced number of instructions preserves decode bandwidth.

Example

Consider the following C code, which adds the elements of two arrays and stores them in a third array:

int a[MAXSIZE], b[MAXSIZE], c[MAXSIZE], i;

for (i = 0; i < MAXSIZE; i++) { c[i] = a[i] + b[i];

}

154

Scheduling Optimizations

Chapter 7

Page 170
Image 170
AMD 250 manual Pointer Arithmetic in Loops, 154