Skip to content

Fix unit-result special cases in real pow - #1460

Merged
DiamonDinoia merged 1 commit into
xtensor-stack:masterfrom
umrndem:fix/pow-unit-special-cases
Nov 26, 2026
Merged

DiamonDinoia merged 1 commit into
xtensor-stack:masterfrom
umrndem:fix/pow-unit-special-cases

Conversation

@umrndem

@umrndem umrndem commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Fix real pow to return 1 for a zero exponent, a base of 1, or a base of -1 with an infinite exponent. Handle those lanes before multiplying the exponent by the logarithm.

Added four regression checks to the existing power test for float and double. The real and complex power tests passed on SSE2 and AVX2 with FMA.

Fixes #1458.

@DiamonDinoia

Copy link
Copy Markdown
Contributor

sorry, there have been too many PRs lately, so I’m scaling back how many I review carefully. PRs that take more time to review compared of fixing the problem myself will not be reviewed.

I wouldn’t review this PR until the diff is much smaller than 72 lines. We’re fixing a single special case, and I don’t see a reason for such a large diff.

The test should be minimal too. AI tends to overtest by checking the same thing repeatedly. Also it tends to change more things than needed.

If you’re using AI, try Ponytail, ste and cpp skills, or Unslop to reduce boilerplate.

@umrndem

umrndem commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

sorry, there have been too many PRs lately, so I’m scaling back how many I review carefully. PRs that take more time to review compared of fixing the problem myself will not be reviewed.

I wouldn’t review this PR until the diff is much smaller than 72 lines. We’re fixing a single special case, and I don’t see a reason for such a large diff.

The test should be minimal too. AI tends to overtest by checking the same thing repeatedly. Also it tends to change more things than needed.

If you’re using AI, try Ponytail, ste and cpp skills, or Unslop to reduce boilerplate.

Understood, I'll take another look and see if something can be done to reduce the diff.

@umrndem

umrndem commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

thanks for the feedback. i got the diff down to 13 changed lines. the fix uses one mask to identify the cases where pow should return 1 and feeds those lanes 1^0 before the existing calculation. that avoids intermediate NaNs and keeps the rest of the calculation the same.

i cut the regression coverage down to four checks in the existing power test. the power tests passed on SSE2 and AVX2.

@DiamonDinoia DiamonDinoia left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This starts to make sense. Now we can try to integrate in the original algorithm by simplifying things.

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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep using base and exponent instead of self/other since you are touching this code. I think is more clear.

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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to simplify the control flow? We are now checking things equal zero here for example.

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));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there is a one variable above we should keep using it.

@umrndem

umrndem commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

thanks, i kept base and exponent throughout and reused one. the zero-base check now only looks at positive exponents, since the mask already handles zero exponents. i also reused the infinity check in both places. the power tests and all cases from #1458 passed on SSE2 and AVX2.

@DiamonDinoia

Copy link
Copy Markdown
Contributor

This make sense. Could you do a pass by adding comment such as x^0=1 and so on on the code to make it more readable. Just the math and the cases it handles?

Could you check the performance of the code? If we have regressions compared to the old code? If so, instead of select is it better to have a plain if with [[unlikely]] annotations that pushes the code out of the way? I feel like these corner cases are rare and should not impact the average use case.

@umrndem

umrndem commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

thanks, i added the three identities above the mask. the two select calls now run only when at least one lane needs the fix, and mixed batches still handle those cases lane by lane.

i benchmarked ordinary float and double inputs against 4c44d61 on SSE2 and AVX2 with FMA, using GCC 15.2 at -O3 on one pinned i7-12800H core. the unconditional selects were about 8–17% slower than upstream. the plain guard brought that down to about 1–5% in paired runs. i tried [[unlikely]] too, and the plain guard was a little faster across all four configurations. the power tests and all the cases from #1458 passed on both targets.

@DiamonDinoia DiamonDinoia left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do:

--- a/include/xsimd/arch/common/xsimd_common_math.hpp
+++ b/include/xsimd/arch/common/xsimd_common_math.hpp
@@ -2034,7 +2034,7 @@ namespace xsimd
          * ====================================================
          */
         template <class A, class T>
-        XSIMD_INLINE batch<T, A> pow(batch<T, A> base, batch<T, A> exponent, requires_arch<common>) noexcept
+        XSIMD_INLINE batch<T, A> pow(batch<T, A> const& base, batch<T, A> const& exponent, requires_arch<common>) noexcept
         {
             using batch_type = batch<T, A>;
             const auto zero = batch_type(0.);
@@ -2042,18 +2042,20 @@ namespace xsimd
             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 zero_pow_positive = base == zero && exponent > zero;
+            const auto log_arg = select(zero_pow_positive, one, abs(base));
+            auto z = select(zero_pow_positive, zero, exp(exponent * log(log_arg)));
             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);
+            // y is odd iff |y| - 2*trunc(|y|/2) == 1: one trunc, exact for every
+            // finite y, unlike is_odd(y-1), which rounds y-1 for |y| >= 2^24.
+            const auto abs_exponent = abs(exponent);
+            const auto half_trunc = trunc(abs_exponent * T(0.5));
+            const auto rem = abs_exponent - (half_trunc + half_trunc);
+            const auto odd_exponent = rem == one;
+            const auto int_exponent = (rem == zero) || odd_exponent;
+            z = select(odd_exponent && negative_base, -z, z);
+            const auto invalid = negative_base && !(int_exponent || infinite_exponent);
+            return select(unit_result, one, select(invalid, constants::nan<batch_type>(), z));
         }
 
         template <class A, class T>

@umrndem

umrndem commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

thanks, applied your approach and added one regression for large even exponents. the wasm job exposed a NaN-to-int conversion inside exp before the final select, so i moved the unit-result mask to exp's input. those lanes now compute exp(0).

all 446 wasm tests pass locally with Emscripten 4.0.21, along with the real and complex power tests on SSE2 and AVX2/FMA. the paired benchmarks are roughly flat for AVX2 double and 1–5% faster than upstream in the other configurations.

@DiamonDinoia DiamonDinoia left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good. Can you squash?

@umrndem
umrndem force-pushed the fix/pow-unit-special-cases branch from 0406c45 to d5d4f1c Compare November 26, 2026 10:28
@umrndem

umrndem commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

squashed into one commit and pushed. the diff is unchanged. thanks for reviewing!

@DiamonDinoia
DiamonDinoia merged commit d744e7f into xtensor-stack:master Nov 26, 2026
97 of 98 checks passed
@Ivorforce

Copy link
Copy Markdown

Thanks for taking care of this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pow returns incorrect values for 0 as exponent or 1 as base

3 participants