This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/51821] New: 64bit > 32bit conversion produces incorrect results with optimizations
- From: "nos.utelsystems at gmail dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Wed, 11 Jan 2012 11:01:43 +0000
- Subject: [Bug c/51821] New: 64bit > 32bit conversion produces incorrect results with optimizations
- Auto-submitted: auto-generated
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)