Boost logo

Boost :

From: Gennaro Prota (gennaro_prota_at_[hidden])
Date: 2006-08-03 10:59:25


On Thu, 3 Aug 2006 11:35:19 +0100, "John Maddock"
<john_at_[hidden]> wrote:

>And the endianness of the float's if you're doing bit testing.

Which shouldn't be a problem for you ;-) Or just position/bitmask of
the sign bit.

>But of course if there is a macro already it's easy enough to convert it into a
>function.

Yep. Even if no macro exists you can perhaps hide the
machine-dependent stuff in one single "low-level primitive" function
upon which you can build all (or much of) the rest. I'm thinking to
something like

 // your namespace...
 enum type { finite, zero, nan, inf };
 
 template <typename T>
 type get_type(T t);

Also you could probably automatically generate much of the system
dependent stuff, for the most common systems. For instance:

 // widely (not completely) portable
 void calculate()
 {
    typedef float T; // for instance
    typedef unsigned char byte;

    T plus_1 ( 1 );
    T minus_1( -1 );

    const volatile byte * b_plus
        = reinterpret_cast<const volatile byte *>(&plus_1);
    const volatile byte * b_minus
        = reinterpret_cast<const volatile byte *>(&minus_1);

    int i = 0;
    int bit = 0;
    bool found = false;
    for (i = 0; i < sizeof( T ); ++i )
    {
        for (bit = 0; bit < CHAR_BIT; ++bit)
        {
            const int mask = 1 << bit;
            if( (b_plus[i] & mask)
                != (b_minus[i] & mask) )
            {
                assert(found == false);
                found = true;
            }
        }
    }

    assert(found == true);

    std::cout << "sign bit at byte " << (i-1)
                <<", bit " << (bit-1) << '\n'
        ;

 }

Ok, that will never compile but should give the idea. If any of the
asserts triggers just pretend nothing happened, calmly shut the
computer down and do not walk... run!

--
[ Gennaro Prota, C++ developer for hire ]

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