Boost logo

Boost :

Subject: Re: [boost] Bits and Ints: Intention Request
From: Tim Blechmann (tim_at_[hidden])
Date: 2010-07-14 16:29:56


hi,

> - *Count Leading Zeros*

as for:

template <typename T>
inline int count_leading_zeros(T value)
{
#ifndef BOOST_HAS_NO_INT64_T
        return __builtin_clzll(value) - (64 - (sizeof(T) << 3));
#else
        return __builtin_clz(value) - (32 - (sizeof(T) << 3));
#endif
}

__builtin_clz comes in different versions for unsigned int, unsigned long
and unsigned long long. i think the safest way to make use of the correct
version is by using template specialization ... maybe similar to [1] ... i
am also not sure, how it will behave for signed types ...

but it is great to see an ilog2 implementation ...

cheers, tim

[1]
http://tim.klingt.org/git?p=boost_heap.git;a=blob;f=boost/heap/detail/ilog2.hpp

-- 
tim_at_[hidden]
http://tim.klingt.org
Cheat your landlord if you can and must, but do not try to shortchange
the Muse. It cannot be done. You can't fake quality any more than you
can fake a good meal.
  William S. Burroughs

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