Skip to content

Commit

Permalink
[GR-48705] Infer input msb during stamp inversion for integer SignExt…
Browse files Browse the repository at this point in the history
…end.
  • Loading branch information
rmosaner committed Oct 6, 2023
1 parent 1ec8ea4 commit 9be87d0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ public void invertIntegerSignExtend04() {
assertTrue("Stamp cannot be inverted and should be empty!", invertSignExtend(stamp).isEmpty());
}

@Test
public void invertIntegerSignExtend05() {
// 32 -> 8bit: xx...x0 xxxxxxxx -> 0xxxxxxx (msb has to be 0)
IntegerStamp stamp = IntegerStamp.stampForMask(32, 0, CodeUtil.mask(32) ^ 256);
Stamp expected = IntegerStamp.stampForMask(8, 0, CodeUtil.mask(7));
assertEquals(expected, invertSignExtend(stamp));
}

@Test
public void invertIntegerSignExtend06() {
// 32 -> 8bit: xx...x1 xxxxxxxx -> 1xxxxxxx (msb has to be 1)
IntegerStamp stamp = IntegerStamp.stampForMask(32, 256, CodeUtil.mask(32));
Stamp expected = IntegerStamp.stampForMask(8, 128, CodeUtil.mask(8));
assertEquals(expected, invertSignExtend(stamp));
}

private static Stamp invertZeroExtend(Stamp toInvert) {
IntegerConvertOp<ZeroExtend> signExtend = ArithmeticOpTable.forStamp(toInvert).getZeroExtend();
return signExtend.invertStamp(8, 32, toInvert);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2092,11 +2092,40 @@ public Stamp invertStamp(int inputBits, int resultBits, Stamp outStamp) {
return createEmptyStamp(inputBits);
}

/*
* Calculate bounds and mayBeSet/mustBeSet bits for the input based on
* bit width and potentially inferred msb.
*/
long inputMask = CodeUtil.mask(inputBits);
long inputUpperBound = maxValueForMasks(inputBits, stamp.mustBeSet() & inputMask, stamp.mayBeSet() & inputMask);
long inputLowerBound = minValueForMasks(inputBits, stamp.mustBeSet() & inputMask, stamp.mayBeSet() & inputMask);
long inputMustBeSet = stamp.mustBeSet() & inputMask;
long inputMayBeSet = stamp.mayBeSet() & inputMask;

return StampFactory.forIntegerWithMask(inputBits, inputLowerBound, inputUpperBound, stamp.mustBeSet() & inputMask, stamp.mayBeSet() & inputMask);
if (!inputMSBOne && !inputMSBZero) {
/*
* Input MSB yet unknown, try to infer it from the extension:
*
* @formatter:off
*
* xx0x xxxx implies that the extension is 0000 which implies that the MSB of the input is 0
* x1xx xxxx implies that the extension is 1111 which implies that the MSB of the input is 1
*
* @formatter:on
*/
if (zeroInExtension) {
long msbZeroMask = inputMask ^ (1 << (inputBits - 1));
inputMustBeSet &= msbZeroMask;
inputMayBeSet &= msbZeroMask;
} else if (oneInExtension) {
long msbOneMask = 1 << (inputBits - 1);
inputMustBeSet |= msbOneMask;
inputMayBeSet |= msbOneMask;
}
}

long inputUpperBound = maxValueForMasks(inputBits, inputMustBeSet, inputMayBeSet);
long inputLowerBound = minValueForMasks(inputBits, inputMustBeSet, inputMayBeSet);

return StampFactory.forIntegerWithMask(inputBits, inputLowerBound, inputUpperBound, inputMustBeSet, inputMayBeSet);
}
},

Expand Down

0 comments on commit 9be87d0

Please sign in to comment.