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