public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/26292]  New: Bad code in unsigned shifts
@ 2006-02-14 21:44 igodard at pacbell dot net
  2006-02-14 21:51 ` [Bug c++/26292] " igodard at pacbell dot net
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: igodard at pacbell dot net @ 2006-02-14 21:44 UTC (permalink / raw)
  To: gcc-bugs

In the code:
#include <iostream>
typedef unsigned int uint;
typedef long long int ll;
int main() {
    int i = 0;
    ll a = uint(1);
    ll b = (uint(1) << (32 - i));
    ll c = ((uint(1) << (32 - i)) - 1 );
    ll d = (((uint(1) << (32 - i)) - 1) << 0);
    std::cerr << std::hex <<a << ":" << b << ":" << c << ":" << d << "\n";
    }

the compiler:
~/ootbc/common$ g++ -v
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: ../gcc-4.0.2/configure --prefix=/home/ivan/gcc
--enable-languages=c,c++
Thread model: posix
gcc version 4.0.2

gives you:

~/ootbc/common$ a.out
1:1:0:0


whereas the correct result would be "1:0:ffffffff:ffffffff".

Interestingly, on Cygwin the compiler:
Reading specs from /usr/lib/gcc/i686-pc-cygwin/3.4.4/specs
Configured with: /gcc/gcc-3.4.4/gcc-3.4.4-1/configure --verbose --prefix=/usr
--exec-prefix=/usr --sysconfdir=/etc --libdir=/usr/lib --libexecdir=/usr/lib
--mandir=/usr/share/man --infodir=/usr/share/info
--enable-languages=c,ada,c++,d,f77,java,objc --enable-nls
--without-included-gettext --enable-version-specific-runtime-libs --without-x
--enable-libgcj --disable-java-awt --with-system-zlib --enable-interpreter
--disable-libgcj-debug --enable-threads=posix --enable-java-gc=boehm
--disable-win32-registry --enable-sjlj-exceptions --enable-hash-synchronization
--enable-libstdcxx-debug : (reconfigured) 
Thread model: posix
gcc version 3.4.4 (cygming special) (gdc 0.12, using dmd 0.125)


gives you:
~/ootbc/common: a.exe
1:1:0:7fffffff


Ivan


-- 
           Summary: Bad code in unsigned shifts
           Product: gcc
           Version: 4.0.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: igodard at pacbell dot net


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


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

* [Bug c++/26292] Bad code in unsigned shifts
  2006-02-14 21:44 [Bug c++/26292] New: Bad code in unsigned shifts igodard at pacbell dot net
@ 2006-02-14 21:51 ` igodard at pacbell dot net
  2006-02-14 21:57 ` pinskia at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: igodard at pacbell dot net @ 2006-02-14 21:51 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from igodard at pacbell dot net  2006-02-14 21:51 -------
FWIW, "uint(1) << 32" gives zero, but "int i = 32; uint(1) << i" gives one.

Ivan


-- 


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


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

* [Bug c++/26292] Bad code in unsigned shifts
  2006-02-14 21:44 [Bug c++/26292] New: Bad code in unsigned shifts igodard at pacbell dot net
  2006-02-14 21:51 ` [Bug c++/26292] " igodard at pacbell dot net
@ 2006-02-14 21:57 ` pinskia at gcc dot gnu dot org
  2006-02-14 21:59 ` pinskia at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2006-02-14 21:57 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from pinskia at gcc dot gnu dot org  2006-02-14 21:57 -------
I think there is a dup of this bug and this is code is undefined too.  I have
to double check and get back to you.


-- 


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


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

* [Bug c++/26292] Bad code in unsigned shifts
  2006-02-14 21:44 [Bug c++/26292] New: Bad code in unsigned shifts igodard at pacbell dot net
  2006-02-14 21:51 ` [Bug c++/26292] " igodard at pacbell dot net
  2006-02-14 21:57 ` pinskia at gcc dot gnu dot org
@ 2006-02-14 21:59 ` pinskia at gcc dot gnu dot org
  2006-02-14 22:18 ` igodard at pacbell dot net
  2006-02-14 22:21 ` pinskia at gcc dot gnu dot org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2006-02-14 21:59 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from pinskia at gcc dot gnu dot org  2006-02-14 21:59 -------
Yep this is just undefined code.
5.8/1:
The behavior is undefined if the right operand is negative, or greater than or
equal to the length in bits of the promoted left operand.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


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


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

* [Bug c++/26292] Bad code in unsigned shifts
  2006-02-14 21:44 [Bug c++/26292] New: Bad code in unsigned shifts igodard at pacbell dot net
                   ` (2 preceding siblings ...)
  2006-02-14 21:59 ` pinskia at gcc dot gnu dot org
@ 2006-02-14 22:18 ` igodard at pacbell dot net
  2006-02-14 22:21 ` pinskia at gcc dot gnu dot org
  4 siblings, 0 replies; 6+ messages in thread
From: igodard at pacbell dot net @ 2006-02-14 22:18 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from igodard at pacbell dot net  2006-02-14 22:18 -------
"undefined" takes care of the 4.0.2 case, and the first part of the Cygwin
case. But the shift that produces "d" in the Cygwin case has a right operand of
zero and a left operand (what the undefined gave as "c") also zero, but it
produces "7fffffff". Or does an undefined propagate outward through
parentheses, so that the result of any expression one of whose components
(however deeply nested) is undefined is also undefined? Is "true ||
(<undefined>)" also undefined? If so then neither of my cases is a bug, but if
not then there's a problem on Cygwin 3.4.4.

Ivan


-- 

igodard at pacbell dot net changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |UNCONFIRMED
         Resolution|INVALID                     |


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


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

* [Bug c++/26292] Bad code in unsigned shifts
  2006-02-14 21:44 [Bug c++/26292] New: Bad code in unsigned shifts igodard at pacbell dot net
                   ` (3 preceding siblings ...)
  2006-02-14 22:18 ` igodard at pacbell dot net
@ 2006-02-14 22:21 ` pinskia at gcc dot gnu dot org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2006-02-14 22:21 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from pinskia at gcc dot gnu dot org  2006-02-14 22:21 -------
(((uint(1) << (32 - i)) - 1) << 0)

That whole expression is undefined because 1 << 32 is undefined.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


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


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

end of thread, other threads:[~2006-02-14 22:21 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-02-14 21:44 [Bug c++/26292] New: Bad code in unsigned shifts igodard at pacbell dot net
2006-02-14 21:51 ` [Bug c++/26292] " igodard at pacbell dot net
2006-02-14 21:57 ` pinskia at gcc dot gnu dot org
2006-02-14 21:59 ` pinskia at gcc dot gnu dot org
2006-02-14 22:18 ` igodard at pacbell dot net
2006-02-14 22:21 ` pinskia at gcc dot gnu dot org

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