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     static $type$ firstNonZero($type$ a, $type$ b) {
 77         return $Boxtype$.compare(a, ($type$) 0) != 0 ? a : b;
 78     }
 79 
 80 #if[BITWISE]
 81     private static final $type$ CONST_SHIFT = $Boxtype$.SIZE / 2;
 82 #end[BITWISE]
 83 
 84     @Param("1024")
 85     int size;
 86 
 87     $type$[] fill(IntFunction<$Wideboxtype$> f) {
 88         $type$[] array = new $type$[size];
 89         for (int i = 0; i < array.length; i++) {
 90             array[i] = f.apply(i);
 91         }
 92         return array;
 93     }
 94 
 95     $type$[] a, b, c, r;
 96     boolean[] m, mt, rm;
 97     int[] s;
 98 
 99     @Setup
100     public void init() {
101         size += size % SPECIES.length(); // FIXME: add post-loops
102 
103         a = fill(i -> ($type$)(2*i));
104         b = fill(i -> ($type$)(i+1));
105         c = fill(i -> ($type$)(i+5));
106         r = fill(i -> ($type$)0);
107 
108         m = fillMask(size, i -> (i % 2) == 0);
109         mt = fillMask(size, i -> true);
110         rm = fillMask(size, i -> false);
111 
112         s = fillInt(size, i -> RANDOM.nextInt(SPECIES.length()));
113     }
114 
115     final IntFunction<$type$[]> fa = vl -> a;
116     final IntFunction<$type$[]> fb = vl -> b;
117     final IntFunction<$type$[]> fc = vl -> c;
118     final IntFunction<$type$[]> fr = vl -> r;
119     final IntFunction<boolean[]> fm = vl -> m;
120     final IntFunction<boolean[]> fmt = vl -> mt;
121     final IntFunction<boolean[]> fmr = vl -> rm;
122     final BiFunction<Integer,Integer,int[]> fs = (i,j) -> s;
123