[comp.arch] Byte .vs. Word Alignment

jrb@petro.UUCP (Jon Boede) (04/15/87)

This is probably the most trivial of questions, but I've always wondered if
wordwise alignment affected efficiency on the 68000.

For example, assuming that a structure like:

	struct {
		int an_int;
		char a_char;
	} a_struct;

starts on a word boundry, would a structure placing the /char/ first and the
/int/ therefore a 1/4 word off be less, can we say, "efficient" in terms of
the manipulation needed to get to /an_int/??  Or doesn't the C compiler always
start structures on words boundries, making alignment consideration by the
programmer meaningless?

	struct {
		char a_char;
		int an_int;
	} a_struct;	/* is this less "efficient"? */

Also, whilst I'm on the subject, how much extra effort is required of the
68000 to get to specific bits than to, say, an int?  I often use structures
of bit flags to save space in files.  I can easily compute the space savings,
but an accurate guess as to the time savings is a little more elusive.

Thanks!
-- 
Jon Boede				...!{ihnp4,akgua,gatech}!petro!jrb
512/599-1847				2555 N.E. Loop 410, #1403, 78217
	"People who are incapable of making decisions are
	 the ones who hit those barrels at freeway exits."

dlnash@ut-ngp.UUCP (Donald L. Nash) (04/16/87)

In article <244@petro.UUCP>, jrb@petro.UUCP (Jon Boede) writes:
> This is probably the most trivial of questions, but I've always wondered if
> wordwise alignment affected efficiency on the 68000.
> 
> For example, assuming that a structure like:
> 
> 	struct {
> 		int an_int;
> 		char a_char;
> 	} a_struct;
> 
> starts on a word boundry, would a structure placing the /char/ first and the
> /int/ therefore a 1/4 word off be less, can we say, "efficient" in terms of
> the manipulation needed to get to /an_int/??  Or doesn't the C compiler always
> start structures on words boundries, making alignment consideration by the
> programmer meaningless?
> 
> 	struct {
> 		char a_char;
> 		int an_int;
> 	} a_struct;	/* is this less "efficient"? */

    C compilers reserve the right to pad a structure to make it fit alignment
restrictions.  There might be some unused space between a_char and an_int
to make an_int line up on a word boundary.  I know that 4.3 BSD pcc on a VAX
does this, because I have tested it on similar constructs, ditto 4.2 BSD pcc.
If you are using a 680x0 C compiler derived from either of these (like on 4.2
BSD on a Sun), it probably pads as well.

> Also, whilst I'm on the subject, how much extra effort is required of the
> 68000 to get to specific bits than to, say, an int?  I often use structures
> of bit flags to save space in files.  I can easily compute the space savings,
> but an accurate guess as to the time savings is a little more elusive.

    The 68020 has bit field instructions for dealing with this sort of thing.
How efficient your code is when using bit fields in C depends on whether your
compiler is smart enough to use these instructions.  I don't have a 68000
manual, only a 68020 manual, so I can't tell you for certain if the 68000 has
these instructions, although I assume it does.

				Don Nash

UUCP:    ...!{ihnp4, allegra, seismo!ut-sally}!ut-ngp!dlnash
ARPA:    dlnash@ngp.UTEXAS.EDU
BITNET:	 CCEU001@UTADNX, DLNASH@UTADNX
TEXNET:  UTADNX::CCEU001, UTADNX::DLNASH

UUU       UUU
 U         U                The University of Texas at Austin
 U     TTTTUTTTTTTTTT              Computation Center
 U     T   U TT     T
  U       U  TT            "The world is basically non-linear."
   UUUUUUU   TT
             TT 
            TTTT

marcus@illusion.UUCP (04/22/87)

In article <244@petro.UUCP> jrb@petro.UUCP (Jon Boede) writes:
>For example, assuming that a structure like:
>
>	struct {
>		int an_int;
>		char a_char;
>	} a_struct;
>
>starts on a word boundry, would a structure placing the /char/ first and the
>/int/ therefore a 1/4 word off be less, can we say, "efficient" in terms of
>the manipulation needed to get to /an_int/??  Or doesn't the C compiler always
>start structures on words boundries, making alignment consideration by the
>programmer meaningless?
>
>	struct {
>		char a_char;
>		int an_int;
>	} a_struct;	/* is this less "efficient"? */

Most compilers would probably pad between a_char and an_int in the second
example so that an_int would be at a word boundary.  It is worth noting that
even in the first example, most compilers will pad 3 bytes to the end of
the structure so that in either case the structure will have a size of 8
bytes (68000 => 32 bit ints).  This is done so that if you had an array
of a_structs, each one would begin at an aligned address.  This seems odd
at first, but after sufficient consideration starts to make sense.

As far as efficiency of referencing an_int is concerned, the first case is
probably going to be more efficient, but not because of alignment.  The
first element in a structure can usually be accessed easier than any other
elements because its offset in the structure is 0 and the compiler can
probably generate simplified addressing modes to get to it.

Marcus Hall
..!{ihnp4,mcdchg}!illusion!marcus
	The illusion is complete, it is reality, the reality
	is an illusion, and the ambiguity is the only truth.