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 java.util.concurrent.TimeUnit;
 29 import java.util.function.IntFunction;
 30 
 31 import org.openjdk.jmh.annotations.*;
 32 import org.openjdk.jmh.infra.Blackhole;
 33 
 34 @BenchmarkMode(Mode.Throughput)
 35 @OutputTimeUnit(TimeUnit.MILLISECONDS)
 36 @State(Scope.Benchmark)
 37 @Warmup(iterations = 3, time = 1)
 38 @Measurement(iterations = 5, time = 1)
 39 @Fork(value = 1, jvmArgsPrepend = {"--add-modules=jdk.incubator.vector"})
 40 public class $Type$Scalar extends AbstractVectorBenchmark {
 41     static final int INVOC_COUNT = 1; // To align with vector benchmarks.
 42 
 43     @Param("1024")
 44     int size;
 45 
 46     $type$[] fill(IntFunction<$Wideboxtype$> f) {
 47         $type$[] array = new $type$[size];
 48         for (int i = 0; i < array.length; i++) {
 49             array[i] = f.apply(i);
 50         }
 51         return array;
 52     }
 53 
 54     static $bitstype$ bits($type$ e) {
 55         return {#if[FP]?$Type$.$type$To$Bitstype$Bits(e):e};
 56     }
 57 
 58     $type$[] as, bs, cs, rs;
 59     boolean[] ms, mt, rms;
 60     int[] ss;
 61 
 62     @Setup
 63     public void init() {
 64         as = fill(i -> ($type$)(2*i));
 65         bs = fill(i -> ($type$)(i+1));
 66         cs = fill(i -> ($type$)(i+5));
 67         rs = fill(i -> ($type$)0);
 68         ms = fillMask(size, i -> (i % 2) == 0);
 69         mt = fillMask(size, i -> true);
 70         rms = fillMask(size, i -> false);
 71 
 72         ss = fillInt(size, i -> RANDOM.nextInt(Math.max(i,1)));
 73     }
 74 
 75     final IntFunction<$type$[]> fa = vl -> as;
 76     final IntFunction<$type$[]> fb = vl -> bs;
 77     final IntFunction<$type$[]> fc = vl -> cs;
 78     final IntFunction<$type$[]> fr = vl -> rs;
 79     final IntFunction<boolean[]> fm = vl -> ms;
 80     final IntFunction<boolean[]> fmt = vl -> mt;
 81     final IntFunction<boolean[]> fmr = vl -> rms;
 82     final IntFunction<int[]> fs = vl -> ss;
 83 
 84     static boolean eq($type$ a, $type$ b) {
 85         return a == b;
 86     }
 87 
 88     static boolean neq($type$ a, $type$ b) {
 89         return a != b;
 90     }
 91 
 92     static boolean lt($type$ a, $type$ b) {
 93         return a < b;
 94     }
 95 
 96     static boolean le($type$ a, $type$ b) {
 97         return a <= b;
 98     }
 99 
100     static boolean gt($type$ a, $type$ b) {
101         return a > b;
102     }
103 
104     static boolean ge($type$ a, $type$ b) {
105         return a >= b;
106     }
107 
108 #if[!FP]
109     static boolean ult($type$ a, $type$ b) {
110         return $Boxtype$.compareUnsigned(a, b) < 0;
111     }
112 
113     static boolean ule($type$ a, $type$ b) {
114         return $Boxtype$.compareUnsigned(a, b) <= 0;
115     }
116 
117     static boolean ugt($type$ a, $type$ b) {
118         return $Boxtype$.compareUnsigned(a, b) > 0;
119     }
120 
121     static boolean uge($type$ a, $type$ b) {
122         return $Boxtype$.compareUnsigned(a, b) >= 0;
123     }
124 #end[!FP]