Boost logo

Boost :

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


Martin Bonner wrote:

> That is not to say that the bit test via mask is not potentially useful, just that one should be aware of its limitations.
>

It could be useful on some platforms that do not have a predefined
signbit macro/function.

The problem we are running into is:
How do you convert a value of type S to a value of type T,
so that the bitpattern is preserved?
(It is of course assumed that sizeof(S) == sizeof(T).)
Maybe this task should be put in a separate template,
something like

     template<class T, class S> T binary_cast(const& S s);

Gennaro Prota suggests implementing this using
reinterpret_cast<const volatile byte*>.

Then the signbit test could be implemented, for some platforms, as follows:

   const std::bitset<sizeof(float)> signbit_mask
     = binary_cast<std::bitset<sizeof(float)> >(1.0f)
     ^ binary_cast<std::bitset<sizeof(float)> >(-1.0f);

   bool signbit(float x)
   {
     return (binary_cast<std::bitset<sizeof(float)> >(x)
       & signbit_mask) == signbit_mask;
   }

This can be templatized, and will handle such things as 80-bit long double.

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