From: Jacob Lifshay Date: Sun, 7 Jul 2019 08:00:10 +0000 (-0700) Subject: add tests for integer and fractional division X-Git-Tag: ls180-24jan2020~898 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=41b364cd804d637c7624072bb724085b600e56b5;p=ieee754fpu.git add tests for integer and fractional division --- diff --git a/src/ieee754/div_rem_sqrt_rsqrt/test_algorithm.py b/src/ieee754/div_rem_sqrt_rsqrt/test_algorithm.py index 7d6b2013..5760dbab 100644 --- a/src/ieee754/div_rem_sqrt_rsqrt/test_algorithm.py +++ b/src/ieee754/div_rem_sqrt_rsqrt/test_algorithm.py @@ -1077,3 +1077,55 @@ class TestFixedUDivRemSqrtRSqrt(unittest.TestCase): def test_radix_16_RSqrt(self): self.helper(4, Operation.RSqrtRem) + + def test_int_div(self): + bit_width = 8 + fract_width = 4 + log2_radix = 3 + for dividend in range(1 << bit_width): + for divisor in range(1, 1 << bit_width): + obj = FixedUDivRemSqrtRSqrt(dividend, + divisor, + Operation.UDivRem, + bit_width, + fract_width, + log2_radix) + obj.calculate() + quotient, remainder = div_rem(dividend, + divisor, + bit_width, + False) + shifted_remainder = remainder << fract_width + with self.subTest(dividend=dividend, + divisor=divisor, + quotient=quotient, + remainder=remainder, + shifted_remainder=shifted_remainder): + self.assertEqual(obj.quotient_root, quotient) + self.assertEqual(obj.remainder, shifted_remainder) + + def test_fract_div(self): + bit_width = 8 + fract_width = 4 + log2_radix = 3 + for dividend in range(1 << bit_width): + for divisor in range(1, 1 << bit_width): + obj = FixedUDivRemSqrtRSqrt(dividend << fract_width, + divisor, + Operation.UDivRem, + bit_width, + fract_width, + log2_radix) + obj.calculate() + quotient = (dividend << fract_width) // divisor + if quotient >= (1 << bit_width): + continue + remainder = (dividend << fract_width) % divisor + shifted_remainder = remainder << fract_width + with self.subTest(dividend=dividend, + divisor=divisor, + quotient=quotient, + remainder=remainder, + shifted_remainder=shifted_remainder): + self.assertEqual(obj.quotient_root, quotient) + self.assertEqual(obj.remainder, shifted_remainder)