add unit tests
[ieee754fpu.git] / src / add / test_add.py
1 import sys
2 import subprocess
3 from random import randint
4 from random import seed
5 from sfpy import Float32
6
7 from nmigen import Module, Signal
8 from nmigen.compat.sim import run_simulation
9
10 from nmigen_add_experiment import FPADD
11
12 def get_mantissa(x):
13 return 0x7fffff & x
14
15 def get_exponent(x):
16 return ((x & 0x7f800000) >> 23) - 127
17
18 def get_sign(x):
19 return ((x & 0x80000000) >> 31)
20
21 def is_nan(x):
22 return get_exponent(x) == 128 and get_mantissa(x) != 0
23
24 def is_inf(x):
25 return get_exponent(x) == 128 and get_mantissa(x) == 0
26
27 def is_pos_inf(x):
28 return is_inf(x) and not get_sign(x)
29
30 def is_neg_inf(x):
31 return is_inf(x) and get_sign(x)
32
33 def match(x, y):
34 return (
35 (is_pos_inf(x) and is_pos_inf(y)) or
36 (is_neg_inf(x) and is_neg_inf(y)) or
37 (is_nan(x) and is_nan(y)) or
38 (x == y)
39 )
40
41 def get_case(dut, a, b):
42 yield dut.in_a.v.eq(a)
43 yield dut.in_a.stb.eq(1)
44 yield
45 yield
46 a_ack = (yield dut.in_a.ack)
47 assert a_ack == 0
48 yield dut.in_b.v.eq(b)
49 yield dut.in_b.stb.eq(1)
50 b_ack = (yield dut.in_b.ack)
51 assert b_ack == 0
52
53 while True:
54 yield
55 out_z_stb = (yield dut.out_z.stb)
56 if not out_z_stb:
57 continue
58 yield dut.in_a.stb.eq(0)
59 yield dut.in_b.stb.eq(0)
60 yield dut.out_z.ack.eq(1)
61 yield
62 yield dut.out_z.ack.eq(0)
63 yield
64 yield
65 break
66
67 out_z = yield dut.out_z.v
68 return out_z
69
70 def check_case(dut, a, b, z):
71 out_z = yield from get_case(dut, a, b)
72 assert out_z == z, "Output z 0x%x not equal to expected 0x%x" % (out_z, z)
73
74
75 def run_test(dut, stimulus_a, stimulus_b):
76
77 expected_responses = []
78 actual_responses = []
79 for a, b in zip(stimulus_a, stimulus_b):
80 af = Float32.from_bits(a)
81 bf = Float32.from_bits(b)
82 z = af + bf
83 expected_responses.append(z.get_bits())
84 print (af, bf, z)
85 actual = yield from get_case(dut, a, b)
86 actual_responses.append(actual)
87
88 if len(actual_responses) < len(expected_responses):
89 print ("Fail ... not enough results")
90 exit(0)
91
92 for expected, actual, a, b in zip(expected_responses, actual_responses,
93 stimulus_a, stimulus_b):
94 passed = match(expected, actual)
95
96 if not passed:
97
98 print ("Fail ... expected:", hex(expected), "actual:", hex(actual))
99
100 print (hex(a))
101 print ("a mantissa:", a & 0x7fffff)
102 print ("a exponent:", ((a & 0x7f800000) >> 23) - 127)
103 print ("a sign:", ((a & 0x80000000) >> 31))
104
105 print (hex(b))
106 print ("b mantissa:", b & 0x7fffff)
107 print ("b exponent:", ((b & 0x7f800000) >> 23) - 127)
108 print ("b sign:", ((b & 0x80000000) >> 31))
109
110 print (hex(expected))
111 print ("expected mantissa:", expected & 0x7fffff)
112 print ("expected exponent:", ((expected & 0x7f800000) >> 23) - 127)
113 print ("expected sign:", ((expected & 0x80000000) >> 31))
114
115 print (hex(actual))
116 print ("actual mantissa:", actual & 0x7fffff)
117 print ("actual exponent:", ((actual & 0x7f800000) >> 23) - 127)
118 print ("actual sign:", ((actual & 0x80000000) >> 31))
119
120 sys.exit(0)
121
122 def testbench(dut):
123 yield from check_case(dut, 0, 0, 0)
124 yield from check_case(dut, 0x40000000, 0xc0000000, 0x00000000)
125 yield from check_case(dut, 0x3F800000, 0x40000000, 0x40400000)
126 yield from check_case(dut, 0x40000000, 0x3F800000, 0x40400000)
127 yield from check_case(dut, 0x447A0000, 0x4488B000, 0x4502D800)
128 yield from check_case(dut, 0x463B800A, 0x42BA8A3D, 0x463CF51E)
129 yield from check_case(dut, 0x42BA8A3D, 0x463B800A, 0x463CF51E)
130 yield from check_case(dut, 0x463B800A, 0xC2BA8A3D, 0x463A0AF6)
131 yield from check_case(dut, 0xC2BA8A3D, 0x463B800A, 0x463A0AF6)
132 yield from check_case(dut, 0xC63B800A, 0x42BA8A3D, 0xC63A0AF6)
133 yield from check_case(dut, 0x42BA8A3D, 0xC63B800A, 0xC63A0AF6)
134 yield from check_case(dut, 0xFFFFFFFF, 0xC63B800A, 0xFFC00000)
135 yield from check_case(dut, 0x7F800000, 0x00000000, 0x7F800000)
136 yield from check_case(dut, 0x00000000, 0x7F800000, 0x7F800000)
137 yield from check_case(dut, 0xFF800000, 0x00000000, 0xFF800000)
138 yield from check_case(dut, 0x00000000, 0xFF800000, 0xFF800000)
139 yield from check_case(dut, 0x7F800000, 0x7F800000, 0x7F800000)
140 yield from check_case(dut, 0xFF800000, 0xFF800000, 0xFF800000)
141 yield from check_case(dut, 0x7F800000, 0xFF800000, 0xFFC00000)
142 yield from check_case(dut, 0xFF800000, 0x7F800000, 0x7FC00000)
143 yield from check_case(dut, 0x00018643, 0x00FA72A4, 0x00FBF8E7)
144 yield from check_case(dut, 0x001A2239, 0x00FA72A4, 0x010A4A6E)
145 yield from check_case(dut, 0x3F7FFFFE, 0x3F7FFFFE, 0x3FFFFFFE)
146 yield from check_case(dut, 0x7EFFFFEE, 0x7EFFFFEE, 0x7F7FFFEE)
147 yield from check_case(dut, 0x7F7FFFEE, 0xFEFFFFEE, 0x7EFFFFEE)
148 yield from check_case(dut, 0x7F7FFFEE, 0x756CA884, 0x7F7FFFFD)
149 yield from check_case(dut, 0x7F7FFFEE, 0x758A0CF8, 0x7F7FFFFF)
150 yield from check_case(dut, 0x42500000, 0x51A7A358, 0x51A7A358)
151 yield from check_case(dut, 0x51A7A358, 0x42500000, 0x51A7A358)
152 yield from check_case(dut, 0x4E5693A4, 0x42500000, 0x4E5693A5)
153 yield from check_case(dut, 0x42500000, 0x4E5693A4, 0x4E5693A5)
154 #yield from check_case(dut, 1, 0, 1)
155 #yield from check_case(dut, 1, 1, 1)
156
157 count = 0
158
159 #regression tests
160 stimulus_a = [0x22cb525a, 0x40000000, 0x83e73d5c, 0xbf9b1e94, 0x34082401, 0x5e8ef81, 0x5c75da81, 0x2b017]
161 stimulus_b = [0xadd79efa, 0xC0000000, 0x1c800000, 0xc038ed3a, 0xb328cd45, 0x114f3db, 0x2f642a39, 0xff3807ab]
162 yield from run_test(dut, stimulus_a, stimulus_b)
163 count += len(stimulus_a)
164 print (count, "vectors passed")
165
166 #corner cases
167 from itertools import permutations
168 stimulus_a = [i[0] for i in permutations([0x80000000, 0x00000000, 0x7f800000, 0xff800000, 0x7fc00000, 0xffc00000], 2)]
169 stimulus_b = [i[1] for i in permutations([0x80000000, 0x00000000, 0x7f800000, 0xff800000, 0x7fc00000, 0xffc00000], 2)]
170 yield from run_test(dut, stimulus_a, stimulus_b)
171 count += len(stimulus_a)
172 print (count, "vectors passed")
173
174 #edge cases
175 stimulus_a = [0x80000000 for i in xrange(1000)]
176 stimulus_b = [randint(0, 1<<32) for i in xrange(1000)]
177 yield from run_test(dut, stimulus_a, stimulus_b)
178 count += len(stimulus_a)
179 print (count, "vectors passed")
180
181 stimulus_a = [0x00000000 for i in xrange(1000)]
182 stimulus_b = [randint(0, 1<<32) for i in xrange(1000)]
183 yield from run_test(dut, stimulus_a, stimulus_b)
184 count += len(stimulus_a)
185 print (count, "vectors passed")
186
187 stimulus_b = [0x80000000 for i in xrange(1000)]
188 stimulus_a = [randint(0, 1<<32) for i in xrange(1000)]
189 yield from run_test(dut, stimulus_a, stimulus_b)
190 count += len(stimulus_a)
191 print (count, "vectors passed")
192
193 stimulus_b = [0x00000000 for i in xrange(1000)]
194 stimulus_a = [randint(0, 1<<32) for i in xrange(1000)]
195 yield from run_test(dut, stimulus_a, stimulus_b)
196 count += len(stimulus_a)
197 print (count, "vectors passed")
198
199 stimulus_a = [0x7F800000 for i in xrange(1000)]
200 stimulus_b = [randint(0, 1<<32) for i in xrange(1000)]
201 yield from run_test(dut, stimulus_a, stimulus_b)
202 count += len(stimulus_a)
203 print (count, "vectors passed")
204
205 stimulus_a = [0xFF800000 for i in xrange(1000)]
206 stimulus_b = [randint(0, 1<<32) for i in xrange(1000)]
207 yield from run_test(dut, stimulus_a, stimulus_b)
208 count += len(stimulus_a)
209 print (count, "vectors passed")
210
211 stimulus_b = [0x7F800000 for i in xrange(1000)]
212 stimulus_a = [randint(0, 1<<32) for i in xrange(1000)]
213 yield from run_test(dut, stimulus_a, stimulus_b)
214 count += len(stimulus_a)
215 print (count, "vectors passed")
216
217 stimulus_b = [0xFF800000 for i in xrange(1000)]
218 stimulus_a = [randint(0, 1<<32) for i in xrange(1000)]
219 yield from run_test(dut, stimulus_a, stimulus_b)
220 count += len(stimulus_a)
221 print (count, "vectors passed")
222
223 stimulus_a = [0x7FC00000 for i in xrange(1000)]
224 stimulus_b = [randint(0, 1<<32) for i in xrange(1000)]
225 yield from run_test(dut, stimulus_a, stimulus_b)
226 count += len(stimulus_a)
227 print (count, "vectors passed")
228
229 stimulus_a = [0xFFC00000 for i in xrange(1000)]
230 stimulus_b = [randint(0, 1<<32) for i in xrange(1000)]
231 yield from run_test(dut, stimulus_a, stimulus_b)
232 count += len(stimulus_a)
233 print (count, "vectors passed")
234
235 stimulus_b = [0x7FC00000 for i in xrange(1000)]
236 stimulus_a = [randint(0, 1<<32) for i in xrange(1000)]
237 yield from run_test(dut, stimulus_a, stimulus_b)
238 count += len(stimulus_a)
239 print (count, "vectors passed")
240
241 stimulus_b = [0xFFC00000 for i in xrange(1000)]
242 stimulus_a = [randint(0, 1<<32) for i in xrange(1000)]
243 yield from run_test(dut, stimulus_a, stimulus_b)
244 count += len(stimulus_a)
245 print (count, "vectors passed")
246
247 #seed(0)
248 for i in xrange(100000):
249 stimulus_a = [randint(0, 1<<32) for i in xrange(1000)]
250 stimulus_b = [randint(0, 1<<32) for i in xrange(1000)]
251 yield from run_test(dut, stimulus_a, stimulus_b)
252 count += 1000
253 print (count, "vectors passed")
254
255 if __name__ == '__main__':
256 dut = FPADD(width=32, single_cycle=True)
257 run_simulation(dut, testbench(dut), vcd_name="test_add.vcd")
258