Bug 23961 - code that compiles and runs correctly with no optimization but code optimizes to nothing.
Summary: code that compiles and runs correctly with no optimization but code optimizes...
Status: RESOLVED DUPLICATE of bug 21920
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.4.4
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-09-19 07:01 UTC by charles harris
Modified: 2005-09-19 07:09 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description charles harris 2005-09-19 07:01:01 UTC
Compiler version:

Reading specs from /usr/lib/gcc/i386-redhat-linux/3.4.4/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-java-awt=gtk --host=i386-redhat-linux
Thread model: posix
gcc version 3.4.4 20050721 (Red Hat 3.4.4-2)

System:

Linux tethys 2.6.12-1.1372_FC3 #1 Fri Jul 15 00:59:10 EDT 2005 i686 athlon i386
GNU/Linux


Code that illustrates the problem

#include <cstdio>

double ulongToDouble(unsigned long lval)
{
    double  work = 1.0;
    ((long *)&work)[1] |= lval & 0x000fffff;
    ((long *)&work)[0] |= lval & 0xfff00000 | 0x00080000;
    return work - 1.0;
}

int main(int argc, char **args)
{
    printf("%27.20f \n", ulongToDouble(0xffffffffUL));
    printf("%27.20f \n", ulongToDouble(0));
    return 1;
}

Unoptimized, correct output:

$[charris@tethys workspace]$ g++ -Wall bug.cpp
$[charris@tethys workspace]$ ./a.out
     0.99999999988358467817
     0.00000000011641532183

Optimized:

$[charris@tethys workspace]$ g++ -Wall -O2 bug.cpp
$[charris@tethys workspace]$ ./a.out
     0.00000000000000000000
     0.00000000000000000000

Do you really want the bug.ii file? It is rather lengthy and I don't see how to
attach it.

Chuck
Comment 1 Andrew Pinski 2005-09-19 07:09:21 UTC
No because you are violating C/C++ aliasing rules.
See http://gcc.gnu.org/bugs.html
"Casting does not work as expected when optimization is turned on."


It applies to both C and C++.

Yes -Wstrict-aliasing should produce a warning but that is PR 14024.

*** This bug has been marked as a duplicate of 21920 ***