public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Alessandro Rubini <rubini-list@gnudd.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] IP_t should be a "packed" struct
Date: Thu, 29 Jan 2009 11:37:11 +0100	[thread overview]
Message-ID: <20090129103711.GA32504@mail.gnudd.com> (raw)
In-Reply-To: <20090129102022.A71D3832E416@gemini.denx.de>

> Now study the data structures used  for  netwrking  -  they  are  all
> carefully  crafted that the natural aligment "just fits", i. e. there
> are no gaps between variables if they are aligned naturally.

Actually, a packed structure not only doesn't have gaps, but can live
at any address. So the compiler must access it byte-by-byte
regardless.  This means that a naturally-aligned structure performs
much worse if declared as packed. And single-word loads and stores are
not atomic any more.

I tried with eldk-4.2 for arm and an older gcc for mips, I have
no ppc or other archs handy right now:

   #include <stdint.h>
   #include <stdio.h>

   #ifdef PACKEDTEST
   #  define P __attribute__ ((packed))
   #else
   #  define P
   #endif

   struct {uint32_t i; uint16_t s; uint8_t c, d;} P datum;

   uint32_t i;
   uint16_t s;
   uint8_t c;

   int main(int argc, char **argv)
   {
   	printf("ih\n");
   	i = datum.i;
   	printf("oh\n");
   	s = datum.s;
   	printf("ah\n");
   	c = datum.c;
   	return 0;
   }  

Compilation: ARM non-packed (only interesting lines):

    8378:       e5942000        ldr     r2, [r4]
    8384:       e5832000        str     r2, [r3]

    838c:       e1d420b4        ldrh    r2, [r4, #4]
    8398:       e1c320b0        strh    r2, [r3]

    83a0:       e5d42006        ldrb    r2, [r4, #6]
    83ac:       e5c32000        strb    r2, [r3]

Compilation: ARM packed (only interesting lines):

    8378:       e5d42001        ldrb    r2, [r4, #1]
    837c:       e5d43000        ldrb    r3, [r4]
    8380:       e5d40002        ldrb    r0, [r4, #2]
    8384:       e5d41003        ldrb    r1, [r4, #3]
    8388:       e1833402        orr     r3, r3, r2, lsl #8
    838c:       e1833800        orr     r3, r3, r0, lsl #16
    8394:       e1833c01        orr     r3, r3, r1, lsl #24
    8398:       e5823000        str     r3, [r2]

    83a4:       e5d43005        ldrb    r3, [r4, #5]
    83a8:       e5d42004        ldrb    r2, [r4, #4]
    83b0:       e1822403        orr     r2, r2, r3, lsl #8
    83b8:       e1c320b0        strh    r2, [r3]

    83c0:       e5d42006        ldrb    r2, [r4, #6]
    83cc:       e5c32000        strb    r2, [r3]

Compilation: MIPS non-packed (only interesting lines):

  44:   8e030000        lw      v1,0(s0)
  5c:   ac230000        sw      v1,0(at)

  74:   96030004        lhu     v1,4(s0)
  8c:   a4230000        sh      v1,0(at)

  a4:   92030006        lbu     v1,6(s0)
  bc:   a0230000        sb      v1,0(at)


Compilation: MIPS packed (only interesting lines):

  44:   8a030003        lwl     v1,3(s0)
  48:   9a030000        lwr     v1,0(s0)
  60:   ac230000        sw      v1,0(at)

  78:   92030005        lbu     v1,5(s0)
  7c:   92020004        lbu     v0,4(s0)
  80:   00031a00        sll     v1,v1,0x8
  84:   00621825        or      v1,v1,v0
  9c:   a4230000        sh      v1,0(at)

  b4:   92030006        lbu     v1,6(s0)
  cc:   a0230000        sb      v1,0(at)


/alessandro

  reply	other threads:[~2009-01-29 10:37 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-01-27 18:32 [U-Boot] IP_t should be a "packed" struct Luigi 'Comio' Mantellini
2009-01-28  9:42 ` Luigi 'Comio' Mantellini
2009-01-28 17:55 ` Ben Warren
2009-01-28 18:36   ` Jerry Van Baren
2009-01-28 18:58     ` Ben Warren
2009-01-28 20:40       ` Luigi Mantellini
2009-01-28 21:21         ` Ben Warren
2009-01-28 21:38           ` Wolfgang Denk
2009-01-28 21:52             ` Ben Warren
2009-01-28 22:21               ` Wolfgang Denk
2009-01-28 22:16             ` Luigi Mantellini
2009-01-28 22:27               ` Wolfgang Denk
2009-01-28 23:04                 ` Luigi Mantellini
2009-01-29 10:20                   ` Wolfgang Denk
2009-01-29 10:37                     ` Alessandro Rubini [this message]
2009-01-28 20:58   ` Luigi Mantellini
2009-01-28 21:26     ` Wolfgang Denk

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=20090129103711.GA32504@mail.gnudd.com \
    --to=rubini-list@gnudd.com \
    --cc=u-boot@lists.denx.de \
    /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