public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Mike Stump <mrs@apple.com>
To: Shaun Jackman <sjackman@gmail.com>
Cc: GCC Development <gcc@gcc.gnu.org>
Subject: Re: memcpy to an unaligned address
Date: Tue, 02 Aug 2005 18:03:00 -0000	[thread overview]
Message-ID: <17CAD874-5898-4914-B18E-DA0115715974@apple.com> (raw)
In-Reply-To: <7f45d9390508021032aea5a61@mail.gmail.com>

On Aug 2, 2005, at 10:32 AM, Shaun Jackman wrote:
> In a typical Ethernet/IP ARP header the source IP address is
> unaligned. Instead of using...
>     out->srcIPAddr = in->dstIPAddr;
> ... I used...
>     memcpy(&out->srcIPAddr, &in->dstIPAddr, sizeof(uint32_t));
> ... to account for the unaligned destination. This worked until gcc 4,
> which now generates a simple load/store.
>     ldr     r3, [r6, #24]
>     adds    r2, r4, #0
>     adds    r2, #14
>     str     r3, [r2, #0]
> A nice optimisation, but in this case it's incorrect. $r4 is aligned,
> and the result of adding #14 to $r4 is an unaligned pointer.
>
> Should gcc know better, or do I need to give it a little more
> information to help it out?

gcc-help is the correct list to ask this question on.  Anyway, I  
suspect people would be aided in helping you by seeing the source  
code and knowing what version of gcc you're using...  I suspect you  
don't mark the structure as packed and as using 1 or 2 byte  
alignment.  If you do that, then the compiler should generate the  
correct code, for example:

mrs $ cat t1.c
struct {
   char a[14];
   int i __attribute__((aligned(1), packed));
} s, d;

main() {
   d.i = s.i;
}


$ arm-gcc -O4 t1.c -S

gives:

_main:
         @ args = 0, pretend = 0, frame = 0
         @ frame_needed = 0, uses_anonymous_args = 0
         @ link register save eliminated.
         str     sl, [sp, #-4]!
         ldr     sl, .L3
         ldr     r2, .L3+4
.L2:
         add     sl, pc, sl
         ldr     ip, [sl, r2]
         ldr     r0, .L3+8
         ldrh    r1, [ip, #16]
         ldr     r2, [sl, r0]
         ldrh    r3, [ip, #14]
         @ lr needed for prologue
         strh    r1, [r2, #16]   @ movhi
         strh    r3, [r2, #14]   @ movhi
         ldmfd   sp!, {sl}
         mov     pc, lr

for me.  Notice the adding of 14, notice the two 16 bit moves instead  
of one 4 byte move.  If you lie to the compiler, it will make your  
life rough.  Telling it that it is aligned, when the data isn't  
aligned, is a lie.

  parent reply	other threads:[~2005-08-02 18:03 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-08-02 17:32 Shaun Jackman
2005-08-02 17:43 ` Dave Korn
2005-08-02 19:13   ` Shaun Jackman
2005-08-02 19:19     ` Paul Koning
2005-08-02 19:26       ` Shaun Jackman
2005-08-02 19:40         ` Dave Korn
2005-08-02 19:48           ` Paul Koning
2005-08-02 20:15           ` Shaun Jackman
2005-08-02 20:29             ` Mike Stump
2005-08-02 20:38               ` Andrew Pinski
2005-08-02 20:45                 ` Ian Lance Taylor
2005-08-02 21:30                   ` Mike Stump
2005-08-02 21:34                     ` Joe Buck
2005-08-03 18:00                   ` Richard Henderson
2005-08-03 18:15                     ` Shaun Jackman
2005-08-03 18:19                       ` Dave Korn
2005-08-03 21:26                       ` Richard Henderson
2005-08-04  4:42                     ` Ian Lance Taylor
2005-08-04 12:40                     ` Paul Koning
2005-08-02 20:46                 ` Paul Koning
2005-08-02 22:17                   ` Shaun Jackman
2005-08-03 17:16                     ` Paul Koning
2005-08-02 22:26                   ` Shaun Jackman
2005-08-02 22:29                     ` Shaun Jackman
2005-08-02 21:05                 ` Mike Stump
2005-08-02 21:11                   ` Joe Buck
2005-08-02 22:15                     ` Shaun Jackman
2005-08-02 22:12                       ` Joe Buck
2005-08-02 20:29             ` Paul Koning
2005-08-02 17:48 ` Falk Hueffner
2005-08-02 18:03 ` Mike Stump [this message]
     [not found] <345be691050804025955c0b4ab@mail.gmail.com>
2005-08-04 15:06 ` Shaun Jackman
2005-08-04 15:09   ` Christian Joensson
2005-08-05  8:41   ` Carl Whitwell
2005-08-05 16:09     ` Shaun Jackman

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=17CAD874-5898-4914-B18E-DA0115715974@apple.com \
    --to=mrs@apple.com \
    --cc=gcc@gcc.gnu.org \
    --cc=sjackman@gmail.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).