add fp cvt 64 to 32 test
[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, single_op=False):
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 if single_op:
19 return stimulus_a
20 stimulus_b = [i[1] for i in permutations(cc, 2)]
21 return zip(stimulus_a, stimulus_b)
22
23
24 def replicate(fixed_num, maxcount):
25 if isinstance(fixed_num, int):
26 return [fixed_num for i in range(maxcount)]
27 else:
28 return fixed_num
29
30 def get_rval(width):
31 mval = (1<<width)-1
32 return randint(0, mval)
33
34 def get_rand1(mod, fixed_num, maxcount, width, single_op=False):
35 stimulus_b = [get_rval(width) for i in range(maxcount)]
36 if single_op:
37 yield from stimulus_b
38 return
39 stimulus_a = replicate(fixed_num, maxcount)
40 yield from zip(stimulus_a, stimulus_b)
41 yield from zip(stimulus_b, stimulus_a)
42
43
44 def get_nan_noncan(mod, fixed_num, maxcount, width, single_op=False):
45 # non-canonical NaNs.
46 stimulus_b = [mod.set_exponent(get_rval(width), mod.max_e) \
47 for i in range(maxcount)]
48 if single_op:
49 yield from stimulus_b
50 return
51 stimulus_a = replicate(fixed_num, maxcount)
52 yield from zip(stimulus_a, stimulus_b)
53 yield from zip(stimulus_b, stimulus_a)
54
55
56 def get_n127(mod, fixed_num, maxcount, width, single_op=False):
57 # -127
58 stimulus_b = [mod.set_exponent(get_rval(width), -mod.max_e+1) \
59 for i in range(maxcount)]
60 if single_op:
61 yield from stimulus_b
62 return
63 stimulus_a = replicate(fixed_num, maxcount)
64 yield from zip(stimulus_a, stimulus_b)
65 yield from zip(stimulus_b, stimulus_a)
66
67
68 def get_nearly_zero(mod, fixed_num, maxcount, width, single_op=False):
69 # nearly zero
70 stimulus_b = [mod.set_exponent(get_rval(width), -mod.max_e+2) \
71 for i in range(maxcount)]
72 if single_op:
73 yield from stimulus_b
74 return
75 stimulus_a = replicate(fixed_num, maxcount)
76 yield from zip(stimulus_a, stimulus_b)
77 yield from zip(stimulus_b, stimulus_a)
78
79
80 def get_nearly_inf(mod, fixed_num, maxcount, width, single_op=False):
81 # nearly inf
82 stimulus_b = [mod.set_exponent(get_rval(width), mod.max_e-1) \
83 for i in range(maxcount)]
84 if single_op:
85 yield from stimulus_b
86 return
87 stimulus_a = replicate(fixed_num, maxcount)
88 yield from zip(stimulus_a, stimulus_b)
89 yield from zip(stimulus_b, stimulus_a)
90
91
92 def get_corner_rand(mod, fixed_num, maxcount, width, single_op=False):
93 # random
94 stimulus_b = [get_rval(width) for i in range(maxcount)]
95 if single_op:
96 yield from stimulus_b
97 return
98 stimulus_a = replicate(fixed_num, maxcount)
99 yield from zip(stimulus_a, stimulus_b)
100 yield from zip(stimulus_b, stimulus_a)
101
102
103 class PipeFPCase:
104 def __init__(self, dut, name, mod, fmod, width, fpfn, count, single_op):
105 self.dut = dut
106 self.name = name
107 self.mod = mod
108 self.fmod = fmod
109 self.width = width
110 self.fpfn = fpfn
111 self.count = count
112 self.single_op = single_op
113
114 def run(self, name, fn):
115 name = "%s_%s" % (self.name, name)
116 pipe_cornercases_repeat(self.dut, name, self.mod, self.fmod,
117 self.width, fn, corner_cases, self.fpfn,
118 self.count, self.single_op)
119
120 def run_cornercases(self):
121 ccs = get_corner_cases(self.mod, self.single_op)
122 vals = repeat(self.dut.num_rows, ccs)
123 tname = "test_fp%s_pipe_fp%d_cornercases" % (self.name, self.width)
124 runfp(self.dut, self.width, tname, self.fmod, self.fpfn, vals=vals,
125 single_op=self.single_op)
126
127 def run_regressions(self, regressions_fn):
128 vals = repeat(self.dut.num_rows, regressions_fn())
129 #print ("regressions", self.single_op, vals)
130 tname = "test_fp%s_pipe_fp%d_regressions" % (self.name, self.width)
131 runfp(self.dut, self.width, tname, self.fmod, self.fpfn, vals=vals,
132 single_op=self.single_op)
133
134 def run_random(self):
135 tname = "test_fp%s_pipe_fp%d_rand" % (self.name, self.width)
136 runfp(self.dut, self.width, tname, self.fmod, self.fpfn,
137 single_op=self.single_op)
138
139
140 def run_pipe_fp(dut, width, name, mod, fmod, regressions, fpfn, count,
141 single_op=False):
142 pc = PipeFPCase(dut, name, mod, fmod, width, fpfn, count, single_op)
143 pc.run_regressions(regressions)
144 pc.run_cornercases()
145 pc.run("rand1", get_rand1)
146 pc.run("n127", get_n127)
147 pc.run("noncan", get_nan_noncan)
148 pc.run("nearlyzero", get_nearly_zero)
149 pc.run("nearlyinf", get_nearly_inf)
150 pc.run("corner_rand", get_corner_rand)
151 pc.run_random()
152