Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:51:09

0001 /* boost random/detail/integer_log2.hpp header file
0002  *
0003  * Copyright Steven Watanabe 2011
0004  * Distributed under the Boost Software License, Version 1.0. (See
0005  * accompanying file LICENSE_1_0.txt or copy at
0006  * http://www.boost.org/LICENSE_1_0.txt)
0007  *
0008  * See http://www.boost.org for most recent version including documentation.
0009  *
0010  * $Id$
0011  *
0012  */
0013 
0014 #ifndef BOOST_RANDOM_DETAIL_INTEGER_LOG2_HPP
0015 #define BOOST_RANDOM_DETAIL_INTEGER_LOG2_HPP
0016 
0017 #include <boost/config.hpp>
0018 #include <boost/limits.hpp>
0019 #include <boost/integer/integer_log2.hpp>
0020 
0021 namespace boost {
0022 namespace random {
0023 namespace detail {
0024 
0025 #if !defined(BOOST_NO_CXX11_CONSTEXPR)
0026 #define BOOST_RANDOM_DETAIL_CONSTEXPR constexpr
0027 #elif defined(BOOST_MSVC)
0028 #define BOOST_RANDOM_DETAIL_CONSTEXPR __forceinline
0029 #elif defined(__GNUC__) && __GNUC__ >= 4
0030 #define BOOST_RANDOM_DETAIL_CONSTEXPR inline __attribute__((__const__)) __attribute__((__always_inline__))
0031 #else
0032 #define BOOST_RANDOM_DETAIL_CONSTEXPR inline
0033 #endif
0034 
0035 template<int Shift>
0036 struct integer_log2_impl
0037 {
0038 #if defined(BOOST_NO_CXX11_CONSTEXPR)
0039     template<class T>
0040     BOOST_RANDOM_DETAIL_CONSTEXPR static int apply(T t, int accum)
0041     {
0042         int update = ((t >> Shift) != 0) * Shift;
0043         return integer_log2_impl<Shift / 2>::apply(t >> update, accum + update);
0044     }
0045 #else
0046     template<class T>
0047     BOOST_RANDOM_DETAIL_CONSTEXPR static int apply2(T t, int accum, int update)
0048     {
0049         return integer_log2_impl<Shift / 2>::apply(t >> update, accum + update);
0050     }
0051 
0052     template<class T>
0053     BOOST_RANDOM_DETAIL_CONSTEXPR static int apply(T t, int accum)
0054     {
0055         return apply2(t, accum, ((t >> Shift) != 0) * Shift);
0056     }
0057 #endif
0058 };
0059 
0060 template<>
0061 struct integer_log2_impl<1>
0062 {
0063     template<class T>
0064     BOOST_RANDOM_DETAIL_CONSTEXPR static int apply(T t, int accum)
0065     {
0066         return int(t >> 1) + accum;
0067     }
0068 };
0069 
0070 template<class T>
0071 BOOST_RANDOM_DETAIL_CONSTEXPR int integer_log2(T t)
0072 {
0073     return integer_log2_impl<
0074         ::boost::detail::max_pow2_less<
0075             ::std::numeric_limits<T>::digits, 4
0076         >::value
0077     >::apply(t, 0);
0078 }
0079 
0080 } // namespace detail
0081 } // namespace random
0082 } // namespace boost
0083 
0084 #endif // BOOST_RANDOM_DETAIL_INTEGER_LOG2_HPP