AMD Athlon™ Processor x86 Code Optimization

22007E/0 — November 1999

Pad by Multiple of Pad the structure to a multiple of the largest base type size of

Largest Base Type any member. In this fashion, if the first member of a structure is

Sizenaturally aligned, all other members are naturally aligned as well. The padding of the structure to a multiple of the largest based type size allows, for example, arrays of structures to be perfectly aligned.

The following example demonstrates the reordering of structure member declarations:

Original ordering (Avoid):

struct {

char a[5];

long k; double x;

} baz;

New ordering, with padding (Preferred):

struct

{

 

 

double

x;

 

long

k;

 

char

a[5];

 

char

pad[7];

} baz;

 

 

See “C Language Structure Component Considerations” on page 55 for a different perspective.

Sort Local Variables According to Base Type Size

When a compiler allocates local variables in the same order in which they are declared in the source code, it can be helpful to declare local variables in such a manner that variables with a larger base type size are declared ahead of the variables with smaller base type size. Then, if the first variable is allocated so that it is naturally aligned, all other variables are allocated contiguously in the order they are declared, and are naturally aligned without any padding.

Some compilers do not allocate variables in the order they are declared. In these cases, the compiler should automatically allocate variables in such a manner as to make them naturally aligned with the minimum amount of padding. In addition, some compilers do not guarantee that the stack is aligned suitably for the largest base type (that is, they do not guarantee

28

Sort Local Variables According to Base Type Size

Page 44
Image 44
AMD x86 Sort Local Variables According to Base Type Size, Original ordering Avoid, New ordering, with padding Preferred