From 7d4d77e5594a4662ca7977221e0f410c7f7d0427 Mon Sep 17 00:00:00 2001 From: Muhammad Umar Nadeem Date: Fri, 20 Nov 2026 23:52:58 +0500 Subject: [PATCH 1/4] Fix unit-result special cases in real pow --- .../xsimd/arch/common/xsimd_common_math.hpp | 20 ++++--- test/test_power.cpp | 59 +++++++++++++++++++ 2 files changed, 72 insertions(+), 7 deletions(-) diff --git a/include/xsimd/arch/common/xsimd_common_math.hpp b/include/xsimd/arch/common/xsimd_common_math.hpp index b9484fa81..29c3245f7 100644 --- a/include/xsimd/arch/common/xsimd_common_math.hpp +++ b/include/xsimd/arch/common/xsimd_common_math.hpp @@ -2038,13 +2038,19 @@ namespace xsimd { using batch_type = batch; 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(other); + const auto unit_result = other == zero || self == one || (self == -one && infinite_exponent); + // Evaluate lanes that must return 1 as 1^0 to avoid invalid products. + const auto base = select(unit_result, one, self); + const auto exponent = select(unit_result, zero, other); + const auto negative_base = base < zero; + const auto zero_to_positive = base == zero && exponent > zero; + const auto abs_base = select(zero_to_positive, one, abs(base)); + batch_type z = exp(exponent * log(abs_base)); + z = select(zero_to_positive, zero, z); + z = select(is_odd(exponent) && negative_base, -z, z); + const auto invalid = negative_base && !(is_flint(exponent) || infinite_exponent); return select(invalid, constants::nan(), z); } diff --git a/test/test_power.cpp b/test/test_power.cpp index de2ed14ff..1c7ecdb57 100644 --- a/test/test_power.cpp +++ b/test/test_power.cpp @@ -181,4 +181,63 @@ TEST_CASE_TEMPLATE("[power]", B, BATCH_FLOAT_TYPES) power_test Test; Test.test_power_functions(); } + +TEST_CASE_TEMPLATE("[power] special cases", B, BATCH_FLOAT_TYPES) +{ + using value_type = typename B::value_type; + struct test_case + { + value_type base; + value_type exponent; + value_type expected; + }; +#ifndef __FAST_MATH__ + const auto nan = std::numeric_limits::quiet_NaN(); + const auto inf = std::numeric_limits::infinity(); +#endif + const test_case cases[] = { + { 0, 0, 1 }, + { 0, 2, 0 }, + { 0, -0., 1 }, + { -1, 3, -1 }, + { -0., 0, 1 }, + { 2, 3, 8 }, + { 2, 0, 1 }, + { 1, 2, 1 }, +#ifndef __FAST_MATH__ + { nan, 0, 1 }, + { inf, 0, 1 }, + { -inf, -0., 1 }, + { 1, nan, 1 }, + { 1, inf, 1 }, + { 1, -inf, 1 }, + { -1, inf, 1 }, + { -1, -inf, 1 }, + { -1, nan, nan }, + { -1, 0.5, nan }, +#endif + }; + constexpr size_t case_count = sizeof(cases) / sizeof(cases[0]); + for (size_t i = 0; i < case_count; i += B::size) + { + std::array bases, exponents, expected; + for (size_t lane = 0; lane < B::size; ++lane) + { + const auto& c = cases[(i + lane) % case_count]; + bases[lane] = c.base; + exponents[lane] = c.exponent; + expected[lane] = c.expected; + } + const auto actual = xsimd::pow(B::load_unaligned(bases.data()), B::load_unaligned(exponents.data())); + for (size_t lane = 0; lane < B::size; ++lane) + { + const auto result = actual.get(lane); + CAPTURE(bases[lane]); + CAPTURE(exponents[lane]); + CAPTURE(expected[lane]); + CAPTURE(result); + CHECK(detail::expect_scalar_near(expected[lane], result)); + } + } +} #endif From db935bdbb67c57eb9832499b64456ae8427e1786 Mon Sep 17 00:00:00 2001 From: Muhammad Umar Nadeem Date: Sat, 21 Nov 2026 03:12:06 +0500 Subject: [PATCH 2/4] Simplify pow special-case handling and tests --- .../xsimd/arch/common/xsimd_common_math.hpp | 24 ++++--- test/test_power.cpp | 66 ++----------------- 2 files changed, 18 insertions(+), 72 deletions(-) diff --git a/include/xsimd/arch/common/xsimd_common_math.hpp b/include/xsimd/arch/common/xsimd_common_math.hpp index 29c3245f7..d8e00d544 100644 --- a/include/xsimd/arch/common/xsimd_common_math.hpp +++ b/include/xsimd/arch/common/xsimd_common_math.hpp @@ -2034,23 +2034,21 @@ namespace xsimd * ==================================================== */ template - XSIMD_INLINE batch pow(batch const& self, batch const& other, requires_arch) noexcept + XSIMD_INLINE batch pow(batch const& base, batch const& exponent, requires_arch) noexcept { using batch_type = batch; const auto zero = batch_type(0.); const auto one = batch_type(1.); - const auto infinite_exponent = isinf(other); - const auto unit_result = other == zero || self == one || (self == -one && infinite_exponent); - // Evaluate lanes that must return 1 as 1^0 to avoid invalid products. - const auto base = select(unit_result, one, self); - const auto exponent = select(unit_result, zero, other); - const auto negative_base = base < zero; - const auto zero_to_positive = base == zero && exponent > zero; - const auto abs_base = select(zero_to_positive, one, abs(base)); - batch_type z = exp(exponent * log(abs_base)); - z = select(zero_to_positive, zero, z); - z = select(is_odd(exponent) && negative_base, -z, z); - const auto invalid = negative_base && !(is_flint(exponent) || infinite_exponent); + const auto unit_result = exponent == zero || base == one || (base == -one && isinf(exponent)); + const auto self = select(unit_result, one, base); + const auto other = select(unit_result, zero, exponent); + 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)); return select(invalid, constants::nan(), z); } diff --git a/test/test_power.cpp b/test/test_power.cpp index 1c7ecdb57..624f2950a 100644 --- a/test/test_power.cpp +++ b/test/test_power.cpp @@ -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::quiet_NaN(); + const auto inf = std::numeric_limits::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(), @@ -181,63 +188,4 @@ TEST_CASE_TEMPLATE("[power]", B, BATCH_FLOAT_TYPES) power_test Test; Test.test_power_functions(); } - -TEST_CASE_TEMPLATE("[power] special cases", B, BATCH_FLOAT_TYPES) -{ - using value_type = typename B::value_type; - struct test_case - { - value_type base; - value_type exponent; - value_type expected; - }; -#ifndef __FAST_MATH__ - const auto nan = std::numeric_limits::quiet_NaN(); - const auto inf = std::numeric_limits::infinity(); -#endif - const test_case cases[] = { - { 0, 0, 1 }, - { 0, 2, 0 }, - { 0, -0., 1 }, - { -1, 3, -1 }, - { -0., 0, 1 }, - { 2, 3, 8 }, - { 2, 0, 1 }, - { 1, 2, 1 }, -#ifndef __FAST_MATH__ - { nan, 0, 1 }, - { inf, 0, 1 }, - { -inf, -0., 1 }, - { 1, nan, 1 }, - { 1, inf, 1 }, - { 1, -inf, 1 }, - { -1, inf, 1 }, - { -1, -inf, 1 }, - { -1, nan, nan }, - { -1, 0.5, nan }, -#endif - }; - constexpr size_t case_count = sizeof(cases) / sizeof(cases[0]); - for (size_t i = 0; i < case_count; i += B::size) - { - std::array bases, exponents, expected; - for (size_t lane = 0; lane < B::size; ++lane) - { - const auto& c = cases[(i + lane) % case_count]; - bases[lane] = c.base; - exponents[lane] = c.exponent; - expected[lane] = c.expected; - } - const auto actual = xsimd::pow(B::load_unaligned(bases.data()), B::load_unaligned(exponents.data())); - for (size_t lane = 0; lane < B::size; ++lane) - { - const auto result = actual.get(lane); - CAPTURE(bases[lane]); - CAPTURE(exponents[lane]); - CAPTURE(expected[lane]); - CAPTURE(result); - CHECK(detail::expect_scalar_near(expected[lane], result)); - } - } -} #endif From 53d40c58111a34b5ce82e596e76c547b3e64aef4 Mon Sep 17 00:00:00 2001 From: Muhammad Umar Nadeem Date: Sat, 21 Nov 2026 16:36:47 +0500 Subject: [PATCH 3/4] Simplify pow zero and infinity checks --- .../xsimd/arch/common/xsimd_common_math.hpp | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/include/xsimd/arch/common/xsimd_common_math.hpp b/include/xsimd/arch/common/xsimd_common_math.hpp index d8e00d544..973d2afaa 100644 --- a/include/xsimd/arch/common/xsimd_common_math.hpp +++ b/include/xsimd/arch/common/xsimd_common_math.hpp @@ -2034,21 +2034,21 @@ namespace xsimd * ==================================================== */ template - XSIMD_INLINE batch pow(batch const& base, batch const& exponent, requires_arch) noexcept + XSIMD_INLINE batch pow(batch base, batch exponent, requires_arch) noexcept { using batch_type = batch; const auto zero = batch_type(0.); const auto one = batch_type(1.); - const auto unit_result = exponent == zero || base == one || (base == -one && isinf(exponent)); - const auto self = select(unit_result, one, base); - const auto other = select(unit_result, zero, exponent); - 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 infinite_exponent = isinf(exponent); + const auto unit_result = exponent == zero || base == one || (base == -one && infinite_exponent); + 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(), z); } From 90062c7e31279649c5abd0125ae1258a6d13da86 Mon Sep 17 00:00:00 2001 From: Muhammad Umar Nadeem Date: Sun, 22 Nov 2026 00:26:42 +0500 Subject: [PATCH 4/4] Skip pow special-case selects on ordinary batches --- include/xsimd/arch/common/xsimd_common_math.hpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/include/xsimd/arch/common/xsimd_common_math.hpp b/include/xsimd/arch/common/xsimd_common_math.hpp index 973d2afaa..daa93799b 100644 --- a/include/xsimd/arch/common/xsimd_common_math.hpp +++ b/include/xsimd/arch/common/xsimd_common_math.hpp @@ -2040,9 +2040,13 @@ namespace xsimd const auto zero = batch_type(0.); 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); - base = select(unit_result, one, base); - exponent = select(unit_result, zero, 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)));