mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 23:04:50 +02:00
8203352: Improve java implementation of Integer/Long.numberOfLeadingZeros
Co-authored-by: Ivan Gerasimov <ivan.gerasimov@oracle.com> Reviewed-by: martin, igerasim
This commit is contained in:
parent
bff7296db3
commit
55ba3cad8b
4 changed files with 35 additions and 31 deletions
|
@ -1618,16 +1618,15 @@ public final class Integer extends Number implements Comparable<Integer> {
|
|||
*/
|
||||
@HotSpotIntrinsicCandidate
|
||||
public static int numberOfLeadingZeros(int i) {
|
||||
// HD, Figure 5-6
|
||||
// HD, Count leading 0's
|
||||
if (i <= 0)
|
||||
return i == 0 ? 32 : 0;
|
||||
int n = 1;
|
||||
if (i >>> 16 == 0) { n += 16; i <<= 16; }
|
||||
if (i >>> 24 == 0) { n += 8; i <<= 8; }
|
||||
if (i >>> 28 == 0) { n += 4; i <<= 4; }
|
||||
if (i >>> 30 == 0) { n += 2; i <<= 2; }
|
||||
n -= i >>> 31;
|
||||
return n;
|
||||
int n = 31;
|
||||
if (i >= 1 << 16) { n -= 16; i >>>= 16; }
|
||||
if (i >= 1 << 8) { n -= 8; i >>>= 8; }
|
||||
if (i >= 1 << 4) { n -= 4; i >>>= 4; }
|
||||
if (i >= 1 << 2) { n -= 2; i >>>= 2; }
|
||||
return n - (i >>> 1);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1761,18 +1761,9 @@ public final class Long extends Number implements Comparable<Long> {
|
|||
*/
|
||||
@HotSpotIntrinsicCandidate
|
||||
public static int numberOfLeadingZeros(long i) {
|
||||
// HD, Figure 5-6
|
||||
if (i <= 0)
|
||||
return i == 0 ? 64 : 0;
|
||||
int n = 1;
|
||||
int x = (int)(i >>> 32);
|
||||
if (x == 0) { n += 32; x = (int)i; }
|
||||
if (x >>> 16 == 0) { n += 16; x <<= 16; }
|
||||
if (x >>> 24 == 0) { n += 8; x <<= 8; }
|
||||
if (x >>> 28 == 0) { n += 4; x <<= 4; }
|
||||
if (x >>> 30 == 0) { n += 2; x <<= 2; }
|
||||
n -= x >>> 31;
|
||||
return n;
|
||||
return x == 0 ? 32 + Integer.numberOfLeadingZeros((int)i)
|
||||
: Integer.numberOfLeadingZeros(x);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue