84ef7b444d87c4a5c1ae1d3a08562c76d54c2daa
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 goldschmidt_div
, FixedPoint
12 class TestFixedPoint(FHDLTestCase
):
13 def test_str_roundtrip(self
):
14 for frac_wid
in range(8):
15 for bits
in range(-1 << 9, 1 << 9):
16 with self
.subTest(bits
=hex(bits
), frac_wid
=frac_wid
):
17 value
= FixedPoint(bits
, frac_wid
)
18 round_trip_value
= FixedPoint
.cast(str(value
))
19 self
.assertEqual(value
, round_trip_value
)
22 class TestGoldschmidtDiv(FHDLTestCase
):
24 assert isinstance(width
, int)
25 for d
in range(1, 1 << width
):
26 for n
in range(d
<< width
):
28 with self
.subTest(width
=width
, n
=hex(n
), d
=hex(d
),
29 expected
=hex(expected
)):
30 result
= goldschmidt_div(n
, d
, width
)
31 self
.assertEqual(result
, expected
, f
"result={hex(result)}")
33 def test_1_through_5(self
):
34 for width
in range(1, 5 + 1):
41 if __name__
== "__main__":