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