Implement 32 bit cntlz and cnttz
authorMichael Nolan <mtnolan2640@gmail.com>
Tue, 19 May 2020 16:06:52 +0000 (12:06 -0400)
committerMichael Nolan <mtnolan2640@gmail.com>
Tue, 19 May 2020 16:06:52 +0000 (12:06 -0400)
src/soc/fu/logical/main_stage.py
src/soc/fu/logical/test/test_pipe_caller.py

index 96ace02eb8a62a3b01f5faddaed32f264edc6edb..5f93d3e6825e8810937e992518c133094ec7b304 100644 (file)
@@ -113,9 +113,22 @@ class LogicalMainStage(PipeModBase):
                 XO = self.fields.FormX.XO[0:-1]
                 count_right = Signal(reset_less=True)
                 comb += count_right.eq(XO[-1])
+
+                cntz_input = Signal(64, reset_less=True)
+                with m.If(self.i.ctx.op.is_32bit):
+                    with m.If(count_right):
+                        comb += cntz_input.eq(a[0:32][::-1])
+                    with m.Else():
+                        comb += cntz_input.eq(a[0:32])
+                with m.Else():
+                    with m.If(count_right):
+                        comb += cntz_input.eq(a[::-1])
+                    with m.Else():
+                        comb += cntz_input.eq(a)
                 m.submodules.clz = clz = CLZ(64)
-                comb += clz.sig_in.eq(Mux(count_right, a[::-1], a))
-                comb += o.eq(clz.lz)
+                comb += clz.sig_in.eq(cntz_input)
+                comb += o.eq(Mux(self.i.ctx.op.is_32bit,
+                                 clz.lz-32, clz.lz))
 
             ###### bpermd #######
             # TODO with m.Case(InternalOp.OP_BPERM): - not in microwatt
index f3760c7c15755758a9835e8d00948847283e985f..b44245212b50523b849d04262a88d509ea53ad78 100644 (file)
@@ -124,7 +124,7 @@ class LogicalTestCase(FHDLTestCase):
             self.run_tst_program(Program(lst), initial_regs)
 
     def test_cntz(self):
-        insns = ["cntlzd", "cnttzd"]
+        insns = ["cntlzd", "cnttzd", "cntlzw", "cnttzw"]
         for i in range(100):
             choice = random.choice(insns)
             lst = [f"{choice} 3, 1"]