add operator argument to unit tests
[ieee754fpu.git] / src / add / test_add.py
1 from random import randint
2 from random import seed
3 from operator import add
4
5 from nmigen import Module, Signal
6 from nmigen.compat.sim import run_simulation
7
8 from nmigen_add_experiment import FPADD
9
10 from unit_test_single import (get_mantissa, get_exponent, get_sign, is_nan,
11 is_inf, is_pos_inf, is_neg_inf,
12 match, get_case, check_case, run_test)
13
14 def testbench(dut):
15 yield from check_case(dut, 0xfe34f995, 0xff5d59ad, 0xff800000)
16 yield from check_case(dut, 0x82471f51, 0x243985f, 0x801c3790)
17 yield from check_case(dut, 0, 0, 0)
18 yield from check_case(dut, 0x40000000, 0xc0000000, 0x00000000)
19 yield from check_case(dut, 0x3F800000, 0x40000000, 0x40400000)
20 yield from check_case(dut, 0x40000000, 0x3F800000, 0x40400000)
21 yield from check_case(dut, 0x447A0000, 0x4488B000, 0x4502D800)
22 yield from check_case(dut, 0x463B800A, 0x42BA8A3D, 0x463CF51E)
23 yield from check_case(dut, 0x42BA8A3D, 0x463B800A, 0x463CF51E)
24 yield from check_case(dut, 0x463B800A, 0xC2BA8A3D, 0x463A0AF6)
25 yield from check_case(dut, 0xC2BA8A3D, 0x463B800A, 0x463A0AF6)
26 yield from check_case(dut, 0xC63B800A, 0x42BA8A3D, 0xC63A0AF6)
27 yield from check_case(dut, 0x42BA8A3D, 0xC63B800A, 0xC63A0AF6)
28 yield from check_case(dut, 0xFFFFFFFF, 0xC63B800A, 0xFFC00000)
29 yield from check_case(dut, 0x7F800000, 0x00000000, 0x7F800000)
30 yield from check_case(dut, 0x00000000, 0x7F800000, 0x7F800000)
31 yield from check_case(dut, 0xFF800000, 0x00000000, 0xFF800000)
32 yield from check_case(dut, 0x00000000, 0xFF800000, 0xFF800000)
33 yield from check_case(dut, 0x7F800000, 0x7F800000, 0x7F800000)
34 yield from check_case(dut, 0xFF800000, 0xFF800000, 0xFF800000)
35 yield from check_case(dut, 0x7F800000, 0xFF800000, 0xFFC00000)
36 yield from check_case(dut, 0xFF800000, 0x7F800000, 0x7FC00000)
37 yield from check_case(dut, 0x00018643, 0x00FA72A4, 0x00FBF8E7)
38 yield from check_case(dut, 0x001A2239, 0x00FA72A4, 0x010A4A6E)
39 yield from check_case(dut, 0x3F7FFFFE, 0x3F7FFFFE, 0x3FFFFFFE)
40 yield from check_case(dut, 0x7EFFFFEE, 0x7EFFFFEE, 0x7F7FFFEE)
41 yield from check_case(dut, 0x7F7FFFEE, 0xFEFFFFEE, 0x7EFFFFEE)
42 yield from check_case(dut, 0x7F7FFFEE, 0x756CA884, 0x7F7FFFFD)
43 yield from check_case(dut, 0x7F7FFFEE, 0x758A0CF8, 0x7F7FFFFF)
44 yield from check_case(dut, 0x42500000, 0x51A7A358, 0x51A7A358)
45 yield from check_case(dut, 0x51A7A358, 0x42500000, 0x51A7A358)
46 yield from check_case(dut, 0x4E5693A4, 0x42500000, 0x4E5693A5)
47 yield from check_case(dut, 0x42500000, 0x4E5693A4, 0x4E5693A5)
48 #yield from check_case(dut, 1, 0, 1)
49 #yield from check_case(dut, 1, 1, 1)
50
51 count = 0
52
53 #regression tests
54 stimulus_a = [0x22cb525a, 0x40000000, 0x83e73d5c, 0xbf9b1e94, 0x34082401, 0x5e8ef81, 0x5c75da81, 0x2b017]
55 stimulus_b = [0xadd79efa, 0xC0000000, 0x1c800000, 0xc038ed3a, 0xb328cd45, 0x114f3db, 0x2f642a39, 0xff3807ab]
56 yield from run_test(dut, stimulus_a, stimulus_b, add)
57 count += len(stimulus_a)
58 print (count, "vectors passed")
59
60 #corner cases
61 from itertools import permutations
62 stimulus_a = [i[0] for i in permutations([0x80000000, 0x00000000, 0x7f800000, 0xff800000, 0x7fc00000, 0xffc00000], 2)]
63 stimulus_b = [i[1] for i in permutations([0x80000000, 0x00000000, 0x7f800000, 0xff800000, 0x7fc00000, 0xffc00000], 2)]
64 yield from run_test(dut, stimulus_a, stimulus_b, add)
65 count += len(stimulus_a)
66 print (count, "vectors passed")
67
68 #edge cases
69 stimulus_a = [0x80000000 for i in range(1000)]
70 stimulus_b = [randint(0, 1<<32) for i in range(1000)]
71 yield from run_test(dut, stimulus_a, stimulus_b, add)
72 count += len(stimulus_a)
73 print (count, "vectors passed")
74
75 stimulus_a = [0x00000000 for i in range(1000)]
76 stimulus_b = [randint(0, 1<<32) for i in range(1000)]
77 yield from run_test(dut, stimulus_a, stimulus_b, add)
78 count += len(stimulus_a)
79 print (count, "vectors passed")
80
81 stimulus_b = [0x80000000 for i in range(1000)]
82 stimulus_a = [randint(0, 1<<32) for i in range(1000)]
83 yield from run_test(dut, stimulus_a, stimulus_b, add)
84 count += len(stimulus_a)
85 print (count, "vectors passed")
86
87 stimulus_b = [0x00000000 for i in range(1000)]
88 stimulus_a = [randint(0, 1<<32) for i in range(1000)]
89 yield from run_test(dut, stimulus_a, stimulus_b, add)
90 count += len(stimulus_a)
91 print (count, "vectors passed")
92
93 stimulus_a = [0x7F800000 for i in range(1000)]
94 stimulus_b = [randint(0, 1<<32) for i in range(1000)]
95 yield from run_test(dut, stimulus_a, stimulus_b, add)
96 count += len(stimulus_a)
97 print (count, "vectors passed")
98
99 stimulus_a = [0xFF800000 for i in range(1000)]
100 stimulus_b = [randint(0, 1<<32) for i in range(1000)]
101 yield from run_test(dut, stimulus_a, stimulus_b, add)
102 count += len(stimulus_a)
103 print (count, "vectors passed")
104
105 stimulus_b = [0x7F800000 for i in range(1000)]
106 stimulus_a = [randint(0, 1<<32) for i in range(1000)]
107 yield from run_test(dut, stimulus_a, stimulus_b, add)
108 count += len(stimulus_a)
109 print (count, "vectors passed")
110
111 stimulus_b = [0xFF800000 for i in range(1000)]
112 stimulus_a = [randint(0, 1<<32) for i in range(1000)]
113 yield from run_test(dut, stimulus_a, stimulus_b, add)
114 count += len(stimulus_a)
115 print (count, "vectors passed")
116
117 stimulus_a = [0x7FC00000 for i in range(1000)]
118 stimulus_b = [randint(0, 1<<32) for i in range(1000)]
119 yield from run_test(dut, stimulus_a, stimulus_b, add)
120 count += len(stimulus_a)
121 print (count, "vectors passed")
122
123 stimulus_a = [0xFFC00000 for i in range(1000)]
124 stimulus_b = [randint(0, 1<<32) for i in range(1000)]
125 yield from run_test(dut, stimulus_a, stimulus_b, add)
126 count += len(stimulus_a)
127 print (count, "vectors passed")
128
129 stimulus_b = [0x7FC00000 for i in range(1000)]
130 stimulus_a = [randint(0, 1<<32) for i in range(1000)]
131 yield from run_test(dut, stimulus_a, stimulus_b, add)
132 count += len(stimulus_a)
133 print (count, "vectors passed")
134
135 stimulus_b = [0xFFC00000 for i in range(1000)]
136 stimulus_a = [randint(0, 1<<32) for i in range(1000)]
137 yield from run_test(dut, stimulus_a, stimulus_b, add)
138 count += len(stimulus_a)
139 print (count, "vectors passed")
140
141 #seed(0)
142 for i in range(100000):
143 stimulus_a = [randint(0, 1<<32) for i in range(1000)]
144 stimulus_b = [randint(0, 1<<32) for i in range(1000)]
145 yield from run_test(dut, stimulus_a, stimulus_b, add)
146 count += 1000
147 print (count, "random vectors passed")
148
149 if __name__ == '__main__':
150 dut = FPADD(width=32, single_cycle=True)
151 run_simulation(dut, testbench(dut), vcd_name="test_add.vcd")
152