AMD x86 Use Array Style Instead of Pointer Style Code, Example Avoid, Use unsigned types for

Models: x86

1 256
Download 256 pages 58.62 Kb
Page 31
Image 31

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 memory location, while *p and *q could . It is highly recommended to use the array style, as significant performance advantages can be achieved with most compilers.

Use Array Style Instead of Pointer Style Code

15

Page 31
Image 31
AMD x86 manual Use Array Style Instead of Pointer Style Code, Example Avoid, Use unsigned types for, Use signed types for