Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions include/xsimd/arch/common/xsimd_common_math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2034,17 +2034,25 @@ namespace xsimd
* ====================================================
*/
template <class A, class T>
XSIMD_INLINE batch<T, A> pow(batch<T, A> const& self, batch<T, A> const& other, requires_arch<common>) noexcept
XSIMD_INLINE batch<T, A> pow(batch<T, A> base, batch<T, A> exponent, requires_arch<common>) noexcept
{
using batch_type = batch<T, A>;
const auto zero = batch_type(0.);
auto negself = self < zero;
auto iszeropowpos = self == zero && other >= zero;
auto adj_self = select(iszeropowpos, batch_type(1), abs(self));
batch_type z = exp(other * log(adj_self));
z = select(iszeropowpos, zero, z);
z = select(is_odd(other) && negself, -z, z);
auto invalid = negself && !(is_flint(other) || isinf(other));
const auto one = batch_type(1.);
const auto infinite_exponent = isinf(exponent);
// x^(+/-0), 1^y, and (-1)^(+/-inf) all evaluate to 1.
const auto unit_result = exponent == zero || base == one || (base == -one && infinite_exponent);
if (any(unit_result))
{
base = select(unit_result, one, base);
exponent = select(unit_result, zero, exponent);
}
const auto zero_to_positive = base == zero && exponent > zero;
const auto magnitude = select(zero_to_positive, one, abs(base));
auto z = select(zero_to_positive, zero, exp(exponent * log(magnitude)));
const auto negative_base = base < zero;
z = select(is_odd(exponent) && negative_base, -z, z);
const auto invalid = negative_base && !(is_flint(exponent) || infinite_exponent);
return select(invalid, constants::nan<batch_type>(), z);
}

Expand Down
7 changes: 7 additions & 0 deletions test/test_power.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,14 @@ struct power_test
}
#endif
}
CHECK(xsimd::all(pow(batch_type(0), batch_type(0)) == batch_type(1)));
#ifndef __FAST_MATH__
const auto nan = std::numeric_limits<value_type>::quiet_NaN();
const auto inf = std::numeric_limits<value_type>::infinity();
CHECK(xsimd::all(pow(batch_type(nan), batch_type(0)) == batch_type(1)));
CHECK(xsimd::all(pow(batch_type(1), batch_type(nan)) == batch_type(1)));
CHECK(xsimd::all(pow(batch_type(-1), batch_type(inf)) == batch_type(1)));

// pow 0^-x
{
std::transform(zero_input.cbegin(), zero_input.cend(), rhs_input.cbegin(), expected.begin(),
Expand Down