RFR: 6506405: Math.abs(float) is slow

Joe Darcy joe.darcy at oracle.com
Thu Jul 8 00:50:21 UTC 2021


On 7/7/2021 5:43 PM, Brian Burkhalter wrote:
> On Thu, 8 Jul 2021 00:38:45 GMT, Joe Darcy <darcy at openjdk.org> wrote:
>
>>> Please consider this change to make the `float` and `double` versions of `java.lang.Math.abs()` branch-free.
>> src/java.base/share/classes/java/lang/Math.java line 1530:
>>
>>> 1528:     @IntrinsicCandidate
>>> 1529:     public static float abs(float a) {
>>> 1530:         return Float.intBitsToFloat(Float.floatToRawIntBits(a) & 0x7fffffff);
>> I'd like to see a brief comment on these methods along the lines of "zero out sign bit in the representation." Using underscores in the literal to better group the digits or using the constants in jdk.internal.math.DoubleConsts.
> There is no specific constant in `{Float,Double}Consts` to mask all but the sign bit, but I had thought it might be good to add one there


The sign bit mask can be bit-wise complemented though :-)

-Joe



More information about the core-libs-dev mailing list