Boost logo

Boost :

From: Daryle Walker (darylew_at_[hidden])
Date: 2004-04-12 01:06:19


On 4/11/04 10:34 PM, "David Abrahams" <dave_at_[hidden]> wrote:

> Gennaro Prota <gennaro_prota_at_[hidden]> writes:
>
>>>> [two new header files:]
>>>>
>>>> a) http://tinyurl.com/36zsj
>>>> b) http://tinyurl.com/2lb26
>>>>
>>>> I think they provide two useful facilities in themselves. Would it be
>>>> ok to commit them in the boost/ folder? Or should they belong to
>>>> boost/detail/? Or maybe boost/pending/?
>>>
>>> If they're going into boost/, they need to be documented. THat's the
>>> only constraint, IMO.
>>
>> Oh, no problem for that. But where the docs go? I mean: they are not a
>> "library" in themselves; and "utility" seems too much an easy
>> collector for everything :)
>
> These look to me like they should be part of
> http://www.boost.org/libs/integer/index.html, no?

That seems like an OK place. Don't forget to update <boost/integer_fwd.hpp>
too.

>> PS: should I also ask for a mini-review?

Probably.

> Or at least consult Darlye Walker.

Question: are these functions really meant for any integer type, including
user-defined ones? Or are they just for the built-in types? If the latter,
couldn't you skip the templating by using the largest built-in type?
(Promotions will handle the smaller built-in types.)

int
integer_log2( uintmax_t x )
{
    int result = 0;

    for ( int n = std::numeric_limits<uintmax_t>::digits - 1
     ; x > 1u ; n /= 2 )
    {
        if ( uintmax_t const t = x >> n )
        {
            result += n;
            x = t;
        }
    }

    return result;
}

int
lowest_bit( uintmax_t x )
{
    return integer_log2( x - (x & ( x - 1 )) );
}

Maybe the two functions could share the same header.

We don't need negative "x" values, since they don't have a defined (real)
logarithm, and the bitwise operations may have strange results for them. We
give a junk return for a zero-value argument instead of crashing. We could
mention that using zero is undefined (just like negative values, but we
can't get rid of it by switching to an unsigned type).

Why does the "lowest_bit" function work? I can't see it.

-- 
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT hotmail DOT com

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