e3c28b6413c7c621c57b9cf8b153a136007d04a8
[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,
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 def tst(self, width):
25 assert isinstance(width, int)
26 for d in range(1, 1 << width):
27 for n in range(d << width):
28 expected = n // d
29 with self.subTest(width=width, n=hex(n), d=hex(d),
30 expected=hex(expected)):
31 result = goldschmidt_div(n, d, width)
32 self.assertEqual(result, expected, f"result={hex(result)}")
33
34 def test_1_through_5(self):
35 for width in range(1, 5 + 1):
36 self.tst(width)
37
38 def test_6(self):
39 self.tst(6)
40
41
42 if __name__ == "__main__":
43 unittest.main()