| 1 |
// Boost pow.hpp header file |
| 2 |
// Computes a power with exponent known at compile-time |
| 3 |
|
| 4 |
// (C) Copyright Bruno Lalande 2008. |
| 5 |
// (C) Copyright Matt Borland 2024. |
| 6 |
// Distributed under the Boost Software License, Version 1.0. |
| 7 |
// (See accompanying file LICENSE_1_0.txt or copy at |
| 8 |
// http://www.boost.org/LICENSE_1_0.txt) |
| 9 |
|
| 10 |
// See http://www.boost.org for updates, documentation, and revision history. |
| 11 |
|
| 12 |
|
| 13 |
#ifndef BOOST_MATH_POW_HPP |
| 14 |
#define BOOST_MATH_POW_HPP |
| 15 |
|
| 16 |
#include <boost/math/tools/config.hpp> |
| 17 |
#include <boost/math/policies/policy.hpp> |
| 18 |
#include <boost/math/policies/error_handling.hpp> |
| 19 |
#include <boost/math/tools/promotion.hpp> |
| 20 |
|
| 21 |
#ifndef BOOST_MATH_HAS_NVRTC |
| 22 |
#include <boost/math/special_functions/math_fwd.hpp> |
| 23 |
#endif |
| 24 |
|
| 25 |
namespace boost { |
| 26 |
namespace math { |
| 27 |
|
| 28 |
#ifdef _MSC_VER |
| 29 |
#pragma warning(push) |
| 30 |
#pragma warning(disable:4702) // Unreachable code, only triggered in release mode and /W4 |
| 31 |
#endif |
| 32 |
|
| 33 |
namespace detail { |
| 34 |
|
| 35 |
|
| 36 |
template <int N, int M = N%2> |
| 37 |
struct positive_power |
| 38 |
{ |
| 39 |
template <typename T> |
| 40 |
BOOST_MATH_GPU_ENABLED static constexpr T result(T base) |
| 41 |
{ |
| 42 |
T power = positive_power<N/2>::result(base); |
| 43 |
return power * power; |
| 44 |
} |
| 45 |
}; |
| 46 |
|
| 47 |
template <int N> |
| 48 |
struct positive_power<N, 1> |
| 49 |
{ |
| 50 |
template <typename T> |
| 51 |
BOOST_MATH_GPU_ENABLED static constexpr T result(T base) |
| 52 |
{ |
| 53 |
T power = positive_power<N/2>::result(base); |
| 54 |
return base * power * power; |
| 55 |
} |
| 56 |
}; |
| 57 |
|
| 58 |
template <> |
| 59 |
struct positive_power<1, 1> |
| 60 |
{ |
| 61 |
template <typename T> |
| 62 |
BOOST_MATH_GPU_ENABLED static constexpr T result(T base){ return base; } |
| 63 |
}; |
| 64 |
|
| 65 |
|
| 66 |
template <int N, bool> |
| 67 |
struct power_if_positive |
| 68 |
{ |
| 69 |
template <typename T, class Policy> |
| 70 |
BOOST_MATH_GPU_ENABLED static constexpr T result(T base, const Policy&) |
| 71 |
{ return positive_power<N>::result(base); } |
| 72 |
}; |
| 73 |
|
| 74 |
template <int N> |
| 75 |
struct power_if_positive<N, false> |
| 76 |
{ |
| 77 |
template <typename T, class Policy> |
| 78 |
BOOST_MATH_GPU_ENABLED static constexpr T result(T base, const Policy& policy) |
| 79 |
{ |
| 80 |
if (base == 0) |
| 81 |
{ |
| 82 |
return policies::raise_overflow_error<T>( |
| 83 |
"boost::math::pow(%1%)", |
| 84 |
"Attempted to compute a negative power of 0", |
| 85 |
policy |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
return T(1) / positive_power<-N>::result(base); |
| 90 |
} |
| 91 |
}; |
| 92 |
|
| 93 |
template <> |
| 94 |
struct power_if_positive<0, true> |
| 95 |
{ |
| 96 |
template <typename T, class Policy> |
| 97 |
BOOST_MATH_GPU_ENABLED static constexpr T result(T base, const Policy& policy) |
| 98 |
{ |
| 99 |
if (base == 0) |
| 100 |
{ |
| 101 |
return policies::raise_indeterminate_result_error<T>( |
| 102 |
"boost::math::pow(%1%)", |
| 103 |
"The result of pow<0>(%1%) is undetermined", |
| 104 |
base, |
| 105 |
T(1), |
| 106 |
policy |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
return T(1); |
| 111 |
} |
| 112 |
}; |
| 113 |
|
| 114 |
|
| 115 |
template <int N> |
| 116 |
struct select_power_if_positive |
| 117 |
{ |
| 118 |
using type = power_if_positive<N, (N >= 0)>; |
| 119 |
}; |
| 120 |
|
| 121 |
|
| 122 |
} // namespace detail |
| 123 |
|
| 124 |
|
| 125 |
template <int N, typename T, class Policy> |
| 126 |
BOOST_MATH_GPU_ENABLED constexpr inline typename tools::promote_args<T>::type pow(T base, const Policy& policy) |
| 127 |
{ |
| 128 |
using result_type = typename tools::promote_args<T>::type; |
| 129 |
return detail::select_power_if_positive<N>::type::result(static_cast<result_type>(base), policy); |
| 130 |
} |
| 131 |
|
| 132 |
template <int N, typename T> |
| 133 |
BOOST_MATH_GPU_ENABLED constexpr inline typename tools::promote_args<T>::type pow(T base) |
| 134 |
{ return pow<N>(base, policies::policy<>()); } |
| 135 |
|
| 136 |
#ifdef _MSC_VER |
| 137 |
#pragma warning(pop) |
| 138 |
#endif |
| 139 |
|
| 140 |
} // namespace math |
| 141 |
} // namespace boost |
| 142 |
|
| 143 |
|
| 144 |
#endif |