add fpmul tests
[ieee754fpu.git] / src / ieee754 / fpcommon / test / case_gen.py
1 from ieee754.fpcommon.test.fpmux import runfp, repeat, pipe_cornercases_repeat
2
3 from random import randint
4 from random import seed
5
6 import sys
7
8 def corner_cases(mod):
9 return [mod.zero(1), mod.zero(0),
10 mod.inf(1), mod.inf(0),
11 mod.nan(1), mod.nan(0)]
12
13 def get_corner_cases(mod):
14 #corner cases
15 from itertools import permutations
16 cc = corner_cases(mod)
17 stimulus_a = [i[0] for i in permutations(cc, 2)]
18 stimulus_b = [i[1] for i in permutations(cc, 2)]
19 return zip(stimulus_a, stimulus_b)
20
21
22 def replicate(fixed_num, maxcount):
23 if isinstance(fixed_num, int):
24 return [fixed_num for i in range(maxcount)]
25 else:
26 return fixed_num
27
28 def get_rval(width):
29 mval = (1<<width)-1
30 return randint(0, mval)
31
32 def get_rand1(mod, fixed_num, maxcount, width):
33 stimulus_a = replicate(fixed_num, maxcount)
34 stimulus_b = [get_rval(width) for i in range(maxcount)]
35 yield from zip(stimulus_a, stimulus_b)
36 yield from zip(stimulus_b, stimulus_a)
37
38
39 def get_nan_noncan(mod, fixed_num, maxcount, width):
40 stimulus_a = replicate(fixed_num, maxcount)
41 # non-canonical NaNs.
42 stimulus_b = [mod.set_exponent(get_rval(width), mod.max_e) \
43 for i in range(maxcount)]
44 yield from zip(stimulus_a, stimulus_b)
45 yield from zip(stimulus_b, stimulus_a)
46
47
48 def get_n127(mod, fixed_num, maxcount, width):
49 stimulus_a = replicate(fixed_num, maxcount)
50 # -127
51 stimulus_b = [mod.set_exponent(get_rval(width), -mod.max_e+1) \
52 for i in range(maxcount)]
53 yield from zip(stimulus_a, stimulus_b)
54 yield from zip(stimulus_b, stimulus_a)
55
56
57 def get_nearly_zero(mod, fixed_num, maxcount, width):
58 stimulus_a = replicate(fixed_num, maxcount)
59 # nearly zero
60 stimulus_b = [mod.set_exponent(get_rval(width), -mod.max_e+2) \
61 for i in range(maxcount)]
62 yield from zip(stimulus_a, stimulus_b)
63 yield from zip(stimulus_b, stimulus_a)
64
65
66 def get_nearly_inf(mod, fixed_num, maxcount, width):
67 stimulus_a = replicate(fixed_num, maxcount)
68 # nearly inf
69 stimulus_b = [mod.set_exponent(get_rval(width), mod.max_e-1) \
70 for i in range(maxcount)]
71 yield from zip(stimulus_a, stimulus_b)
72 yield from zip(stimulus_b, stimulus_a)
73
74
75 def get_corner_rand(mod, fixed_num, maxcount, width):
76 stimulus_a = replicate(fixed_num, maxcount)
77 # random
78 stimulus_b = [get_rval(width) for i in range(maxcount)]
79 yield from zip(stimulus_a, stimulus_b)
80 yield from zip(stimulus_b, stimulus_a)
81
82
83 class PipeFPCase:
84 def __init__(self, dut, name, mod, fmod, width, fpfn, count):
85 self.dut = dut
86 self.name = name
87 self.mod = mod
88 self.fmod = fmod
89 self.width = width
90 self.fpfn = fpfn
91 self.count = count
92
93 def run(self, name, fn):
94 name = "%s_%s" % (self.name, name)
95 pipe_cornercases_repeat(self.dut, name, self.mod, self.fmod,
96 self.width, fn, corner_cases, self.fpfn,
97 self.count)
98
99 def run_cornercases(self):
100 vals = repeat(self.dut.num_rows, get_corner_cases(self.mod))
101 tname = "test_fp%s_pipe_fp%d_cornercases" % (self.name, self.width)
102 runfp(self.dut, self.width, tname, self.fmod, self.fpfn, vals=vals)
103
104 def run_regressions(self, regressions_fn):
105 vals = repeat(self.dut.num_rows, regressions_fn())
106 print ("regressions", vals)
107 tname = "test_fp%s_pipe_fp%d_regressions" % (self.name, self.width)
108 runfp(self.dut, self.width, tname, self.fmod, self.fpfn, vals=vals)
109
110 def run_random(self):
111 tname = "test_fp%s_pipe_fp%d_rand" % (self.name, self.width)
112 runfp(self.dut, self.width, tname, self.fmod, self.fpfn)
113
114
115 def run_pipe_fp(dut, width, name, mod, fmod, regressions, fpfn, count):
116 pc = PipeFPCase(dut, name, mod, fmod, width, fpfn, count)
117 pc.run_regressions(regressions)
118 pc.run_cornercases()
119 pc.run("rand1", get_rand1)
120 pc.run("n127", get_n127)
121 pc.run("noncan", get_nan_noncan)
122 pc.run("nearlyzero", get_nearly_zero)
123 pc.run("nearlyinf", get_nearly_inf)
124 pc.run("corner_rand", get_corner_rand)
125 pc.run_random()
126