[jdk21u-dev] RFR: 8349637: Integer.numberOfLeadingZeros outputs incorrectly in certain cases [v2]

Paul Hohensee phh at openjdk.org
Thu Apr 10 10:19:10 UTC 2025


On Thu, 3 Apr 2025 21:25:50 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.
>
> Paul Hohensee has updated the pull request with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains two additional commits since the last revision:
> 
>  - Merge branch 'master' into 8349637
>  - Backport 3657e92ead1e678942fcb272e77c3867eb5aa13e

Thanks you, Goetz, fixed.

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

PR Comment: https://git.openjdk.org/jdk21u-dev/pull/1584#issuecomment-2792259624


More information about the jdk-updates-dev mailing list