Use Array Style Instead of Pointer Style Code
15
22007E/0—November 1999
AMD Athlon™ Processor x86 Code Optimization
Example (Avoid):
int i; ====>
MOV EAX, i
CDQ
i = i / 4;
AND EDX, 3
ADD EAX, EDX
SAR EAX, 2
MOV i, EAX
Example (Preferred):
unsigned int i; ====>
SHR i, 2
i = i / 4;
In summary:
Use unsigned types for:
■
Division and remainders
■
Loop counters
■
Array indexing
Use signed types for:
■
Integer-to-float conversion
Use Array Style Instead of Pointer Style Code
The use of pointers in C makes work difficult for the optimizers
in C compilers. Without detailed and aggressive pointer
analysis, the compiler has to assume that writes through a
pointer can write to any place in memory. This includes storage
allocated to other variables, creating the issue of aliasing, i.e.,
the same block of memory is accessible in more than one way.
In order to help the optimizer of the C compiler in its analysis,
avoid the use of pointers where possible. One example where
this is trivially possible is in the access of data organized as
arrays. C allows the use of either the array operator [] or
pointers to access the array. Using array-style code makes the
task of the optimizer easier by reducing possible aliasing.
For example, x[0] and x[2] can not possibly refer to the same
m e m o ry l o c a t i o n , w h i l e * p a n d * q c o u l d . I t i s h i g h ly
recommended to use the array style, as significant performance
advantages can be achieved with most compilers.
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...