format code
[ieee754fpu.git] / src / ieee754 / fcvt / test / test_fcvt_f2int_pipe.py
1 """ test of FPCVTMuxInOut
2 """
3
4 from ieee754.fcvt.pipeline import FPCVTF2IntMuxInOut
5 from ieee754.fpcommon.test.fpmux import (runfp, create_random)
6 from ieee754.fcvt.test.rangelimited import create_int
7
8 import sfpy
9 from sfpy import Float64, Float32, Float16
10
11
12 def fcvt_f64_ui32(x):
13 return sfpy.float.f64_to_ui32(x)
14
15
16 def fcvt_f64_i32(x):
17 return sfpy.float.f64_to_i32(x) & 0xffffffff
18
19
20 def fcvt_i16_f32(x):
21 print("fcvt i16_f32", hex(x))
22 return sfpy.float.i32_to_f32(x) # XXX no i16_to_f32, it's ok though
23
24
25 def fcvt_i32_f32(x):
26 print("fcvt i32_f32", hex(x))
27 return sfpy.float.i32_to_f32(x)
28
29
30 def fcvt_i32_f64(x):
31 print("fcvt i32_f64", hex(x))
32 return sfpy.float.i32_to_f64(x)
33
34
35 def fcvt_f32_ui32(x):
36 return sfpy.float.f32_to_ui32(x)
37
38
39 def fcvt_64_to_32(x):
40 return sfpy.float.ui64_to_f32(x)
41
42
43 def fcvt_f64_ui64(x):
44 return sfpy.float.f64_to_ui64(x)
45
46
47 def fcvt_f64_ui16(x):
48 x = sfpy.float.f64_to_ui32(x)
49 if x >= 0xffff:
50 return 0xffff
51 return x
52
53
54 def fcvt_f16_ui32(x):
55 return sfpy.float.f16_to_ui32(x)
56
57
58 def fcvt_f16_ui16(x):
59 return sfpy.float.f16_to_ui32(x) & 0xffff
60
61
62 def fcvt_f16_i16(x):
63 x = sfpy.float.f16_to_i32(x)
64 if x >= 0x7fff:
65 return 0x7fff
66 if x <= -0x8000:
67 return 0x8000
68 return x & 0xffff
69
70
71 def fcvt_f64_i16(x):
72 x = sfpy.float.f64_to_i32(x)
73 if x >= 0x7fff:
74 return 0x7fff
75 if x <= -0x8000:
76 return 0x8000
77 return x & 0xffff
78
79
80 def fcvt_f32_i32(x):
81 return sfpy.float.f32_to_i32(x) & 0xffffffff
82
83
84 def fcvt_f64_i64(x):
85 return sfpy.float.f64_to_i64(x) & 0xffffffffffffffff
86
87
88 ######################
89 # signed int to fp
90 ######################
91
92 def test_int_pipe_i16_f32():
93 # XXX softfloat-3 doesn't have i16_to_xxx so use ui32 instead.
94 # should be fine.
95 dut = FPCVTIntMuxInOut(16, 32, 4, op_wid=1)
96 runfp(dut, 16, "test_fcvt_int_pipe_i16_f32", to_int16, fcvt_i16_f32, True,
97 n_vals=100, opcode=0x1)
98
99
100 def test_int_pipe_i32_f64():
101 dut = FPCVTIntMuxInOut(32, 64, 4, op_wid=1)
102 runfp(dut, 32, "test_fcvt_int_pipe_i32_f64", to_int32, fcvt_i32_f64, True,
103 n_vals=100, opcode=0x1)
104
105
106 def test_int_pipe_i32_f32():
107 dut = FPCVTIntMuxInOut(32, 32, 4, op_wid=1)
108 runfp(dut, 32, "test_fcvt_int_pipe_i32_f32", to_int32, fcvt_i32_f32, True,
109 n_vals=100, opcode=0x1)
110
111
112 def test_int_pipe_f64_i64():
113 dut = FPCVTF2IntMuxInOut(64, 64, 4, op_wid=1)
114 vals = []
115 for i in range(100):
116 vals.append(create_int(Float64, 64))
117 vals += create_random(dut.num_rows, 64, True, 10)
118 runfp(dut, 64, "test_fcvt_f2int_pipe_f64_i64", Float64, fcvt_f64_i64,
119 True, vals=vals, opcode=0x1)
120
121
122 def test_int_pipe_f64_i32():
123 # XXX TODO: reduce range of FP num to actually fit (almost) into I32
124 # http://bugs.libre-riscv.org/show_bug.cgi?id=113
125 dut = FPCVTF2IntMuxInOut(64, 32, 4, op_wid=1)
126 vals = []
127 for i in range(100):
128 vals.append(create_int(Float64, 32))
129 vals += create_random(dut.num_rows, 32, True, 10)
130 runfp(dut, 64, "test_fcvt_f2int_pipe_f64_i32", Float64, fcvt_f64_i32,
131 True, vals=vals, opcode=0x1)
132
133
134 def test_int_pipe_f64_i16():
135 # XXX TODO: reduce range of FP num to actually fit (almost) into I16
136 # http://bugs.libre-riscv.org/show_bug.cgi?id=113
137 dut = FPCVTF2IntMuxInOut(64, 16, 4, op_wid=1)
138 vals = []
139 for i in range(100):
140 vals.append(create_int(Float64, 16))
141 vals += create_random(dut.num_rows, 16, True, 10)
142 runfp(dut, 64, "test_fcvt_f2int_pipe_f64_i16", Float64, fcvt_f64_i16,
143 True, vals=vals, opcode=0x1)
144
145
146 def test_int_pipe_f32_i32():
147 dut = FPCVTF2IntMuxInOut(32, 32, 4, op_wid=1)
148 runfp(dut, 32, "test_fcvt_f2int_pipe_f32_i32", Float32, fcvt_f32_i32,
149 True, n_vals=100, opcode=0x1)
150
151
152 def test_int_pipe_f16_i16():
153 dut = FPCVTF2IntMuxInOut(16, 16, 4, op_wid=1)
154 runfp(dut, 16, "test_fcvt_f2int_pipe_f16_i16", Float16, fcvt_f16_i16,
155 True, n_vals=100, opcode=0x1)
156
157 ######################
158 # fp to unsigned int
159 ######################
160
161
162 def test_int_pipe_f16_ui16():
163 # XXX softfloat-3 doesn't have ui16_to_xxx so use ui32 instead.
164 # should be fine.
165 dut = FPCVTF2IntMuxInOut(16, 16, 4, op_wid=1)
166 runfp(dut, 16, "test_fcvt_f2int_pipe_f16_ui16", Float16, fcvt_f16_ui16,
167 True, n_vals=100)
168
169
170 def test_int_pipe_ui16_f64():
171 dut = FPCVTIntMuxInOut(16, 64, 4, op_wid=1)
172 runfp(dut, 16, "test_fcvt_int_pipe_ui16_f64", to_uint16, fcvt_64, True,
173 n_vals=100)
174
175
176 def test_int_pipe_f32_ui32():
177 dut = FPCVTF2IntMuxInOut(32, 32, 4, op_wid=1)
178 vals = []
179 for i in range(100):
180 vals.append(create_int(Float32, 32))
181 vals += create_random(dut.num_rows, 32, True, 10)
182 runfp(dut, 32, "test_fcvt_f2int_pipe_f32_ui32", Float32, fcvt_f32_ui32,
183 True, vals=vals)
184
185
186 def test_int_pipe_ui32_f64():
187 dut = FPCVTIntMuxInOut(32, 64, 4, op_wid=1)
188 runfp(dut, 32, "test_fcvt_int_pipe_ui32_64", to_uint32, fcvt_64, True,
189 n_vals=100)
190
191
192 def test_int_pipe_ui64_f32():
193 # ok, doing 33 bits here because it's pretty pointless (not entirely)
194 # to do random numbers statistically likely 99.999% of the time to be
195 # converted to Inf
196 dut = FPCVTIntMuxInOut(64, 32, 4, op_wid=1)
197 runfp(dut, 33, "test_fcvt_int_pipe_ui64_32", to_uint64, fcvt_64_to_32, True,
198 n_vals=100)
199
200
201 def test_int_pipe_ui64_f16():
202 # ok, doing 17 bits here because it's pretty pointless (not entirely)
203 # to do random numbers statistically likely 99.999% of the time to be
204 # converted to Inf
205 dut = FPCVTIntMuxInOut(64, 16, 4, op_wid=1)
206 runfp(dut, 17, "test_fcvt_int_pipe_ui64_16", to_uint64, fcvt_16, True,
207 n_vals=100)
208
209
210 def test_int_pipe_ui32_f16():
211 # ok, doing 17 bits here because it's pretty pointless (not entirely)
212 # to do random numbers statistically likely 99.999% of the time to be
213 # converted to Inf
214 dut = FPCVTIntMuxInOut(32, 16, 4, op_wid=1)
215 runfp(dut, 17, "test_fcvt_int_pipe_ui32_16", to_uint32, fcvt_16, True,
216 n_vals=100)
217
218
219 def test_int_pipe_f64_ui64():
220 dut = FPCVTF2IntMuxInOut(64, 64, 4, op_wid=1)
221 vals = []
222 for i in range(100):
223 vals.append(create_int(Float64, 64))
224 vals += create_random(dut.num_rows, 64, True, 10)
225 runfp(dut, 64, "test_fcvt_f2int_pipe_f64_ui64", Float64, fcvt_f64_ui64,
226 True, vals=vals)
227
228
229 def test_int_pipe_f64_ui32():
230 dut = FPCVTF2IntMuxInOut(64, 32, 4, op_wid=1)
231 vals = []
232 for i in range(100):
233 vals.append(create_int(Float64, 32))
234 vals += create_random(dut.num_rows, 32, True, 10)
235 runfp(dut, 64, "test_fcvt_f2int_pipe_f64_ui32", Float64, fcvt_f64_ui32,
236 True, vals=vals)
237
238
239 def test_int_pipe_f64_ui16():
240 dut = FPCVTF2IntMuxInOut(64, 16, 4, op_wid=1)
241 vals = []
242 for i in range(100):
243 vals.append(create_int(Float64, 16))
244 vals += create_random(dut.num_rows, 16, True, 10)
245 runfp(dut, 64, "test_fcvt_f2int_pipe_f64_ui16", Float64, fcvt_f64_ui16,
246 True, vals=vals)
247
248
249 if __name__ == '__main__':
250 for i in range(200):
251 test_int_pipe_f64_i16()
252 test_int_pipe_f64_i32()
253 test_int_pipe_f64_ui16()
254 test_int_pipe_f64_ui32()
255 test_int_pipe_f16_i16()
256 test_int_pipe_f32_i32()
257 test_int_pipe_f64_i64()
258 test_int_pipe_f64_ui64()
259 test_int_pipe_f32_ui32()
260 test_int_pipe_f16_ui16()