From 59831e0485643453eb2495c607e64620cbe3688f Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Wed, 28 Nov 2012 18:39:44 +0100 Subject: [PATCH] fhdl/structure: improved bits_for function --- migen/fhdl/structure.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/migen/fhdl/structure.py b/migen/fhdl/structure.py index 16dd435f..443ddea8 100644 --- a/migen/fhdl/structure.py +++ b/migen/fhdl/structure.py @@ -1,31 +1,30 @@ -import math import inspect import re from collections import defaultdict from migen.fhdl import tracer -def log2_int(n): +def log2_int(n, need_pow2=True): l = 1 r = 0 while l < n: l *= 2 r += 1 - if l == n: - return r - else: + if need_pow2 and l != n: raise ValueError("Not a power of 2") + return r -def bits_for(n): +def bits_for(n, require_sign_bit=False): if isinstance(n, Constant): return len(n) + if n > 0: + r = log2_int(n + 1, False) else: - if n < 0: - return bits_for(-n) + 1 - elif n == 0: - return 1 - else: - return int(math.ceil(math.log(n+1, 2))) + require_sign_bit = True + r = log2_int(-n, False) + if require_sign_bit: + r += 1 + return r class BV: def __init__(self, width=1, signed=False): -- 2.30.2