add clz function
authorJacob Lifshay <programmerjake@gmail.com>
Thu, 5 May 2022 05:49:37 +0000 (22:49 -0700)
committerJacob Lifshay <programmerjake@gmail.com>
Thu, 5 May 2022 05:49:37 +0000 (22:49 -0700)
src/nmutil/clz.py

index c94222610bc29c49583a0eb12a01c58bac4760f8..70e0f513c45b52034c9aac5aac8d86dd55d76a38 100644 (file)
@@ -12,6 +12,14 @@ import math
 """
 
 
+def clz(v, width):
+    """count leading zeros."""
+    assert isinstance(width, int) and 0 <= width
+    max_v = (1 << width) - 1
+    assert isinstance(v, int) and 0 <= v <= max_v
+    return max_v.bit_length() - v.bit_length()
+
+
 class CLZ(Elaboratable):
     def __init__(self, width):
         self.width = width