JDK-8234331 : Add robust and optimized utility for rounding up to next power of two
  • Type: Enhancement
  • Component: hotspot
  • Sub-Component: compiler
  • Affected Version: 14
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2019-11-18
  • Updated: 2020-02-11
  • Resolved: 2019-12-06
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 14
14 b27Fixed
Related Reports
Relates :  
Relates :  
Description
In various places we have code that does the equivalent of

do_something(uint x) {
  uint next_pow2 = 1;
  while (next_pow2 < x)  {
    next_pow2 <<= 1;
  }
  ...
}

This is brittle (would loop forever if x > (1 << 31)) and also somewhat inefficient. Adding a utility that does the equivalent of 

if (x == 0) {
  next_pow2 = 1;
} else if (x < (1 << 31)) {
  next_pow2 = 1U << (32 - count_leading_zeros(x));
} else {
  next_pow2 = uint_max; // might need to be able to allow different behaviors here 

might avoid infinite loops at the extreme (although it's debatable what should be returned when x > 2^31) and potentially be more efficient. 
Comments
URL: https://hg.openjdk.java.net/jdk/jdk/rev/0edc7fd0d7a3 User: redestad Date: 2019-12-06 17:24:28 +0000
06-12-2019