/* tested with java 12-ea 2019-03-19 Java(TM) SE Runtime Environment 19.3 (build 12-ea+5) Java HotSpot(TM) 64-Bit Server VM 19.3 (build 12-ea+5, mixed mode) $java -client -jar target/benchmarks.jar -i 6 -wi 6 -f 2 -tu us -w 1 -r 1 Benchmark (arg) Mode Cnt Score Error Units MyBenchmark.test00_org 0 thrpt 12 276.550 ± 23.602 ops/us MyBenchmark.test00_org 3062447746611937280 thrpt 12 190.267 ± 8.663 ops/us MyBenchmark.test00_org 3074515986238406656 thrpt 12 193.637 ± 12.115 ops/us MyBenchmark.test00_org 3074457344191496192 thrpt 12 208.619 ± 8.106 ops/us MyBenchmark.test01 0 thrpt 12 245.539 ± 20.515 ops/us MyBenchmark.test01 3062447746611937280 thrpt 12 238.965 ± 9.597 ops/us MyBenchmark.test01 3074515986238406656 thrpt 12 238.982 ± 6.689 ops/us MyBenchmark.test01 3074457344191496192 thrpt 12 245.606 ± 12.411 ops/us MyBenchmark.test02 0 thrpt 12 296.420 ± 10.770 ops/us MyBenchmark.test02 3062447746611937280 thrpt 12 219.834 ± 5.916 ops/us MyBenchmark.test02 3074515986238406656 thrpt 12 218.644 ± 3.561 ops/us MyBenchmark.test02 3074457344191496192 thrpt 12 203.284 ± 9.313 ops/us MyBenchmark.test03 0 thrpt 12 253.633 ± 10.008 ops/us MyBenchmark.test03 3062447746611937280 thrpt 12 237.982 ± 9.450 ops/us MyBenchmark.test03 3074515986238406656 thrpt 12 226.336 ± 9.281 ops/us MyBenchmark.test03 3074457344191496192 thrpt 12 245.213 ± 6.975 ops/us MyBenchmark.test04 0 thrpt 12 163.577 ± 5.116 ops/us MyBenchmark.test04 3062447746611937280 thrpt 12 163.459 ± 5.256 ops/us MyBenchmark.test04 3074515986238406656 thrpt 12 162.688 ± 7.052 ops/us MyBenchmark.test04 3074457344191496192 thrpt 12 161.502 ± 7.353 ops/us MyBenchmark.test05 0 thrpt 12 305.378 ± 24.901 ops/us MyBenchmark.test05 3062447746611937280 thrpt 12 224.753 ± 9.102 ops/us MyBenchmark.test05 3074515986238406656 thrpt 12 212.854 ± 10.267 ops/us MyBenchmark.test05 3074457344191496192 thrpt 12 220.351 ± 10.847 ops/us MyBenchmark.test06 0 thrpt 12 256.497 ± 12.564 ops/us MyBenchmark.test06 3062447746611937280 thrpt 12 224.505 ± 10.529 ops/us MyBenchmark.test06 3074515986238406656 thrpt 12 236.096 ± 11.764 ops/us MyBenchmark.test06 3074457344191496192 thrpt 12 226.279 ± 7.253 ops/us MyBenchmark.testBase 0 thrpt 12 322.541 ± 10.884 ops/us MyBenchmark.testBase 3062447746611937280 thrpt 12 329.572 ± 5.924 ops/us MyBenchmark.testBase 3074515986238406656 thrpt 12 321.705 ± 16.060 ops/us MyBenchmark.testBase 3074457344191496192 thrpt 12 328.094 ± 8.795 ops/us $java -server -jar target/benchmarks.jar -i 6 -wi 6 -f 2 -tu us -w 1 -r 1 Benchmark (arg) Mode Cnt Score Error Units MyBenchmark.test00_org 0 thrpt 12 289.116 ± 11.663 ops/us MyBenchmark.test00_org 3062447746611937280 thrpt 12 196.190 ± 15.895 ops/us MyBenchmark.test00_org 3074515986238406656 thrpt 12 195.946 ± 9.745 ops/us MyBenchmark.test00_org 3074457344191496192 thrpt 12 210.976 ± 11.918 ops/us MyBenchmark.test01 0 thrpt 12 251.568 ± 9.509 ops/us MyBenchmark.test01 3062447746611937280 thrpt 12 240.513 ± 4.984 ops/us MyBenchmark.test01 3074515986238406656 thrpt 12 241.396 ± 11.397 ops/us MyBenchmark.test01 3074457344191496192 thrpt 12 246.844 ± 8.394 ops/us MyBenchmark.test02 0 thrpt 12 316.356 ± 6.912 ops/us MyBenchmark.test02 3062447746611937280 thrpt 12 217.359 ± 4.947 ops/us MyBenchmark.test02 3074515986238406656 thrpt 12 211.059 ± 7.756 ops/us MyBenchmark.test02 3074457344191496192 thrpt 12 165.144 ± 47.292 ops/us MyBenchmark.test03 0 thrpt 12 245.772 ± 15.881 ops/us MyBenchmark.test03 3062447746611937280 thrpt 12 227.928 ± 8.851 ops/us MyBenchmark.test03 3074515986238406656 thrpt 12 226.082 ± 20.405 ops/us MyBenchmark.test03 3074457344191496192 thrpt 12 208.792 ± 61.142 ops/us MyBenchmark.test04 0 thrpt 12 107.294 ± 59.730 ops/us MyBenchmark.test04 3062447746611937280 thrpt 12 163.844 ± 7.082 ops/us MyBenchmark.test04 3074515986238406656 thrpt 12 160.768 ± 9.231 ops/us MyBenchmark.test04 3074457344191496192 thrpt 12 165.041 ± 6.382 ops/us MyBenchmark.test05 0 thrpt 12 315.000 ± 10.061 ops/us MyBenchmark.test05 3062447746611937280 thrpt 12 218.748 ± 9.142 ops/us MyBenchmark.test05 3074515986238406656 thrpt 12 215.687 ± 13.104 ops/us MyBenchmark.test05 3074457344191496192 thrpt 12 218.930 ± 8.655 ops/us MyBenchmark.test06 0 thrpt 12 257.878 ± 13.808 ops/us MyBenchmark.test06 3062447746611937280 thrpt 12 216.691 ± 16.181 ops/us MyBenchmark.test06 3074515986238406656 thrpt 12 235.911 ± 7.639 ops/us MyBenchmark.test06 3074457344191496192 thrpt 12 224.535 ± 7.030 ops/us MyBenchmark.testBase 0 thrpt 12 332.631 ± 9.095 ops/us MyBenchmark.testBase 3062447746611937280 thrpt 12 333.880 ± 10.564 ops/us MyBenchmark.testBase 3074515986238406656 thrpt 12 327.763 ± 13.561 ops/us MyBenchmark.testBase 3074457344191496192 thrpt 12 331.448 ± 13.382 ops/us */ /* * Copyright (c) 2014, Oracle America, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * * Neither the name of Oracle nor the names of its contributors may be used * to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ package org.sample; import org.openjdk.jmh.annotations.*; @State(Scope.Benchmark) public class MyBenchmark { @Param({"0", "3062447746611937280", "3074515986238406656", "3074457344191496192"}) public long arg; // @Benchmark // public long testBase() { // return (int)arg; // } // @Benchmark // public int test00_org() { // return numberOfTrailingZeros_00_org(arg); // } // @Benchmark // public int test01() { // return numberOfTrailingZeros_01(arg); // } // @Benchmark // public int test02() { // return numberOfTrailingZeros_02(arg); // } @Benchmark public int test03() { return numberOfTrailingZeros_03(arg); } // @Benchmark // public int test04() { // return numberOfTrailingZeros_04(arg); // } // @Benchmark // public int test05() { // return numberOfTrailingZeros_05(arg); // } @Benchmark public int test06() { return numberOfTrailingZeros_06(arg); } static int numberOfTrailingZeros_00_org(long i) { // HD, Figure 5-14 int x, y; if (i == 0) return 64; int n = 63; y = (int)i; if (y != 0) { n = n -32; x = y; } else x = (int)(i>>>32); y = x <<16; if (y != 0) { n = n -16; x = y; } y = x << 8; if (y != 0) { n = n - 8; x = y; } y = x << 4; if (y != 0) { n = n - 4; x = y; } y = x << 2; if (y != 0) { n = n - 2; x = y; } return n - ((x << 1) >>> 31); } static int numberOfTrailingZeros_01(long i) { i &= -i; // lowest one bit if (i <= 0) return i == 0 ? 64 : 63; int n = -1; if (i >= 1L << 32) { n += 32; i >>>= 32; } if (i >= 1L << 16) { n += 16; i >>>= 16; } if (i >= 1L << 8) { n += 8; i >>>= 8; } if (i >= 1L << 4) { n += 4; i >>>= 4; } if (i >= 1L << 2) { n += 2; i >>>= 2; } return n + (int)i; } static int numberOfTrailingZeros_02(long i) { if (i == 0) return 64; return 63 - numberOfLeadingZeros(i & -i); } static int numberOfTrailingZeros_03(long i) { int x = (int)i; return x == 0 ? 32 + Integer_numberOfTrailingZeros((int)(i >>> 32)) : Integer_numberOfTrailingZeros(x); } static int numberOfTrailingZeros_04(long i) { return bitCount(~i & (i - 1)); } static int numberOfTrailingZeros_05(long i) { if (i == 0L) return 64; int x = (int)i; if (x == 0) { x = (int)(i >>> 32); return 63 - Integer_numberOfLeadingZeros(x & -x); } else { return 31 - Integer_numberOfLeadingZeros(x & -x); } } static int numberOfTrailingZeros_06(long i) { return 64 - numberOfLeadingZeros(~i & (i - 1)); } static int Integer_numberOfLeadingZeros(int i) { // HD, Count leading 0's if (i <= 0) return i == 0 ? 32 : 0; 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); } static int Integer_numberOfTrailingZeros(int i) { return 32 - Integer_numberOfLeadingZeros(~i & (i - 1)); } static int numberOfLeadingZeros(long i) { int x = (int)(i >>> 32); return x == 0 ? 32 + Integer_numberOfLeadingZeros((int)i) : Integer_numberOfLeadingZeros(x); } static int bitCount(long i) { // HD, Figure 5-2 i = i - ((i >>> 1) & 0x5555555555555555L); i = (i & 0x3333333333333333L) + ((i >>> 2) & 0x3333333333333333L); i = (i + (i >>> 4)) & 0x0f0f0f0f0f0f0f0fL; i = i + (i >>> 8); i = i + (i >>> 16); i = i + (i >>> 32); return (int)i & 0x7f; } }