This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/23793] Unhealthy optimization. Accessing double with reinterpret_cast.


------- 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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]