data alignment (was Re: RISC data alignment, in comp.arch)

Richard A. O'Keefe ok at quintus.UUCP
Thu Feb 18 11:58:20 AEST 1988


In article <2309 at umd5.umd.edu>, chris at trantor.umd.edu (Chris Torek) writes:
> >In article <28200092 at ccvaxa> aglew at ccvaxa.UUCP writes:
> >>I have often wanted an optimizing compiler that could [rearrange]
> >>struct { long a; int b; long c; char d; }
> >>... and make it into AAAACCCCBBDx.
> Yes.  In fact, I have the perfect name for it, stolen from Craig
> Stanfill's thesis hacking days here at U of MD. ... Hence the
> proper name for an unordered aggregate type: a `bag'.
> 
Please don't do that.  "bag" is a very common name for the "multiset"
data type.  (That is, a homogeneous collection like a set, except that
items can be present more than once.)

There is a trivial solution, and a non-trivial solution.
If you put
	all doubles, arrays of doubles, or structs with double elements first,
then	all floats, arrays of floats, or structs with float elements,
then	all pointers, arrays of pointers, or structs with pointer elements,
then	all longs, arrays of longs, or structs with long elements,
then	all ints, arrays of ints, or structs with int elements,
then	all shorts, arrays of shorts, or structs with short elements,
then	all chars, arrays of chars, or structs with char elements
the resulting 'struct' will be reasonably packed on a wide range of machines.
That is the trivial solution.

The non-trivial solution is to write your structure declaration in two
steps.  Suppose you have a collection of typedefs and macros in a file
header.h, and a collection of struct declarations you would like to
optimise.  Now write another program which does includes header.h and
uses the sizeof() information to determinate an appropriate order for
each struct, and have that program write out a structs.h file with the
optimised declarations.



More information about the Comp.lang.c mailing list