2013-10-09 07:14:41 +00:00
|
|
|
/*=============================================================================
|
2019-01-07 08:47:17 +00:00
|
|
|
Copyright (c) 2011-2019 Bolero MURAKAMI
|
2013-10-09 07:14:41 +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)
|
|
|
|
=============================================================================*/
|
|
|
|
#ifndef SPROUT_INTEGER_STATIC_POW_HPP
|
|
|
|
#define SPROUT_INTEGER_STATIC_POW_HPP
|
|
|
|
|
|
|
|
#include <sprout/config.hpp>
|
2013-11-22 12:11:08 +00:00
|
|
|
#include <sprout/type_traits/integral_constant.hpp>
|
2013-10-09 07:14:41 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
//
|
|
|
|
// static_pow2
|
|
|
|
//
|
|
|
|
template<typename T, T X>
|
|
|
|
struct static_pow2
|
2013-11-22 12:11:08 +00:00
|
|
|
: public sprout::integral_constant<T, (1 << X)>
|
2013-10-09 07:14:41 +00:00
|
|
|
{};
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
template<typename T, T X, bool IsZero = (X == 0)>
|
|
|
|
struct static_pow2m1;
|
|
|
|
template<typename T, T X>
|
|
|
|
struct static_pow2m1<T, X, true>
|
2013-11-22 12:11:08 +00:00
|
|
|
: public sprout::integral_constant<T, 1>
|
2013-10-09 07:14:41 +00:00
|
|
|
{};
|
|
|
|
template<typename T, T X>
|
|
|
|
struct static_pow2m1<T, X, false>
|
2013-11-22 12:11:08 +00:00
|
|
|
: public sprout::integral_constant<T, ((((T(1) << (X - 1)) - 1) << 1) + 1)>
|
2013-10-09 07:14:41 +00:00
|
|
|
{};
|
|
|
|
} // namespace detail
|
|
|
|
//
|
|
|
|
// static_pow2m1
|
|
|
|
//
|
|
|
|
template<typename T, T X>
|
|
|
|
struct static_pow2m1
|
|
|
|
: public sprout::detail::static_pow2m1<T, X>
|
|
|
|
{};
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
template<typename T, T X, T Y, int K = (Y < 3 ? int(Y) : 3), bool IsOdd = (Y % 2 != 0)>
|
|
|
|
struct static_pow;
|
|
|
|
template<typename T, T X, T Y>
|
|
|
|
struct static_pow<T, X, Y, 0, false>
|
2013-11-22 12:11:08 +00:00
|
|
|
: public sprout::integral_constant<T, 1>
|
2013-10-09 07:14:41 +00:00
|
|
|
{};
|
|
|
|
template<typename T, T X, T Y>
|
|
|
|
struct static_pow<T, X, Y, 1, true>
|
2013-11-22 12:11:08 +00:00
|
|
|
: public sprout::integral_constant<T, X>
|
2013-10-09 07:14:41 +00:00
|
|
|
{};
|
|
|
|
template<typename T, T X, T Y>
|
|
|
|
struct static_pow<T, X, Y, 2, false>
|
2013-11-22 12:11:08 +00:00
|
|
|
: public sprout::integral_constant<T, X * X>
|
2013-10-09 07:14:41 +00:00
|
|
|
{};
|
|
|
|
template<typename T, T X, T Y, int K>
|
|
|
|
struct static_pow<T, X, Y, K, false>
|
|
|
|
: public sprout::detail::static_pow<T, sprout::detail::static_pow<T, X, Y / 2>::value, 2>
|
|
|
|
{};
|
|
|
|
template<typename T, T X, T Y, int K>
|
|
|
|
struct static_pow<T, X, Y, K, true>
|
2013-11-22 12:11:08 +00:00
|
|
|
: public sprout::integral_constant<T, X * sprout::detail::static_pow<T, X, Y - 1>::value>
|
2013-10-09 07:14:41 +00:00
|
|
|
{};
|
|
|
|
} // namespace detail
|
|
|
|
//
|
|
|
|
// static_pow
|
|
|
|
//
|
|
|
|
template<typename T, T X, T Y>
|
|
|
|
struct static_pow
|
|
|
|
: public sprout::detail::static_pow<T, X, Y>
|
|
|
|
{};
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_INTEGER_STATIC_POW_HPP
|