diff options
author | Tim Peters <tim.peters@gmail.com> | 2022-01-03 20:41:16 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-03 20:41:16 -0600 |
commit | 3aa5242b54b0627293d95cfb4a26b2f917f667be (patch) | |
tree | 8eabfea439813b7a1abe90fe7dfa1b81eaf02910 /Lib/test/test_long.py | |
parent | f1a58441eea6b7788c64d03a80ea35996301e550 (diff) | |
download | cpython-3aa5242b54b0627293d95cfb4a26b2f917f667be.tar.gz cpython-3aa5242b54b0627293d95cfb4a26b2f917f667be.zip |
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 'Lib/test/test_long.py')
-rw-r--r-- | Lib/test/test_long.py | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py index 3c8e9e22e17..f2a622b5868 100644 --- a/Lib/test/test_long.py +++ b/Lib/test/test_long.py @@ -1502,6 +1502,17 @@ class LongTest(unittest.TestCase): self.assertEqual(type(numerator), int) self.assertEqual(type(denominator), int) + def test_square(self): + # Multiplication makes a special case of multiplying an int with + # itself, using a special, faster algorithm. This test is mostly + # to ensure that no asserts in the implementation trigger, in + # cases with a maximal amount of carries. + for bitlen in range(1, 400): + n = (1 << bitlen) - 1 # solid string of 1 bits + with self.subTest(bitlen=bitlen, n=n): + # (2**i - 1)**2 = 2**(2*i) - 2*2**i + 1 + self.assertEqual(n**2, + (1 << (2 * bitlen)) - (1 << (bitlen + 1)) + 1) if __name__ == "__main__": unittest.main() |