change reference algorithm to be more amenable to bitwise operations
authorJacob Lifshay <programmerjake@gmail.com>
Thu, 7 Apr 2022 02:59:03 +0000 (19:59 -0700)
committerJacob Lifshay <programmerjake@gmail.com>
Thu, 7 Apr 2022 03:12:19 +0000 (20:12 -0700)
src/nmigen_gf/hdl/cldivrem.py

index 9a89c43046e2535fb272f47efefe51fb256db482..c63ad605c072a02520df65e2b620708ecfd677bf 100644 (file)
@@ -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