From 1105e1d4c6903edcde553a3bc190978fb8420e77 Mon Sep 17 00:00:00 2001 From: Luke Kenneth Casson Leighton Date: Tue, 19 Feb 2019 08:24:20 +0000 Subject: [PATCH] add FP16 add unit test --- src/add/test_add16.py | 35 +++++++ src/add/unit_test_half.py | 209 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 244 insertions(+) create mode 100644 src/add/test_add16.py create mode 100644 src/add/unit_test_half.py diff --git a/src/add/test_add16.py b/src/add/test_add16.py new file mode 100644 index 00000000..3beccfd3 --- /dev/null +++ b/src/add/test_add16.py @@ -0,0 +1,35 @@ +from random import randint +from random import seed +from operator import add + +from nmigen import Module, Signal +from nmigen.compat.sim import run_simulation + +from nmigen_add_experiment import FPADD + +from unit_test_half import (get_mantissa, get_exponent, get_sign, is_nan, + is_inf, is_pos_inf, is_neg_inf, + match, get_case, check_case, run_test, + run_edge_cases, run_corner_cases) + +def testbench(dut): + yield from check_case(dut, 0xfc00, 0x7c00, 0xfe00) + yield from check_case(dut, 0x8000, 0, 0) + yield from check_case(dut, 0, 0, 0) + + count = 0 + + #regression tests + stimulus_a = [ 0x8000 ] + stimulus_b = [ 0x0000 ] + yield from run_test(dut, stimulus_a, stimulus_b, add) + count += len(stimulus_a) + print (count, "vectors passed") + + yield from run_corner_cases(dut, count, add) + yield from run_edge_cases(dut, count, add) + +if __name__ == '__main__': + dut = FPADD(width=16, single_cycle=True) + run_simulation(dut, testbench(dut), vcd_name="test_add16.vcd") + diff --git a/src/add/unit_test_half.py b/src/add/unit_test_half.py new file mode 100644 index 00000000..12a31c1e --- /dev/null +++ b/src/add/unit_test_half.py @@ -0,0 +1,209 @@ +from random import randint +from random import seed + +import sys +from sfpy import Float16 + +def get_mantissa(x): + return 0x3ff & x + +def get_exponent(x): + return ((x & 0xf800) >> 11) - 15 + +def get_sign(x): + return ((x & 0x8000) >> 15) + +def is_nan(x): + return get_exponent(x) == 16 and get_mantissa(x) != 0 + +def is_inf(x): + return get_exponent(x) == 16 and get_mantissa(x) == 0 + +def is_pos_inf(x): + return is_inf(x) and not get_sign(x) + +def is_neg_inf(x): + return is_inf(x) and get_sign(x) + +def match(x, y): + return ( + (is_pos_inf(x) and is_pos_inf(y)) or + (is_neg_inf(x) and is_neg_inf(y)) or + (is_nan(x) and is_nan(y)) or + (x == y) + ) + +def get_case(dut, a, b): + yield dut.in_a.v.eq(a) + yield dut.in_a.stb.eq(1) + yield + yield + a_ack = (yield dut.in_a.ack) + assert a_ack == 0 + yield dut.in_b.v.eq(b) + yield dut.in_b.stb.eq(1) + b_ack = (yield dut.in_b.ack) + assert b_ack == 0 + + while True: + yield + out_z_stb = (yield dut.out_z.stb) + if not out_z_stb: + continue + yield dut.in_a.stb.eq(0) + yield dut.in_b.stb.eq(0) + yield dut.out_z.ack.eq(1) + yield + yield dut.out_z.ack.eq(0) + yield + yield + break + + out_z = yield dut.out_z.v + return out_z + +def check_case(dut, a, b, z): + out_z = yield from get_case(dut, a, b) + assert out_z == z, "Output z 0x%x not equal to expected 0x%x" % (out_z, z) + + +def run_test(dut, stimulus_a, stimulus_b, op): + + expected_responses = [] + actual_responses = [] + for a, b in zip(stimulus_a, stimulus_b): + af = Float16.from_bits(a) + bf = Float16.from_bits(b) + z = op(af, bf) + expected_responses.append(z.get_bits()) + #print (af, bf, z) + actual = yield from get_case(dut, a, b) + actual_responses.append(actual) + + if len(actual_responses) < len(expected_responses): + print ("Fail ... not enough results") + exit(0) + + for expected, actual, a, b in zip(expected_responses, actual_responses, + stimulus_a, stimulus_b): + passed = match(expected, actual) + + if not passed: + + print ("Fail ... expected:", hex(expected), "actual:", hex(actual)) + + print (hex(a)) + print ("a mantissa:", get_mantissa(a)) + print ("a exponent:", get_exponent(a)) + print ("a sign:", get_sign(a)) + + print (hex(b)) + print ("b mantissa:", get_mantissa(b)) + print ("b exponent:", get_exponent(b)) + print ("b sign:", get_sign(b)) + + print (hex(expected)) + print ("expected mantissa:", get_mantissa(expected)) + print ("expected exponent:", get_exponent(expected)) + print ("expected sign:", get_sign(expected)) + + print (hex(actual)) + print ("actual mantissa:", get_mantissa(actual)) + print ("actual exponent:", get_exponent(actual)) + print ("actual sign:", get_sign(actual)) + + sys.exit(0) + +def run_corner_cases(dut, count, op): + #corner cases + corners = [0x8000, 0x0000, 0x7800, 0xf800, 0x7c00, 0xfc00] + from itertools import permutations + stimulus_a = [i[0] for i in permutations(corners, 2)] + stimulus_b = [i[1] for i in permutations(corners, 2)] + yield from run_test(dut, stimulus_a, stimulus_b, op) + count += len(stimulus_a) + print (count, "vectors passed") + + +def run_edge_cases(dut, count, op): + #edge cases + stimulus_a = [0x8000 for i in range(1000)] + stimulus_b = [randint(0, 1<<16) for i in range(1000)] + yield from run_test(dut, stimulus_a, stimulus_b, op) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_a = [0x0000 for i in range(1000)] + stimulus_b = [randint(0, 1<<16) for i in range(1000)] + yield from run_test(dut, stimulus_a, stimulus_b, op) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_b = [0x8000 for i in range(1000)] + stimulus_a = [randint(0, 1<<16) for i in range(1000)] + yield from run_test(dut, stimulus_a, stimulus_b, op) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_b = [0x0000 for i in range(1000)] + stimulus_a = [randint(0, 1<<16) for i in range(1000)] + yield from run_test(dut, stimulus_a, stimulus_b, op) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_a = [0x7800 for i in range(1000)] + stimulus_b = [randint(0, 1<<16) for i in range(1000)] + yield from run_test(dut, stimulus_a, stimulus_b, op) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_a = [0xF800 for i in range(1000)] + stimulus_b = [randint(0, 1<<16) for i in range(1000)] + yield from run_test(dut, stimulus_a, stimulus_b, op) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_b = [0x7800 for i in range(1000)] + stimulus_a = [randint(0, 1<<16) for i in range(1000)] + yield from run_test(dut, stimulus_a, stimulus_b, op) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_b = [0xF800 for i in range(1000)] + stimulus_a = [randint(0, 1<<16) for i in range(1000)] + yield from run_test(dut, stimulus_a, stimulus_b, op) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_a = [0x7C00 for i in range(1000)] + stimulus_b = [randint(0, 1<<16) for i in range(1000)] + yield from run_test(dut, stimulus_a, stimulus_b, op) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_a = [0xFC00 for i in range(1000)] + stimulus_b = [randint(0, 1<<16) for i in range(1000)] + yield from run_test(dut, stimulus_a, stimulus_b, op) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_b = [0x7C00 for i in range(1000)] + stimulus_a = [randint(0, 1<<16) for i in range(1000)] + yield from run_test(dut, stimulus_a, stimulus_b, op) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_b = [0xFC00 for i in range(1000)] + stimulus_a = [randint(0, 1<<16) for i in range(1000)] + yield from run_test(dut, stimulus_a, stimulus_b, op) + count += len(stimulus_a) + print (count, "vectors passed") + + #seed(0) + for i in range(100000): + stimulus_a = [randint(0, 1<<16) for i in range(1000)] + stimulus_b = [randint(0, 1<<16) for i in range(1000)] + yield from run_test(dut, stimulus_a, stimulus_b, op) + count += 1000 + print (count, "random vectors passed") + -- 2.30.2