From f86878c336b3155821189648b7b6d142fc87c1e5 Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Wed, 6 Apr 2022 19:59:03 -0700 Subject: [PATCH] change reference algorithm to be more amenable to bitwise operations --- src/nmigen_gf/hdl/cldivrem.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/nmigen_gf/hdl/cldivrem.py b/src/nmigen_gf/hdl/cldivrem.py index 9a89c43..c63ad60 100644 --- a/src/nmigen_gf/hdl/cldivrem.py +++ b/src/nmigen_gf/hdl/cldivrem.py @@ -23,16 +23,19 @@ def equal_leading_zero_count_reference(a, b, width): assert isinstance(b, int) and 0 <= b < (1 << width) eq = True # both have no leading zeros so far... for i in range(width): - if (a >> i) & 1: - if (b >> i) & 1: - eq = True # both have no leading zeros so far... - else: - eq = False # different number of leading zeros + a_bit = (a >> i) & 1 + b_bit = (b >> i) & 1 + # `both_ones` is set if both have no leading zeros so far + both_ones = a_bit & b_bit + # `different` is set if there are a different number of leading + # zeros so far + different = a_bit != b_bit + if both_ones: + eq = True + elif different: + eq = False else: - if (b >> i) & 1: - eq = False # different number of leading zeros - else: - pass # propagate results from lower bits + pass # propagate from lower bits return eq -- 2.30.2