[jdk21u-dev] Integrated: 8349637: Integer.numberOfLeadingZeros outputs incorrectly in certain cases
Paul Hohensee
phh at openjdk.org
Mon Apr 14 15:06:16 UTC 2025
On Thu, 3 Apr 2025 13:37:04 GMT, Paul Hohensee <phh at openjdk.org> wrote:
> Backport for parity with Oracle 21.0.8. 24u fix request applies:
>
> Fixes an incorrect result of Integer.numberOfLeadingZeros. Fix is low risk (two lines) and there is a workaround (disable the intrinsic). Applies cleanly and was tested in JDK 25 for several days in the CI.
>
> More detail:
>
> Clean except for copyright in TestNumberOfContinuousZeros.java. Passes tier2 and the modified test. Low risk: algorithmically, it's zero risk, vis
>
> The algorithm works by converting the argument to a double and using the resulting exponent as the basis for the number of leading zeros in the argument: the exponent reflects the number of leading zeros because the fraction equals the argument shifted left until the highest set bit is implied by the fraction. The bug was that if the 2nd-to-highest bit is also set, the exponent may be off by one due to rounding up. The fix is to unconditionally clear the 2nd-to-highest argument bit by andn'ing the argument-shifted-right-by-1 with itself. andn is 1 iff the left is 0 and the right is 1, so the result is guaranteed to keep the high bit set because a zero is shifted into that position in the argument-shifted-right-by-1. It's also guaranteed to clear the 2nd-to-highest bit because the highest set bit in the argument-shifted-left-by-1 will be the left argument of the andn, so guaranteed to clear the 2nd-to-highest bit since the result of andn is always zero if its left argument is 1.
This pull request has now been integrated.
Changeset: 2439c6d5
Author: Paul Hohensee <phh at openjdk.org>
URL: https://git.openjdk.org/jdk21u-dev/commit/2439c6d5aaff7d9ee2a5be4c7caa5f9ccd65b748
Stats: 222 lines in 3 files changed: 215 ins; 0 del; 7 mod
8349637: Integer.numberOfLeadingZeros outputs incorrectly in certain cases
Reviewed-by: rkennke
Backport-of: 3657e92ead1e678942fcb272e77c3867eb5aa13e
-------------
PR: https://git.openjdk.org/jdk21u-dev/pull/1584
More information about the jdk-updates-dev
mailing list