-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrlwinm.py
48 lines (39 loc) · 973 Bytes
/
rlwinm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import sys
def rl(x, n):
return ((x << n) & 0xffffffff) | \
((x >> (32 - n)) & 0xffffffff)
def gen_mask(b, e):
mask = 0
if b < e+1:
for i in range(b, e+1):
mask |= 1<<(31 - i)
else:
mask = 0xffffffff
if b > e+1:
for i in range(e+1, b):
mask &= ~(1 << (31 - i))
return mask
def get_int(s):
if s.startswith('0x'):
return int(s, 16)
else:
return int(s)
def rlwinm(x, shift, mb, me):
if shift > 16:
op = "(0x%x >>> %d) " % (x, 32-shift)
else:
op = "(0x%x <<< %d) " % (x, shift)
mask = gen_mask(mb, me)
op += "& 0x%08x " % mask
res = rl(x, shift) & mask
op += "= 0x%x " % res
print(op)
return res
def main():
x = get_int(sys.argv[1])
shift = get_int(sys.argv[2])
mb = get_int(sys.argv[3])
me = get_int(sys.argv[4])
rlwinm(x, shift, mb, me)
if __name__ == "__main__":
main()