/* 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 -XX:+TieredCompilation -XX:TieredStopAtLevel=1 \ -jar target/benchmarks.jar \ -i 5 -wi 4 -f 5 -tu us -w 3 -r 3 Benchmark (arg) Mode Cnt Score Error Units MyBenchmark.test01 0 thrpt 25 37.085 ± 0.111 ops/us MyBenchmark.test01 16 thrpt 25 34.760 ± 1.047 ops/us MyBenchmark.test01 42 thrpt 25 35.013 ± 0.128 ops/us MyBenchmark.test01 1431306240 thrpt 25 34.479 ± 0.037 ops/us MyBenchmark.test05 0 thrpt 25 42.286 ± 0.089 ops/us MyBenchmark.test05 16 thrpt 25 38.491 ± 0.048 ops/us MyBenchmark.test05 42 thrpt 25 38.430 ± 0.067 ops/us MyBenchmark.test05 1431306240 thrpt 25 38.917 ± 0.047 ops/us MyBenchmark.test09 0 thrpt 25 45.363 ± 2.933 ops/us MyBenchmark.test09 16 thrpt 25 43.956 ± 0.060 ops/us MyBenchmark.test09 42 thrpt 25 43.971 ± 0.057 ops/us MyBenchmark.test09 1431306240 thrpt 25 42.848 ± 0.070 ops/us $java -XX:-TieredCompilation -server \ -jar target/benchmarks.jar \ -i 5 -wi 4 -f 5 -tu us -w 3 -r 3 Benchmark (arg) Mode Cnt Score Error Units MyBenchmark.test01 0 thrpt 25 84.894 ± 0.766 ops/us MyBenchmark.test01 16 thrpt 25 75.238 ± 0.191 ops/us MyBenchmark.test01 42 thrpt 25 78.733 ± 0.104 ops/us MyBenchmark.test01 1431306240 thrpt 25 78.743 ± 0.108 ops/us MyBenchmark.test05 0 thrpt 25 84.816 ± 0.320 ops/us MyBenchmark.test05 16 thrpt 25 86.805 ± 0.151 ops/us MyBenchmark.test05 42 thrpt 25 85.521 ± 1.391 ops/us MyBenchmark.test05 1431306240 thrpt 25 82.829 ± 1.657 ops/us MyBenchmark.test09 0 thrpt 25 111.552 ± 3.180 ops/us MyBenchmark.test09 16 thrpt 25 99.524 ± 0.125 ops/us MyBenchmark.test09 42 thrpt 25 99.574 ± 0.189 ops/us MyBenchmark.test09 1431306240 thrpt 25 93.032 ± 2.437 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", "16", "42", "1431306240"}) public int arg; // @Benchmark // public int testBase() { // return arg; // } // @Benchmark // public int test00_org() { // return numberOfTrailingZeros_00_org(arg); // } @Benchmark public int test01() { return numberOfTrailingZeros_01(arg + 0) + numberOfTrailingZeros_01(arg + 1) + numberOfTrailingZeros_01(arg + 2) + numberOfTrailingZeros_01(arg + 3) + numberOfTrailingZeros_01(arg + 4) + numberOfTrailingZeros_01(arg + 5) + numberOfTrailingZeros_01(arg + 6) + numberOfTrailingZeros_01(arg + 7); } // @Benchmark // public int test01a() { // return // numberOfTrailingZeros_01a(arg + 0) + // numberOfTrailingZeros_01a(arg + 1) + // numberOfTrailingZeros_01a(arg + 2) + // numberOfTrailingZeros_01a(arg + 3) + // numberOfTrailingZeros_01a(arg + 4) + // numberOfTrailingZeros_01a(arg + 5) + // numberOfTrailingZeros_01a(arg + 6) + // numberOfTrailingZeros_01a(arg + 7); // } // @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 + 0) + numberOfTrailingZeros_05(arg + 1) + numberOfTrailingZeros_05(arg + 2) + numberOfTrailingZeros_05(arg + 3) + numberOfTrailingZeros_05(arg + 4) + numberOfTrailingZeros_05(arg + 5) + numberOfTrailingZeros_05(arg + 6) + numberOfTrailingZeros_05(arg + 7); } // @Benchmark // public int test06() { // return numberOfTrailingZeros_06(arg); // } // @Benchmark // public int test06a() { // return numberOfTrailingZeros_06a(arg); // } // @Benchmark // public int test07() { // return numberOfTrailingZeros_07(arg); // } // @Benchmark // public int test08() { // return numberOfTrailingZeros_08(arg); // } @Benchmark public int test09() { return numberOfTrailingZeros_09(arg + 0) + numberOfTrailingZeros_09(arg + 1) + numberOfTrailingZeros_09(arg + 2) + numberOfTrailingZeros_09(arg + 3) + numberOfTrailingZeros_09(arg + 4) + numberOfTrailingZeros_09(arg + 5) + numberOfTrailingZeros_09(arg + 6) + numberOfTrailingZeros_09(arg + 7); } static int numberOfTrailingZeros_00_org(int i) { // HD, Figure 5-14 int y; if (i == 0) return 32; int n = 31; y = i <<16; if (y != 0) { n = n -16; i = y; } y = i << 8; if (y != 0) { n = n - 8; i = y; } y = i << 4; if (y != 0) { n = n - 4; i = y; } y = i << 2; if (y != 0) { n = n - 2; i = y; } return n - ((i << 1) >>> 31); } static int numberOfTrailingZeros_01(int i) { i &= -i; // lowest one bit if (i <= 0) return i == 0 ? 32 : 31; int n = -1; 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; } static int numberOfTrailingZeros_01a(int i) { i &= -i; // lowest one bit if (i <= 0) return 32 + (i >> 31); int n = -1; 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; } static int numberOfTrailingZeros_02(int i) { if (i == 0) return 32; return 31 - numberOfLeadingZeros(i & -i); } static int numberOfTrailingZeros_03(int i) { return bitCount(~i & (i - 1)); } static int numberOfTrailingZeros_04(int i) { if (i == 0) return 32; return bitCount(i ^ (i - 1)) - 1; } static int numberOfTrailingZeros_05(int i) { return 32 - numberOfLeadingZeros(~i & (i - 1)); } static int numberOfTrailingZeros_06(int i) { if (i == 0) return 32; int n = 0; if ((i << 16) == 0) { n += 16; i >>>= 16; } if ((i & 0xFF) == 0) { n += 8; i >>>= 8; } if ((i & 0xF) == 0) { n += 4; i >>>= 4; } if ((i & 0x3) == 0) { n += 2; i >>>= 2; } return n + (~i & 1); } static int numberOfTrailingZeros_06a(int i) { if (i == 0) return 32; int n = 1; if ((i << 16) == 0) { n += 16; i >>>= 16; } if ((i & 0xFF) == 0) { n += 8; i >>>= 8; } if ((i & 0xF) == 0) { n += 4; i >>>= 4; } if ((i & 0x3) == 0) { n += 2; i >>>= 2; } return n - (i & 1); } static int numberOfTrailingZeros_07(int i) { i = ~i & (i - 1); if (i <= 0) return i == 0 ? 0 : 32; int n = 0; 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 - (i >> 1); } static int numberOfTrailingZeros_08(int i) { i = ~i & (i - 1); if (i <= 0) return i == 0 ? 0 : 32; int n = 0; if (i > 1 << 16) { n += 16; i >>= 16; } if (i > 1 << 8) { n += 8; i >>= 8; } if (i > 1 << 4) { n += 4; i >>= 4; } i -= (i >> 1) & 5; return n + (i >> 2) + (i & 3); } static int numberOfTrailingZeros_09(int i) { i = ~i & (i - 1); if (i <= 0) return i & 32; int n = 1; 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 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 bitCount(int i) { // HD, Figure 5-2 i = i - ((i >>> 1) & 0x55555555); i = (i & 0x33333333) + ((i >>> 2) & 0x33333333); i = (i + (i >>> 4)) & 0x0f0f0f0f; i = i + (i >>> 8); i = i + (i >>> 16); return i & 0x3f; } }