1/* boost random/detail/integer_log2.hpp header file
2 *
3 * Copyright Steven Watanabe 2011
4 * Distributed under the Boost Software License, Version 1.0. (See
5 * accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
7 *
8 * See http://www.boost.org for most recent version including documentation.
9 *
10 * $Id$
11 *
12 */
13
14#ifndef BOOST_RANDOM_DETAIL_INTEGER_LOG2_HPP
15#define BOOST_RANDOM_DETAIL_INTEGER_LOG2_HPP
16
17#include <boost/config.hpp>
18#include <boost/limits.hpp>
19#include <boost/pending/integer_log2.hpp>
20
21namespace boost {
22namespace random {
23namespace detail {
24
25#if !defined(BOOST_NO_CXX11_CONSTEXPR)
26#define BOOST_RANDOM_DETAIL_CONSTEXPR constexpr
27#elif defined(BOOST_MSVC)
28#define BOOST_RANDOM_DETAIL_CONSTEXPR __forceinline
29#elif defined(__GNUC__) && __GNUC__ >= 4
30#define BOOST_RANDOM_DETAIL_CONSTEXPR inline __attribute__((__const__)) __attribute__((__always_inline__))
31#else
32#define BOOST_RANDOM_DETAIL_CONSTEXPR inline
33#endif
34
35template<int Shift>
36struct integer_log2_impl
37{
38#if defined(BOOST_NO_CXX11_CONSTEXPR)
39 template<class T>
40 BOOST_RANDOM_DETAIL_CONSTEXPR static int apply(T t, int accum)
41 {
42 int update = ((t >> Shift) != 0) * Shift;
43 return integer_log2_impl<Shift / 2>::apply(t >> update, accum + update);
44 }
45#else
46 template<class T>
47 BOOST_RANDOM_DETAIL_CONSTEXPR static int apply2(T t, int accum, int update)
48 {
49 return integer_log2_impl<Shift / 2>::apply(t >> update, accum + update);
50 }
51
52 template<class T>
53 BOOST_RANDOM_DETAIL_CONSTEXPR static int apply(T t, int accum)
54 {
55 return apply2(t, accum, ((t >> Shift) != 0) * Shift);
56 }
57#endif
58};
59
60template<>
61struct integer_log2_impl<1>
62{
63 template<class T>
64 BOOST_RANDOM_DETAIL_CONSTEXPR static int apply(T t, int accum)
65 {
66 return int(t >> 1) + accum;
67 }
68};
69
70template<class T>
71BOOST_RANDOM_DETAIL_CONSTEXPR int integer_log2(T t)
72{
73 return integer_log2_impl<
74 ::boost::detail::max_pow2_less<
75 ::std::numeric_limits<T>::digits, 4
76 >::value
77 >::apply(t, 0);
78}
79
80} // namespace detail
81} // namespace random
82} // namespace boost
83
84#endif // BOOST_RANDOM_DETAIL_INTEGER_LOG2_HPP
85