1 //
  2 // Copyright (c) 2003, 2021, 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 //
 24 package org.openjdk.bench.jdk.incubator.vector;
 25 
 26 import java.util.Random;
 27 import jdk.incubator.vector.*;
 28 import java.util.concurrent.TimeUnit;
 29 import org.openjdk.jmh.annotations.*;
 30 import org.openjdk.jmh.infra.Blackhole;
 31 
 32 @OutputTimeUnit(TimeUnit.MILLISECONDS)
 33 @State(Scope.Thread)
 34 public class MaskQueryOperationsBenchmark {
 35     @Param({"128","256","512"})
 36     int bits;
 37 
 38     @Param({"1","2","3"})
 39     int inputs;
 40 
 41     VectorSpecies<Byte> bspecies;
 42     VectorSpecies<Short> sspecies;
 43     VectorSpecies<Integer> ispecies;
 44     VectorSpecies<Long> lspecies;
 45     VectorMask<Byte> bmask;
 46     VectorMask<Short> smask;
 47     VectorMask<Integer> imask;
 48     VectorMask<Long> lmask;
 49     boolean [] mask_arr;
 50 
 51     static final boolean [] mask_avg_case = {
 52        false, false, false, true, false, false, false, false,
 53        false, false, false, true, false, false, false, false,
 54        false, false, false, true, false, false, false, false,
 55        true, true, true, true, true, true, true, true,
 56        true, true, true, true, true, true, true, true,
 57        false, false, false, true, false, false, false, false,
 58        false, false, false, true, false, false, false, false,
 59        false, false, false, true, false, false, false, false
 60     };
 61 
 62     static final boolean [] mask_best_case  = {
 63        true, true, true, true, true, true, true, true,
 64        true, true, true, true, true, true, true, true,
 65        true, true, true, true, true, true, true, true,
 66        true, true, true, true, true, true, true, true,
 67        true, true, true, true, true, true, true, true,
 68        true, true, true, true, true, true, true, true,
 69        true, true, true, true, true, true, true, true,
 70        true, true, true, true, true, true, true, true
 71     };
 72 
 73     static final boolean [] mask_worst_case  = {
 74        false, false, false, false, false, false, false, false,
 75        false, false, false, false, false, false, false, false,
 76        false, false, false, false, false, false, false, false,
 77        false, false, false, false, false, false, false, false,
 78        false, false, false, false, false, false, false, false,
 79        false, false, false, false, false, false, false, false,
 80        false, false, false, false, false, false, false, false,
 81        false, false, false, false, false, false, false, false
 82     };
 83 
 84     @Setup(Level.Trial)
 85     public void BmSetup() {
 86         bspecies = VectorSpecies.of(byte.class, VectorShape.forBitSize(bits));
 87         sspecies = VectorSpecies.of(short.class, VectorShape.forBitSize(bits));
 88         ispecies = VectorSpecies.of(int.class, VectorShape.forBitSize(bits));
 89         lspecies = VectorSpecies.of(long.class, VectorShape.forBitSize(bits));
 90 
 91         if( 1 == inputs) {
 92           mask_arr = mask_best_case;
 93         } else if ( 2 == inputs ) {
 94           mask_arr = mask_worst_case;
 95         } else {
 96           mask_arr = mask_avg_case;
 97         }
 98 
 99         bmask   = VectorMask.fromArray(bspecies, mask_arr, 0);
100         smask   = VectorMask.fromArray(sspecies, mask_arr, 0);
101         imask   = VectorMask.fromArray(ispecies, mask_arr, 0);
102         lmask   = VectorMask.fromArray(lspecies, mask_arr, 0);
103     }
104 
105     @Benchmark
106     public int testTrueCountByte(Blackhole bh) {
107         return bmask.trueCount();
108     }
109 
110     @Benchmark
111     public int testTrueCountShort(Blackhole bh) {
112         return smask.trueCount();
113     }
114     @Benchmark
115     public int testTrueCountInt(Blackhole bh) {
116         return imask.trueCount();
117     }
118     @Benchmark
119     public int testTrueCountLong(Blackhole bh) {
120         return lmask.trueCount();
121     }
122 
123     @Benchmark
124     public int testFirstTrueByte(Blackhole bh) {
125         return bmask.firstTrue();
126     }
127 
128     @Benchmark
129     public int testFirstTrueShort(Blackhole bh) {
130         return smask.firstTrue();
131     }
132     @Benchmark
133     public int testFirstTrueInt(Blackhole bh) {
134         return imask.firstTrue();
135     }
136     @Benchmark
137     public int testFirstTrueLong(Blackhole bh) {
138         return lmask.firstTrue();
139     }
140 
141     @Benchmark
142     public int testLastTrueByte(Blackhole bh) {
143         return bmask.lastTrue();
144     }
145 
146     @Benchmark
147     public int testLastTrueShort(Blackhole bh) {
148         return smask.lastTrue();
149     }
150     @Benchmark
151     public int testLastTrueInt(Blackhole bh) {
152         return imask.lastTrue();
153     }
154     @Benchmark
155     public int testLastTrueLong(Blackhole bh) {
156         return lmask.lastTrue();
157     }
158 
159     @Benchmark
160     public long testToLongByte(Blackhole bh) {
161         return bmask.toLong();
162     }
163 
164     @Benchmark
165     public long testToLongShort(Blackhole bh) {
166         return smask.toLong();
167     }
168     @Benchmark
169     public long testToLongInt(Blackhole bh) {
170         return imask.toLong();
171     }
172     @Benchmark
173     public long testToLongLong(Blackhole bh) {
174         return lmask.toLong();
175     }
176 
177 }
--- EOF ---