Un-alignment in structures

John Chambers jc at mit-athena.UUCP
Tue Mar 19 00:40:15 AEST 1985


Once again we hahear a justification of C's alignment on the grounds
that some machines can't load and store unaligned quantities.  THIS
IS NOT TRUE!  Every machine I've ever used, and I suspect all that
have ever been built, can do this quite easily.

The method is simple.  For unaligned loads, you write code that loads
the containing words into two registers, use AND instructions to zero
out the unwanted portions, SHIFT instructions to align the wanted bits
properly, and an OR instruction to combine them.  Every machine I've
ever seen has LOAD, AND, SHIFT and OR instructions.

But, but, but... This is inefficient, I hear people muttering.  Well,
sure it is, but it's not any more efficient when I have to code it 
myself because the @#$#%@# compiler refuses to do it for me.  If the
data is unaligned (as often happens when you are getting it from another
machine), someone has to write the inefficient code to extract the data.
It is either me or the compiler.  This is not a difficult job for a
compiler to do.  I know, I've written several.  

Compilers were invented back in the 50's to save people time by making
the machine do the drudge work in generating machine language.  Handling
misaligned data is an especially dreary piece of drudge work that is both 
hard for me and easy for the machine.  Why can't compilers do their job 
(making life easy for programmers) right, rather than making feeble excuses 
that don't stand up to the slightest investigation?

I'm getting sort of tired of writing the same pieces of stereotyped code 
over and over and over and over and over, when I know very well how easy 
it would be for the compiler to do it for me.  (Well, sure, I could dig 
in and modify the compiler.  I did this once.  Then I moved to another 
machine, and its C compiler needed the change, too, and...)

Grumble, grumble, grumble....
-- 

			John Chambers [...!decvax!mit-athena]

If you're not part of the solution, then you're part of the precipitate.



More information about the Comp.lang.c mailing list