add set-logic ALL clause to stop cvc5 warning
[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 # http://bugs.libre-riscv.org/show_bug.cgi?id=113
101 dut = FPCVTF2IntMuxInOut(64, 32, 4, op_wid=1)
102 runfp(dut, 64, "test_fcvt_f2int_pipe_f64_i32", Float64, fcvt_f64_i32,
103 True, n_vals=100, opcode=0x1)
104
105 def test_int_pipe_f64_i16():
106 # XXX TODO: reduce range of FP num to actually fit (almost) into I16
107 # http://bugs.libre-riscv.org/show_bug.cgi?id=113
108 dut = FPCVTF2IntMuxInOut(64, 16, 4, op_wid=1)
109 runfp(dut, 64, "test_fcvt_f2int_pipe_f64_i16", Float64, fcvt_f64_i16,
110 True, n_vals=100, opcode=0x1)
111
112 def test_int_pipe_f32_i32():
113 dut = FPCVTF2IntMuxInOut(32, 32, 4, op_wid=1)
114 runfp(dut, 32, "test_fcvt_f2int_pipe_f32_i32", Float32, fcvt_f32_i32,
115 True, n_vals=100, opcode=0x1)
116
117 def test_int_pipe_f16_i16():
118 dut = FPCVTF2IntMuxInOut(16, 16, 4, op_wid=1)
119 runfp(dut, 16, "test_fcvt_f2int_pipe_f16_i16", Float16, fcvt_f16_i16,
120 True, n_vals=100, opcode=0x1)
121
122 ######################
123 # fp to unsigned int
124 ######################
125
126 def test_int_pipe_f16_ui16():
127 # XXX softfloat-3 doesn't have ui16_to_xxx so use ui32 instead.
128 # should be fine.
129 dut = FPCVTF2IntMuxInOut(16, 16, 4, op_wid=1)
130 runfp(dut, 16, "test_fcvt_f2int_pipe_f16_ui16", Float16, fcvt_f16_ui16,
131 True, n_vals=100)
132
133 def test_int_pipe_ui16_f64():
134 dut = FPCVTIntMuxInOut(16, 64, 4, op_wid=1)
135 runfp(dut, 16, "test_fcvt_int_pipe_ui16_f64", to_uint16, fcvt_64, True,
136 n_vals=100)
137
138 def test_int_pipe_f32_ui32():
139 dut = FPCVTF2IntMuxInOut(32, 32, 4, op_wid=1)
140 runfp(dut, 32, "test_fcvt_f2int_pipe_f32_ui32", Float32, fcvt_f32_ui32,
141 True, n_vals=100)
142
143 def test_int_pipe_ui32_f64():
144 dut = FPCVTIntMuxInOut(32, 64, 4, op_wid=1)
145 runfp(dut, 32, "test_fcvt_int_pipe_ui32_64", to_uint32, fcvt_64, True,
146 n_vals=100)
147
148 def test_int_pipe_ui64_f32():
149 # ok, doing 33 bits here because it's pretty pointless (not entirely)
150 # to do random numbers statistically likely 99.999% of the time to be
151 # converted to Inf
152 dut = FPCVTIntMuxInOut(64, 32, 4, op_wid=1)
153 runfp(dut, 33, "test_fcvt_int_pipe_ui64_32", to_uint64, fcvt_64_to_32, True,
154 n_vals=100)
155
156 def test_int_pipe_ui64_f16():
157 # ok, doing 17 bits here because it's pretty pointless (not entirely)
158 # to do random numbers statistically likely 99.999% of the time to be
159 # converted to Inf
160 dut = FPCVTIntMuxInOut(64, 16, 4, op_wid=1)
161 runfp(dut, 17, "test_fcvt_int_pipe_ui64_16", to_uint64, fcvt_16, True,
162 n_vals=100)
163
164 def test_int_pipe_ui32_f16():
165 # ok, doing 17 bits here because it's pretty pointless (not entirely)
166 # to do random numbers statistically likely 99.999% of the time to be
167 # converted to Inf
168 dut = FPCVTIntMuxInOut(32, 16, 4, op_wid=1)
169 runfp(dut, 17, "test_fcvt_int_pipe_ui32_16", to_uint32, fcvt_16, True,
170 n_vals=100)
171
172 def test_int_pipe_f64_ui64():
173 dut = FPCVTF2IntMuxInOut(64, 64, 4, op_wid=1)
174 runfp(dut, 64, "test_fcvt_f2int_pipe_f64_ui64", Float64, fcvt_f64_ui64,
175 True, n_vals=100)
176
177 def test_int_pipe_f64_ui32():
178 dut = FPCVTF2IntMuxInOut(64, 32, 4, op_wid=1)
179 runfp(dut, 64, "test_fcvt_f2int_pipe_f64_ui32", Float64, fcvt_f64_ui32,
180 True, n_vals=100)
181
182 def test_int_pipe_f64_ui16():
183 dut = FPCVTF2IntMuxInOut(64, 16, 4, op_wid=1)
184 runfp(dut, 64, "test_fcvt_f2int_pipe_f64_ui16", Float64, fcvt_f64_ui16,
185 True, n_vals=100)
186
187 if __name__ == '__main__':
188 for i in range(200):
189 test_int_pipe_f64_i16()
190 test_int_pipe_f64_i32()
191 test_int_pipe_f64_ui16()
192 test_int_pipe_f64_ui32()
193 test_int_pipe_f16_i16()
194 test_int_pipe_f32_i32()
195 test_int_pipe_f64_i64()
196 test_int_pipe_f64_ui64()
197 test_int_pipe_f32_ui32()
198 test_int_pipe_f16_ui16()