From: Jacob Lifshay Date: Thu, 5 May 2022 05:49:37 +0000 (-0700) Subject: add clz function X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=06a4710a5fcc39f956d142cf03cb150d14d17e63;p=nmutil.git add clz function --- diff --git a/src/nmutil/clz.py b/src/nmutil/clz.py index c942226..70e0f51 100644 --- a/src/nmutil/clz.py +++ b/src/nmutil/clz.py @@ -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