From 06a4710a5fcc39f956d142cf03cb150d14d17e63 Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Wed, 4 May 2022 22:49:37 -0700 Subject: [PATCH] add clz function --- src/nmutil/clz.py | 8 ++++++++ 1 file changed, 8 insertions(+) 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 -- 2.30.2