2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2014-01-08 07:48:12 +00:00
|
|
|
Copyright (c) 2011-2014 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)
|
|
|
|
=============================================================================*/
|
2012-12-06 17:31:16 +00:00
|
|
|
#ifndef SPROUT_DETAIL_POW_HPP
|
|
|
|
#define SPROUT_DETAIL_POW_HPP
|
2012-12-11 16:47:14 +00:00
|
|
|
|
2013-02-06 18:11:17 +00:00
|
|
|
#include <type_traits>
|
2012-12-06 17:31:16 +00:00
|
|
|
#include <sprout/config.hpp>
|
2013-02-06 18:11:17 +00:00
|
|
|
#include <sprout/type_traits/is_int.hpp>
|
|
|
|
#include <sprout/type_traits/is_uint.hpp>
|
|
|
|
#include <sprout/type_traits/enabler_if.hpp>
|
2012-12-06 17:31:16 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace detail {
|
|
|
|
template<typename T>
|
|
|
|
inline SPROUT_CONSTEXPR T
|
|
|
|
pow2(T const& x) {
|
|
|
|
return x * x;
|
|
|
|
}
|
2012-12-08 04:45:09 +00:00
|
|
|
|
2012-12-06 17:31:16 +00:00
|
|
|
template<typename T>
|
|
|
|
inline SPROUT_CONSTEXPR T
|
|
|
|
pow3(T const& x) {
|
|
|
|
return x * x * x;
|
|
|
|
}
|
2012-12-08 04:45:09 +00:00
|
|
|
|
2013-02-06 18:11:17 +00:00
|
|
|
template<typename T, typename IntType>
|
2012-12-06 17:31:16 +00:00
|
|
|
inline SPROUT_CONSTEXPR T
|
2013-02-06 18:11:17 +00:00
|
|
|
pow_n_impl(T const& x, IntType n) {
|
2012-12-06 17:31:16 +00:00
|
|
|
return n == 1 ? x
|
2012-12-08 04:45:09 +00:00
|
|
|
: n % 2 ? x * sprout::detail::pow2(sprout::detail::pow_n_impl(x, n / 2))
|
|
|
|
: sprout::detail::pow2(sprout::detail::pow_n_impl(x, n / 2))
|
|
|
|
;
|
|
|
|
}
|
2013-02-06 18:11:17 +00:00
|
|
|
template<
|
|
|
|
typename T, typename UIntType,
|
|
|
|
typename sprout::enabler_if<sprout::is_uint<UIntType>::value>::type = sprout::enabler
|
|
|
|
>
|
2012-12-08 04:45:09 +00:00
|
|
|
inline SPROUT_CONSTEXPR T
|
2013-02-06 18:11:17 +00:00
|
|
|
pow_n(T const& x, UIntType n) {
|
2012-12-08 04:45:09 +00:00
|
|
|
return n == 0 ? T(1)
|
|
|
|
: sprout::detail::pow_n_impl(x, n)
|
2012-12-06 17:31:16 +00:00
|
|
|
;
|
|
|
|
}
|
2013-02-06 18:11:17 +00:00
|
|
|
template<
|
|
|
|
typename T, typename IntType,
|
|
|
|
typename sprout::enabler_if<sprout::is_int<IntType>::value>::type = sprout::enabler
|
|
|
|
>
|
|
|
|
inline SPROUT_CONSTEXPR T
|
|
|
|
pow_n(T const& x, IntType n) {
|
|
|
|
return n == 0 ? T(1)
|
|
|
|
: n > 0 ? sprout::detail::pow_n_impl(x, n)
|
|
|
|
: T(1) / sprout::detail::pow_n_impl(x, -n)
|
|
|
|
;
|
|
|
|
}
|
2012-12-06 17:31:16 +00:00
|
|
|
} // namespace detail
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_DETAIL_POW_HPP
|