RFR: 6506405: Math.abs(float) is slow [v7]

Joe Darcy darcy at openjdk.java.net
Fri Jul 9 22:12:53 UTC 2021


On Fri, 9 Jul 2021 21:30:22 GMT, Brian Burkhalter <bpb at openjdk.org> wrote:

>> Please consider this change to make the `float` and `double` versions of `java.lang.Math.abs()` branch-free.
>
> Brian Burkhalter has refreshed the contents of this pull request, and previous commits have been removed. The incremental views will show differences compared to the previous content of the PR.

src/java.base/share/classes/jdk/internal/math/DoubleConsts.java line 89:

> 87:                 ((SIGN_BIT_MASK & SIGNIF_BIT_MASK) == 0L) &&
> 88:                 ((EXP_BIT_MASK & SIGNIF_BIT_MASK) == 0L)) &&
> 89:                 ((SIGN_BIT_MASK | MAG_BIT_MASK) == ~0));

Nit: please use "~0L" instead so the long-ness of 0 is explicit.

test/jdk/java/lang/Math/AbsTests.java line 202:

> 200:             return Float.floatToRawIntBits(result) !=
> 201:                 Float.floatToRawIntBits(f) ? 1 : 0;
> 202:         } else if ((f >= 0 && result != f) || (f < 0 && result != -f)) {

Please look at the Tests.java file in the jdk/test/java/lang/Math directory. One of the methods like

    public static int test(String testName, double input,
                           double result, double expected

will be helpful here; e.g.

Tests.test("Math.abs", f, Math.abs(-f), f)

I would also add combination for StrictMath.abs and negations:

Tests.test("Math.abs", f, Math.abs(f), f)
Tests.test("StrictMath.abs", -f, Math.abs(f), f)
Tests.test("StrictMath.abs", f, Math.abs(f), f)

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

PR: https://git.openjdk.java.net/jdk/pull/4711


More information about the core-libs-dev mailing list