Skip to content

Commit

Permalink
XXX move k_mul() implementation to Lib/_pylong.py
Browse files Browse the repository at this point in the history
  • Loading branch information
skirpichev committed Oct 29, 2023
1 parent 14ab5e5 commit d1a9cb4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 372 deletions.
16 changes: 16 additions & 0 deletions Lib/_pylong.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,19 @@ def int_divmod(a, b):
return ~q, b + ~r
else:
return _divmod_pos(a, b)


def k_mul(x, y):
n = max(x.bit_length(), y.bit_length()) // 2
x0 = x >> n
x -= x0 << n
if x == y:
y0 = x0
y = x
else:
y0 = y >> n
y -= y0 << n
s1 = x0 * y0
s2 = x * y
s3 = (x0 + x) * (y0 + y) - s1 - s2
return (s1 << (n << 1)) + (s3 << n) + s2
Loading

0 comments on commit d1a9cb4

Please sign in to comment.