[Bug c++/27533] wrong result after reinterpret_cast from float* to int*
--- Comment #1 from pluto at agmk dot net 2006-05-10 14:51 --- you're violating the aliasing rules, so use -fno-strict-aliasing option. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27533
[Bug c++/27533] wrong result after reinterpret_cast from float* to int*
--- Comment #2 from ulrich dot lauther at siemens dot com 2006-05-10 14:53 --- Created an attachment (id=11434) -- (http://gcc.gnu.org/bugzilla/attachment.cgi?id=11434action=view) Complete tiny example exposing the problem I submit this version that includes stdio.h for readability, expanded version will follow -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27533
[Bug c++/27533] wrong result after reinterpret_cast from float* to int*
--- Comment #3 from ulrich dot lauther at siemens dot com 2006-05-10 14:55 --- Created an attachment (id=11435) -- (http://gcc.gnu.org/bugzilla/attachment.cgi?id=11435action=view) same as before, but stdio.h expanded -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27533
[Bug c++/27533] wrong result after reinterpret_cast from float* to int*
--- Comment #4 from reichelt at gcc dot gnu dot org 2006-05-10 14:59 --- As Pawel already pointed out: Your code is brokan as you are violating the aliasing rules. Please read about this in the non-bug section of http://gcc.gnu.org/bugs.html : Casting does not work as expected when optimization is turned on. *** This bug has been marked as a duplicate of 21920 *** -- reichelt at gcc dot gnu dot org changed: What|Removed |Added Status|UNCONFIRMED |RESOLVED Resolution||DUPLICATE http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27533
[Bug c++/27533] wrong result after reinterpret_cast from float* to int*
--- Comment #5 from pluto at agmk dot net 2006-05-10 15:26 --- (In reply to comment #0) The code inline int almost_equal(float a, float b, int maxUlps = 16) { int intDiff = *(reinterpret_castint*(a)) - *(reinterpret_castint*(b)); if you really need such casting and still use strict-aliasing you should rewrite almost_equal(). /*inline*/ int almost_equal( float a, float b ) { int ia; memcpy( ia, a, sizeof(int) ); int ib; memcpy( ib, b, sizeof(int) ); return ( ia - ib ); } compiler will optimize it nicely ;) almost_equal(float, float): 0: 55 push %ebp 1: 89 e5 mov%esp,%ebp 3: 83 ec 10sub$0x10,%esp 6: 8b 45 08mov0x8(%ebp),%eax=== 9: 2b 45 0csub0xc(%ebp),%eax=== c: c9 leave d: c3 ret -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27533