Boost logo

Boost :

From: Johan Råde (rade_at_[hidden])
Date: 2006-08-04 06:02:02


John Maddock wrote:
> Johan Råde wrote:
>> This can be templatized, and will handle such things as 80-bit long
>> double.
>
> Maybe, don't forget the padding bytes that are often injected into such
> types (only an issue if the endianness put's the sign bit at the end of the
> 10-byte sequence rather than the beginning).
>
> John.
>
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
>

John,

I think the following is a reasonable compromise:

const boost::uint32_t signbit_mask
     = binary_cast<boost::uint32_t>(1.0f)
     ^ binary_cast<boost::uint32_t>(-1.0f);

inline bool signbit_impl(float x)
{
     return binary_cast<boost::uint32_t>(x) & signbit_mask;
}

inline bool signbit_impl(double x)
{
     return sign_bit(static_cast<float>(x));
}

inline bool signbit_impl(long double x)
{
     return sign_bit(static_cast<float>(x));
}

Here I assume that

1. IEEE 754 or some similar representation
2. sizeof(float) == 4
3. casts between different numeric types preserve the sign bits, also
for non-finite numbers.

That should cover a large number of platforms.

--Johan Råde


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk