Sort Local Variables According To Base Type Size - AMD Athlon Processor x86 Optimization Manual

X86 code optimization
Table of Contents

Advertisement

AMD Athlon™ Processor x86 Code Optimization
Pad by Multiple of
Largest Base Type
Size

Sort Local Variables According to Base Type Size

28
Pad the structure to a multiple of the largest base type size of
any member. In this fashion, if the first member of a structure is
naturally 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.
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
Sort Local Variables According to Base Type Size
22007E/0—November 1999

Advertisement

Table of Contents
loading

Table of Contents