public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/51821] New: 64bit > 32bit conversion produces incorrect results with optimizations
@ 2012-01-11 11:02 nos.utelsystems at gmail dot com
  2012-01-11 11:21 ` [Bug c/51821] [4.5/4.6/4.7 Regression] " rguenth at gcc dot gnu.org
                   ` (22 more replies)
  0 siblings, 23 replies; 24+ messages in thread
From: nos.utelsystems at gmail dot com @ 2012-01-11 11:02 UTC (permalink / raw)
  To: gcc-bugs

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)


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

end of thread, other threads:[~2012-01-15 20:44 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-01-11 11:02 [Bug c/51821] New: 64bit > 32bit conversion produces incorrect results with optimizations nos.utelsystems at gmail dot com
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

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