1 from migen
.fhdl
import structure
as f
4 __all__
= ["log2_int", "bits_for", "value_bits_sign"]
7 def log2_int(n
, need_pow2
=True):
13 if need_pow2
and l
!= n
:
14 raise ValueError("Not a power of 2")
18 def bits_for(n
, require_sign_bit
=False):
20 r
= log2_int(n
+ 1, False)
22 require_sign_bit
= True
23 r
= log2_int(-n
, False)
29 def value_bits_sign(v
):
30 """Bit length and signedness of a value.
39 Number of bits required to store `v` or available in `v`, followed by
40 whether `v` has a sign bit (included in the bit count).
44 >>> value_bits_sign(f.Signal(8))
46 >>> value_bits_sign(C(0xaa))
49 if isinstance(v
, (f
.Constant
, f
.Signal
)):
50 return v
.nbits
, v
.signed
51 elif isinstance(v
, (f
.ClockSignal
, f
.ResetSignal
)):
53 elif isinstance(v
, f
._Operator
):
54 obs
= list(map(value_bits_sign
, v
.operands
))
55 if v
.op
== "+" or v
.op
== "-":
56 if not obs
[0][1] and not obs
[1][1]:
57 # both operands unsigned
58 return max(obs
[0][0], obs
[1][0]) + 1, False
59 elif obs
[0][1] and obs
[1][1]:
60 # both operands signed
61 return max(obs
[0][0], obs
[1][0]) + 1, True
62 elif not obs
[0][1] and obs
[1][1]:
63 # first operand unsigned (add sign bit), second operand signed
64 return max(obs
[0][0] + 1, obs
[1][0]) + 1, True
66 # first signed, second operand unsigned (add sign bit)
67 return max(obs
[0][0], obs
[1][0] + 1) + 1, True
69 if not obs
[0][1] and not obs
[1][1]:
70 # both operands unsigned
71 return obs
[0][0] + obs
[1][0], False
72 elif obs
[0][1] and obs
[1][1]:
73 # both operands signed
74 return obs
[0][0] + obs
[1][0] - 1, True
76 # one operand signed, the other unsigned (add sign bit)
77 return obs
[0][0] + obs
[1][0] + 1 - 1, True
80 extra
= 2**(obs
[1][0] - 1) - 1
82 extra
= 2**obs
[1][0] - 1
83 return obs
[0][0] + extra
, obs
[0][1]
86 extra
= 2**(obs
[1][0] - 1)
89 return obs
[0][0] + extra
, obs
[0][1]
90 elif v
.op
== "&" or v
.op
== "^" or v
.op
== "|":
91 if not obs
[0][1] and not obs
[1][1]:
92 # both operands unsigned
93 return max(obs
[0][0], obs
[1][0]), False
94 elif obs
[0][1] and obs
[1][1]:
95 # both operands signed
96 return max(obs
[0][0], obs
[1][0]), True
97 elif not obs
[0][1] and obs
[1][1]:
98 # first operand unsigned (add sign bit), second operand signed
99 return max(obs
[0][0] + 1, obs
[1][0]), True
101 # first signed, second operand unsigned (add sign bit)
102 return max(obs
[0][0], obs
[1][0] + 1), True
103 elif v
.op
== "<" or v
.op
== "<=" or v
.op
== "==" or v
.op
== "!=" \
104 or v
.op
== ">" or v
.op
== ">=":
110 elif isinstance(v
, f
._Slice
):
111 return v
.stop
- v
.start
, value_bits_sign(v
.value
)[1]
112 elif isinstance(v
, f
.Cat
):
113 return sum(value_bits_sign(sv
)[0] for sv
in v
.l
), False
114 elif isinstance(v
, f
.Replicate
):
115 return (value_bits_sign(v
.v
)[0])*v
.n
, False
116 elif isinstance(v
, f
._ArrayProxy
):
117 bsc
= list(map(value_bits_sign
, v
.choices
))
118 return max(bs
[0] for bs
in bsc
), any(bs
[1] for bs
in bsc
)
120 raise TypeError("Can not calculate bit length of {} {}".format(