1 # SPDX-License-Identifier: LGPL-3-or-later
2 # Copyright 2022 Jacob Lifshay programmerjake@gmail.com
4 # Funded by NLnet Assure Programme 2021-02-052, https://nlnet.nl/assure part
5 # of Horizon 2020 EU Programme 957073.
8 from nmutil
.formaltest
import FHDLTestCase
9 from soc
.fu
.div
.experiment
.goldschmidt_div_sqrt
import (
10 GoldschmidtDivParams
, ParamsNotAccurateEnough
, goldschmidt_div
, FixedPoint
)
13 class TestFixedPoint(FHDLTestCase
):
14 def test_str_roundtrip(self
):
15 for frac_wid
in range(8):
16 for bits
in range(-1 << 9, 1 << 9):
17 with self
.subTest(bits
=hex(bits
), frac_wid
=frac_wid
):
18 value
= FixedPoint(bits
, frac_wid
)
19 round_trip_value
= FixedPoint
.cast(str(value
))
20 self
.assertEqual(value
, round_trip_value
)
23 class TestGoldschmidtDiv(FHDLTestCase
):
25 with self
.assertRaises(ParamsNotAccurateEnough
):
26 GoldschmidtDivParams(io_width
=3, extra_precision
=2,
27 table_addr_bits
=3, table_data_bits
=5,
31 with self
.assertRaises(ParamsNotAccurateEnough
):
32 GoldschmidtDivParams(io_width
=4, extra_precision
=1,
33 table_addr_bits
=1, table_data_bits
=5,
36 def tst(self
, io_width
):
37 assert isinstance(io_width
, int)
38 params
= GoldschmidtDivParams
.get(io_width
)
39 with self
.subTest(params
=str(params
)):
40 for d
in range(1, 1 << io_width
):
41 for n
in range(d
<< io_width
):
42 expected_q
, expected_r
= divmod(n
, d
)
43 with self
.subTest(n
=hex(n
), d
=hex(d
),
44 expected_q
=hex(expected_q
),
45 expected_r
=hex(expected_r
)):
46 q
, r
= goldschmidt_div(n
, d
, params
)
47 with self
.subTest(q
=hex(q
), r
=hex(r
)):
48 self
.assertEqual((q
, r
), (expected_q
, expected_r
))
50 def test_1_through_4(self
):
51 for io_width
in range(1, 4 + 1):
52 with self
.subTest(io_width
=io_width
):
61 def tst_params(self
, io_width
):
62 assert isinstance(io_width
, int)
63 params
= GoldschmidtDivParams
.get(io_width
)
67 def test_params_1(self
):
70 def test_params_2(self
):
73 def test_params_3(self
):
76 def test_params_4(self
):
79 def test_params_5(self
):
82 def test_params_6(self
):
85 def test_params_7(self
):
88 def test_params_8(self
):
91 def test_params_9(self
):
94 def test_params_10(self
):
97 def test_params_11(self
):
100 def test_params_12(self
):
103 def test_params_13(self
):
106 def test_params_14(self
):
109 def test_params_15(self
):
112 def test_params_16(self
):
115 def test_params_17(self
):
118 def test_params_18(self
):
121 def test_params_19(self
):
124 def test_params_20(self
):
127 def test_params_21(self
):
130 def test_params_22(self
):
133 def test_params_23(self
):
136 def test_params_24(self
):
139 def test_params_25(self
):
142 def test_params_26(self
):
145 def test_params_27(self
):
148 def test_params_28(self
):
151 def test_params_29(self
):
154 def test_params_30(self
):
157 def test_params_31(self
):
160 def test_params_32(self
):
163 def test_params_33(self
):
166 def test_params_34(self
):
169 def test_params_35(self
):
172 def test_params_36(self
):
175 def test_params_37(self
):
178 def test_params_38(self
):
181 def test_params_39(self
):
184 def test_params_40(self
):
187 def test_params_41(self
):
190 def test_params_42(self
):
193 def test_params_43(self
):
196 def test_params_44(self
):
199 def test_params_45(self
):
202 def test_params_46(self
):
205 def test_params_47(self
):
208 def test_params_48(self
):
211 def test_params_49(self
):
214 def test_params_50(self
):
217 def test_params_51(self
):
220 def test_params_52(self
):
223 def test_params_53(self
):
226 def test_params_54(self
):
229 def test_params_55(self
):
232 def test_params_56(self
):
235 def test_params_57(self
):
238 def test_params_58(self
):
241 def test_params_59(self
):
244 def test_params_60(self
):
247 def test_params_61(self
):
250 def test_params_62(self
):
253 def test_params_63(self
):
256 def test_params_64(self
):
260 if __name__
== "__main__":