[net.lang.c] The importance of alignment on a VAX

brooks@lll-crg.ARPA (Eugene D. Brooks III) (03/17/85)

For those who think that aligment is not important on a VAX I suggest your
actually trying a code without alignment before makeing any claims that it
is not important.  The very simple example

	for(i = 0; i < NTIMES; i += 1) {
		fa = fb + fc;
		da = db + dc;
		ia = ib + ic;
	}

Where the variables f, d, i are floats, doubles and ints in static data
has a speed reduction of a factor of two if you take out the .align pseudos
and padd in some bytes to break alignment.  Alignmet if very important
as far as performance is concerned.

For those who want to read structures from files using a single read
the best course to follow is write the stuff into the file using the
same structure.  The padding for alignment is automatically taken care of.
If the padding cant be tolerated then the most portable course of action
is to read each data item into the structure seperately.  This will save you
a lot of headaches when moving your code from one machine to another.

ron@brl-tgr.ARPA (Ron Natalie <ron>) (03/19/85)

> For those who think that aligment is not important on a VAX I suggest your
> actually trying a code without alignment before makeing any claims that it
> is not important.  The very simple example
> 
You're twisting what I was saying.  What I was complaining about was that
there was no feature for defeating it even on machines like the VAX where
the hardware will support it easily.  It would be nice to read structures
from other machines (like PDP-11's) sometime.

-Ron

ndiamond@watdaisy.UUCP (Norman Diamond) (03/20/85)

> For those who want to read structures from files using a single read
> the best course to follow is write the stuff into the file using the
> same structure.  The padding for alignment is automatically taken care of.
> If the padding cant be tolerated then the most portable course of action
> is to read each data item into the structure seperately.  This will save you
> a lot of headaches when moving your code from one machine to another.

So if your data files are supposed to be accessible by programs in any
other language, or by programs in the same language compiled by a different
compiler -- on the same machine and/or on other machines -- then you should
read each data item separately.

If you're writing a network interface, don't dare read N bytes of header
into a structure.  Read each byte separately.  Hope your code can respond
in time.

If you have a bunch of old files and a new compiler, don't just copy your
files -- convert them and convert the padding.  How can one source program
adapt a structure to different padding for the output file than for the
input file?  By coding in assembly?

Yes, padding really has some disadvantages.  There should be an OPTION to
disable it.

-- 

   Norman Diamond

UUCP:  {decvax|utzoo|ihnp4|allegra}!watmath!watdaisy!ndiamond
CSNET: ndiamond%watdaisy@waterloo.csnet
ARPA:  ndiamond%watdaisy%waterloo.csnet@csnet-relay.arpa

"Opinions are those of the keyboard, and do not reflect on me or higher-ups."

mwm@ucbtopaz.CC.Berkeley.ARPA (03/21/85)

In article <9339@brl-tgr.ARPA> ron@brl-tgr.ARPA (Ron Natalie <ron>) writes:
>> For those who think that aligment is not important on a VAX I suggest your
>You're twisting what I was saying.  What I was complaining about was that
>there was no feature for defeating it even on machines like the VAX where
>the hardware will support it easily.  It would be nice to read structures
>from other machines (like PDP-11's) sometime.

Ron is right, C needs a "packed" struct type. I.e. -

static packed struct gezornin {
	<objects which will not have alignment> ;
	} ;

There: by careful choice of wording, I've managed to insure that such a
feature will *never* appear in C :-).

	<mike

alexis@reed.UUCP (Alexis Dimitriadis) (03/24/85)

In article <9339@brl-tgr.ARPA> ron@brl-tgr.ARPA (Ron Natalie <ron>) writes:

> What I was complaining about was that
>there was no feature for defeating it even on machines like the VAX where
>the hardware will support it easily.  It would be nice to read structures
>from other machines (like PDP-11's) sometime.
>
>-Ron

  So C needs something like the Pascal keyword `packed'. (hee hee)


-- 
_______________________________________________
	  alexis @ reed
	...ihnp4!{harvard|tektronix}!reed
	...decvax!tektronix!reed
	...teneron!reed

ndiamond@watdaisy.UUCP (Norman Diamond) (03/26/85)

*** ALIGN THIS MESS WITH YOUR REPLACEMENT ***

-- 

   Norman Diamond

UUCP:  {decvax|utzoo|ihnp4|allegra}!watmath!watdaisy!ndiamond
CSNET: ndiamond%watdaisy@waterloo.csnet
ARPA:  ndiamond%watdaisy%waterloo.csnet@csnet-relay.arpa

"Opinions are those of the keyboard, and do not reflect on me or higher-ups."

brooks@lll-crg.ARPA (Eugene D. Brooks III) (03/27/85)

> >the hardware will support it easily.  It would be nice to read structures
> >from other machines (like PDP-11's) sometime.
>   So C needs something like the Pascal keyword `packed'. (hee hee)

A packed structure type would only give portability between a few machines.
Although I would never want a machine with a 36 bit word they do exist and
some of them have C compilers.

The portability problem is best handled with a subroutine or macro to read
the structure.  Using a 'packed' structure introduces a speed penalty when
the structure is used elsewhere and a hole host of other troubles just for
the sake of giving limited I/O portability.  I can't see any sense in this.
This is against the philosophy of the language.  The language has no I/O at all.
Lets keep the I/O in routines where it belongs.  If you want to read a structure
from a pdp11 write a routine or a macro.

It appears to me that packed structures are even jumping the gun.  Does the C
language even define the order in which the elements of a structure are placed
in memory? The chapter on structures in K&R only states that the structure is a
grouping of data items and I failed to find any statements about ordering of the
elements.  Am I mistaken about this?