/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. #ifndef BOOST_RANDOM_DETAIL_INTEGER_LOG2_HPP
  14. #define BOOST_RANDOM_DETAIL_INTEGER_LOG2_HPP
  15. #include <boost/config.hpp>
  16. #include <boost/limits.hpp>
  17. #include <boost/pending/integer_log2.hpp>
  18. namespace boost {
  19. namespace random {
  20. namespace detail {
  21. // Daniel James: Disabled use of constexpr because integer_log2_impl is not a
  22. // valid constexpr.
  23. #if 0 && !defined(BOOST_NO_CONSTEXPR)
  24. #define BOOST_RANDOM_DETAIL_CONSTEXPR constexpr
  25. #elif defined(BOOST_MSVC)
  26. #define BOOST_RANDOM_DETAIL_CONSTEXPR __forceinline
  27. #elif defined(__GNUC__) && __GNUC__ >= 4
  28. #define BOOST_RANDOM_DETAIL_CONSTEXPR __attribute__((const)) __attribute__((always_inline))
  29. #else
  30. #define BOOST_RANDOM_DETAIL_CONSTEXPR inline
  31. #endif
  32. template<int Shift>
  33. struct integer_log2_impl
  34. {
  35. template<class T>
  36. BOOST_RANDOM_DETAIL_CONSTEXPR static int apply(T t, int accum)
  37. {
  38. int update = ((t >> Shift) != 0) * Shift;
  39. return integer_log2_impl<Shift / 2>::apply(t >> update, accum + update);
  40. }
  41. };
  42. template<>
  43. struct integer_log2_impl<1>
  44. {
  45. template<class T>
  46. BOOST_RANDOM_DETAIL_CONSTEXPR static int apply(T t, int accum)
  47. {
  48. return int(t >> 1) + accum;
  49. }
  50. };
  51. template<class T>
  52. BOOST_RANDOM_DETAIL_CONSTEXPR int integer_log2(T t)
  53. {
  54. return integer_log2_impl<
  55. ::boost::detail::max_pow2_less<
  56. ::std::numeric_limits<T>::digits, 4
  57. >::value
  58. >::apply(t, 0);
  59. }
  60. } // namespace detail
  61. } // namespace random
  62. } // namespace boost
  63. #endif // BOOST_RANDOM_DETAIL_INTEGER_LOG2_HPP