1 /*
  2  * Copyright (c) 2018, 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.operation;
 25 
 26 #warn This file is preprocessed before being compiled
 27 
 28 import jdk.incubator.vector.Vector;
 29 import jdk.incubator.vector.VectorMask;
 30 import jdk.incubator.vector.VectorOperators;
 31 import jdk.incubator.vector.VectorShape;
 32 import jdk.incubator.vector.VectorSpecies;
 33 import jdk.incubator.vector.VectorShuffle;
 34 import jdk.incubator.vector.$Type$Vector;
 35 
 36 import java.util.concurrent.TimeUnit;
 37 import java.util.function.BiFunction;
 38 import java.util.function.IntFunction;
 39 
 40 import org.openjdk.jmh.annotations.*;
 41 import org.openjdk.jmh.infra.Blackhole;
 42 
 43 @BenchmarkMode(Mode.Throughput)
 44 @OutputTimeUnit(TimeUnit.MILLISECONDS)
 45 @State(Scope.Benchmark)
 46 @Warmup(iterations = 3, time = 1)
 47 @Measurement(iterations = 5, time = 1)
 48 @Fork(value = 1, jvmArgsPrepend = {"--add-modules=jdk.incubator.vector"})
 49 public class $vectorbenchtype$ extends AbstractVectorBenchmark {
 50 #if[MaxBit]
 51     static final VectorSpecies<$Wideboxtype$> SPECIES = $Type$Vector.SPECIES_MAX;
 52 #else[MaxBit]
 53     static final VectorSpecies<$Wideboxtype$> SPECIES = $Type$Vector.SPECIES_$bits$;
 54 #end[MaxBit]
 55 
 56     static final int INVOC_COUNT = 1; // get rid of outer loop
 57 
 58 #if[BITWISE]
 59     static void replaceZero($type$[] a, $type$ v) {
 60         for (int i = 0; i < a.length; i++) {
 61             if (a[i] == 0) {
 62                 a[i] = v;
 63             }
 64         }
 65     }
 66 
 67     static void replaceZero($type$[] a, boolean[] mask, $type$ v) {
 68         for (int i = 0; i < a.length; i++) {
 69             if (mask[i % mask.length] && a[i] == 0) {
 70                 a[i] = v;
 71             }
 72         }
 73     }
 74 #end[BITWISE]
 75 
 76     @Param("1024")
 77     int size;
 78 
 79     $type$[] fill(IntFunction<$Wideboxtype$> f) {
 80         $type$[] array = new $type$[size];
 81         for (int i = 0; i < array.length; i++) {
 82             array[i] = f.apply(i);
 83         }
 84         return array;
 85     }
 86 
 87     $type$[] a, b, c, r;
 88     boolean[] m, mt, rm;
 89     int[] s;
 90 
 91     @Setup
 92     public void init() {
 93         size += size % SPECIES.length(); // FIXME: add post-loops
 94 
 95         a = fill(i -> ($type$)(2*i));
 96         b = fill(i -> ($type$)(i+1));
 97         c = fill(i -> ($type$)(i+5));
 98         r = fill(i -> ($type$)0);
 99 
100         m = fillMask(size, i -> (i % 2) == 0);
101         mt = fillMask(size, i -> true);
102         rm = fillMask(size, i -> false);
103 
104         s = fillInt(size, i -> RANDOM.nextInt(SPECIES.length()));
105     }
106 
107     final IntFunction<$type$[]> fa = vl -> a;
108     final IntFunction<$type$[]> fb = vl -> b;
109     final IntFunction<$type$[]> fc = vl -> c;
110     final IntFunction<$type$[]> fr = vl -> r;
111     final IntFunction<boolean[]> fm = vl -> m;
112     final IntFunction<boolean[]> fmt = vl -> mt;
113     final IntFunction<boolean[]> fmr = vl -> rm;
114     final BiFunction<Integer,Integer,int[]> fs = (i,j) -> s;
115