From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 1820 invoked by alias); 11 Jan 2012 11:02:10 -0000 Received: (qmail 1802 invoked by uid 22791); 11 Jan 2012 11:02:07 -0000 X-SWARE-Spam-Status: No, hits=-2.8 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00,TW_CX,TW_GC,TW_IB X-Spam-Check-By: sourceware.org Received: from localhost (HELO gcc.gnu.org) (127.0.0.1) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 11 Jan 2012 11:01:47 +0000 From: "nos.utelsystems at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/51821] New: 64bit > 32bit conversion produces incorrect results with optimizations Date: Wed, 11 Jan 2012 11:02:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: nos.utelsystems at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2012-01/txt/msg01156.txt.bz2 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D51821 Bug #: 51821 Summary: 64bit > 32bit conversion produces incorrect results with optimizations Classification: Unclassified Product: gcc Version: 4.6.2 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c AssignedTo: unassigned@gcc.gnu.org ReportedBy: nos.utelsystems@gmail.com #include #include #include uint32_t test(unsigned int shift_size) { uint64_t res =3D 0; res =3D ~res; res =3D res << shift_size; //Shift size < uint64_t width so this should= work return res; //Implicit cast to uint32_t } int main(int argc, char *argv[]) { unsigned int dst; sscanf(argv[1], "%u", &dst); //Get arg from outside so optimizer doesn't eat everything printf("%"PRIu32"\n", test(dst)); return 0; } This program produces different output if compiled without optimization, or compiled with -O2, on i686 machines. [11:56:58 0 ~/tmp] $ gcc -Wall -Wextra shiftbug.c shiftbug.c: In function =E2=80=98main=E2=80=99: shiftbug.c:12:14: warning: unused parameter =E2=80=98argc=E2=80=99 [-Wunuse= d-parameter] [11:57:01 0 ~/tmp] $ ./a.out 32 0 [11:57:03 0 ~/tmp] $ gcc -O2 -Wall -Wextra shiftbug.c shiftbug.c: In function =E2=80=98main=E2=80=99: shiftbug.c:12:14: warning: unused parameter =E2=80=98argc=E2=80=99 [-Wunuse= d-parameter] [11:57:14 0 ~/tmp] $ ./a.out 32 4294967295 [11:57:15 0 ~/tmp] $ I cannot find anything in the program that is undefined/implementation defi= ned behavior, the program should give the same output with or without optimizat= ion. On an x86_64 machine, the output is 0 with and without optimization. Note that it seems not directly connected to the types in the shift express= ion, res =3D (uint64_t)res << (uint64_t)shift_size; produces the same result. Run on: Linux localhost 3.1.2-1.fc16.i686 #1 SMP Tue Nov 22 08:56:28 UTC 2011 i686 = i686 i386 GNU/Linux $ gcc -v Using built-in specs. COLLECT_GCC=3Dgcc COLLECT_LTO_WRAPPER=3D/usr/libexec/gcc/i686-redhat-linux/4.6.2/lto-wrapper Target: i686-redhat-linux Configured with: ../configure --prefix=3D/usr --mandir=3D/usr/share/man --infodir=3D/usr/share/info --with-bugurl=3Dhttp://bugzilla.redhat.com/bugz= illa --enable-bootstrap --enable-shared --enable-threads=3Dposix --enable-checking=3Drelease --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --enable-languages=3Dc,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-pl= ugin --enable-java-awt=3Dgtk --disable-dssi --with-java-home=3D/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=3D/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=3Dgeneric --with-arch=3Di686 --build=3Di686-redhat-linux Thread model: posix gcc version 4.6.2 20111027 (Red Hat 4.6.2-1) (GCC)