Skip to content

Commit 0a90cdd

Browse files
committed
Recognize lossless unsigned integer to floating pointer conversion.
1 parent e35ed3a commit 0a90cdd

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/nodes/calc/FloatConvertNode.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,16 @@ public boolean isLossless() {
9999
case I2F:
100100
case L2D:
101101
case L2F:
102-
if (value.stamp(NodeView.DEFAULT) instanceof IntegerStamp) {
103-
return isLosslessIntegerToFloatingPoint((IntegerStamp) value.stamp(NodeView.DEFAULT), (FloatStamp) stamp(NodeView.DEFAULT));
102+
if (value.stamp(NodeView.DEFAULT) instanceof IntegerStamp inputStamp) {
103+
return isLosslessIntegerToFloatingPoint(inputStamp, (FloatStamp) stamp(NodeView.DEFAULT), false);
104+
} else {
105+
return false;
106+
}
107+
case UI2F:
108+
case UL2D:
109+
case UL2F:
110+
if (value.stamp(NodeView.DEFAULT) instanceof IntegerStamp inputStamp) {
111+
return isLosslessIntegerToFloatingPoint(inputStamp, (FloatStamp) stamp(NodeView.DEFAULT), true);
104112
} else {
105113
return false;
106114
}
@@ -119,7 +127,7 @@ public boolean canOverflow() {
119127
return ArithmeticOpTable.forStamp(inputStamp).getFloatConvert(op).canOverflowInteger(inputStamp);
120128
}
121129

122-
private static boolean isLosslessIntegerToFloatingPoint(IntegerStamp inputStamp, FloatStamp resultStamp) {
130+
private static boolean isLosslessIntegerToFloatingPoint(IntegerStamp inputStamp, FloatStamp resultStamp, boolean unsigned) {
123131
int mantissaBits;
124132
switch (resultStamp.getBits()) {
125133
case 32:
@@ -133,7 +141,11 @@ private static boolean isLosslessIntegerToFloatingPoint(IntegerStamp inputStamp,
133141
}
134142
long max = 1L << mantissaBits;
135143
long min = -(1L << mantissaBits);
136-
return min <= inputStamp.lowerBound() && inputStamp.upperBound() <= max;
144+
if (unsigned) {
145+
return Long.compareUnsigned(inputStamp.unsignedUpperBound(), max) <= 0;
146+
} else {
147+
return min <= inputStamp.lowerBound() && inputStamp.upperBound() <= max;
148+
}
137149
}
138150

139151
@Override

0 commit comments

Comments
 (0)