5a1661a159c6c1167cd6580e23ad60aa6bfc7457
[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 return zip(stimulus_a, stimulus_b)
36
37
38 def get_nan_noncan(mod, fixed_num, maxcount, width):
39 stimulus_a = replicate(fixed_num, maxcount)
40 # non-canonical NaNs.
41 stimulus_b = [mod.set_exponent(get_rval(width), mod.max_e) \
42 for i in range(maxcount)]
43 return zip(stimulus_a, stimulus_b)
44
45
46 def get_n127(mod, fixed_num, maxcount, width):
47 stimulus_a = replicate(fixed_num, maxcount)
48 # -127
49 stimulus_b = [mod.set_exponent(get_rval(width), -mod.max_e+1) \
50 for i in range(maxcount)]
51 return zip(stimulus_a, stimulus_b)
52
53
54 def get_nearly_zero(mod, fixed_num, maxcount, width):
55 stimulus_a = replicate(fixed_num, maxcount)
56 # nearly zero
57 stimulus_b = [mod.set_exponent(get_rval(width), -mod.max_e+2) \
58 for i in range(maxcount)]
59 return zip(stimulus_a, stimulus_b)
60
61
62 def get_nearly_inf(mod, fixed_num, maxcount, width):
63 stimulus_a = replicate(fixed_num, maxcount)
64 # nearly inf
65 stimulus_b = [mod.set_exponent(get_rval(width), mod.max_e-1) \
66 for i in range(maxcount)]
67 return zip(stimulus_a, stimulus_b)
68
69
70 def get_corner_rand(mod, fixed_num, maxcount, width):
71 stimulus_a = replicate(fixed_num, maxcount)
72 # random
73 stimulus_b = [get_rval(width) for i in range(maxcount)]
74 return zip(stimulus_a, stimulus_b)
75
76
77 class PipeFPCase:
78 def __init__(self, dut, name, mod, fmod, width, fpfn, count):
79 self.dut = dut
80 self.name = name
81 self.mod = mod
82 self.fmod = fmod
83 self.width = width
84 self.fpfn = fpfn
85 self.count = count
86
87 def run(self, name, fn):
88 name = "%s_%s" % (self.name, name)
89 pipe_cornercases_repeat(self.dut, name, self.mod, self.fmod,
90 self.width, fn, corner_cases, self.fpfn,
91 self.count)
92
93 def run_cornercases(self):
94 vals = repeat(self.dut.num_rows, get_corner_cases(self.mod))
95 tname = "test_fp%s_pipe_fp%d_cornercases" % (self.name, self.width)
96 runfp(self.dut, self.width, tname, self.fmod, self.fpfn, vals=vals)
97
98 def run_regressions(self, regressions_fn):
99 vals = repeat(self.dut.num_rows, regressions_fn())
100 tname = "test_fp%s_pipe_fp%d_regressions" % (self.name, self.width)
101 runfp(self.dut, self.width, tname, self.fmod, self.fpfn, vals=vals)
102
103 def run_random(self):
104 tname = "test_fp%s_pipe_fp%d_rand" % (self.name, self.width)
105 runfp(self.dut, self.width, tname, self.fmod, self.fpfn)
106
107
108 def run_pipe_fp(dut, width, name, mod, fmod, regressions, fpfn, count):
109 pc = PipeFPCase(dut, name, mod, fmod, width, fpfn, count)
110 pc.run("rand1", get_rand1)
111 pc.run("n127", get_n127)
112 pc.run("noncan", get_nan_noncan)
113 pc.run("nearlyzero", get_nearly_zero)
114 pc.run("nearlyinf", get_nearly_inf)
115 pc.run("corner_rand", get_corner_rand)
116 pc.run_cornercases()
117 pc.run_regressions(regressions)
118 pc.run_random()
119