Re: [myhdl-list] Slicing an unsigned intbv to a signed one
Brought to you by:
jandecaluwe
From: Christopher L. F. <cf...@uc...> - 2008-06-16 13:26:21
|
Yes, that is a much better solution. Implementing something similar appears to achieve the goals. As before I did not implement anything for the slicing to test. The slicing approach would be similar (and testing probably pretty weak, need to test other bit widths, unsigned, etc.). ------ Example Output ------ >>> x = intbv(7, min=-8, max=8) >>> x intbv(7) >>> str(x) '0111' >>> x[3] = 1 >>> x intbv(-1L) >>> str(x) '1111' >>> x[0] = 0 >>> x intbv(-2L) >>> str(x) '1110' >>> x[1] = 0 >>> x intbv(-4L) >>> str(x) '1100' >>> x[2] = 0 >>> x intbv(-8L) >>> str(x) '1000' >>> x[3] = 0 >>> x intbv(0L) >>> str(x) '0000' >>> x[3] = 1 >>> x intbv(-8L) >>> str(x) '1000' >>> x[:] = 3 >>> x intbv(3L) >>> str(x) '0011' >>> x[3] = 1 >>> x intbv(-5L) >>> str(x) '1011' >>> ----- Diff ------ 154,163d153 < < #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ < # CFELTON EDIT < # Create a sign bit mask < if self._min < 0 and key == self._nrbits-1: < smask = (-1 << self._nrbits-1) < else: < smask = 0 < #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ < 165c155 < self._val |= ((1L << i) | smask) --- > self._val |= (1L << i) 167,169c157 < self._val &= (~(1L << i) & ~smask) < < --- > self._val &= ~(1L << i) 196d183 < 449,455c436 < s = '' < for i in range(self._nrbits): < if self._val & (1 << i): < s = '1' + s < else: < s = '0' + s < return s #str(self._val) --- > return str(self._val) On Jun 13, 2008, at 12:20 PM, Günter Dannoritzer wrote: > Christopher L. Felton wrote: >> 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). > ... > > I think what you need to do is not just multiply by -1, but create a > mask that is ORed with the actual number. If the sign bit gets set, > the > mask will be all 1 upwards from the sign bit and all 0 down to bit 0. > Then OR that with the current intbv value. > > So instead of this: > > self._val = -1 * self._val > > do this: > > self._val = (-1 << self._nrbits-2) | self._val > > This is basically a merging between the existing bits below the sign > bit > and the sign change to negative. > > For clearing the sign bit the simplest way would be to slice the bits > below the sign bit out and return them as a new intbv value. > > Guenter > > > > ------------------------------------------------------------------------- > 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 |