-
Notifications
You must be signed in to change notification settings - Fork 2
/
bit_vector_tests.py
235 lines (199 loc) · 4.41 KB
/
bit_vector_tests.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
from z3 import *
from constants import BV_LENGTH, bv
def check_test(test, input, output):
if not isinstance(input, list):
input = [input]
s = Solver()
bv_inputs = [BitVecVal(i, BV_LENGTH) for i in input]
s.add(test(*bv_inputs) == output)
return s.check() # s.model()[output]
def eval_test(test, input):
output = BitVec('output', BV_LENGTH)
return check_test(test, input, output)
def bitshift(x, y):
return simplify(LShR(x, bv(y))).as_long()
def ule(x, y):
return simplify(If(ULE(bv(x), bv(y)), 1, 0)).as_long()
def ult(x, y):
return simplify(If(ULT(bv(x), bv(y)), 1, 0)).as_long()
def bvredor(x):
return simplify(BVRedOr(bv(x))).as_long()
def Psimple_inc(x):
"""Increment."""
res = x + 1
return res
def Psimple_dec(x):
"""Decrement."""
res = x - 1
return res
def Psimple_and(x, y):
"""And."""
res = x & y
return res
def Psimple_or(x, y):
"""Or."""
res = x | y
return res
def Psimple_xor(x, y):
"""Xor."""
res = x ^ y
return res
def P1(x):
"""Turn-off rightmost 1 bit."""
o1 = x - 1
res = x & o1
return res
def P2(x):
"""Test whether an unsigned integer is of the form 2^n - 1, returns 0 if yes."""
o1 = x + 1
res = x & o1
return res
def P3(x):
"""Isolate the rightmost 1-bit."""
o1 = -x
res = x & o1
return res
def P4(x):
"""Form a mask that identifies the rightmost 1 bit and trailing 0s."""
o1 = x - 1
res = x ^ o1
return res
def P5(x):
"""Right propagate rightmost 1-bit."""
o1 = x - 1
res = x | o1
return res
def P6(x):
"""Turn on the rightmost 0-bit."""
o1 = x + 1
res = x | o1
return res
def P7(x):
"""Isolate the rightmost 0-bit."""
o1 = ~x
o2 = x + 1
res = o1 & o2
return res
def P8(x):
"""Form a mask that identifies the trailing 0s."""
o1 = x - 1
o2 = ~x
res = o1 & o2
return res
def P9(x):
"""Absolute value function."""
o1 = bitshift(x, BV_LENGTH - 1)
o2 = x ^ o1
res = o2 - o1
return res
def P10(x, y):
"""Test if nlz(x) == nlz(y) where nlz is number of leading zeroes."""
o1 = x & y
o2 = x ^ y
res = ule(o2, o1)
return res
def P11(x, y):
"""Test if nlz(x) < nlz(y)."""
o1 = ~y
o2 = x & o1
res = ult(y, o2)
return res
def P12(x, y):
"""Test if nlz(x) <= nlz(y)."""
o1 = ~y
o2 = x & o1
res = ule(o2, y)
return res
def P13(x):
"""Sign function."""
o1 = bitshift(x, BV_LENGTH - 1)
o2 = -x
o3 = bitshift(o2, BV_LENGTH - 1)
res = o1 | o3
return res
def P14(x, k):
"""Round up x to a multiple of kth power of 2."""
o1 = -1 << k
o2 = o1 + 1
o3 = x - o2
res = o3 & o1
return res
def P15(x, y):
"""Floor of average of two integers without overflowing."""
o1 = x & y
o2 = x ^ y
o3 = bitshift(o2, 1)
res = o1 + o3
return res
def P16(x, y):
"""Compute max of two integers."""
o1 = x ^ y
o2 = ule(y, x)
o3 = -o2
o4 = o1 & o3
res = o4 ^ y
return res
def P17(x, y):
"""Compute min of two integers."""
o1 = x ^ y
o2 = ule(x, y)
o3 = -o2
o4 = o1 & o3
res = o4 ^ y
return res
def P18(x, y):
"""Ceil of average of two integers without overflowing."""
o1 = x | y
o2 = x ^ y
o3 = bitshift(o2, 1)
res = o1 - o3
return res
def P19(x):
"""Turn off the rightmost contiguous string of 1 bits."""
o1 = x - 1
o2 = x | o1
o3 = o2 + 1
res = o3 & x
return res
def P20(x):
"""Determine whether an integer is a power of 2.
Equivalent to:
def ispower2(x):
c = str(bin(x)).count('1')
if c == 1:
return 0
return 1
"""
o1 = x - 1
o2 = bitshift(o1, BV_LENGTH - 1)
o3 = o1 & x
o4 = bvredor(o3)
res = o2 | o4
return res
def P21(x):
"""Next higher unsigned number with same number of 1 bits.
Note: Can't use 0 because of the division
"""
o1 = -x
o2 = x & o1
o3 = x + o2
o4 = x ^ o3
o5 = o4 // o2
o6 = bitshift(o5, 2)
res = o6 | o3
return res
def P22(x):
"""Round up to the next highest power of 2. (32 bit)."""
o1 = x - 1
o2 = bitshift(o1, 1)
o3 = o1 | o2
o4 = bitshift(o3, 2)
o5 = o3 | o4
o6 = bitshift(o5, 4)
o7 = o5 | o6
o8 = bitshift(o7, 8)
o9 = o7 | o8
o10 = bitshift(o9, 16)
o11 = o9 | o10
res = o11 + 1
return res