Exemplo n.º 1
0
def test_comparison(op, reference, width):
    for _ in range(NTESTS):
        I0, I1 = SIntVector.random(width), SIntVector.random(width)
        if op is operator.floordiv and I1 == 0:
            # Skip divide by zero
            continue
        expected = Bit(reference(int(I0), int(I1)))
        assert expected == bool(op(I0, I1))
Exemplo n.º 2
0
        #print("ln", bfbin2float(test_vector[0]), bfbin2float(test_vector[1]),
        #      golden_res, actual_res, delta, max_error)

        assert delta <= max_error


def test_add32_targeted():
    add32 = Add32()
    assert Data32(10) == add32(Data32(2), Data32(8))
    assert Data32(100000) == add32(Data32(20000), Data32(80000))
    assert Data32(2**17 - 2) == add32(Data32(2**16 - 1), Data32(2**16 - 1))
    assert Data32(2**31 - 2) == add32(Data32(2**30 - 1), Data32(2**30 - 1))


op = namedtuple("op", ["complex", "func"])


@pytest.mark.parametrize("op", [
    op(Add32, lambda x, y: x + y),
    op(Sub32, lambda x, y: x - y),
])
@pytest.mark.parametrize("args",
                         [(SIntVector.random(32), SIntVector.random(32))
                          for _ in range(NTESTS)])
def test_addsub(op, args):
    cop = op.complex()
    in0 = args[0]
    in1 = args[1]
    res = cop(in0, in1)
    assert res == op.func(in0, in1)
Exemplo n.º 3
0
#Generate random bfloat
def random_bfloat():
    return fpdata(BitVector.random(1), BitVector.random(8), BitVector.random(7))

@pytest.mark.parametrize("fpdata", [
    random_bfloat() for _ in range(NTESTS)
])
def test_bfloat_construct(fpdata):
    fp = BFloat(fpdata)
    assert fp[-1] == fpdata.sign
    assert fp[7:-1] == fpdata.exp
    assert fp[:7] == fpdata.frac

@pytest.mark.parametrize("args", [
    (random_bfloat(), SIntVector.random(DATAWIDTH)) for _ in range(NTESTS)
])
def test_get_mant(args):
    #output = input.mantissa (7 bit)
    fp0 = args[0]
    in0 = BFloat(fp0)
    in1 = args[1]
    inst = asm.fgetmant()
    res, res_p, _ = pe(inst, in0, in1)
    assert res == Data(fp0.frac)
    rtl_tester(inst, in0, in1, res=Data(fp0.frac))


def test_add_exp_imm_targeted():
    inst = asm.faddiexp()
    data0 = Data(0x7F8A)
Exemplo n.º 4
0
def test_operator_by_0(op, reference):
    I0, I1 = SIntVector.random(5), 0
    expected = signed(reference(int(I0), int(I1)), 5)
    assert expected == int(op(I0, I1))
Exemplo n.º 5
0
def test_operator_int_shift(op, reference, width):
    for _ in range(NTESTS):
        I0, I1 = SIntVector.random(width), UIntVector.random(width)
        expected = signed(reference(int(I0), int(I1)), width)
        assert expected == int(op(I0, I1))
Exemplo n.º 6
0
def test_operator_int1(op, reference, width):
    for _ in range(NTESTS):
        I = SIntVector.random(width)
        expected = signed(reference(int(I)), width)
        assert expected == int(op(I))
Exemplo n.º 7
0
             for _ in range(NTESTS)])
def test_unsigned_binary(op, args):
    x, y = args
    res, _, _ = pe(op.inst, Data(x), Data(y))
    assert res == op.func(x, y)
    rtl_tester(op, x, y, res=res)


@pytest.mark.parametrize("op", [
    op(asm.lsl(), lambda x, y: x << y),
    op(asm.asr(), lambda x, y: x >> y),
    op(asm.smin(), lambda x, y: (x < y).ite(x, y)),
    op(asm.smax(), lambda x, y: (x > y).ite(x, y)),
])
@pytest.mark.parametrize(
    "args", [(SIntVector.random(DATAWIDTH), SIntVector.random(DATAWIDTH))
             for _ in range(NTESTS)])
def test_signed_binary(op, args):
    x, y = args
    res, _, _ = pe(op.inst, Data(x), Data(y))
    assert res == op.func(x, y)
    rtl_tester(op, x, y, res=res)


@pytest.mark.parametrize("op", [
    op(asm.abs(), lambda x: x if x > 0 else -x),
])
@pytest.mark.parametrize("args",
                         [SIntVector.random(DATAWIDTH) for _ in range(NTESTS)])
def test_signed_unary(op, args):
    x = args