public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "nos.utelsystems at gmail dot com" <gcc-bugzilla@gcc.gnu.org> 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 [thread overview] Message-ID: <bug-51821-4@http.gcc.gnu.org/bugzilla/> (raw) http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51821 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 <stdint.h> #include <stdio.h> #include <inttypes.h> uint32_t test(unsigned int shift_size) { uint64_t res = 0; res = ~res; res = 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 ‘main’: shiftbug.c:12:14: warning: unused parameter ‘argc’ [-Wunused-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 ‘main’: shiftbug.c:12:14: warning: unused parameter ‘argc’ [-Wunused-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 defined behavior, the program should give the same output with or without optimization. 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 expression, res = (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=gcc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/i686-redhat-linux/4.6.2/lto-wrapper Target: i686-redhat-linux Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-java-awt=gtk --disable-dssi --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 --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch=i686 --build=i686-redhat-linux Thread model: posix gcc version 4.6.2 20111027 (Red Hat 4.6.2-1) (GCC)
next reply other threads:[~2012-01-11 11:02 UTC|newest] Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top 2012-01-11 11:02 nos.utelsystems at gmail dot com [this message] 2012-01-11 11:21 ` [Bug c/51821] [4.5/4.6/4.7 Regression] " rguenth at gcc dot gnu.org 2012-01-11 11:28 ` [Bug target/51821] " rguenth at gcc dot gnu.org 2012-01-11 12:34 ` ubizjak at gmail dot com 2012-01-11 13:13 ` ubizjak at gmail dot com 2012-01-11 13:21 ` [Bug rtl-optimization/51821] " ubizjak at gmail dot com 2012-01-11 14:15 ` ubizjak at gmail dot com 2012-01-11 22:03 ` ebotcazou at gcc dot gnu.org 2012-01-11 22:35 ` ubizjak at gmail dot com 2012-01-11 22:57 ` ubizjak at gmail dot com 2012-01-12 7:44 ` ebotcazou at gcc dot gnu.org 2012-01-12 8:04 ` ubizjak at gmail dot com 2012-01-12 11:18 ` ebotcazou at gcc dot gnu.org 2012-01-12 11:35 ` ubizjak at gmail dot com 2012-01-12 11:42 ` ubizjak at gmail dot com 2012-01-12 13:20 ` ebotcazou at gcc dot gnu.org 2012-01-12 17:01 ` ubizjak at gmail dot com 2012-01-12 17:36 ` ubizjak at gmail dot com 2012-01-12 19:18 ` ubizjak at gmail dot com 2012-01-15 18:45 ` uros at gcc dot gnu.org 2012-01-15 20:02 ` uros at gcc dot gnu.org 2012-01-15 20:38 ` uros at gcc dot gnu.org 2012-01-15 21:01 ` uros at gcc dot gnu.org 2012-01-15 21:14 ` ubizjak at gmail dot com
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=bug-51821-4@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /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: linkBe 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).