Skip to content

Commit

Permalink
Update mult.py: branchless, compact
Browse files Browse the repository at this point in the history
  • Loading branch information
daedalus authored Feb 10, 2024
1 parent 11da0b7 commit 3f01f59
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions mult.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
def mult(x, y):
z = 0
while y > 0:
z = z + x
y = y - 1
z += x
y -= 1
return z


def russian_peasant(x, y):
z = 0
while y > 0:
if y % 2 == 1: z = z + x
x = x << 1
y = y >> 1
return z
z += x * (y & 1)
x <<= 1
y >>= 1
return z

0 comments on commit 3f01f59

Please sign in to comment.