PageRenderTime 26ms CodeModel.GetById 18ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/random/detail/integer_log2.hpp

http://hadesmem.googlecode.com/
C++ Header | 72 lines | 49 code | 9 blank | 14 comment | 3 complexity | 3003967f341e362460ad8d35a4482e4e MD5 | raw file
 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: integer_log2.hpp 72861 2011-07-02 20:26:19Z danieljames $
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// Daniel James: Disabled use of constexpr because integer_log2_impl is not a
26// valid constexpr.
27#if 0 && !defined(BOOST_NO_CONSTEXPR)
28#define BOOST_RANDOM_DETAIL_CONSTEXPR constexpr
29#elif defined(BOOST_MSVC)
30#define BOOST_RANDOM_DETAIL_CONSTEXPR __forceinline
31#elif defined(__GNUC__) && __GNUC__ >= 4
32#define BOOST_RANDOM_DETAIL_CONSTEXPR __attribute__((const)) __attribute__((always_inline))
33#else
34#define BOOST_RANDOM_DETAIL_CONSTEXPR inline
35#endif
36
37template<int Shift>
38struct integer_log2_impl
39{
40    template<class T>
41    BOOST_RANDOM_DETAIL_CONSTEXPR static int apply(T t, int accum)
42    {
43        int update = ((t >> Shift) != 0) * Shift;
44        return integer_log2_impl<Shift / 2>::apply(t >> update, accum + update);
45    }
46};
47
48template<>
49struct integer_log2_impl<1>
50{
51    template<class T>
52    BOOST_RANDOM_DETAIL_CONSTEXPR static int apply(T t, int accum)
53    {
54        return int(t >> 1) + accum;
55    }
56};
57
58template<class T>
59BOOST_RANDOM_DETAIL_CONSTEXPR int integer_log2(T t)
60{
61    return integer_log2_impl<
62        ::boost::detail::max_pow2_less<
63            ::std::numeric_limits<T>::digits, 4
64        >::value
65    >::apply(t, 0);
66}
67
68} // namespace detail
69} // namespace random
70} // namespace boost
71
72#endif // BOOST_RANDOM_DETAIL_INTEGER_LOG2_HPP