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