[Bug c++/20269] New: Optimizer problem with aliasing floating point variable
gcc -v output: Reading specs from /opt/gcc-3.4/lib/gcc/i686-pc-linux-gnu/3.4.3/specs Configured with: ./configure --prefix=/opt/gcc-3.4 --enable-languages=c,c++ Thread model: posix gcc version 3.4.3 See following preprocessed output: extern "C" { extern void __assert_fail (__const char *__assertion, __const char *__file, unsigned int __line, __const char *__function) throw () __attribute__ ((__noreturn__)); } static void DoTest() { double doubleOne = 1.0; float floatOne = 1.0F; unsigned int *intDptr = (unsigned int *) &doubleOne; unsigned int *intFptr = (unsigned int *) &floatOne; (static_cast (__builtin_expect (!!((intDptr[1] == 0x3ff0) && (intDptr[0] == 0)), 1)? 0 : (__assert_fail ("(intDptr[1] == 0x3ff0) && (intDptr[0] == 0)", "fptest.cpp", 12, __PRETTY_FUNCTION__), 0))); (static_cast (__builtin_expect (!!(*intFptr == 0x3f80), 1) ? 0 : (__assert_fail ("*intFptr == 0x3f80", "fptest.cpp", 13, __PRETTY_FUNCTION__), 0))); } int main() { DoTest(); } This compiles fine up to -O1, but with -O2 the test fails. When looking at the generated assembler it seems to me that the compiler first issues the compare before putting a value in it. Generated assembler: gcc -O2 -S -o fptest.S fptest.cpp main: .LFB3: pushl %ebp .LCFI0: fld1 movl%esp, %ebp .LCFI1: subl$24, %esp .LCFI2: andl$-16, %esp subl$16, %esp cmpl$1072693248, -4(%ebp) fstpl -8(%ebp) jne .L4 movl-8(%ebp), %eax testl %eax, %eax jne .L4 leave xorl%eax, %eax ret .p2align 4,,7 .L4: movl$.LC2, (%esp) movl$_ZZ6DoTestvE19__PRETTY_FUNCTION__, %ecx movl$12, %edx movl%ecx, 12(%esp) movl$.LC1, %eax movl%edx, 8(%esp) movl%eax, 4(%esp) call__assert_fail Specifying volatile for all the local variables fixes it. -- Summary: Optimizer problem with aliasing floating point variable Product: gcc Version: 3.4.3 Status: UNCONFIRMED Severity: normal Priority: P2 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: bobm75 at gmail dot com CC: gcc-bugs at gcc dot gnu dot org GCC build triplet: i686-pc-linux-gnu GCC host triplet: i686-pc-linux-gnu GCC target triplet: i686-pc-linux-gnu http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20269
[Bug c++/20269] Optimizer problem with aliasing floating point variable
--- Additional Comments From bobm75 at gmail dot com 2005-03-02 00:33 --- I read the documentation on -fstrict-aliasing and it makes sense to me why this code breaks those rules. I just wonder why the compiler didn't warn about it, since this seems to be a pretty straight forward case of aliasing happening. Thanks for responding so quickly. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20269
[Bug c++/20269] Optimizer problem with aliasing floating point variable
--- Additional Comments From bobm75 at gmail dot com 2005-03-02 18:03 --- Ok, thank you for your explanation. We have a huge codebase and we just increased the optimization level from -O to -O2 (together with -march=pentium3), that's when this came up. Since most of this code has been around for a long time (and not written by me) there is no way to guarantee that I caught all these cases. So I guess I'll just have to disable the strict aliasing option for now. Thanks for all your help. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20269
[Bug c++/19869] New: Allow to non-friend class to get access to function pointer
Reading specs from /opt/gcc-3.3/lib/gcc-lib/i686-pc-linux-gnu/3.3.6/specs Configured with: ../configure --prefix=/opt/gcc-3.3 --enable-languages=c,c++ Thread model: posix gcc version 3.3.6 20050209 (prerelease) The following compiles without errors on a vanilla build of gcc 3.3.5 and gcc 3.3.6. The 3.4 and 4.0 builds both correctly give an error while compiling this. typedef void (*MyCall)(int j); class P { private: MyCall myCall; }; class J { public: J(P* p) { p->myCall(5); } }; -- Summary: Allow to non-friend class to get access to function pointer Product: gcc Version: 3.3.6 Status: UNCONFIRMED Severity: normal Priority: P2 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: bobm75 at gmail dot com CC: gcc-bugs at gcc dot gnu dot org GCC build triplet: i686-pc-linux GCC host triplet: i686-pc-linux GCC target triplet: i686-pc-linux http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19869