Esempio n. 1
0
def test_signed_add():
    A = bitvec('A', 8)
    B = bitvec('B', 8)

    for adder in (rca, ksa, bka):
        S, C = adder(A, B)

        # 0 + 0 = 0
        assert sadd(S, A, B, 0, 0) == 0
        # -64 + -64 = -128
        assert sadd(S, A, B, -64, -64) == -128
        # -1 + 1 = 0
        assert sadd(S, A, B, -1, 1) == 0
        # -64 + 64 = 0
        assert sadd(S, A, B, -64, 64) == 0

        # signed random vectors
        for i in range(NVECS):
            ra = random.randint(-2**6, 2**6-1) # -64..63
            rb = random.randint(-2**6, 2**6)   # -64..64
            assert sadd(S, A, B, ra, rb) == ra + rb

        # 64 + 64, overflow
        R = C.vrestrict({A: int2vec(64, 8), B: int2vec(64, 8)})
        assert R[7] != R[6]
        # -65 + -64, overflow
        R = C.vrestrict({A: int2vec(-65, 8), B: int2vec(-64, 8)})
        assert R[7] != R[6]
Esempio n. 2
0
def test_bitvec():
    assert_raises(TypeError, bitvec, 'x', "foo")
    X = bitvec('x')
    assert X.name == 'x'
    X = bitvec('x', 4)
    assert X.shape[0][0] == 0 and X.shape[0][1] == 4 and len(X.items) == 4
    X = bitvec('x', (4, 8))
    assert X.shape[0][0] == 4 and X.shape[0][1] == 8 and len(X.items) == 4
Esempio n. 3
0
def test_ops():
    X = bitvec('x', 4)
    Y = bitvec('y', 4)

    assert X.uor().equivalent(X[3] | X[2] | X[1] | X[0])
    assert X.unor().equivalent(~(X[3] | X[2] | X[1] | X[0]))
    assert X.uand().equivalent(X[3] & X[2] & X[1] & X[0])
    assert X.unand().equivalent(~(X[3] & X[2] & X[1] & X[0]))
    assert X.uxor().equivalent(X[3] ^ X[2] ^ X[1] ^ X[0])
    assert X.uxnor().equivalent(~(X[3] ^ X[2] ^ X[1] ^ X[0]))

    assert str(~X) == "farray([~x[0], ~x[1], ~x[2], ~x[3]])"
    assert str(X | Y) == "farray([Or(x[0], y[0]), Or(x[1], y[1]), Or(x[2], y[2]), Or(x[3], y[3])])"
    assert str(X & Y) == "farray([And(x[0], y[0]), And(x[1], y[1]), And(x[2], y[2]), And(x[3], y[3])])"
    assert str(X ^ Y) == "farray([Xor(x[0], y[0]), Xor(x[1], y[1]), Xor(x[2], y[2]), Xor(x[3], y[3])])"
Esempio n. 4
0
def test_decode():
    A = bitvec('a', 2)
    d = A.decode()
    d.vrestrict({A: "00"}) == [1, 0, 0, 0]
    d.vrestrict({A: "10"}) == [0, 1, 0, 0]
    d.vrestrict({A: "01"}) == [0, 0, 1, 0]
    d.vrestrict({A: "11"}) == [0, 0, 0, 1]
Esempio n. 5
0
def test_shift():
    X = bitvec('x', 8)
    Y = bitvec('y', 4)

    # left shift
    assert_raises(ValueError, X.lsh, -1)
    assert_raises(ValueError, X.lsh, 9)
    assert_raises(ValueError, X.lsh, 2, Y)
    a, b = X.lsh(4)
    assert str(a) == "farray([0, 0, 0, 0, x[0], x[1], x[2], x[3]])"
    assert str(b) == "farray([x[4], x[5], x[6], x[7]])"
    a, b = X.lsh(4, Y)
    assert str(a) == "farray([y[0], y[1], y[2], y[3], x[0], x[1], x[2], x[3]])"
    assert str(b) == "farray([x[4], x[5], x[6], x[7]])"
    a, b = X.lsh(0)
    assert str(a) == "farray([x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7]])"
    assert not b
    assert_raises(TypeError, X.__lshift__, "foo")
    assert str(X << 4) == "farray([0, 0, 0, 0, x[0], x[1], x[2], x[3]])"
    assert str(X << (4, Y)) == "farray([y[0], y[1], y[2], y[3], x[0], x[1], x[2], x[3]])"

    # right shift
    assert_raises(ValueError, X.rsh, -1)
    assert_raises(ValueError, X.rsh, 9)
    assert_raises(ValueError, X.rsh, 2, Y)
    a, b = X.rsh(4)
    assert str(a) == "farray([x[4], x[5], x[6], x[7], 0, 0, 0, 0])"
    assert str(b) == "farray([x[0], x[1], x[2], x[3]])"
    a, b = X.rsh(4, Y)
    assert str(a) == "farray([x[4], x[5], x[6], x[7], y[0], y[1], y[2], y[3]])"
    assert str(b) == "farray([x[0], x[1], x[2], x[3]])"
    a, b = X.rsh(0)
    assert str(a) == "farray([x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7]])"
    assert not b
    assert_raises(TypeError, X.__rshift__, "foo")
    assert str(X >> 4) == "farray([x[4], x[5], x[6], x[7], 0, 0, 0, 0])"
    assert str(X >> (4, Y)) == "farray([x[4], x[5], x[6], x[7], y[0], y[1], y[2], y[3]])"

    # arithmetic right shift
    assert_raises(ValueError, X.arsh, -1)
    assert_raises(ValueError, X.arsh, 9)
    a, b = X.arsh(4)
    assert str(a) == "farray([x[4], x[5], x[6], x[7], x[7], x[7], x[7], x[7]])"
    assert str(b) == "farray([x[0], x[1], x[2], x[3]])"
    a, b = X.arsh(0)
    assert str(a) == "farray([x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7]])"
    assert not b
def test_espresso():
    A = bitvec('a', 16)
    B = bitvec('b', 16)
    S, C = ripple_carry_add(A, B)
    s0, s1, s2, s3 = espresso_exprs(S[0].to_dnf(), S[1].to_dnf(),
                                    S[2].to_dnf(), S[3].to_dnf())

    assert s0.equivalent(S[0])
    assert s1.equivalent(S[1])
    assert s2.equivalent(S[2])
    assert s3.equivalent(S[3])

    X = bitvec('x', 4)
    f1 = truthtable(X, "0000011111------")
    f2 = truthtable(X, "0001111100------")
    f1m, f2m = espresso_tts(f1, f2)
    truthtable2expr(f1).equivalent(f1m)
    truthtable2expr(f2).equivalent(f2m)
Esempio n. 7
0
def test_unsigned_add():
    N = 9

    A = bitvec('A', N)
    B = bitvec('B', N)

    for adder in (rca, ksa, bka):
        S, C = adder(A, B)
        S.append(C[N-1])

        # 0 + 0 = 0
        assert uadd(S, A, B, 0, 0) == 0
        # 255 + 255 = 510
        assert uadd(S, A, B, 2**N-1, 2**N-1) == (2**(N+1)-2)
        # 255 + 1 = 256
        assert uadd(S, A, B, 2**N-1, 1) == 2**N

        # unsigned random vectors
        for i in range(NVECS):
            ra = random.randint(0, 2**N-1)
            rb = random.randint(0, 2**N-1)
            assert uadd(S, A, B, ra, rb) == ra + rb
 def __init__(self, varname='x'):
     self.X = bitvec(varname, (1, 10), (1, 10), (1, 10))
     V = And(*[
             And(*[OneHot(*[self.X[r][c][v] for v in range(1, 10)])
                   for c in range(1, 10)])
               for r in range(1, 10)])
     R = And(*[
             And(*[OneHot(*[self.X[r][c][v] for c in range(1, 10)])
                   for v in range(1, 10)])
               for r in range(1, 10)])
     C = And(*[
             And(*[OneHot(*[self.X[r][c][v] for r in range(1, 10)])
                   for v in range(1, 10)])
               for c in range(1, 10)])
     B = And(*[
             And(*[OneHot(*[self.X[3*br+r][3*bc+c][v]
                            for r in range(1, 4)
                            for c in range(1, 4)])
                   for v in range(1, 10)])
               for br in range(3) for bc in range(3)])
     self.S = expr2dimacscnf(And(V, R, C, B))
Esempio n. 9
0
    exprvar,
    Expression,
    Not, Or, And, Nor, Nand, Xor, Xnor, Equal, Unequal, Implies, ITE,
    OneHot0, OneHot, Majority, AchillesHeel,
    EXPRZERO, EXPRONE,
)
from pyeda.boolalg.vexpr import bitvec

from nose.tools import assert_raises

a, b, c, d, e, p, q, s = map(exprvar, 'abcdepqs')

MAJOR = sys.version_info.major
MINOR = sys.version_info.minor

X = bitvec('x', 16)
Y = bitvec('y', 16, 16, 16)

def test_misc():
    f = a * b + a * c + b * c

    assert f.smoothing(a).equivalent(b + c)
    assert f.consensus(a).equivalent(b * c)
    assert f.derivative(a).equivalent(b * -c + -b * c)

def test_unate():
    # c' * (a' + b')
    f = -c * (-a + -b)
    assert f.is_neg_unate([a, b, c])
    assert f.is_neg_unate([a, b])
    assert f.is_neg_unate([a, c])
Esempio n. 10
0
def test_bin2gray():
    B = bitvec('B', 4)
    G = bin2gray(B)
    gnums = [G.vrestrict({B: uint2vec(i, 4)}).to_uint() for i in range(16)]
    assert gnums == [0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8]
Esempio n. 11
0
def test_gray2bin():
    G = bitvec('G', 4)
    B = gray2bin(G)
    gnums = [0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8]
    bnums = [B.vrestrict({G: uint2vec(i, 4)}).to_uint() for i in gnums]
    assert bnums == list(range(16))
def test_errors():
    A = bitvec('A', 7)
    B = bitvec('B', 9)

    for adder in (rca, ksa, bka):
        assert_raises(ValueError, adder, A, B)