Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/5/84; site tellab2.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!ihnp4!tellab1!tellab2!thoth From: thoth@tellab2.UUCP (Marcus Hall) Newsgroups: net.lang.c Subject: Re: Un-alignment in structures Message-ID: <230@tellab2.UUCP> Date: Wed, 20-Mar-85 22:15:09 EST Article-I.D.: tellab2.230 Posted: Wed Mar 20 22:15:09 1985 Date-Received: Fri, 22-Mar-85 02:43:19 EST References: <9239@brl-tgr.ARPA> <120@mit-athena.UUCP> Reply-To: thoth@tellab2.UUCP (Marcus Hall) Organization: Tellabs, Inc., Lisle, IL Lines: 45 In article <120@mit-athena.UUCP> jc@mit-athena.UUCP (John Chambers) writes: >If the data is unaligned (...) 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. > >Handling >misaligned data is an especially dreary piece of drudge work that is both >hard for me and easy for the machine. > OK, how does the compiler know at compile time whether a pointer will be pointing to an aligned structure or not? It would have to assume that is never is aligned, which would produce needlessly innefficient code 95% of the time. Consider x(p) int *p; { return (*p); } On the Z8000 which cannot do un-aligned word addressing, the following code would be produced: Alignment assumed: Alignment of stack No alignment assumed: only assumed: ld r5,2(sp) ld r5,2(sp) ldb rh5,2(sp) ld r4,@r5 ldb rh4,@r5 ldb rl5,3(sp) ret ldb rl4,1(r5) ldb rh4,@r5 ret ldb rl4,1(r5) ret Now, it isn't all that terrible to assume alignment, is it? Actually, as has already been stated, the biggest problem to portability is byte ordering, so if you have to worry about alignment problems you must also worry about byte ordering, so you've got to put the bytes together yourself anyhow. Of course, you could load the int into a register, then asm() in a swab instruction :-). marcus hall ..!ihnp4!tellab1!tellab2!thoth