1 // -----------------------------------------------------------
4 // Gives the integer part of the logarithm, in base 2, of a
5 // given number. Behavior is undefined if the argument is <= 0.
7 // Copyright (c) 2003-2004, 2008 Gennaro Prota
9 // Distributed under the Boost Software License, Version 1.0.
10 // (See accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
13 // -----------------------------------------------------------
15 #ifndef BOOST_INTEGER_LOG2_HPP_GP_20030301
16 #define BOOST_INTEGER_LOG2_HPP_GP_20030301
22 #include "boost/limits.hpp"
23 #include "boost/config.hpp"
30 int integer_log2_impl(T x
, int n
) {
36 const T t
= static_cast<T
>(x
>> n
);
50 // helper to find the maximum power of two
51 // less than p (more involved than necessary,
54 template <int p
, int n
>
55 struct max_pow2_less
{
59 BOOST_STATIC_CONSTANT(int, value
=
60 c
? (max_pow2_less
< c
*p
, 2*c
*n
>::value
) : n
);
65 struct max_pow2_less
<0, 0> {
67 BOOST_STATIC_CONSTANT(int, value
= 0);
70 // this template is here just for Borland :(
71 // we could simply rely on numeric_limits but sometimes
72 // Borland tries to use numeric_limits<const T>, because
73 // of its usual const-related problems in argument deduction
79 BOOST_STATIC_CONSTANT(int, value
= sizeof(T
) * CHAR_BIT
);
81 BOOST_STATIC_CONSTANT(int, value
= (std::numeric_limits
<T
>::digits
));
94 int integer_log2(T x
) {
98 const int n
= detail::max_pow2_less
<
99 detail::width
<T
> :: value
, 4
102 return detail::integer_log2_impl(x
, n
);
112 #endif // include guard