[Mesa-dev] [PATCH 1/2] mesa: add signbit() macro

Jose Fonseca jfonseca at vmware.com
Mon Sep 24 14:28:21 PDT 2012



----- Original Message -----
> On 09/24/2012 02:53 PM, Jose Fonseca wrote:
> >> From signbit manpage:
> >
> >    This is not the same as x<  0.0, because IEEE 754 floating point
> >    allows zero to be signed.   The  comparison  -0.0<   0.0  is
> >       false, but signbit(-0.0) will return a nonzero value.
> >
> > I think that for consistency (and especially because MSVC ends not
> > being as widely tested as gcc), we should match the spec. This can
> > easily be done using an inline and an union.
> 
> The DIFFERENT_SIGNS and IS_NEGATIVE macro are only used in the tnl
> clipping code and I don't think the signbit() of 0 vs -0 would make
> any real difference in the end.
> 
> But I see your point.  Who knows where signbit might be used in the
> future and consistency is good.  The question then is how closely do
> we want to follow Linux's example?  The code in math.h checks
> sizeof(x) to handle float and double (and long double) specially:
> 
> /* Return nonzero value if sign of X is negative.  */
> # ifdef __NO_LONG_DOUBLE_MATH
> #  define signbit(x) \
>       (sizeof (x) == sizeof (float) ? __signbitf (x) : __signbit (x))
> # else
> #  define signbit(x) \
>       (sizeof (x) == sizeof (float)					      \
>        ? __signbitf (x)							      \
>        : sizeof (x) == sizeof (double)					      \
>        ? __signbit (x) : __signbitl (x))
> # endif

I see, it's polymorphic macro, that works with all float types...

One MSVC implement one can often see in google is 

  #  define signbit(x) (_copysign(1.0, (x)))

floats are converted to double so it is correct for at least float and doubles (and I don't think we'll ever need to care for long doubles). I'm not sure if it is slower for single precision due to the double converstion though...

Jose


More information about the mesa-dev mailing list