2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2015-01-10 10:13:57 +00:00
|
|
|
Copyright (c) 2011-2015 Bolero MURAKAMI
|
2013-08-08 09:54:33 +00:00
|
|
|
https://github.com/bolero-MURAKAMI/Sprout
|
|
|
|
|
|
|
|
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
=============================================================================*/
|
2011-10-18 13:28:32 +00:00
|
|
|
#ifndef SPROUT_DETAIL_INTEGER_STATIC_LOG2_HPP
|
|
|
|
#define SPROUT_DETAIL_INTEGER_STATIC_LOG2_HPP
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <sprout/config.hpp>
|
2013-11-22 12:11:08 +00:00
|
|
|
#include <sprout/type_traits/integral_constant.hpp>
|
2011-10-18 13:28:32 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace detail {
|
|
|
|
typedef std::uintmax_t static_log2_argument_type;
|
|
|
|
typedef int static_log2_result_type;
|
|
|
|
|
|
|
|
namespace static_log2_impl {
|
|
|
|
typedef sprout::detail::static_log2_argument_type argument_type;
|
|
|
|
typedef sprout::detail::static_log2_result_type result_type;
|
|
|
|
|
|
|
|
template<result_type n>
|
|
|
|
struct choose_initial_n {
|
2012-10-05 15:58:56 +00:00
|
|
|
SPROUT_STATIC_CONSTEXPR bool c
|
|
|
|
= (sprout::detail::static_log2_impl::argument_type(1) << n << n) != 0
|
|
|
|
;
|
|
|
|
SPROUT_STATIC_CONSTEXPR sprout::detail::static_log2_impl::result_type value
|
|
|
|
= !c * n + choose_initial_n<2 * c * n>::value
|
|
|
|
;
|
2011-10-18 13:28:32 +00:00
|
|
|
};
|
|
|
|
template<>
|
|
|
|
struct choose_initial_n<0> {
|
|
|
|
SPROUT_STATIC_CONSTEXPR sprout::detail::static_log2_impl::result_type value = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
SPROUT_STATIC_CONSTEXPR sprout::detail::static_log2_impl::result_type n_zero = 16;
|
2012-10-05 15:58:56 +00:00
|
|
|
SPROUT_STATIC_CONSTEXPR sprout::detail::static_log2_impl::result_type initial_n
|
|
|
|
= sprout::detail::static_log2_impl::choose_initial_n<n_zero>::value
|
|
|
|
;
|
2011-10-18 13:28:32 +00:00
|
|
|
|
|
|
|
template<
|
|
|
|
sprout::detail::static_log2_impl::argument_type x,
|
|
|
|
sprout::detail::static_log2_impl::result_type n = sprout::detail::static_log2_impl::initial_n
|
|
|
|
>
|
|
|
|
struct static_log2_impl {
|
|
|
|
SPROUT_STATIC_CONSTEXPR bool c = (x >> n) > 0;
|
2012-10-05 15:58:56 +00:00
|
|
|
SPROUT_STATIC_CONSTEXPR sprout::detail::static_log2_impl::result_type value
|
|
|
|
= c * n + (static_log2_impl<(x >> c * n), n / 2>::value)
|
|
|
|
;
|
2011-10-18 13:28:32 +00:00
|
|
|
};
|
|
|
|
template<>
|
|
|
|
struct static_log2_impl<1, 0> {
|
|
|
|
SPROUT_STATIC_CONSTEXPR sprout::detail::static_log2_impl::result_type value = 0;
|
|
|
|
};
|
|
|
|
} // namespace static_log2_impl
|
|
|
|
|
|
|
|
template<sprout::detail::static_log2_argument_type x>
|
2013-11-09 08:39:57 +00:00
|
|
|
struct static_log2
|
2013-11-22 12:11:08 +00:00
|
|
|
: public sprout::integral_constant<
|
2013-11-09 08:39:57 +00:00
|
|
|
sprout::detail::static_log2_result_type,
|
|
|
|
sprout::detail::static_log2_impl::static_log2_impl<x>::value
|
|
|
|
>
|
|
|
|
{};
|
2011-10-18 13:28:32 +00:00
|
|
|
template<>
|
|
|
|
struct static_log2<0> {};
|
|
|
|
} // namespace detail
|
|
|
|
} // namespace sprout
|
|
|
|
|
2013-03-22 05:24:19 +00:00
|
|
|
#endif // #ifndef SPROUT_DETAIL_INTEGER_STATIC_LOG2_HPP
|