working on goldschmidt division algorithm
[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 print(params)
29 for d in range(1, 1 << io_width):
30 for n in range(d << io_width):
31 expected = n // d
32 with self.subTest(io_width=io_width, n=hex(n), d=hex(d),
33 expected=hex(expected)):
34 result = goldschmidt_div(n, d, params)
35 self.assertEqual(result, expected, f"result={hex(result)}")
36
37 def test_1_through_5(self):
38 for width in range(1, 5 + 1):
39 self.tst(width)
40
41 def test_6(self):
42 self.tst(6)
43
44
45 if __name__ == "__main__":
46 unittest.main()