From 628b86b268cef21f799f47b7c4a7f5a32c8d4c9c Mon Sep 17 00:00:00 2001 From: Luke Kenneth Casson Leighton Date: Thu, 28 Feb 2019 00:50:10 +0000 Subject: [PATCH] sorting out unit tests, comply with IEEE754 on RISCV --- src/add/nmigen_add_experiment.py | 4 +-- src/add/test_add.py | 15 +++++---- src/add/test_add16.py | 10 +++--- src/add/test_add64.py | 2 +- src/add/unit_test_half.py | 56 +++++++++++++++++--------------- 5 files changed, 45 insertions(+), 42 deletions(-) diff --git a/src/add/nmigen_add_experiment.py b/src/add/nmigen_add_experiment.py index 649d241a..f7b57fc0 100644 --- a/src/add/nmigen_add_experiment.py +++ b/src/add/nmigen_add_experiment.py @@ -80,7 +80,7 @@ class FPAddSpecialCasesMod: # if a is NaN or b is NaN return NaN with m.If(self.in_a.is_nan | self.in_b.is_nan): m.d.comb += self.out_do_z.eq(1) - m.d.comb += self.out_z.nan(1) + m.d.comb += self.out_z.nan(0) # XXX WEIRDNESS for FP16 non-canonical NaN handling # under review @@ -111,7 +111,7 @@ class FPAddSpecialCasesMod: m.d.comb += self.out_z.inf(self.in_a.s) # if a is inf and signs don't match return NaN with m.If(self.in_b.exp_128 & s_nomatch): - m.d.comb += self.out_z.nan(1) + m.d.comb += self.out_z.nan(0) # if b is inf return inf with m.Elif(self.in_b.is_inf): diff --git a/src/add/test_add.py b/src/add/test_add.py index d3ec562c..0d9eb858 100644 --- a/src/add/test_add.py +++ b/src/add/test_add.py @@ -1,5 +1,3 @@ -from random import randint -from random import seed from operator import add from nmigen import Module, Signal @@ -14,10 +12,10 @@ from unit_test_single import (get_mantissa, get_exponent, get_sign, is_nan, def testbench(dut): yield from check_case(dut, 0, 0, 0) - yield from check_case(dut, 0xFFFFFFFF, 0xC63B800A, 0xFFC00000) - yield from check_case(dut, 0xFF800000, 0x7F800000, 0xFFC00000) + yield from check_case(dut, 0xFFFFFFFF, 0xC63B800A, 0x7FC00000) + yield from check_case(dut, 0xFF800000, 0x7F800000, 0x7FC00000) #yield from check_case(dut, 0xFF800000, 0x7F800000, 0x7FC00000) - yield from check_case(dut, 0x7F800000, 0xFF800000, 0xFFC00000) + yield from check_case(dut, 0x7F800000, 0xFF800000, 0x7FC00000) yield from check_case(dut, 0x42540000, 0xC2540000, 0x00000000) yield from check_case(dut, 0xC2540000, 0x42540000, 0x00000000) yield from check_case(dut, 0xfe34f995, 0xff5d59ad, 0xff800000) @@ -38,6 +36,7 @@ def testbench(dut): yield from check_case(dut, 0x00000000, 0xFF800000, 0xFF800000) yield from check_case(dut, 0x7F800000, 0x7F800000, 0x7F800000) yield from check_case(dut, 0xFF800000, 0xFF800000, 0xFF800000) + yield from check_case(dut, 0xFF800000, 0x7F800000, 0x7FC00000) yield from check_case(dut, 0x00018643, 0x00FA72A4, 0x00FBF8E7) yield from check_case(dut, 0x001A2239, 0x00FA72A4, 0x010A4A6E) yield from check_case(dut, 0x3F7FFFFE, 0x3F7FFFFE, 0x3FFFFFFE) @@ -56,9 +55,11 @@ def testbench(dut): count = 0 #regression tests - stimulus_a = [0x22cb525a, 0x40000000, 0x83e73d5c, 0xbf9b1e94, 0x34082401, + stimulus_a = [0x80000000, 0x22cb525a, 0x40000000, 0x83e73d5c, + 0xbf9b1e94, 0x34082401, 0x5e8ef81, 0x5c75da81, 0x2b017] - stimulus_b = [0xadd79efa, 0xC0000000, 0x1c800000, 0xc038ed3a, 0xb328cd45, + stimulus_b = [0xff800001, 0xadd79efa, 0xC0000000, 0x1c800000, + 0xc038ed3a, 0xb328cd45, 0x114f3db, 0x2f642a39, 0xff3807ab] yield from run_test(dut, stimulus_a, stimulus_b, add) count += len(stimulus_a) diff --git a/src/add/test_add16.py b/src/add/test_add16.py index 4ee8428b..41b35f68 100644 --- a/src/add/test_add16.py +++ b/src/add/test_add16.py @@ -1,5 +1,3 @@ -from random import randint -from random import seed from operator import add from nmigen import Module, Signal @@ -22,15 +20,17 @@ def testbench(dut): #yield from check_case(dut, 0x8000, 0x7c8c, 0x7e8c) #yield from check_case(dut, 0x8000, 0xfc55, 0xfe55) #yield from check_case(dut, 0x8000, 0x7e1a, 0x7e1a) - #yield from check_case(dut, 0xfc00, 0x7c00, 0xfe00) + + #yield from check_case(dut, 0x8000, 0xfc01, 0x7e00) + yield from check_case(dut, 0xfc00, 0x7c00, 0x7e00) 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 ] + stimulus_a = [ 0x8000, 0x8000 ] + stimulus_b = [ 0x0000, 0xfc01 ] yield from run_test(dut, stimulus_a, stimulus_b, add) count += len(stimulus_a) print (count, "vectors passed") diff --git a/src/add/test_add64.py b/src/add/test_add64.py index 5890251b..dcca12c6 100644 --- a/src/add/test_add64.py +++ b/src/add/test_add64.py @@ -40,6 +40,6 @@ def testbench(dut): if __name__ == '__main__': - dut = FPADD(width=64, single_cycle=True) + dut = FPADD(width=64, single_cycle=False) run_simulation(dut, testbench(dut), vcd_name="test_add64.vcd") diff --git a/src/add/unit_test_half.py b/src/add/unit_test_half.py index 12a31c1e..73c9b653 100644 --- a/src/add/unit_test_half.py +++ b/src/add/unit_test_half.py @@ -126,84 +126,86 @@ def run_corner_cases(dut, count, op): def run_edge_cases(dut, count, op): + maxint16 = 1<<16 + maxcount = 10 #edge cases - stimulus_a = [0x8000 for i in range(1000)] - stimulus_b = [randint(0, 1<<16) for i in range(1000)] + stimulus_a = [0x8000 for i in range(maxcount)] + stimulus_b = [randint(0, maxint16-1) for i in range(maxcount)] 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)] + stimulus_a = [0x0000 for i in range(maxcount)] + stimulus_b = [randint(0, maxint16-1) for i in range(maxcount)] 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)] + stimulus_b = [0x8000 for i in range(maxcount)] + stimulus_a = [randint(0, maxint16-1) for i in range(maxcount)] 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)] + stimulus_b = [0x0000 for i in range(maxcount)] + stimulus_a = [randint(0, maxint16-1) for i in range(maxcount)] 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)] + stimulus_a = [0x7800 for i in range(maxcount)] + stimulus_b = [randint(0, maxint16-1) for i in range(maxcount)] 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)] + stimulus_a = [0xF800 for i in range(maxcount)] + stimulus_b = [randint(0, maxint16-1) for i in range(maxcount)] 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)] + stimulus_b = [0x7800 for i in range(maxcount)] + stimulus_a = [randint(0, maxint16-1) for i in range(maxcount)] 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)] + stimulus_b = [0xF800 for i in range(maxcount)] + stimulus_a = [randint(0, maxint16-1) for i in range(maxcount)] 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)] + stimulus_a = [0x7C00 for i in range(maxcount)] + stimulus_b = [randint(0, maxint16-1) for i in range(maxcount)] 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)] + stimulus_a = [0xFC00 for i in range(maxcount)] + stimulus_b = [randint(0, maxint16-1) for i in range(maxcount)] 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)] + stimulus_b = [0x7C00 for i in range(maxcount)] + stimulus_a = [randint(0, maxint16-1) for i in range(maxcount)] 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)] + stimulus_b = [0xFC00 for i in range(maxcount)] + stimulus_a = [randint(0, maxint16-1) for i in range(maxcount)] 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)] + stimulus_a = [randint(0, maxint16-1) for i in range(maxcount)] + stimulus_b = [randint(0, maxint16-1) for i in range(maxcount)] yield from run_test(dut, stimulus_a, stimulus_b, op) - count += 1000 + count += maxcount print (count, "random vectors passed") -- 2.30.2