JDK-8308856 : jdk.internal.classfile.impl.EntryMap::nextPowerOfTwo math problem
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang.classfile
  • Affected Version: 21
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • Submitted: 2023-05-25
  • Updated: 2024-01-25
  • Resolved: 2023-06-01
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 21
21 b26Fixed
Related Reports
Relates :  
Description
I noticed the math problem in jdk.internal.classfile.impl.EntryMap::nextPowerOfTwo:

% jshell
|  Welcome to JShell -- Version 19.0.2
|  For an introduction type: /help intro

jshell>     public static long nextPowerOfTwo( long x ) {
   ...>         x = -1 >>> Long.numberOfLeadingZeros(x - 1);
   ...>         return x + 1;
   ...>     }
|  created method nextPowerOfTwo(long)

jshell> nextPowerOfTwo(0)
$2 ==> 0

jshell> nextPowerOfTwo(1)
$3 ==> 0

jshell> nextPowerOfTwo(2)
$4 ==> 2

jshell> nextPowerOfTwo(3)
$5 ==> 4

Note how "1" yields "0" as the next power of two.

Also check if you really need "next" power of two, which should give next(2) = 4. Maybe this is just "round"?
Comments
Changeset: a6109bf1 Author: Adam Sotona <asotona@openjdk.org> Date: 2023-06-01 11:50:20 +0000 URL: https://git.openjdk.org/jdk/commit/a6109bf1ea2acbebd6a3517813c0b82fdba00c2f
01-06-2023

I've provided patch returning correct zero power of two. However the returned values 0 or 1 have no impact on the EntryMap functionality. Calculation starting from the given number (inclusive) is common practise (see for example BitSet::nextSetBit).
25-05-2023

A pull request was submitted for review. URL: https://git.openjdk.org/jdk/pull/14148 Date: 2023-05-25 12:24:59 +0000
25-05-2023