working on goldschmidt_div_sqrt.py
[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 (GoldschmidtDivParams, goldschmidt_div,
10 FixedPoint)
11
12
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)
21
22
23 class TestGoldschmidtDiv(FHDLTestCase):
24 @unittest.skip("goldschmidt_div isn't finished yet")
25 def tst(self, io_width):
26 assert isinstance(io_width, int)
27 params = GoldschmidtDivParams.get(io_width)
28 with self.subTest(params=str(params)):
29 for d in range(1, 1 << io_width):
30 for n in range(d << io_width):
31 expected_q, expected_r = divmod(n, d)
32 with self.subTest(n=hex(n), d=hex(d),
33 expected_q=hex(expected_q),
34 expected_r=hex(expected_r)):
35 q, r = goldschmidt_div(n, d, params)
36 with self.subTest(q=hex(q), r=hex(r)):
37 self.assertEqual((q, r), (expected_q, expected_r))
38
39 def test_1_through_5(self):
40 for io_width in range(1, 5 + 1):
41 with self.subTest(io_width=io_width):
42 self.tst(io_width)
43
44 def test_6(self):
45 self.tst(6)
46
47
48 if __name__ == "__main__":
49 unittest.main()