Re: [myhdl-list] intbv with min -1, max 1 has 2 bits?
Brought to you by:
jandecaluwe
|
From: Jan D. <ja...@ja...> - 2008-06-12 20:27:04
|
Günter Dannoritzer wrote:
> Hi,
>
> I tried to create a signed intbv with one bit in the form:
>
> >>>a = intbv(-1,-1,1)
> >>>print "a: %d, min: %d, max: %d, _nrbits: %d"%(
> a, a.min, a.max, a._nrbits)
>
> a: -1, min: -1, max: 1, _nrbits: 2
>
> It turns out that intbv uses 2 bits for it. Looking in the
> intbv.__init__() function there is this code section starting at line 46:
>
> if max is not None and min is not None:
> _nrbits = maxfunc(len(bin(max-1)), len(bin(min)))
> if min >= 0:
> _nrbits = len(bin(max-1))
> elif max <= 0:
> _nrbits = len(bin(min))
> else:
> # make sure there is a leading zero bit in positive
> numbers
> _nrbits = maxfunc(len(bin(max-1))+1, len(bin(min)))
>
>
> What is interesting about this is that the first assignment to _nrbits
> right after the if clause is actually overwritten by any of the
> following assignments.
>
> So the reason for the -1,1 range to have 2 bits seems to lay in the
> comment about having a leading zero bit for positive numbers.
>
> I might be oblivious and the reason is obvious, but I don't see it at
> the moment. Anyone can explain why I need a leading zero bit and cannot
> create a 1 bit intbv?
1) why a leading zero bit
Well, for a number that can be both positive or negative you need to
have room for a sign bit, right? that's what the leading zero bit does.
2) why not a 1 bit signed intbv
That seems to be a bug. The one bit can function as a sign bit in
this (corner) case. I guess the test should really be:
...
elif max <= 1: # not 0
_nrbits = len(bin(min))
...
A sidenote: To get insight in this, I used the brand new web interface
to the brand new mercurial repository at:
http://myhdl.sourceforge.net/hg/myhdl
If you go to _intbv.py through "files", and then "annotate", you see the changeset
when these lines were introduced. Click on it and then go to "changeset" to review
that changeset. Great isn't it :-)
Jan
--
Jan Decaluwe - Resources bvba - http://www.jandecaluwe.com
Kaboutermansstraat 97, B-3000 Leuven, Belgium
From Python to silicon:
http://myhdl.jandecaluwe.com
|