Use higher precision arithmetic when calculating the atan2 table
[ieee754fpu.git] / src / ieee754 / cordic / fpsin_cos.py
index fa7ed8c1c16217fdbc187d1604d420d6aa0adde2..3c96cafa952a5ab33452eef2e28c7271c93fbce7 100644 (file)
@@ -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))))
-                  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,
@@ -61,7 +71,7 @@ class CORDIC(Elaboratable):
         self.ready = Signal(reset=True)
 
         self.width = self.z0.width
-        self.iterations = self.width - 1
+        self.iterations = self.fracbits - 1
 
     def elaborate(self, platform):
         m = Module()
@@ -121,7 +131,6 @@ class CORDIC(Elaboratable):
             sync += i.eq(0)
             sync += state.eq(CordicState.RUNNING)
             sync += anglerom.addr.eq(1)
-            sync += self.ready.eq(1)  # debug
         with m.If(state == CordicState.RUNNING):
             with m.If(z >= 0):
                 sync += x.eq(x - dx)