goldschmidt division works! still needs better parameter selection tho...
[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 (
10 GoldschmidtDivParams, ParamsNotAccurateEnough, goldschmidt_div, 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 test_case1(self):
25 with self.assertRaises(ParamsNotAccurateEnough):
26 GoldschmidtDivParams(io_width=3, extra_precision=2,
27 table_addr_bits=3, table_data_bits=5,
28 iter_count=2)
29
30 def test_case2(self):
31 with self.assertRaises(ParamsNotAccurateEnough):
32 GoldschmidtDivParams(io_width=4, extra_precision=1,
33 table_addr_bits=1, table_data_bits=5,
34 iter_count=1)
35
36 def tst(self, io_width):
37 assert isinstance(io_width, int)
38 params = GoldschmidtDivParams.get(io_width)
39 with self.subTest(params=str(params)):
40 for d in range(1, 1 << io_width):
41 for n in range(d << io_width):
42 expected_q, expected_r = divmod(n, d)
43 with self.subTest(n=hex(n), d=hex(d),
44 expected_q=hex(expected_q),
45 expected_r=hex(expected_r)):
46 q, r = goldschmidt_div(n, d, params)
47 with self.subTest(q=hex(q), r=hex(r)):
48 self.assertEqual((q, r), (expected_q, expected_r))
49
50 def test_1_through_4(self):
51 for io_width in range(1, 4 + 1):
52 with self.subTest(io_width=io_width):
53 self.tst(io_width)
54
55 def test_5(self):
56 self.tst(5)
57
58 def test_6(self):
59 self.tst(6)
60
61 def tst_params(self, io_width):
62 assert isinstance(io_width, int)
63 params = GoldschmidtDivParams.get(io_width)
64 print()
65 print(params)
66
67 def test_params_1(self):
68 self.tst_params(1)
69
70 def test_params_2(self):
71 self.tst_params(2)
72
73 def test_params_3(self):
74 self.tst_params(3)
75
76 def test_params_4(self):
77 self.tst_params(4)
78
79 def test_params_5(self):
80 self.tst_params(5)
81
82 def test_params_6(self):
83 self.tst_params(6)
84
85 def test_params_7(self):
86 self.tst_params(7)
87
88 def test_params_8(self):
89 self.tst_params(8)
90
91 def test_params_9(self):
92 self.tst_params(9)
93
94 def test_params_10(self):
95 self.tst_params(10)
96
97 def test_params_11(self):
98 self.tst_params(11)
99
100 def test_params_12(self):
101 self.tst_params(12)
102
103 def test_params_13(self):
104 self.tst_params(13)
105
106 def test_params_14(self):
107 self.tst_params(14)
108
109 def test_params_15(self):
110 self.tst_params(15)
111
112 def test_params_16(self):
113 self.tst_params(16)
114
115 def test_params_17(self):
116 self.tst_params(17)
117
118 def test_params_18(self):
119 self.tst_params(18)
120
121 def test_params_19(self):
122 self.tst_params(19)
123
124 def test_params_20(self):
125 self.tst_params(20)
126
127 def test_params_21(self):
128 self.tst_params(21)
129
130 def test_params_22(self):
131 self.tst_params(22)
132
133 def test_params_23(self):
134 self.tst_params(23)
135
136 def test_params_24(self):
137 self.tst_params(24)
138
139 def test_params_25(self):
140 self.tst_params(25)
141
142 def test_params_26(self):
143 self.tst_params(26)
144
145 def test_params_27(self):
146 self.tst_params(27)
147
148 def test_params_28(self):
149 self.tst_params(28)
150
151 def test_params_29(self):
152 self.tst_params(29)
153
154 def test_params_30(self):
155 self.tst_params(30)
156
157 def test_params_31(self):
158 self.tst_params(31)
159
160 def test_params_32(self):
161 self.tst_params(32)
162
163 def test_params_33(self):
164 self.tst_params(33)
165
166 def test_params_34(self):
167 self.tst_params(34)
168
169 def test_params_35(self):
170 self.tst_params(35)
171
172 def test_params_36(self):
173 self.tst_params(36)
174
175 def test_params_37(self):
176 self.tst_params(37)
177
178 def test_params_38(self):
179 self.tst_params(38)
180
181 def test_params_39(self):
182 self.tst_params(39)
183
184 def test_params_40(self):
185 self.tst_params(40)
186
187 def test_params_41(self):
188 self.tst_params(41)
189
190 def test_params_42(self):
191 self.tst_params(42)
192
193 def test_params_43(self):
194 self.tst_params(43)
195
196 def test_params_44(self):
197 self.tst_params(44)
198
199 def test_params_45(self):
200 self.tst_params(45)
201
202 def test_params_46(self):
203 self.tst_params(46)
204
205 def test_params_47(self):
206 self.tst_params(47)
207
208 def test_params_48(self):
209 self.tst_params(48)
210
211 def test_params_49(self):
212 self.tst_params(49)
213
214 def test_params_50(self):
215 self.tst_params(50)
216
217 def test_params_51(self):
218 self.tst_params(51)
219
220 def test_params_52(self):
221 self.tst_params(52)
222
223 def test_params_53(self):
224 self.tst_params(53)
225
226 def test_params_54(self):
227 self.tst_params(54)
228
229 def test_params_55(self):
230 self.tst_params(55)
231
232 def test_params_56(self):
233 self.tst_params(56)
234
235 def test_params_57(self):
236 self.tst_params(57)
237
238 def test_params_58(self):
239 self.tst_params(58)
240
241 def test_params_59(self):
242 self.tst_params(59)
243
244 def test_params_60(self):
245 self.tst_params(60)
246
247 def test_params_61(self):
248 self.tst_params(61)
249
250 def test_params_62(self):
251 self.tst_params(62)
252
253 def test_params_63(self):
254 self.tst_params(63)
255
256 def test_params_64(self):
257 self.tst_params(64)
258
259
260 if __name__ == "__main__":
261 unittest.main()