Skip to content

Commit

Permalink
log2_int: use bit_length()
Browse files Browse the repository at this point in the history
jordens committed Nov 4, 2016
1 parent 749704b commit 565927a
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions migen/fhdl/bitcontainer.py
Original file line number Diff line number Diff line change
@@ -5,12 +5,10 @@


def log2_int(n, need_pow2=True):
l = 1
r = 0
while l < n:
l *= 2
r += 1
if need_pow2 and l != n:
if n == 0:
return 0
r = (n - 1).bit_length()
if need_pow2 and (1 << r) != n:
raise ValueError("Not a power of 2")
return r

0 comments on commit 565927a

Please sign in to comment.