public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Peter Barada <pbarada@wavemark.com>
To: mrs@wrs.com
Cc: wilson@cygnus.com, egcs@cygnus.com
Subject: Re: m68k structure packing
Date: Wed, 01 Oct 1997 15:56:00 -0000	[thread overview]
Message-ID: <199710012255.SAA11990@miacomet.wavemark.com> (raw)
In-Reply-To: <199710012207.PAA01493@kankakee.wrs.com>

>> If you have a short in the struct:
>
>> struct {
>> 	short b;
>> 	char a;
>> } z, list[10];
>
>> then sizeof(z) could be 3 but is 4 due to alignment requirements(i.e
>> malloc(n*sizeof(z))) and sizeof(list) is 40.
>
>Is this fact, or theory?  On the sparc, which is unaffacted by my
>change, with -fpack-struct we get 3 bytes.

If you pack the structure on any hardware with strict alignment then
you have to access any non-byte element using ONLY byte accesses since
you can not assume the alignment of the address of the start of the structure.
Unfortunately on the 68k this produces some really slow code. Ex: 

struct foo {
	char c;
	short b;
} *p;

When foo is not packed, the size is 4, and the offset to b is 2.
compiling p->b += 1 when foo is not packed gives:

; for sake of theexample, I'll load p->b into a register.
;
move.l	 p,a0		; load p into a0
move.w	 2(a0),d0	; load short 2 off of a0 into d0 (ofset to b)
addq.w	 #1,d0		; increment d0 as a word
move.w	 d0,2(a0)	; store short d0 2 off of a0

This works since it is assumed thant any pointer to the structure is even.

When foo is packed, the size is 3, and offset to b is 1.
To compile p->b += 1 when foo is packed requires:

move.l	 p,a0		; load p into a0
move.b	 1(a0),d0	; load the high byte of p->b into d0
lsl.w	 #8,d0		; shift left into the high byte
move.b	 2(a0),d0	; load the low byte of p->b into d0
addq.w	 #1,d0		; increment as a word
move.b	 d0,2(a0)	; store low byte
lsr.w	 #8,d0		; shift high byte into low byte
move.b	 d0,1(a0)	; store high byte

Obviously this takes more instructions, and many more clock cycles.
On the 68000 it takes 4 clocks for each bit shift, so in this code the
shifts alone take up 64 clocks.  I don't think the tradeoff is worth
it here.

On other processors that allow unaligned acces, then the code is the
same in either case, and the only hit is having to double cycle the
bus.

>Should the above code fail at runtime?  (You have to have a
>STRICT_ALIGNMENT machine, pack a structure, and you may have to use
>int or long or float or double to make it fail).  If not, should we
>make it work by aligning it or by requiring what the pointer points to
>is a packed short, then on dereference of a pointer to a packed short,
>we could know to do byte accesses.

Again it all depends. If you pack and use short accesses, then yes I
would expect a SIGBUS error on the short load instruction. If you can
pack it and the compiler can produce code that uses only byte
accesses, then the bloated code will go dog slow but not SIGBUS.

If you want packed structures then they should be sized up to a short, and
anything that is not a char in a packed struct should be aligned on a
short boundary(BIGGEST_ALIGNMENT). But this is what happens on the 68k
anyway.

-- 
Peter Barada				pbarada@wavemark.com
Wizard					617-270-7098 x1226
WaveMark Technologies, Inc.		617-270-0193 (fax)

"Real men know that you should never attempt to accomplish with words
what you can do with a flame thrower" --Bruce Ferstein

  reply	other threads:[~1997-10-01 15:56 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-10-01 15:08 Mike Stump
1997-10-01 15:56 ` Peter Barada [this message]
1997-10-01 16:16   ` Per Bothner
1997-10-02 20:14     ` Jim Wilson
1997-10-02  6:49   ` Paul Koning
1997-10-02 20:09   ` Jim Wilson
1997-10-02 20:01 ` Jim Wilson
1997-10-02 21:40   ` Richard Henderson
  -- strict thread matches above, loose matches on Subject: below --
1997-10-01 12:16 Mike Stump
1997-10-01 12:39 ` Joel Sherrill
1997-10-02 18:37 ` Jim Wilson
1997-09-30 21:42 Jim Wilson
1997-10-01  5:44 ` Kamil Iskra
1997-09-30 20:16 Mike Stump
1997-09-30 22:03 ` Jeffrey A Law
1997-09-30 16:58 Mike Stump
1997-09-30 18:20 ` Jim Wilson
1997-10-01  9:02   ` Peter Barada
1997-09-30 13:32 Mike Stump
1997-09-30 14:56 ` Jim Wilson
1997-09-30 12:08 Mike Stump
1997-09-30 12:57 ` Charles M. Hannum
1997-09-30 19:58 ` Jim Wilson
1997-09-30 21:13   ` Richard Henderson
1997-09-30 21:22   ` Jim Wilson
1997-10-01 15:14 ` Jim Wilson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=199710012255.SAA11990@miacomet.wavemark.com \
    --to=pbarada@wavemark.com \
    --cc=egcs@cygnus.com \
    --cc=mrs@wrs.com \
    --cc=wilson@cygnus.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).