changeset 49682:3c0a12972165

8189230: JDK method:java.lang.Integer.numberOfLeadingZeros(int) can be optimized Summary: Directly return zero for a negative parameter instead of calculating Reviewed-by: psandoz
author bpb
date Thu, 15 Mar 2018 08:11:01 -0700
parents 7443b946694a
children 6628683fde28
files src/java.base/share/classes/java/lang/Integer.java src/java.base/share/classes/java/lang/Long.java
diffstat 2 files changed, 6 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/lang/Integer.java	Thu Mar 15 12:07:59 2018 +0100
+++ b/src/java.base/share/classes/java/lang/Integer.java	Thu Mar 15 08:11:01 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1994, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1994, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -1625,8 +1625,8 @@
     @HotSpotIntrinsicCandidate
     public static int numberOfLeadingZeros(int i) {
         // HD, Figure 5-6
-        if (i == 0)
-            return 32;
+        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; }
--- a/src/java.base/share/classes/java/lang/Long.java	Thu Mar 15 12:07:59 2018 +0100
+++ b/src/java.base/share/classes/java/lang/Long.java	Thu Mar 15 08:11:01 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1994, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1994, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -1771,8 +1771,8 @@
     @HotSpotIntrinsicCandidate
     public static int numberOfLeadingZeros(long i) {
         // HD, Figure 5-6
-         if (i == 0)
-            return 64;
+         if (i <= 0)
+            return i == 0 ? 64 : 0;
         int n = 1;
         int x = (int)(i >>> 32);
         if (x == 0) { n += 32; x = (int)i; }