346
|
1
|
|
2
|
|
3 def rotate_right(v, n):
|
|
4 """ bit-wise Rotate right n times """
|
|
5 mask = (2**n) - 1
|
|
6 mask_bits = v & mask
|
|
7 return (v >> n) | (mask_bits << (32 - n))
|
|
8
|
|
9 def rotate_left(v, n):
|
|
10 assert n >= 0
|
|
11 assert n < 32
|
|
12 return rotate_right(v, 32 - n)
|
|
13
|
365
|
14 def encode_imm32(v):
|
|
15 """ Bundle 32 bit value into 4 bits rotation and 8 bits value
|
|
16 """
|
|
17 for i in range(0, 16):
|
|
18 v2 = rotate_left(v, i*2)
|
|
19 if (v2 & 0xFFFFFF00) == 0:
|
|
20 rotation = i
|
|
21 val = v2 & 0xFF
|
|
22 x = (rotation << 8) | val
|
|
23 return x
|
|
24 raise Exception("Invalid value {}".format(v))
|
346
|
25
|