84ef7b444d87c4a5c1ae1d3a08562c76d54c2daa
[soc.git] / src / soc / fu / div / experiment / test / test_goldschmidt_div_sqrt.py
1 # SPDX-License-Identifier: LGPL-3-or-later
2 # Copyright 2022 Jacob Lifshay programmerjake@gmail.com
3
4 # Funded by NLnet Assure Programme 2021-02-052, https://nlnet.nl/assure part
5 # of Horizon 2020 EU Programme 957073.
6
7 import unittest
8 from nmutil.formaltest import FHDLTestCase
9 from soc.fu.div.experiment.goldschmidt_div_sqrt import goldschmidt_div, FixedPoint
10
11
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)
20
21
22 class TestGoldschmidtDiv(FHDLTestCase):
23 def tst(self, width):
24 assert isinstance(width, int)
25 for d in range(1, 1 << width):
26 for n in range(d << width):
27 expected = n // d
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)}")
32
33 def test_1_through_5(self):
34 for width in range(1, 5 + 1):
35 self.tst(width)
36
37 def test_6(self):
38 self.tst(6)
39
40
41 if __name__ == "__main__":
42 unittest.main()