Re: [myhdl-list] Slicing an unsigned intbv to a signed one
Brought to you by:
jandecaluwe
From: Christopher L. F. <cf...@uc...> - 2008-06-13 00:46:43
|
Ok, below was my quick test to double check if it could be done. A real implementation probably need more thought (if feasible at all). With that said, I do think this approach will work. Even if you have a non power of 2 limit you still need N bits to represent the max and min values. If the msb of the "bit container" is modified it will change the sign. If the designer is changing any of the other bits she/he hopefully know what they are trying to achieve. I didn't think much about the slice assignment but again I believe the same approach could be taken just the implementation would need some thought. Because this is handled in the intbv class the conversion to Verilog/ VHDL should still be valid. < < #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ < # CFELTON EDIT < if self._min < 0: < msb = (self._val >> self._nrbits-1) & 0x1 < if msb and not val: < self._val = -1 * self._val < #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ < 167,181d157 < < #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ < # CFELTON EDIT < # If the new value msb is set and the number is not < # negative make it negative < # If the intbv is a signed number (min < 0) then < # modify the value accordingly < if self._min < 0: < msb = (self._val >> self._nrbits-1) & 0x1 < print val, msb, self._val, self._min, self._max, self._nrbits < if msb and self._val > 0: < self._val = -1 * self._val < < #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ On Jun 12, 2008, at 2:39 PM, Christopher Felton wrote: > Yes I will send the patch when I get home. I believe you are correct > my quick implementation probably only covers the power of 2 cases. > For the non-power of 2 cases, I think it still works cause you have > to have a power of 2 (can't have fraction of a bit) the msb of the > least number of bits (which is the _nrbits) is still the sign bit? > Just thinking out loud. > > Thanks > > On Thu, 12 Jun 2008 20:45:12 +0200 > Jan Decaluwe <ja...@ja...> wrote: >> Christopher L. Felton wrote: >> >>> Below is the output example for a small change that I made to the >>> bit >>> vector class >>>>>> from myhdl import * >>>>>> x = intbv(0, min=-8, max=8) >>>>>> x[3] = 1 >>>>>> x >>> intbv(-8L) >>>>>> x[3] = 0 >>>>>> x >>> intbv(0L) >>> >>> To implement the above I simply modified __setitems__ and used the >>> _min >>> and _nrbits values determine what type of number is being >>> represented. >>> If the number is signed multiply the value by -1 if the msb is >>> being >>> modified. >> >> Could you show your patch (just a diff)? I don't see how this >> would be correct for non-powers of 2. >> >> Jan >> >> -- >> Jan Decaluwe - Resources bvba - http://www.jandecaluwe.com >> Kaboutermansstraat 97, B-3000 Leuven, Belgium >> From Python to silicon: >> http://myhdl.jandecaluwe.com >> >> >> ------------------------------------------------------------------------- >> Check out the new SourceForge.net Marketplace. >> It's the best place to buy or sell services for >> just about anything Open Source. >> http://sourceforge.net/services/buy/index.php >> _______________________________________________ >> myhdl-list mailing list >> myh...@li... >> https://lists.sourceforge.net/lists/listinfo/myhdl-list > > > ------------------------------------------------------------------------- > Check out the new SourceForge.net Marketplace. > It's the best place to buy or sell services for > just about anything Open Source. > http://sourceforge.net/services/buy/index.php > _______________________________________________ > myhdl-list mailing list > myh...@li... > https://lists.sourceforge.net/lists/listinfo/myhdl-list |