1 /* 2 * Copyright (c) 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.bigdata; 25 26 import jdk.incubator.vector.ByteVector; 27 import jdk.incubator.vector.Vector; 28 import jdk.incubator.vector.VectorMask; 29 import jdk.incubator.vector.VectorSpecies; 30 31 import java.util.concurrent.TimeUnit; 32 33 import org.openjdk.jmh.annotations.*; 34 35 @BenchmarkMode(Mode.Throughput) 36 @OutputTimeUnit(TimeUnit.MILLISECONDS) 37 @State(Scope.Benchmark) 38 @Warmup(iterations = 3, time = 1) 39 @Measurement(iterations = 5, time = 1) 40 @Fork(value = 1, jvmArgsPrepend = {"--add-modules=jdk.incubator.vector"}) 41 public class BooleanArrayCheck { 42 43 @Param("1024") 44 int ARRAY_LENGTH; 45 46 static final VectorSpecies<Byte> SPECIES = ByteVector.SPECIES_PREFERRED; 47 48 private boolean[] bitsArray; 49 50 @Setup 51 public void init() { 52 System.out.println("SPECIES's length: " + SPECIES.length()); 53 54 bitsArray = new boolean[ARRAY_LENGTH]; 55 for (int i = 0; i < ARRAY_LENGTH; i++) { 56 bitsArray[i] = true; 57 } 58 } 59 60 @Benchmark 61 public boolean filterAll_vec() { 62 int filterPos = 0; 63 64 for (; filterPos < ARRAY_LENGTH; filterPos += SPECIES.length()) { 65 VectorMask<Byte> mask = VectorMask.fromArray(SPECIES, bitsArray, filterPos); 66 if (!mask.allTrue()) { 67 return false; 68 } 69 } 70 71 for (filterPos -= SPECIES.length(); filterPos < ARRAY_LENGTH; filterPos++) { 72 if (!bitsArray[filterPos]) { 73 return false; 74 } 75 } 76 return true; 77 } 78 79 @Benchmark 80 public boolean filterAll() { 81 int filterPos = 0; 82 83 for (; filterPos < ARRAY_LENGTH; filterPos++) { 84 if (!bitsArray[filterPos]) { 85 return false; 86 } 87 } 88 return true; 89 } 90 91 } 92