public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Dave Korn" <dk@artimi.com>
To: "'Dale Johannesen'" <dalej@apple.com>
Cc: <gcc@gcc.gnu.org>, "'Andrew Haley'" <aph@redhat.com>,
	"'Nathan Sidwell'" <nathan@codesourcery.com>
Subject: RE: warning: right shift count >= width of type
Date: Mon, 29 Nov 2004 17:14:00 -0000	[thread overview]
Message-ID: <NUTMEGvGPCNue2lHq6J000001b8@NUTMEG.CAM.ARTIMI.COM> (raw)
In-Reply-To: <13C7A994-4224-11D9-B898-000A95D7D16C@apple.com>

> -----Original Message-----
> From: Dale Johannesen 
> Sent: 29 November 2004 16:31

> On Nov 29, 2004, at 8:18 AM, Dave Korn wrote:
> >   Afternoon all.  Here's something that's piqued my curiosity; it's 
> > probably
> > owing to some language-lawyerly issue, but it isn't obvious to me.  
> > This is
> > on gcc-3.3.3, (cygwin variant, but that's probably not relevant):
> >
> > -------------------------<snip!>-------------------------
> > dk@mace /test/shift-test> cat foo.c
> >
> > unsigned int bar (unsigned int baz)
> > {
> > unsigned int quux;
> >
> >         quux = baz >> 32;
> >         return quux;
> > }
> >   Why isn't the shift operation optimised away and replaced with 
> > const_int
> > 0?
> 
> Because that's not what it means.  Shifts by >= word size are 
> undefined 
> behavior
> and will give different results depending on optimization 
> level and on 
> whether
> the shift count is constant or variable.  Don't do that.  (If 
> you think 
> it ought to be 0,
> reflect that most popular CPUs have only 5 bit shift counts, and 
> consider what the
> code for x >> y would have to look like.)


  Absolutely so; my curiosity was piqued when I noticed that my
cross-compiler was generating illegal assembler code with an out-of-range
operand value that the assembler couldn't fit into the relevant opcode
bitfield.

  So my question is really "Given that it's undefined, which means that
whatever the compiler does is correct, and given that there's already code
in there to detect the situation and issue a warning, which probably means
that it would be very easy at such a point to replace the offending RTL with
(const_int 0), is there any specific reason why not to?"  And it looks like
the answer is that there's no reason not to, but nobody has wanted to make
it work that way; it's historical accident rather than any kind of policy or
language issue.

  I imagine that this is one of those areas where "undefined behaviour" is
the standard's way of saying "Well, some compilers in the past got it right
and some got it wrong and since the standard is largely about codifying the
de-facto behaviour of existing C compilers we'll leave this one alone", but
it's surely only an issue of bugward-compatibility: mathematically, there's
really no problem with right-shifting more than the width of the integer,
all that happens is that _all_ the bits drop out the right-hand side and
you're left with nothing.  ISTM reasonable that the result of a right-shift
by 32 bits could be assumed to be the same thing you get if you right-shift
by 1 bit 32 times....


    cheers, 
      DaveK
-- 
Can't think of a witty .sigline today....

  reply	other threads:[~2004-11-29 17:01 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-11-29 16:30 Dave Korn
2004-11-29 16:33 ` Nathan Sidwell
2004-11-29 17:01 ` Andrew Haley
2004-11-29 17:12 ` Dale Johannesen
2004-11-29 17:14   ` Dave Korn [this message]
2004-11-29 17:26     ` Andrew Haley
2004-11-29 18:29       ` Peter Barada
2004-11-29 18:53         ` Dave Korn
2004-11-29 18:52       ` Dave Korn
2004-11-29 19:09         ` Andrew Haley
2004-11-29 19:17           ` Dale Johannesen
2004-11-29 19:54             ` Dave Korn
2004-11-29 17:38     ` Chris Jefferson
2004-11-29 18:46       ` Dave Korn
2004-11-29 19:19         ` Chris Jefferson
2004-11-29 19:46           ` Dave Korn
2004-11-29 23:49 Paul Schlie
2004-11-30  1:50 ` Andreas Schwab
2004-11-30  4:23 Paul Schlie

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=NUTMEGvGPCNue2lHq6J000001b8@NUTMEG.CAM.ARTIMI.COM \
    --to=dk@artimi.com \
    --cc=aph@redhat.com \
    --cc=dalej@apple.com \
    --cc=gcc@gcc.gnu.org \
    --cc=nathan@codesourcery.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).