switch to exact version of cython
[ieee754fpu.git] / src / ieee754 / fcvt / test / test_fcvt_int_pipe.py
1 """ test of FPCVTMuxInOut
2 """
3
4 from ieee754.fcvt.pipeline import FPCVTIntMuxInOut
5 from ieee754.fpcommon.test.fpmux import runfp
6
7 import sfpy
8 from sfpy import Float64, Float32, Float16
9
10
11 def to_int16(x):
12 """ input: an unsigned int in the range 0..65535
13 output: a signed int in the range -32768..32767
14 """
15 if x > 32767:
16 return x-0x10000
17 return x
18
19
20 def to_int32(x):
21 """ input: an unsigned int in the range 0..2^32-1
22 output: a signed int in the range -2^31..2^31-1
23 """
24 if x > ((1 << 31)-1):
25 return x-(1 << 32)
26 return x
27
28
29 def to_uint16(x):
30 return x
31
32
33 def to_uint32(x):
34 return x
35
36
37 def to_uint64(x):
38 return x
39
40
41 def fcvt_64(x):
42 return sfpy.float.ui32_to_f64(x)
43
44
45 def fcvt_i16_f32(x):
46 print("fcvt i16_f32", hex(x))
47 return sfpy.float.i32_to_f32(x) # XXX no i16_to_f32, it's ok though
48
49
50 def fcvt_i32_f32(x):
51 print("fcvt i32_f32", hex(x))
52 return sfpy.float.i32_to_f32(x)
53
54
55 def fcvt_i32_f64(x):
56 print("fcvt i32_f64", hex(x))
57 return sfpy.float.i32_to_f64(x)
58
59
60 def fcvt_32(x):
61 return sfpy.float.ui32_to_f32(x)
62
63
64 def fcvt_64_to_32(x):
65 return sfpy.float.ui64_to_f32(x)
66
67
68 def fcvt_16(x):
69 return sfpy.float.ui32_to_f16(x)
70
71 ######################
72 # signed int to fp
73 ######################
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=20, opcode=0x1)
82
83
84 def test_int_pipe_i32_f64():
85 dut = FPCVTIntMuxInOut(32, 64, 4, op_wid=1)
86 runfp(dut, 32, "test_fcvt_int_pipe_i32_f64", to_int32, fcvt_i32_f64, True,
87 n_vals=20, opcode=0x1)
88
89
90 def test_int_pipe_i32_f32():
91 dut = FPCVTIntMuxInOut(32, 32, 4, op_wid=1)
92 runfp(dut, 32, "test_fcvt_int_pipe_i32_f32", to_int32, fcvt_i32_f32, True,
93 n_vals=20, opcode=0x1)
94
95 ######################
96 # unsigned int to fp
97 ######################
98
99
100 def test_int_pipe_ui16_f32():
101 # XXX softfloat-3 doesn't have ui16_to_xxx so use ui32 instead.
102 # should be fine.
103 dut = FPCVTIntMuxInOut(16, 32, 4, op_wid=1)
104 runfp(dut, 16, "test_fcvt_int_pipe_ui16_f32", to_uint16, fcvt_32, True,
105 n_vals=20)
106
107
108 def test_int_pipe_ui16_f64():
109 dut = FPCVTIntMuxInOut(16, 64, 4, op_wid=1)
110 runfp(dut, 16, "test_fcvt_int_pipe_ui16_f64", to_uint16, fcvt_64, True,
111 n_vals=20)
112
113
114 def test_int_pipe_ui32_f32():
115 dut = FPCVTIntMuxInOut(32, 32, 4, op_wid=1)
116 runfp(dut, 32, "test_fcvt_int_pipe_ui32_32", to_uint32, fcvt_32, True,
117 n_vals=20)
118
119
120 def test_int_pipe_ui32_f64():
121 dut = FPCVTIntMuxInOut(32, 64, 4, op_wid=1)
122 runfp(dut, 32, "test_fcvt_int_pipe_ui32_64", to_uint32, fcvt_64, True,
123 n_vals=20)
124
125
126 def test_int_pipe_ui64_f32():
127 # ok, doing 33 bits here because it's pretty pointless (not entirely)
128 # to do random numbers statistically likely 99.999% of the time to be
129 # converted to Inf
130 dut = FPCVTIntMuxInOut(64, 32, 4, op_wid=1)
131 runfp(dut, 33, "test_fcvt_int_pipe_ui64_32", to_uint64, fcvt_64_to_32, True,
132 n_vals=20)
133
134
135 def test_int_pipe_ui64_f16():
136 # ok, doing 17 bits here because it's pretty pointless (not entirely)
137 # to do random numbers statistically likely 99.999% of the time to be
138 # converted to Inf
139 dut = FPCVTIntMuxInOut(64, 16, 4, op_wid=1)
140 runfp(dut, 17, "test_fcvt_int_pipe_ui64_16", to_uint64, fcvt_16, True,
141 n_vals=20)
142
143
144 def test_int_pipe_ui32_f16():
145 # ok, doing 17 bits here because it's pretty pointless (not entirely)
146 # to do random numbers statistically likely 99.999% of the time to be
147 # converted to Inf
148 dut = FPCVTIntMuxInOut(32, 16, 4, op_wid=1)
149 runfp(dut, 17, "test_fcvt_int_pipe_ui32_16", to_uint32, fcvt_16, True,
150 n_vals=20)
151
152
153 if __name__ == '__main__':
154 for i in range(200):
155 test_int_pipe_i16_f32()
156 test_int_pipe_i32_f64()
157 test_int_pipe_ui32_f32()
158 test_int_pipe_i32_f32()
159 test_int_pipe_ui16_f32()
160 test_int_pipe_ui64_f32()
161 test_int_pipe_ui32_f16()
162 test_int_pipe_ui64_f16()
163 test_int_pipe_ui16_f64()
164 test_int_pipe_ui32_f64()