From: Jacob Lifshay Date: Thu, 7 Apr 2022 02:59:03 +0000 (-0700) Subject: change reference algorithm to be more amenable to bitwise operations X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=f86878c336b3155821189648b7b6d142fc87c1e5;p=nmigen-gf.git change reference algorithm to be more amenable to bitwise operations --- 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