Skip to content

Commit 1a85a7d

Browse files
committed
Add unsigned integer to float conversion methods.
1 parent 3b19b52 commit 1a85a7d

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

truffle/src/com.oracle.truffle.api/snapshot.sigtest

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ supr java.lang.Object
122122

123123
CLSS public final com.oracle.truffle.api.ExactMath
124124
meth public static double truncate(double)
125+
meth public static double unsignedToDouble(long)
125126
meth public static float truncate(float)
127+
meth public static float unsignedToFloat(long)
126128
meth public static int multiplyHigh(int,int)
127129
meth public static int multiplyHighUnsigned(int,int)
128130
meth public static int truncateToUnsignedInt(double)

truffle/src/com.oracle.truffle.api/src/com/oracle/truffle/api/ExactMath.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,40 @@ public static int truncateToUnsignedInt(float x) {
236236
return (signedResult & ~(signedResult >> 31)); // max(result, 0)
237237
}
238238
}
239+
240+
/**
241+
* Converts the given unsigned {@code long} to the closest {@code double} value.
242+
*
243+
* @param x unsigned integer input, wrapped in a signed integer
244+
* @return the {@code double} result
245+
* @since 25.0
246+
*/
247+
public static double unsignedToDouble(long x) {
248+
if (CompilerDirectives.injectBranchProbability(CompilerDirectives.LIKELY_PROBABILITY, x >= 0)) {
249+
return x;
250+
} else {
251+
// unsigned ceil div by 2, convert to double, and multiply by 2.
252+
// the lsb is needed to ensure correct rounding to nearest even.
253+
double halfRoundUp = ((x >>> 1) | (x & 1));
254+
return halfRoundUp + halfRoundUp;
255+
}
256+
}
257+
258+
/**
259+
* Converts the given unsigned {@code long} to the closest {@code float} value.
260+
*
261+
* @param x unsigned integer input, wrapped in a signed integer
262+
* @return the {@code float} result
263+
* @since 25.0
264+
*/
265+
public static float unsignedToFloat(long x) {
266+
if (CompilerDirectives.injectBranchProbability(CompilerDirectives.LIKELY_PROBABILITY, x >= 0)) {
267+
return x;
268+
} else {
269+
// unsigned ceil div by 2, convert to float, and multiply by 2.
270+
// the lsb is needed to ensure correct rounding to nearest even.
271+
float halfRoundUp = ((x >>> 1) | (x & 1));
272+
return halfRoundUp + halfRoundUp;
273+
}
274+
}
239275
}

0 commit comments

Comments
 (0)