2021-01-21 15:20:22 +00:00
|
|
|
from z3 import BitVecVal, Concat, If
|
2019-06-14 15:47:54 +00:00
|
|
|
|
|
|
|
def BVUnsignedUpCast(x, n_bits):
|
2021-01-21 15:17:01 +00:00
|
|
|
assert x.size() <= n_bits
|
2019-06-14 15:47:54 +00:00
|
|
|
if x.size() < n_bits:
|
|
|
|
return Concat(BitVecVal(0, n_bits - x.size()), x)
|
|
|
|
else:
|
|
|
|
return x
|
|
|
|
|
|
|
|
def BVUnsignedMax(type_bits, n_bits):
|
2021-01-21 15:17:01 +00:00
|
|
|
assert type_bits <= n_bits
|
2019-06-14 15:47:54 +00:00
|
|
|
return BitVecVal((1 << type_bits) - 1, n_bits)
|
|
|
|
|
|
|
|
def BVSignedUpCast(x, n_bits):
|
2021-01-21 15:17:01 +00:00
|
|
|
assert x.size() <= n_bits
|
2019-06-14 15:47:54 +00:00
|
|
|
if x.size() < n_bits:
|
|
|
|
return Concat(If(x < 0, BitVecVal(-1, n_bits - x.size()), BitVecVal(0, n_bits - x.size())), x)
|
|
|
|
else:
|
|
|
|
return x
|
|
|
|
|
|
|
|
def BVSignedMax(type_bits, n_bits):
|
2021-01-21 15:17:01 +00:00
|
|
|
assert type_bits <= n_bits
|
2019-06-14 15:47:54 +00:00
|
|
|
return BitVecVal((1 << (type_bits - 1)) - 1, n_bits)
|
|
|
|
|
|
|
|
def BVSignedMin(type_bits, n_bits):
|
2021-01-21 15:17:01 +00:00
|
|
|
assert type_bits <= n_bits
|
2019-06-14 15:47:54 +00:00
|
|
|
return BitVecVal(-(1 << (type_bits - 1)), n_bits)
|
2022-06-20 22:19:20 +00:00
|
|
|
|
|
|
|
def BVSignedCleanupFunction(x, type_bits):
|
|
|
|
assert x.size() >= type_bits
|
|
|
|
sign_mask = BitVecVal(1, x.size()) << (type_bits - 1)
|
|
|
|
bit_mask = (BitVecVal(1, x.size()) << type_bits) - 1
|
|
|
|
return If(
|
|
|
|
x & sign_mask == 0,
|
|
|
|
x & bit_mask,
|
|
|
|
x | ~bit_mask
|
|
|
|
)
|
|
|
|
|
|
|
|
def BVUnsignedCleanupFunction(x, type_bits):
|
|
|
|
assert x.size() >= type_bits
|
|
|
|
bit_mask = (BitVecVal(1, x.size()) << type_bits) - 1
|
|
|
|
return x & bit_mask
|