Skip to content

Instantly share code, notes, and snippets.

@pervognsen
Created July 30, 2018 04:01
Show Gist options
  • Save pervognsen/f2e0121d2f734932cfcec5a41d5d53b0 to your computer and use it in GitHub Desktop.
Save pervognsen/f2e0121d2f734932cfcec5a41d5d53b0 to your computer and use it in GitHub Desktop.
def funnel_shifter(x, n):
assert 2 * 2**len(n) - 1 == len(x)
for i, b in enumerate(n):
x = when(b, x[2**i:], x[:-2**i])
return x
def funnel_right_shifter(x, n):
return funnel_shifter(x @ bit[len(x)-1](0), n)
def funnel_arithmetic_right_shifter(x, n):
return funnel_shifter(x @ rep(x[-1], len(x)-1), n)
def funnel_left_shifter(x, n):
return funnel_shifter(bit[len(x)-1](0) @ x, ~n)
def funnel_right_rotator(x, n):
return funnel_shifter(x @ x[:-1], n)
def funnel_left_rotator(x, n):
return funnel_shifter(x[1:] @ x, ~n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment