mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2024-11-16 10:49:05 +00:00
63 lines
1.9 KiB
C++
63 lines
1.9 KiB
C++
/*=============================================================================
|
|
Copyright (c) 2011-2013 Bolero MURAKAMI
|
|
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_DETAIL_POW_HPP
|
|
#define SPROUT_DETAIL_POW_HPP
|
|
|
|
#include <type_traits>
|
|
#include <sprout/config.hpp>
|
|
#include <sprout/type_traits/is_int.hpp>
|
|
#include <sprout/type_traits/is_uint.hpp>
|
|
#include <sprout/type_traits/enabler_if.hpp>
|
|
|
|
namespace sprout {
|
|
namespace detail {
|
|
template<typename T>
|
|
inline SPROUT_CONSTEXPR T
|
|
pow2(T const& x) {
|
|
return x * x;
|
|
}
|
|
|
|
template<typename T>
|
|
inline SPROUT_CONSTEXPR T
|
|
pow3(T const& x) {
|
|
return x * x * x;
|
|
}
|
|
|
|
template<typename T, typename IntType>
|
|
inline SPROUT_CONSTEXPR T
|
|
pow_n_impl(T const& x, IntType n) {
|
|
return n == 1 ? x
|
|
: 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))
|
|
;
|
|
}
|
|
template<
|
|
typename T, typename UIntType,
|
|
typename sprout::enabler_if<sprout::is_uint<UIntType>::value>::type = sprout::enabler
|
|
>
|
|
inline SPROUT_CONSTEXPR T
|
|
pow_n(T const& x, UIntType n) {
|
|
return n == 0 ? T(1)
|
|
: sprout::detail::pow_n_impl(x, n)
|
|
;
|
|
}
|
|
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)
|
|
;
|
|
}
|
|
} // namespace detail
|
|
} // namespace sprout
|
|
|
|
#endif // #ifndef SPROUT_DETAIL_POW_HPP
|