blob: f7429af2143dd3ed2c89bc73a94d9b2f9963e2b5 [file] [log] [blame]
Jeff Thompson86b6d642013-10-17 15:01:56 -07001/* 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: integer_log2.hpp 83381 2013-03-09 22:55:05Z eric_niebler $
11 *
12 */
13
14#ifndef NDNBOOST_RANDOM_DETAIL_INTEGER_LOG2_HPP
15#define NDNBOOST_RANDOM_DETAIL_INTEGER_LOG2_HPP
16
17#include <ndnboost/config.hpp>
18#include <ndnboost/limits.hpp>
19#include <ndnboost/pending/integer_log2.hpp>
20
21namespace ndnboost {
22namespace random {
23namespace detail {
24
25#if !defined(NDNBOOST_NO_CXX11_CONSTEXPR)
26#define NDNBOOST_RANDOM_DETAIL_CONSTEXPR constexpr
27#elif defined(NDNBOOST_MSVC)
28#define NDNBOOST_RANDOM_DETAIL_CONSTEXPR __forceinline
29#elif defined(__GNUC__) && __GNUC__ >= 4
30#define NDNBOOST_RANDOM_DETAIL_CONSTEXPR inline __attribute__((const)) __attribute__((always_inline))
31#else
32#define NDNBOOST_RANDOM_DETAIL_CONSTEXPR inline
33#endif
34
35template<int Shift>
36struct integer_log2_impl
37{
38#if defined(NDNBOOST_NO_CXX11_CONSTEXPR)
39 template<class T>
40 NDNBOOST_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 NDNBOOST_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 NDNBOOST_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 NDNBOOST_RANDOM_DETAIL_CONSTEXPR static int apply(T t, int accum)
65 {
66 return int(t >> 1) + accum;
67 }
68};
69
70template<class T>
71NDNBOOST_RANDOM_DETAIL_CONSTEXPR int integer_log2(T t)
72{
73 return integer_log2_impl<
74 ::ndnboost::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 ndnboost
83
84#endif // NDNBOOST_RANDOM_DETAIL_INTEGER_LOG2_HPP