From 7aea44d471226db693ee99fd272504248d57375b Mon Sep 17 00:00:00 2001 From: Michael Nolan Date: Fri, 17 Apr 2020 10:44:34 -0400 Subject: [PATCH] Use higher precision arithmetic when calculating the atan2 table --- setup.py | 1 + src/ieee754/cordic/fpsin_cos.py | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 95367ed0..c5bf5cf0 100644 --- a/setup.py +++ b/setup.py @@ -11,6 +11,7 @@ version = '0.0.1' install_requires = [ 'nmutil', # 'sfpy', # XXX temporarily disabled + 'gmpy2' ] test_requires = [ diff --git a/src/ieee754/cordic/fpsin_cos.py b/src/ieee754/cordic/fpsin_cos.py index c61e6bf2..3c96cafa 100644 --- a/src/ieee754/cordic/fpsin_cos.py +++ b/src/ieee754/cordic/fpsin_cos.py @@ -8,6 +8,10 @@ import math from enum import Enum, unique from ieee754.fpcommon.fpbase import FPNumBaseRecord, FPNumDecode +import gmpy2 +from gmpy2 import mpfr + + @unique class CordicState(Enum): @@ -25,8 +29,14 @@ class CordicROM(Elaboratable): self.addr = Signal(range(iterations)) self.data = Signal(range(-M, M-1)) - angles = [int(round(M*math.atan(2**(-i))/(math.pi/2))) - for i in range(self.iterations)] + angles = [] + gmpy2.get_context().precision = 150 + for i in range(self.iterations): + x = mpfr(2) ** -i + x = gmpy2.atan(x) + x = x/(gmpy2.const_pi()/mpfr(2)) + x = x * mpfr(M) + angles.append(int(round(x))) self.mem = Memory(width=self.data.width, depth=self.iterations, -- 2.30.2