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

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


> 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 incrementally with one additional commit since the last revision:

  8349637: Revert Arm copyright change

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

Changes:
  - all: https://git.openjdk.org/jdk21u-dev/pull/1584/files
  - new: https://git.openjdk.org/jdk21u-dev/pull/1584/files/99d08773..2ffff1d9

Webrevs:
 - full: https://webrevs.openjdk.org/?repo=jdk21u-dev&pr=1584&range=02
 - incr: https://webrevs.openjdk.org/?repo=jdk21u-dev&pr=1584&range=01-02

  Stats: 1 line in 1 file changed: 0 ins; 0 del; 1 mod
  Patch: https://git.openjdk.org/jdk21u-dev/pull/1584.diff
  Fetch: git fetch https://git.openjdk.org/jdk21u-dev.git pull/1584/head:pull/1584

PR: https://git.openjdk.org/jdk21u-dev/pull/1584


More information about the jdk-updates-dev mailing list