4 return (value
& (sign
- 1)) - (value
& sign
)
7 return exts(value
, 32) & ((1<<64)-1)
10 return value
& ((1<<32)-1)
12 def rotl(value
, bits
, wordlen
):
13 mask
= (1 << wordlen
) - 1
14 bits
= bits
& (wordlen
- 1)
15 return ((value
<< bits
) |
(value
>> (wordlen
-bits
))) & mask
17 def ROTL64(value
, bits
):
18 return rotl(value
, bits
, 64)
20 def ROTL32(value
, bits
):
21 return rotl(value
, bits
, 32)
27 mask_a
= ((1<<x
) - 1) & ((1<<64) - 1)
28 mask_b
= ((1<<y
) - 1) & ((1<<64) - 1)
32 mask_a
= ((1<<x
) - 1) & ((1<<64) - 1)
33 mask_b
= (~
((1<<y
) - 1)) & ((1<<64) - 1)
34 return mask_a ^ mask_b
37 # For these tests I tried to find power instructions that would let me
38 # isolate each of these helper operations. So for instance, when I was
39 # testing the MASK() function, I chose rlwinm and rldicl because if I
40 # set the shift equal to 0 and passed in a value of all ones, the
41 # result I got would be exactly the same as the output of MASK()
43 class HelperTests(unittest
.TestCase
):
45 # Verified using rlwinm, rldicl, rldicr in qemu
47 # rlwinm reg, 1, 0, 5, 15
48 self
.assertHex(MASK(5+32, 15+32), 0x7ff0000)
49 # rlwinm reg, 1, 0, 15, 5
50 self
.assertHex(MASK(15+32, 5+32), 0xfffffffffc01ffff)
51 self
.assertHex(MASK(30+32, 2+32), 0xffffffffe0000003)
52 # rldicl reg, 1, 0, 37
53 self
.assertHex(MASK(37, 63), 0x7ffffff)
54 self
.assertHex(MASK(10, 63), 0x3fffffffffffff)
55 self
.assertHex(MASK(58, 63), 0x3f)
56 # rldicr reg, 1, 0, 37
57 self
.assertHex(MASK(0, 37), 0xfffffffffc000000)
58 self
.assertHex(MASK(0, 10), 0xffe0000000000000)
59 self
.assertHex(MASK(0, 58), 0xffffffffffffffe0)
61 def test_ROTL64(self
):
62 # r1 = 0xdeadbeef12345678
63 value
= 0xdeadbeef12345678
65 # rldicl reg, 1, 10, 0
66 self
.assertHex(ROTL64(value
, 10), 0xb6fbbc48d159e37a)
67 # rldicl reg, 1, 35, 0
68 self
.assertHex(ROTL64(value
, 35), 0x91a2b3c6f56df778)
69 self
.assertHex(ROTL64(value
, 58), 0xe37ab6fbbc48d159)
70 self
.assertHex(ROTL64(value
, 22), 0xbbc48d159e37ab6f)
72 def test_ROTL32(self
):
76 # rlwinm reg, 1, 10, 0, 31
77 self
.assertHex(ROTL32(value
, 10), 0xb6fbbf7a)
78 # rlwinm reg, 1, 17, 0, 31
79 self
.assertHex(ROTL32(value
, 17), 0x7ddfbd5b)
80 self
.assertHex(ROTL32(value
, 25), 0xdfbd5b7d)
81 self
.assertHex(ROTL32(value
, 30), 0xf7ab6fbb)
83 def test_EXTS64(self
):
84 value_a
= 0xdeadbeef # r1
85 value_b
= 0x73123456 # r2
86 value_c
= 0x80000000 # r3
89 self
.assertHex(EXTS64(value_a
), 0xffffffffdeadbeef)
91 self
.assertHex(EXTS64(value_b
), value_b
)
93 self
.assertHex(EXTS64(value_c
), 0xffffffff80000000)
97 def assertHex(self
, a
, b
):
98 msg
= "{:x} != {:x}".format(a
, b
)
99 return self
.assertEqual(a
, b
, msg
)
101 if __name__
== '__main__':