This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/23793] Unhealthy optimization. Accessing double with reinterpret_cast.
- From: "falk at debian dot org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 9 Sep 2005 10:07:40 -0000
- Subject: [Bug c++/23793] Unhealthy optimization. Accessing double with reinterpret_cast.
- References: <20050909075051.23793.martsummsw@hotmail.com>
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
------- Additional Comments From falk at debian dot org 2005-09-09 10:07 -------
(In reply to comment #0)
> There are many reasons. (One and the best is to switch a double based on
> intervals) Therefore I would like a VERY FAST FUNCTION to return the sign of a
> double (and float and long double) ...
>
> (And compare on a double d<=-0.0 (without branch) wont do the trick. And I can't
> blame you because you will have to respect Nan.
>
> Since c/c++ does not have this fast function (skipping Nan)
> (which I hope will come) I have no other no other options that to
> write it myself (and cheat!)
C99 has this function; it's called signbit. So you should use that.
Newer gcc's also have a __builtin_signbit function.
> The code with the bug is : (Read signbit a push it to be one or zero)
> int is_not_positive(double v)
> {
> return ((reinterpret_cast<unsigned int*>(&v)[1]) >> 31);
> }
This code has undefined behavior, since it violates aliasing rules.
You should use a union, if you absolutely have to do it this way.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23793