Remove print statements from clz.py
[ieee754fpu.git] / src / ieee754 / cordic / clz.py
index a6dec14cbada3aeeddab7bdda9ab11a189b681f8..a32fe50a913dc2c0729106ee205d5e552f51a714 100644 (file)
@@ -10,45 +10,60 @@ class CLZ(Elaboratable):
 
     def generate_pairs(self, m):
         comb = m.d.comb
-        assert self.width % 2 == 0  # TODO handle odd widths
         pairs = []
         for i in range(0, self.width, 2):
-            pair = Signal(2, name="pair%d" % i)
-            comb += pair.eq(self.sig_in[i:i+2])
+            if i+1 >= self.width:
+                pair = Signal(1, name="cnt_1_%d" % (i/2))
+                comb += pair.eq(~self.sig_in[i])
+                pairs.append((pair, 1))
+            else:
+                pair = Signal(2, name="pair%d" % i)
+                comb += pair.eq(self.sig_in[i:i+2])
 
-            pair_cnt = Signal(2, name="cnt_1_%d" % (i/2))
-            with m.Switch(pair):
-                with m.Case(0):
-                    comb += pair_cnt.eq(2)
-                with m.Case(1):
-                    comb += pair_cnt.eq(1)
-                with m.Default():
-                    comb += pair_cnt.eq(0)
-            pairs.append(pair_cnt)
+                pair_cnt = Signal(2, name="cnt_1_%d" % (i/2))
+                with m.Switch(pair):
+                    with m.Case(0):
+                        comb += pair_cnt.eq(2)
+                    with m.Case(1):
+                        comb += pair_cnt.eq(1)
+                    with m.Default():
+                        comb += pair_cnt.eq(0)
+                pairs.append((pair_cnt, 2))  # append pair, max_value
         return pairs
 
     def combine_pairs(self, m, iteration, pairs):
         comb = m.d.comb
         length = len(pairs)
-        assert length % 2 == 0  # TODO handle non powers of 2
         ret = []
         for i in range(0, length, 2):
-            left = pairs[i+1]
-            right = pairs[i]
-            width = left.width + 1
-            print(left)
-            print(f"pair({i}, {i+1}) - cnt_{iteration}_{i}")
-            new_pair = Signal(left.width + 1, name="cnt_%d_%d" %
-                              (iteration, i))
-            with m.If(left[-1] == 1):
-                with m.If(right[-1] == 1):
-                    comb += new_pair.eq(Cat(Repl(0, width-1), 1))
-                with m.Else():
-                    comb += new_pair.eq(Cat(right[0:-1], 0b01))
-            with m.Else():
-                comb += new_pair.eq(Cat(left, 0))
+            if i+1 >= length:
+                right, mv = pairs[i]
+                width = right.width
+                new_pair = Signal(width, name="cnt_%d_%d" % (iteration, i))
+                comb += new_pair.eq(Cat(right, 0))
+                ret.append((new_pair, mv))
+            else:
+                left, lv = pairs[i+1]
+                right, rv = pairs[i]
+                width = right.width + 1
+                new_pair = Signal(width, name="cnt_%d_%d" %
+                                  (iteration, i))
+                if rv == lv:
+                    with m.If(left[-1] == 1):
+                        with m.If(right[-1] == 1):
+                            comb += new_pair.eq(Cat(Repl(0, width-1), 1))
+                        with m.Else():
+                            comb += new_pair.eq(Cat(right[0:-1], 0b01))
+                    with m.Else():
+                        comb += new_pair.eq(Cat(left, 0))
+                else:
+                    with m.If(left == lv):
+                        comb += new_pair.eq(right + left)
+                    with m.Else():
+                        comb += new_pair.eq(left)
+                        
 
-            ret.append(new_pair)
+                ret.append((new_pair, lv+rv))
         return ret
 
     def elaborate(self, platform):
@@ -61,7 +76,7 @@ class CLZ(Elaboratable):
             pairs = self.combine_pairs(m, i, pairs)
             i += 1
 
-        comb += self.lz.eq(pairs[0])
+        comb += self.lz.eq(pairs[0][0])
 
         return m