From: Bakul Shah <bakul@bitblocks.com>
To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net>
Subject: Re: [9fans] compiler bug?
Date: Wed, 27 Nov 2013 12:34:58 -0800 [thread overview]
Message-ID: <20131127203458.7E631B82A@mail.bitblocks.com> (raw)
In-Reply-To: Your message of "Wed, 27 Nov 2013 11:31:26 EST." <12706eb9d1aafea3af76c427a31aae1a@coraid.com>
On Wed, 27 Nov 2013 11:31:26 EST erik quanstrom <quanstro@labs.coraid.com> wrote:
> On Wed Nov 27 11:04:46 EST 2013, steve@quintile.net wrote:
>
> > Whilst porting some code from the net I came across
> > the attached, rather obscure, code.
> >
> > run as:
> >
> > larch% 8c -D 'STATIC=static' t.c && 8l t.8 && 8.out
> > 78780000
> > larch% 8c -D 'STATIC=' t.c && 8l t.8 && 8.out
> > 0
> >
> > I think it is tickling a bug in 8c, though
> > I may be just showing my lack of knowledge of
> > the C standard...
> >
> > anyone any thoughts?
>
> this code is not correct. it breaks the anti-aliasing rules. the
> compiler is allowed to assume that tmp is not modified.
> recently had trouble with floating point in stdio for the same
> reasons.
I think this is a compiler bug. What happens when you
declare a union but still do, for example,
(((uchar*)&tmp.i)[0] = ((uchar*)mem)[3]
etc.? I bet it works.
> to work around this, use a union, or better yet getle(buf, 3)
> (or be as the case may be),
>
> since there's not enough context to sort out if the original is
> supposed to be big or little endian, here's a union version:
>
> - erik
> ------
> #include <u.h>
> #include <libc.h>
>
> static uint
> getdword_n(void *vmem, int n)
> {
> uchar *mem;
> union {
> uint i;
> uchar u[sizeof(uint)];
> } tmp;
>
> mem = vmem;
> tmp.i = 0;
> switch (n){
> case 3:
> tmp.u[1] = mem[2];
> case 2:
> tmp.u[2] = mem[1];
> case 1:
> tmp.u[3] = mem[0];
> }
> return tmp.i;
> }
>
> void
> main(void)
> {
> uint x;
>
> char buf[] = "xxxx";
>
> x = getdword_n(buf, 2);
> print("%x\n", x);
> }
>
next prev parent reply other threads:[~2013-11-27 20:34 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-11-27 16:03 Steve Simon
2013-11-27 16:31 ` erik quanstrom
2013-11-27 18:07 ` Skip Tavakkolian
2013-11-27 18:38 ` erik quanstrom
2013-11-27 19:24 ` Skip Tavakkolian
2013-11-27 20:34 ` Bakul Shah [this message]
2013-11-27 21:18 ` erik quanstrom
2013-11-27 21:29 ` Charles Forsyth
2013-11-27 21:35 ` erik quanstrom
2013-11-27 21:42 ` Charles Forsyth
2013-11-27 20:41 ` Steve Simon
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=20131127203458.7E631B82A@mail.bitblocks.com \
--to=bakul@bitblocks.com \
--cc=9fans@9fans.net \
/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 NNTP newsgroup(s).