1 /* boost random/detail/integer_log2.hpp header file
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)
8 * See http://www.boost.org for most recent version including documentation.
14 #ifndef BOOST_RANDOM_DETAIL_INTEGER_LOG2_HPP
15 #define BOOST_RANDOM_DETAIL_INTEGER_LOG2_HPP
17 #include <boost/config.hpp>
18 #include <boost/limits.hpp>
19 #include <boost/pending/integer_log2.hpp>
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__))
32 #define BOOST_RANDOM_DETAIL_CONSTEXPR inline
36 struct integer_log2_impl
38 #if defined(BOOST_NO_CXX11_CONSTEXPR)
40 BOOST_RANDOM_DETAIL_CONSTEXPR static int apply(T t, int accum)
42 int update = ((t >> Shift) != 0) * Shift;
43 return integer_log2_impl<Shift / 2>::apply(t >> update, accum + update);
47 BOOST_RANDOM_DETAIL_CONSTEXPR static int apply2(T t, int accum, int update)
49 return integer_log2_impl<Shift / 2>::apply(t >> update, accum + update);
53 BOOST_RANDOM_DETAIL_CONSTEXPR static int apply(T t, int accum)
55 return apply2(t, accum, ((t >> Shift) != 0) * Shift);
61 struct integer_log2_impl<1>
64 BOOST_RANDOM_DETAIL_CONSTEXPR static int apply(T t, int accum)
66 return int(t >> 1) + accum;
71 BOOST_RANDOM_DETAIL_CONSTEXPR int integer_log2(T t)
73 return integer_log2_impl<
74 ::boost::detail::max_pow2_less<
75 ::std::numeric_limits<T>::digits, 4
84 #endif // BOOST_RANDOM_DETAIL_INTEGER_LOG2_HPP