|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | +package org.openjdk.bench.vm.compiler; |
| 24 | + |
| 25 | +import org.openjdk.jmh.annotations.*; |
| 26 | +import org.openjdk.jmh.infra.*; |
| 27 | +import java.util.concurrent.TimeUnit; |
| 28 | +import java.util.random.RandomGenerator; |
| 29 | +import java.util.random.RandomGeneratorFactory; |
| 30 | + |
| 31 | +@BenchmarkMode(Mode.AverageTime) |
| 32 | +@OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 33 | +@State(Scope.Thread) |
| 34 | +public abstract class VectorBitCount { |
| 35 | + @Param({"1024"}) |
| 36 | + public int SIZE; |
| 37 | + |
| 38 | + @Param("0") |
| 39 | + private int seed; |
| 40 | + private RandomGenerator rng = RandomGeneratorFactory.getDefault().create(seed); |
| 41 | + private int[] bufferRandInts; |
| 42 | + private long[] bufferRandLongs; |
| 43 | + private int[] bitCounts; |
| 44 | + @Setup |
| 45 | + public void init() { |
| 46 | + bufferRandInts = new int[SIZE]; |
| 47 | + bufferRandLongs = new long[SIZE]; |
| 48 | + bitCounts = new int[SIZE]; |
| 49 | + |
| 50 | + for (int i = 0; i < SIZE; i++) { |
| 51 | + bufferRandInts[i] = rng.nextInt(); |
| 52 | + bufferRandLongs[i] = rng.nextLong(); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Benchmark |
| 57 | + public int[] intBitCount() { |
| 58 | + for (int i = 0; i < SIZE; i++) { |
| 59 | + bitCounts[i] = Integer.bitCount(bufferRandInts[i]); |
| 60 | + } |
| 61 | + return bitCounts; |
| 62 | + } |
| 63 | + |
| 64 | + @Benchmark |
| 65 | + public int[] longBitCount() { |
| 66 | + for (int i = 0; i < SIZE; i++) { |
| 67 | + bitCounts[i] = Long.bitCount(bufferRandLongs[i]); |
| 68 | + } |
| 69 | + return bitCounts; |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + @Fork(value = 1, jvmArgsPrepend = { |
| 74 | + "-XX:+UseSuperWord" |
| 75 | + }) |
| 76 | + public static class WithSuperword extends VectorBitCount { |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + @Fork(value = 1, jvmArgsPrepend = { |
| 81 | + "-XX:-UseSuperWord" |
| 82 | + }) |
| 83 | + public static class NoSuperword extends VectorBitCount { |
| 84 | + } |
| 85 | + |
| 86 | +} |
| 87 | + |
0 commit comments