30fcf4e817791f3902a0dd91caf708ebb2d8936f
[ieee754fpu.git] / src / ieee754 / fclass / test / test_fclass_pipe.py
1 """ test of FPClassMuxInOut
2 """
3
4 from ieee754.fclass.pipeline import (FPClassMuxInOut,)
5 from ieee754.fpcommon.test.fpmux import runfp
6 from ieee754.fpcommon.fpbase import FPFormat
7
8 import sfpy
9 from sfpy import Float64, Float32, Float16
10
11 def fclass(wid, x):
12 """ analyses the FP number and returns a RISC-V "FCLASS" unary bitfield
13
14 this is easy to understand however it has redundant checks (which
15 don't matter because performance of *testing* is not hardware-critical)
16 see FPClassMod for a hardware-optimal (hard-to-read) version
17 """
18 x = x.bits
19 fmt = FPFormat.standard(wid)
20 print (hex(x), "exp", fmt.get_exponent(x), fmt.e_max,
21 "m", hex(fmt.get_mantissa_field(x)),
22 fmt.get_mantissa_field(x) & (1<<fmt.m_width-1))
23 if fmt.is_inf(x):
24 if fmt.get_sign_field(x):
25 return 1<<0
26 else:
27 return 1<<7
28 if fmt.is_zero(x):
29 if fmt.get_sign_field(x):
30 return 1<<3
31 else:
32 return 1<<4
33 if fmt.get_exponent(x) == fmt.e_max and fmt.get_mantissa_field(x) != 0:
34 if fmt.is_nan_signaling(x):
35 return 1<<8
36 else:
37 return 1<<9
38 if fmt.is_subnormal(x) and fmt.get_mantissa_field(x) != 0:
39 if fmt.get_sign_field(x):
40 return 1<<2
41 else:
42 return 1<<5
43 if fmt.get_sign_field(x):
44 return 1<<1
45 else:
46 return 1<<6
47
48
49 def fclass_16(x):
50 return fclass(16, x)
51
52
53 def fclass_32(x):
54 return fclass(32, x)
55
56
57 def fclass_64(x):
58 return fclass(64, x)
59
60
61 def test_class_pipe_f16():
62 dut = FPClassMuxInOut(16, 16, 4, op_wid=1)
63 runfp(dut, 16, "test_fclass_pipe_f16", Float16, fclass_16,
64 True, n_vals=100)
65
66
67 def test_class_pipe_f32():
68 dut = FPClassMuxInOut(32, 32, 4, op_wid=1)
69 runfp(dut, 32, "test_fclass_pipe_f32", Float32, fclass_32,
70 True, n_vals=100)
71
72
73 def test_class_pipe_f64():
74 dut = FPClassMuxInOut(64, 64, 4, op_wid=1)
75 runfp(dut, 64, "test_fclass_pipe_f64", Float64, fclass_64,
76 True, n_vals=100)
77
78
79 if __name__ == '__main__':
80 for i in range(200):
81 test_class_pipe_f16()
82 test_class_pipe_f32()
83 test_class_pipe_f64()