Given how trivial the changes are it looks ok, but not sure it really matters in practice.
— Note that it’s possible to selectively disable intrinsics. For future reference, when you want to avoid copying code if you happen to benchmark intrinsics in the future: -XX:+UnlockDiagnosticVMOptions -XX:DisableIntrinsic=_numberOfLeadingZeros_i But you have to know the intrinsic name, you can find ‘em here: http://hg.openjdk.java.net/jdk/jdk/file/63eceefeb347/src/hotspot/share/classfile/vmSymbols.hpp#l805 Paul. > On Mar 13, 2018, at 4:14 PM, Brian Burkhalter <brian.burkhal...@oracle.com> > wrote: > > https://bugs.openjdk.java.net/browse/JDK-8189230 > > The change included below improves the performance of > {Integer,Long}.numberOfLeadingZeros primarily for negative parameters by 20% > to 33% as measured by JMH benchmarks. For details please refer to the bug > report. Although on certain platforms there could be an intrinsic for the > methods in question, given the simplicity of the change it seems worth making. > > Thanks, > > Brian > > --- a/src/java.base/share/classes/java/lang/Integer.java > +++ b/src/java.base/share/classes/java/lang/Integer.java > @@ -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 > +++ b/src/java.base/share/classes/java/lang/Long.java > @@ -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; } >