From 8e0c48264b1acba0b2d35b0639c6510e1fc63404 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Wed, 29 May 2024 10:58:05 +0300 Subject: [PATCH] Polishing of c_powu(): exclude unreachible cases and magic numbers --- Objects/complexobject.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Objects/complexobject.c b/Objects/complexobject.c index 52d271c39fb655..655a9c7001eeed 100644 --- a/Objects/complexobject.c +++ b/Objects/complexobject.c @@ -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; @@ -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 {