Skip to content

Commit

Permalink
Polishing of c_powu(): exclude unreachible cases and magic numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
skirpichev committed May 30, 2024
1 parent 70d21c7 commit 8e0c482
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions Objects/complexobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,16 @@ _Py_c_pow(Py_complex a, Py_complex b)
return r;
}

#define INT_EXP_CUTOFF 100

static Py_complex
c_powu(Py_complex x, long n)
{
Py_complex p = x, r = n-- ? p : c_1;
long mask = 1;
while (mask > 0 && n >= mask) {
assert(-1 <= n && n <= INT_EXP_CUTOFF);
while (n >= mask) {
assert(mask>0);
if (n & mask)
r = _Py_c_prod(r,p);
mask <<= 1;
Expand Down Expand Up @@ -541,7 +545,7 @@ complex_pow(PyObject *v, PyObject *w, PyObject *z)
errno = 0;
// Check whether the exponent has a small integer value, and if so use
// a faster and more accurate algorithm.
if (b.imag == 0.0 && b.real == floor(b.real) && fabs(b.real) <= 100.0) {
if (b.imag == 0.0 && b.real == floor(b.real) && fabs(b.real) <= INT_EXP_CUTOFF) {
p = c_powi(a, (long)b.real);
}
else {
Expand Down

0 comments on commit 8e0c482

Please sign in to comment.