RFR: JDK-8289551: Conversions between bit representations of half precision values and floats

John R Rose jrose at openjdk.org
Wed Jul 13 06:06:25 UTC 2022


On Fri, 8 Jul 2022 06:11:22 GMT, Joe Darcy <darcy at openjdk.org> wrote:

> Initial implementation.

src/java.base/share/classes/java/lang/Float.java line 1003:

> 1001:         float abs_f = Math.abs(f);
> 1002:         int doppel = Float.floatToRawIntBits(f);
> 1003:         int f_sign = 0x8000_0000 & doppel;

The code would be a bit less branchy if sign were computed like this:

short sign_bit = (short)((doppel >> 16) & 0x8000);

and get rid of `f_sign`; instead of conditionals in various places use bitwise or of `sign_bit`, such as:


if (abs_f > 65504.0f ) {
  return (short)(sign_bit | 0x7c00);  // Positive or negative infinity
}


I'm mainly looking forward to what the JIT produces, but I also think handling the sign in this way makes the code at least as easy to understand as the current branch-based exposition.

-------------

PR: https://git.openjdk.org/jdk/pull/9422


More information about the core-libs-dev mailing list