1 // boost heap: integer log2
3 // Copyright (C) 2010 Tim Blechmann
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 #ifndef BOOST_HEAP_DETAIL_ILOG2_HPP
10 #define BOOST_HEAP_DETAIL_ILOG2_HPP
12 #include <string> // std::size_t
18 template <typename IntType>
21 IntType operator()(IntType value)
24 while( (value >> l) > 1 )
32 struct log2<unsigned int>
34 unsigned int operator()(unsigned int value)
36 return sizeof(unsigned int)*8 - __builtin_clz(value - 1);
41 struct log2<unsigned long>
43 unsigned long operator()(unsigned long value)
45 return sizeof(unsigned long)*8 - __builtin_clzl(value - 1);
51 } /* namespace detail */
54 template <typename IntType>
55 IntType log2(IntType value)
57 detail::log2<IntType> fn;
61 } /* namespace heap */
62 } /* namespace boost */
64 #endif /* BOOST_HEAP_DETAIL_ILOG2_HPP */