This is GCC Bugzilla
This is GCC Bugzilla Version 2.20+
View Bug Activity | Format For Printing | Clone This Bug
GCC version: g++ -v Using built-in specs. Target: i686-pc-linux-gnu Configured with: ../gcc-4.0.2/configure --prefix=/home/source5/rootdir-gcc402 --enable-languages=c,c++ --enable-concept-checks Thread model: posix gcc version 4.0.2 Sample code that reproduces the bug: #include <iostream> #include <stdint.h> // From glibc #define pa_bswap_64(x) \ ((((x) & 0xff00000000000000ull) >> 56) \ | (((x) & 0x00ff000000000000ull) >> 40) \ | (((x) & 0x0000ff0000000000ull) >> 24) \ | (((x) & 0x000000ff00000000ull) >> 8) \ | (((x) & 0x00000000ff000000ull) << 8) \ | (((x) & 0x0000000000ff0000ull) << 24) \ | (((x) & 0x000000000000ff00ull) << 40) \ | (((x) & 0x00000000000000ffull) << 56)) int main() { double d = 42.42; uint64_t conv = *((uint64_t*) &d); conv = pa_bswap_64(conv); uint64_t back = pa_bswap_64(conv); double d2 = *((double*)&back); std::cout << "d = " << d << " d2 = " << d2 << std::endl << "conv = " << conv << " back = " << back << std::endl; } When compiled without optimization, I get: source5@cube tmp $ /home/source5/rootdir-gcc402/bin/g++ -Wall -o swap swap.cc source5@cube tmp $ ./swap d = 42.42 d2 = 42.42 conv = 17737528904907048256 back = 4631166901565532406 When compiled with optimization, I get: source5@cube tmp $ /home/source5/rootdir-gcc402/bin/g++ -Wall -O2 -o swap swap.cc source5@cube tmp $ ./swap d = 42.42 d2 = 10.3501 conv = 17626847325199074312 back = 577737629976797172
You are violating C++ aliasing rules: uint64_t back = pa_bswap_64(conv); double d2 = *((double*)&back); *** This bug has been marked as a duplicate of 21920 ***
Thanks for the info! I can understand the examples in the article at http://mail-index.netbsd.org/tech-kern/2003/08/11/0001.html but with my example source code in this bug report, I can't seem to follow the optimization path that results in what I'm seeing. I understand that the optimizer can ignore duplicated code, or unnecessary code, but conv and d2 need to be initialized somehow, and if the compiler doesn't use the code that's there, what does it use? A warning would be really nice.
(In reply to comment #2) > A warning would be really nice. The warning was filed as PR 14024 and fixed for 4.2.0.
Just adding a link to a comp.lang.c++.moderated discussion on this, for future reference, when other folks run into this again. Subject line: alias rules and optimization http://groups.google.ca/group/comp.lang.c++.moderated/browse_thread/thread/e7bf096832526f8e/5714701b02a2a3cc?hl=en#5714701b02a2a3cc