fixes signed/unsigned conversion for large integer sizes

This commit is contained in:
Eyck Jentzsch 2023-03-18 18:29:34 +01:00
parent 65461ccc48
commit 354c4a4390
3 changed files with 35 additions and 54 deletions

View File

@ -10,6 +10,7 @@
*******************************************************************************/
package com.minres.scviewer.database.ui.swt.internal;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collection;
@ -214,10 +215,10 @@ public class SignalPainter extends TrackPainter {
String label = null;
switch(trackEntry.valueDisplay) {
case SIGNED:
label=Long.toString(last.toSignedValue());
label=last.toSignedValue().toString();
break;
case UNSIGNED:
label=Long.toString(last.toUnsignedValue());
label=last.toUnsignedValue().toString();
break;
case BINARY:
label=last.toString();
@ -248,9 +249,9 @@ public class SignalPainter extends TrackPainter {
final boolean continous;
final boolean signed;
private long maxVal;
private long minVal;
double yRange = (yOffsetB-yOffsetT);
private BigInteger maxVal;
private BigInteger minVal;
int yRange = (yOffsetB-yOffsetT);
public MultiBitStencilAnalog(IEventList entries, Object left, boolean continous, boolean signed) {
this.continous=continous;
this.signed=signed;
@ -260,28 +261,30 @@ public class SignalPainter extends TrackPainter {
maxVal=minVal;
for (EventEntry tp : ievents)
for(IEvent e: tp.events) {
long v = signed?((BitVector)e).toSignedValue():((BitVector)e).toUnsignedValue();
maxVal=Math.max(maxVal, v);
minVal=Math.min(minVal, v);
BigInteger v = signed?((BitVector)e).toSignedValue():((BitVector)e).toUnsignedValue();
maxVal=maxVal.max(v);
minVal=minVal.min(v);
}
if(maxVal==minVal) {
maxVal--;
minVal++;
maxVal=maxVal.subtract(BigInteger.ONE);
minVal=minVal.add(BigInteger.ONE);
}
} else {
minVal--;
maxVal=minVal+2;
minVal=minVal.subtract(BigInteger.ONE);
maxVal=minVal.multiply(BigInteger.valueOf(2));
}
}
public void draw(Projection proj, Rectangle area, IEvent left, IEvent right, int xBegin, int xEnd, boolean multiple) {
long leftVal = signed?((BitVector)left).toSignedValue():((BitVector)left).toUnsignedValue();
long rightVal= signed?((BitVector)right).toSignedValue():((BitVector)right).toUnsignedValue();
BigInteger leftVal = signed?((BitVector)left).toSignedValue():((BitVector)left).toUnsignedValue();
BigInteger rightVal= signed?((BitVector)right).toSignedValue():((BitVector)right).toUnsignedValue();
proj.setForeground(waveCanvas.styleProvider.getColor(WaveformColors.SIGNAL_REAL));
long range = maxVal-minVal;
int yOffsetLeft = (int) ((leftVal-minVal) * yRange / range);
int yOffsetRight = (int) ((rightVal-minVal) * yRange / range);
BigInteger range = maxVal.subtract(minVal);
// ((leftVal-minVal) * yRange / range);
int yOffsetLeft = leftVal.subtract(minVal).multiply(BigInteger.valueOf(yRange)).divide(range).intValue();
// ((rightVal-minVal) * yRange / range);
int yOffsetRight = rightVal.subtract(minVal).multiply(BigInteger.valueOf(yRange)).divide(range).intValue();
if(continous) {
if (xEnd > maxPosX) {
proj.drawLine(xBegin, yOffsetB-yOffsetLeft, maxPosX, yOffsetB-yOffsetRight);

View File

@ -662,10 +662,10 @@ public class WaveformView implements IWaveformView {
else {
switch (entry.valueDisplay) {
case SIGNED:
entry.currentValue = Long.toString(bv.toSignedValue());
entry.currentValue = bv.toSignedValue().toString();
break;
case UNSIGNED:
entry.currentValue = Long.toString(bv.toUnsignedValue());
entry.currentValue = bv.toUnsignedValue().toString();
break;
case BINARY:
entry.currentValue=bv.toString();

View File

@ -10,6 +10,8 @@
*******************************************************************************/
package com.minres.scviewer.database;
import java.math.BigInteger;
/**
* The Class BitVector.
*/
@ -174,8 +176,8 @@ public class BitVector implements IEvent {
*
* @return the long
*/
public long toUnsignedValue() {
long res = 0;
public BigInteger toUnsignedValue() {
BigInteger res = BigInteger.ZERO;
int bitOffset = 0;
int wordOffset = 0;
int currentWord = 0;
@ -185,11 +187,11 @@ public class BitVector implements IEvent {
currentWord = packedValues[wordOffset];
switch (currentWord & 3) {
case 1:
res |= 1 << i;
res=res.add(BigInteger.ONE.shiftLeft(i));
break;
case 2:
case 3:
return 0;
return BigInteger.ZERO;
default:
break;
}
@ -209,38 +211,14 @@ public class BitVector implements IEvent {
*
* @return the long
*/
public long toSignedValue() {
long res = 0;
int bitOffset = 0;
int wordOffset = 0;
int currentWord = 0;
int lastVal = 0;
// Copy values out of packed array
for (int i = 0; i < width; i++) {
if (bitOffset == 0)
currentWord = packedValues[wordOffset];
lastVal = 0;
switch (currentWord & 3) {
case 1:
res |= 1 << i;
lastVal = 1;
break;
case 2:
case 3:
return 0;
default:
}
bitOffset += 2;
if (bitOffset == 32) {
wordOffset++;
bitOffset = 0;
} else {
currentWord >>= 2;
}
public BigInteger toSignedValue() {
BigInteger res = toUnsignedValue();
BigInteger pos_max = BigInteger.ONE.shiftLeft(width-1);
if(res.compareTo(pos_max)<0)
return res;
else {
return res.subtract(BigInteger.ONE.shiftLeft(width));
}
if (lastVal != 0)
res |= -1l << width;
return res;
}
/**