@@ -236,4 +236,40 @@ public static int truncateToUnsignedInt(float x) {
236
236
return (signedResult & ~(signedResult >> 31 )); // max(result, 0)
237
237
}
238
238
}
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
+ }
239
275
}
0 commit comments