From: Luke Kenneth Casson Leighton Date: Mon, 18 Feb 2019 11:27:03 +0000 (+0000) Subject: add unit tests X-Git-Tag: ls180-24jan2020~1915 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=4c62da6b74c69264380985d3041f5a81d52e562b;p=ieee754fpu.git add unit tests --- diff --git a/.gitmodules b/.gitmodules index c6f586c8..a556ea28 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "berkeley-softfloat-3"] path = berkeley-softfloat-3 url = https://github.com/ucb-bar/berkeley-softfloat-3 +[submodule "berkeley-testfloat-3"] + path = berkeley-testfloat-3 + url = https://github.com/ucb-bar/berkeley-testfloat-3 diff --git a/berkeley-testfloat-3 b/berkeley-testfloat-3 new file mode 160000 index 00000000..06b20075 --- /dev/null +++ b/berkeley-testfloat-3 @@ -0,0 +1 @@ +Subproject commit 06b20075dd3c1a5d0dd007a93643282832221612 diff --git a/src/add/test_add.py b/src/add/test_add.py index d3f62745..4bfb3234 100644 --- a/src/add/test_add.py +++ b/src/add/test_add.py @@ -1,22 +1,44 @@ +import sys +import subprocess +from random import randint +from random import seed +from sfpy import Float32 + from nmigen import Module, Signal from nmigen.compat.sim import run_simulation from nmigen_add_experiment import FPADD -class ORGate: - def __init__(self): - self.a = Signal() - self.b = Signal() - self.x = Signal() +def get_mantissa(x): + return 0x7fffff & x - def get_fragment(self, platform=None): +def get_exponent(x): + return ((x & 0x7f800000) >> 23) - 127 - m = Module() - m.d.comb += self.x.eq(self.a | self.b) +def get_sign(x): + return ((x & 0x80000000) >> 31) - return m +def is_nan(x): + return get_exponent(x) == 128 and get_mantissa(x) != 0 -def check_case(dut, a, b, z): +def is_inf(x): + return get_exponent(x) == 128 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 @@ -43,10 +65,63 @@ def check_case(dut, a, b, z): 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): + + expected_responses = [] + actual_responses = [] + for a, b in zip(stimulus_a, stimulus_b): + af = Float32.from_bits(a) + bf = Float32.from_bits(b) + z = 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:", a & 0x7fffff) + print ("a exponent:", ((a & 0x7f800000) >> 23) - 127) + print ("a sign:", ((a & 0x80000000) >> 31)) + + print (hex(b)) + print ("b mantissa:", b & 0x7fffff) + print ("b exponent:", ((b & 0x7f800000) >> 23) - 127) + print ("b sign:", ((b & 0x80000000) >> 31)) + + print (hex(expected)) + print ("expected mantissa:", expected & 0x7fffff) + print ("expected exponent:", ((expected & 0x7f800000) >> 23) - 127) + print ("expected sign:", ((expected & 0x80000000) >> 31)) + + print (hex(actual)) + print ("actual mantissa:", actual & 0x7fffff) + print ("actual exponent:", ((actual & 0x7f800000) >> 23) - 127) + print ("actual sign:", ((actual & 0x80000000) >> 31)) + + sys.exit(0) + def testbench(dut): yield from check_case(dut, 0, 0, 0) + yield from check_case(dut, 0x40000000, 0xc0000000, 0x00000000) yield from check_case(dut, 0x3F800000, 0x40000000, 0x40400000) yield from check_case(dut, 0x40000000, 0x3F800000, 0x40400000) yield from check_case(dut, 0x447A0000, 0x4488B000, 0x4502D800) @@ -79,6 +154,104 @@ def testbench(dut): #yield from check_case(dut, 1, 0, 1) #yield from check_case(dut, 1, 1, 1) + count = 0 + + #regression tests + stimulus_a = [0x22cb525a, 0x40000000, 0x83e73d5c, 0xbf9b1e94, 0x34082401, 0x5e8ef81, 0x5c75da81, 0x2b017] + stimulus_b = [0xadd79efa, 0xC0000000, 0x1c800000, 0xc038ed3a, 0xb328cd45, 0x114f3db, 0x2f642a39, 0xff3807ab] + yield from run_test(dut, stimulus_a, stimulus_b) + count += len(stimulus_a) + print (count, "vectors passed") + + #corner cases + from itertools import permutations + stimulus_a = [i[0] for i in permutations([0x80000000, 0x00000000, 0x7f800000, 0xff800000, 0x7fc00000, 0xffc00000], 2)] + stimulus_b = [i[1] for i in permutations([0x80000000, 0x00000000, 0x7f800000, 0xff800000, 0x7fc00000, 0xffc00000], 2)] + yield from run_test(dut, stimulus_a, stimulus_b) + count += len(stimulus_a) + print (count, "vectors passed") + + #edge cases + stimulus_a = [0x80000000 for i in xrange(1000)] + stimulus_b = [randint(0, 1<<32) for i in xrange(1000)] + yield from run_test(dut, stimulus_a, stimulus_b) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_a = [0x00000000 for i in xrange(1000)] + stimulus_b = [randint(0, 1<<32) for i in xrange(1000)] + yield from run_test(dut, stimulus_a, stimulus_b) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_b = [0x80000000 for i in xrange(1000)] + stimulus_a = [randint(0, 1<<32) for i in xrange(1000)] + yield from run_test(dut, stimulus_a, stimulus_b) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_b = [0x00000000 for i in xrange(1000)] + stimulus_a = [randint(0, 1<<32) for i in xrange(1000)] + yield from run_test(dut, stimulus_a, stimulus_b) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_a = [0x7F800000 for i in xrange(1000)] + stimulus_b = [randint(0, 1<<32) for i in xrange(1000)] + yield from run_test(dut, stimulus_a, stimulus_b) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_a = [0xFF800000 for i in xrange(1000)] + stimulus_b = [randint(0, 1<<32) for i in xrange(1000)] + yield from run_test(dut, stimulus_a, stimulus_b) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_b = [0x7F800000 for i in xrange(1000)] + stimulus_a = [randint(0, 1<<32) for i in xrange(1000)] + yield from run_test(dut, stimulus_a, stimulus_b) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_b = [0xFF800000 for i in xrange(1000)] + stimulus_a = [randint(0, 1<<32) for i in xrange(1000)] + yield from run_test(dut, stimulus_a, stimulus_b) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_a = [0x7FC00000 for i in xrange(1000)] + stimulus_b = [randint(0, 1<<32) for i in xrange(1000)] + yield from run_test(dut, stimulus_a, stimulus_b) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_a = [0xFFC00000 for i in xrange(1000)] + stimulus_b = [randint(0, 1<<32) for i in xrange(1000)] + yield from run_test(dut, stimulus_a, stimulus_b) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_b = [0x7FC00000 for i in xrange(1000)] + stimulus_a = [randint(0, 1<<32) for i in xrange(1000)] + yield from run_test(dut, stimulus_a, stimulus_b) + count += len(stimulus_a) + print (count, "vectors passed") + + stimulus_b = [0xFFC00000 for i in xrange(1000)] + stimulus_a = [randint(0, 1<<32) for i in xrange(1000)] + yield from run_test(dut, stimulus_a, stimulus_b) + count += len(stimulus_a) + print (count, "vectors passed") + + #seed(0) + for i in xrange(100000): + stimulus_a = [randint(0, 1<<32) for i in xrange(1000)] + stimulus_b = [randint(0, 1<<32) for i in xrange(1000)] + yield from run_test(dut, stimulus_a, stimulus_b) + count += 1000 + print (count, "vectors passed") + if __name__ == '__main__': dut = FPADD(width=32, single_cycle=True) run_simulation(dut, testbench(dut), vcd_name="test_add.vcd")