public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/34235]  New: short variable cast to unsigned int fails to right shift as unsigned
@ 2007-11-26 13:09 fgccbz1 at greynode dot net
  2007-11-26 13:43 ` [Bug c/34235] " rguenth at gcc dot gnu dot org
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ messages in thread
From: fgccbz1 at greynode dot net @ 2007-11-26 13:09 UTC (permalink / raw)
  To: gcc-bugs

Shifting the result of a cast does not produce the same results as when using
an intermediate variable.

        short x = -1;
        unsigned a = ((unsigned)(short)-1) >> 1;
        unsigned tmp = (unsigned)x;
        unsigned b = tmp >> 1;
        unsigned c = ((unsigned)x) >> 1;
        printf("a:%x b:%x c:%x",a,b,c);

produces
a:7fffffff b:7fffffff c:ffffffff

while I'd expect the result for c to be the same as the other two.

Similar behavior appears to occur with other types, as long as the source type
is shorter than the target type and the target type is int or longer.


-- 
           Summary: short variable cast to unsigned int fails to right shift
                    as unsigned
           Product: gcc
           Version: 4.1.2
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: fgccbz1 at greynode dot net
  GCC host triplet: x86_64-redhat-linux
GCC target triplet: x86_64-redhat-linux


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34235


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c/34235] short variable cast to unsigned int fails to right shift as unsigned
  2007-11-26 13:09 [Bug c/34235] New: short variable cast to unsigned int fails to right shift as unsigned fgccbz1 at greynode dot net
@ 2007-11-26 13:43 ` rguenth at gcc dot gnu dot org
  2007-11-27 12:08 ` fgccbz1 at greynode dot net
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2007-11-26 13:43 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from rguenth at gcc dot gnu dot org  2007-11-26 13:43 -------
Works for me.  Please be more specific wrt the version of your compiler and the
used optimization flags.


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |WAITING


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34235


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c/34235] short variable cast to unsigned int fails to right shift as unsigned
  2007-11-26 13:09 [Bug c/34235] New: short variable cast to unsigned int fails to right shift as unsigned fgccbz1 at greynode dot net
  2007-11-26 13:43 ` [Bug c/34235] " rguenth at gcc dot gnu dot org
@ 2007-11-27 12:08 ` fgccbz1 at greynode dot net
  2007-11-27 12:09 ` fgccbz1 at greynode dot net
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: fgccbz1 at greynode dot net @ 2007-11-27 12:08 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from fgccbz1 at greynode dot net  2007-11-27 12:08 -------
Sorry - it looks like compiling as c++ is required. I thought I'd eliminated
that  issue in my original analysis, but evidently I was wrong.

I see the weird behavior just when compiling with
g++ cast.c -o cast
but not with
gcc cast.c -o cast

(I'll attach my exact cast.c)

I see the issue using the redhat-built compilers on my Fedora 7 machine:
$ g++ -v
Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--enable-checking=release --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions
--enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk
--disable-dssi --enable-plugin
--with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre
--enable-libgcj-multifile --enable-java-maintainer-mode
--with-ecj-jar=/usr/share/java/eclipse-ecj.jar --with-cpu=generic
--host=x86_64-redhat-linux
Thread model: posix
gcc version 4.1.2 20070925 (Red Hat 4.1.2-27)

also with my own build of gcc from a controlled environment, using straight gnu
source, with binutils-2.16.1:
$ gcc -v
Using built-in specs.
Target: x86_64-unknown-linux-gnu
Configured with:
/localdisk/dev_tools_build/dev_tools/source/gcc-4.1.2/configure
--prefix=/usr/dev_tools/170681 --libdir=/usr/dev_tools/170681/lib64
--enable-languages=c,c++ --with-gnu-as --with-as=/usr/dev_tools/170681/bin/as
--with-gnu-ld --with-ld=/usr/dev_tools/170681/bin/ld
Thread model: posix
gcc version 4.1.2

As a side note I'll mention I also saw the problem using an old version of
gcc-2.95.3 I had lying around, but I don't know the provenance of that
compiler.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34235


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c/34235] short variable cast to unsigned int fails to right shift as unsigned
  2007-11-26 13:09 [Bug c/34235] New: short variable cast to unsigned int fails to right shift as unsigned fgccbz1 at greynode dot net
  2007-11-26 13:43 ` [Bug c/34235] " rguenth at gcc dot gnu dot org
  2007-11-27 12:08 ` fgccbz1 at greynode dot net
@ 2007-11-27 12:09 ` fgccbz1 at greynode dot net
  2007-11-27 12:12 ` fgccbz1 at greynode dot net
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: fgccbz1 at greynode dot net @ 2007-11-27 12:09 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from fgccbz1 at greynode dot net  2007-11-27 12:09 -------
Created an attachment (id=14646)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=14646&action=view)
code to reproduce the problem


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34235


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c/34235] short variable cast to unsigned int fails to right shift as unsigned
  2007-11-26 13:09 [Bug c/34235] New: short variable cast to unsigned int fails to right shift as unsigned fgccbz1 at greynode dot net
                   ` (2 preceding siblings ...)
  2007-11-27 12:09 ` fgccbz1 at greynode dot net
@ 2007-11-27 12:12 ` fgccbz1 at greynode dot net
  2007-12-02 22:33 ` pinskia at gcc dot gnu dot org
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: fgccbz1 at greynode dot net @ 2007-11-27 12:12 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from fgccbz1 at greynode dot net  2007-11-27 12:12 -------
Oh - and thanks for looking at my bug!


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34235


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c/34235] short variable cast to unsigned int fails to right shift as unsigned
  2007-11-26 13:09 [Bug c/34235] New: short variable cast to unsigned int fails to right shift as unsigned fgccbz1 at greynode dot net
                   ` (3 preceding siblings ...)
  2007-11-27 12:12 ` fgccbz1 at greynode dot net
@ 2007-12-02 22:33 ` pinskia at gcc dot gnu dot org
  2008-01-25 23:17 ` [Bug c++/34235] " rguenth at gcc dot gnu dot org
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2007-12-02 22:33 UTC (permalink / raw)
  To: gcc-bugs



-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|major                       |normal


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34235


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/34235] short variable cast to unsigned int fails to right shift as unsigned
  2007-11-26 13:09 [Bug c/34235] New: short variable cast to unsigned int fails to right shift as unsigned fgccbz1 at greynode dot net
                   ` (4 preceding siblings ...)
  2007-12-02 22:33 ` pinskia at gcc dot gnu dot org
@ 2008-01-25 23:17 ` rguenth at gcc dot gnu dot org
  2008-01-25 23:20 ` rguenth at gcc dot gnu dot org
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2008-01-25 23:17 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from rguenth at gcc dot gnu dot org  2008-01-25 22:58 -------
Confirmed.

extern "C" void abort (void);
int main()
{
        short x = -1;
        unsigned int c = ((unsigned int)x) >> 1;
        if (c != 0x7fffffff)
          abort();
        return 0;
}


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|WAITING                     |NEW
          Component|c                           |c++
     Ever Confirmed|0                           |1
           Keywords|                            |wrong-code
   Last reconfirmed|0000-00-00 00:00:00         |2008-01-25 22:58:31
               date|                            |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34235


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/34235] short variable cast to unsigned int fails to right shift as unsigned
  2007-11-26 13:09 [Bug c/34235] New: short variable cast to unsigned int fails to right shift as unsigned fgccbz1 at greynode dot net
                   ` (5 preceding siblings ...)
  2008-01-25 23:17 ` [Bug c++/34235] " rguenth at gcc dot gnu dot org
@ 2008-01-25 23:20 ` rguenth at gcc dot gnu dot org
  2008-01-25 23:24 ` rguenth at gcc dot gnu dot org
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2008-01-25 23:20 UTC (permalink / raw)
  To: gcc-bugs



-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         AssignedTo|unassigned at gcc dot gnu   |rguenth at gcc dot gnu dot
                   |dot org                     |org
             Status|NEW                         |ASSIGNED
   Last reconfirmed|2008-01-25 22:58:31         |2008-01-25 22:58:40
               date|                            |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34235


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/34235] short variable cast to unsigned int fails to right shift as unsigned
  2007-11-26 13:09 [Bug c/34235] New: short variable cast to unsigned int fails to right shift as unsigned fgccbz1 at greynode dot net
                   ` (6 preceding siblings ...)
  2008-01-25 23:20 ` rguenth at gcc dot gnu dot org
@ 2008-01-25 23:24 ` rguenth at gcc dot gnu dot org
  2008-01-25 23:36 ` rguenth at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2008-01-25 23:24 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from rguenth at gcc dot gnu dot org  2008-01-25 23:12 -------
              /* If arg is sign-extended and then unsigned-shifted,
                 we can simulate this with a signed shift in arg's type
                 only if the extended result is at least twice as wide
                 as the arg.  Otherwise, the shift could use up all the
                 ones made by sign-extension and bring in zeros.
                 We can't optimize that case at all, but in most machines
                 it never happens because available widths are 2**N.  */

err - WHAT?!


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34235


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/34235] short variable cast to unsigned int fails to right shift as unsigned
  2007-11-26 13:09 [Bug c/34235] New: short variable cast to unsigned int fails to right shift as unsigned fgccbz1 at greynode dot net
                   ` (7 preceding siblings ...)
  2008-01-25 23:24 ` rguenth at gcc dot gnu dot org
@ 2008-01-25 23:36 ` rguenth at gcc dot gnu dot org
  2008-01-26 11:36 ` rguenth at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2008-01-25 23:36 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from rguenth at gcc dot gnu dot org  2008-01-25 23:23 -------
This has been broken since forever.


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to fail|                            |2.95.4 4.3.0


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34235


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/34235] short variable cast to unsigned int fails to right shift as unsigned
  2007-11-26 13:09 [Bug c/34235] New: short variable cast to unsigned int fails to right shift as unsigned fgccbz1 at greynode dot net
                   ` (8 preceding siblings ...)
  2008-01-25 23:36 ` rguenth at gcc dot gnu dot org
@ 2008-01-26 11:36 ` rguenth at gcc dot gnu dot org
  2008-01-26 11:37 ` rguenth at gcc dot gnu dot org
  2008-01-26 14:06 ` fgccbz1 at greynode dot net
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2008-01-26 11:36 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from rguenth at gcc dot gnu dot org  2008-01-26 11:27 -------
Fixed for 4.3.0.


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
      Known to fail|2.95.4 4.3.0                |2.95.4 4.1.2 4.2.3
         Resolution|                            |FIXED
   Target Milestone|---                         |4.3.0


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34235


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/34235] short variable cast to unsigned int fails to right shift as unsigned
  2007-11-26 13:09 [Bug c/34235] New: short variable cast to unsigned int fails to right shift as unsigned fgccbz1 at greynode dot net
                   ` (9 preceding siblings ...)
  2008-01-26 11:36 ` rguenth at gcc dot gnu dot org
@ 2008-01-26 11:37 ` rguenth at gcc dot gnu dot org
  2008-01-26 14:06 ` fgccbz1 at greynode dot net
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2008-01-26 11:37 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #9 from rguenth at gcc dot gnu dot org  2008-01-26 11:27 -------
Subject: Bug 34235

Author: rguenth
Date: Sat Jan 26 11:26:36 2008
New Revision: 131862

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=131862
Log:
2008-01-26  Richard Guenther  <rguenther@suse.de>

        PR c++/34235
        * typeck.c (build_binary_op): Remove code to shorten compares.

        * g++.dg/torture/pr34235.C: New testcase.

Added:
    trunk/gcc/testsuite/g++.dg/torture/pr34235.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/typeck.c
    trunk/gcc/testsuite/ChangeLog


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34235


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/34235] short variable cast to unsigned int fails to right shift as unsigned
  2007-11-26 13:09 [Bug c/34235] New: short variable cast to unsigned int fails to right shift as unsigned fgccbz1 at greynode dot net
                   ` (10 preceding siblings ...)
  2008-01-26 11:37 ` rguenth at gcc dot gnu dot org
@ 2008-01-26 14:06 ` fgccbz1 at greynode dot net
  11 siblings, 0 replies; 13+ messages in thread
From: fgccbz1 at greynode dot net @ 2008-01-26 14:06 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #10 from fgccbz1 at greynode dot net  2008-01-26 13:29 -------
My thanks for the fix.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34235


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2008-01-26 13:30 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-11-26 13:09 [Bug c/34235] New: short variable cast to unsigned int fails to right shift as unsigned fgccbz1 at greynode dot net
2007-11-26 13:43 ` [Bug c/34235] " rguenth at gcc dot gnu dot org
2007-11-27 12:08 ` fgccbz1 at greynode dot net
2007-11-27 12:09 ` fgccbz1 at greynode dot net
2007-11-27 12:12 ` fgccbz1 at greynode dot net
2007-12-02 22:33 ` pinskia at gcc dot gnu dot org
2008-01-25 23:17 ` [Bug c++/34235] " rguenth at gcc dot gnu dot org
2008-01-25 23:20 ` rguenth at gcc dot gnu dot org
2008-01-25 23:24 ` rguenth at gcc dot gnu dot org
2008-01-25 23:36 ` rguenth at gcc dot gnu dot org
2008-01-26 11:36 ` rguenth at gcc dot gnu dot org
2008-01-26 11:37 ` rguenth at gcc dot gnu dot org
2008-01-26 14:06 ` fgccbz1 at greynode dot net

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).