summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2022-01-04 02:41:16 (GMT)
committerGitHub <noreply@github.com>2022-01-04 02:41:16 (GMT)
commit3aa5242b54b0627293d95cfb4a26b2f917f667be (patch)
tree8eabfea439813b7a1abe90fe7dfa1b81eaf02910 /Objects
parentf1a58441eea6b7788c64d03a80ea35996301e550 (diff)
downloadcpython-3aa5242b54b0627293d95cfb4a26b2f917f667be.zip
cpython-3aa5242b54b0627293d95cfb4a26b2f917f667be.tar.gz
cpython-3aa5242b54b0627293d95cfb4a26b2f917f667be.tar.bz2
bpo-46233: Minor speedup for bigint squaring (GH-30345)
x_mul()'s squaring code can do some redundant and/or useless work at the end of each digit pass. A more careful analysis of worst-case carries at various digit positions allows making that code leaner.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/longobject.c24
1 files changed, 19 insertions, 5 deletions
diff --git a/Objects/longobject.c b/Objects/longobject.c
index b5648fc..2db8701 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -3237,12 +3237,12 @@ x_mul(PyLongObject *a, PyLongObject *b)
* via exploiting that each entry in the multiplication
* pyramid appears twice (except for the size_a squares).
*/
+ digit *paend = a->ob_digit + size_a;
for (i = 0; i < size_a; ++i) {
twodigits carry;
twodigits f = a->ob_digit[i];
digit *pz = z->ob_digit + (i << 1);
digit *pa = a->ob_digit + i + 1;
- digit *paend = a->ob_digit + size_a;
SIGCHECK({
Py_DECREF(z);
@@ -3265,13 +3265,27 @@ x_mul(PyLongObject *a, PyLongObject *b)
assert(carry <= (PyLong_MASK << 1));
}
if (carry) {
+ /* See comment below. pz points at the highest possible
+ * carry position from the last outer loop iteration, so
+ * *pz is at most 1.
+ */
+ assert(*pz <= 1);
carry += *pz;
- *pz++ = (digit)(carry & PyLong_MASK);
+ *pz = (digit)(carry & PyLong_MASK);
carry >>= PyLong_SHIFT;
+ if (carry) {
+ /* If there's still a carry, it must be into a position
+ * that still holds a 0. Where the base
+ ^ B is 1 << PyLong_SHIFT, the last add was of a carry no
+ * more than 2*B - 2 to a stored digit no more than 1.
+ * So the sum was no more than 2*B - 1, so the current
+ * carry no more than floor((2*B - 1)/B) = 1.
+ */
+ assert(carry == 1);
+ assert(pz[1] == 0);
+ pz[1] = (digit)carry;
+ }
}
- if (carry)
- *pz += (digit)(carry & PyLong_MASK);
- assert((carry >> PyLong_SHIFT) == 0);
}
}
else { /* a is not the same as b -- gradeschool int mult */