8199843: Optimize Integer/Long.highestOneBit()
authorigerasim
Mon, 26 Mar 2018 17:30:14 -0700
changeset 49426 7f3986bad197
parent 49425 e9cb414f94eb
child 49427 d2fd2581325b
8199843: Optimize Integer/Long.highestOneBit() Reviewed-by: redestad, plevart
src/java.base/share/classes/java/lang/Integer.java
src/java.base/share/classes/java/lang/Long.java
--- a/src/java.base/share/classes/java/lang/Integer.java	Mon Mar 26 16:24:16 2018 -0700
+++ b/src/java.base/share/classes/java/lang/Integer.java	Mon Mar 26 17:30:14 2018 -0700
@@ -1574,13 +1574,7 @@
      * @since 1.5
      */
     public static int highestOneBit(int i) {
-        // HD, Figure 3-1
-        i |= (i >>  1);
-        i |= (i >>  2);
-        i |= (i >>  4);
-        i |= (i >>  8);
-        i |= (i >> 16);
-        return i - (i >>> 1);
+        return i & (MIN_VALUE >>> numberOfLeadingZeros(i));
     }
 
     /**
--- a/src/java.base/share/classes/java/lang/Long.java	Mon Mar 26 16:24:16 2018 -0700
+++ b/src/java.base/share/classes/java/lang/Long.java	Mon Mar 26 17:30:14 2018 -0700
@@ -1719,14 +1719,7 @@
      * @since 1.5
      */
     public static long highestOneBit(long i) {
-        // HD, Figure 3-1
-        i |= (i >>  1);
-        i |= (i >>  2);
-        i |= (i >>  4);
-        i |= (i >>  8);
-        i |= (i >> 16);
-        i |= (i >> 32);
-        return i - (i >>> 1);
+        return i & (MIN_VALUE >>> numberOfLeadingZeros(i));
     }
 
     /**