changeset 49426:7f3986bad197

8199843: Optimize Integer/Long.highestOneBit() Reviewed-by: redestad, plevart
author igerasim
date Mon, 26 Mar 2018 17:30:14 -0700
parents e9cb414f94eb
children d2fd2581325b
files src/java.base/share/classes/java/lang/Integer.java src/java.base/share/classes/java/lang/Long.java
diffstat 2 files changed, 2 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- 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));
     }
 
     /**