From 7156581344e98812e2ede53924b4523ee6ade3d5 Mon Sep 17 00:00:00 2001 From: Michael Nolan Date: Mon, 4 May 2020 16:01:20 -0400 Subject: [PATCH] Extend clz to work with odd widths --- src/ieee754/cordic/clz.py | 28 ++++++++++++++++------------ src/ieee754/cordic/test/test_clz.py | 2 +- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/ieee754/cordic/clz.py b/src/ieee754/cordic/clz.py index 51239dc2..b8359152 100644 --- a/src/ieee754/cordic/clz.py +++ b/src/ieee754/cordic/clz.py @@ -10,21 +10,25 @@ 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, 2)) # append pair, max_value + 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): diff --git a/src/ieee754/cordic/test/test_clz.py b/src/ieee754/cordic/test/test_clz.py index c3051007..5d952417 100644 --- a/src/ieee754/cordic/test/test_clz.py +++ b/src/ieee754/cordic/test/test_clz.py @@ -39,7 +39,7 @@ class CLZTestCase(FHDLTestCase): def test_non_power_2(self): inputs = [0, 128, 512] - self.run_test(iter(inputs), width=10) + self.run_test(iter(inputs), width=11) if __name__ == "__main__": -- 2.30.2