[Bug c++/27533] wrong result after reinterpret_cast from float* to int*
pluto at agmk dot net
gcc-bugzilla@gcc.gnu.org
Wed May 10 15:26:00 GMT 2006
------- 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_cast<int*>(&a)) -
> *(reinterpret_cast<int*>(&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 ;)
00000000 <almost_equal(float, float)>:
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 83 ec 10 sub $0x10,%esp
6: 8b 45 08 mov 0x8(%ebp),%eax <===
9: 2b 45 0c sub 0xc(%ebp),%eax <===
c: c9 leave
d: c3 ret
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27533
More information about the Gcc-bugs
mailing list