1 /*
2  * Copyright (c) 2020, 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 package org.openjdk.bench.vm.compiler;
24 
25 import org.openjdk.jmh.annotations.*;
26 import org.openjdk.jmh.infra.*;
27 
28 import java.util.concurrent.TimeUnit;
29 import java.util.Random;
30 
31 @BenchmarkMode(Mode.AverageTime)
32 @OutputTimeUnit(TimeUnit.NANOSECONDS)
33 @State(Scope.Thread)
34 public abstract class VectorReduction {
35     @Param({"512"})
36     public int COUNT;
37 
38     private int[] intsA;
39     private int[] intsB;
40     private int[] intsC;
41     private int[] intsD;
42     private int resI;
43     private long[] longsA;
44     private long[] longsB;
45     private long[] longsC;
46     private long[] longsD;
47     private long resL;
48 
49     @Param("0")
50     private int seed;
51     private Random r = new Random(seed);
52 
53     @Setup
init()54     public void init() {
55         intsA = new int[COUNT];
56         intsB = new int[COUNT];
57         intsC = new int[COUNT];
58         intsD = new int[COUNT];
59         longsA = new long[COUNT];
60         longsB = new long[COUNT];
61         longsC = new long[COUNT];
62         longsD = new long[COUNT];
63 
64         for (int i = 0; i < COUNT; i++) {
65             intsA[i] = r.nextInt();
66             intsB[i] = r.nextInt();
67             intsC[i] = r.nextInt();
68             longsA[i] = r.nextLong();
69             longsB[i] = r.nextLong();
70             longsC[i] = r.nextLong();
71         }
72     }
73 
74     @Benchmark
andRedI()75     public void andRedI() {
76         for (int i = 0; i < COUNT; i++) {
77             intsD[i] = (intsA[i] * intsB[i]) + (intsA[i] * intsC[i]) + (intsB[i] * intsC[i]);
78             resI &= intsD[i];
79         }
80     }
81 
82     @Benchmark
orRedI()83     public void orRedI() {
84         for (int i = 0; i < COUNT; i++) {
85             intsD[i] = (intsA[i] * intsB[i]) + (intsA[i] * intsC[i]) + (intsB[i] * intsC[i]);
86             resI |= intsD[i];
87         }
88     }
89 
90     @Benchmark
xorRedI()91     public void xorRedI() {
92         for (int i = 0; i < COUNT; i++) {
93             intsD[i] = (intsA[i] * intsB[i]) + (intsA[i] * intsC[i]) + (intsB[i] * intsC[i]);
94             resI ^= intsD[i];
95         }
96     }
97 
98     @Benchmark
andRedL()99     public void andRedL() {
100         for (int i = 0; i < COUNT; i++) {
101             longsD[i] = (longsA[i] + longsB[i]) + (longsA[i] + longsC[i]) + (longsB[i] + longsC[i]);
102             resL &= longsD[i];
103         }
104     }
105 
106     @Benchmark
orRedL()107     public void orRedL() {
108         for (int i = 0; i < COUNT; i++) {
109             longsD[i] = (longsA[i] + longsB[i]) + (longsA[i] + longsC[i]) + (longsB[i] + longsC[i]);
110             resL |= longsD[i];
111         }
112     }
113 
114     @Benchmark
xorRedL()115     public void xorRedL() {
116         for (int i = 0; i < COUNT; i++) {
117             longsD[i] = (longsA[i] + longsB[i]) + (longsA[i] + longsC[i]) + (longsB[i] + longsC[i]);
118             resL ^= longsD[i];
119         }
120     }
121 
122     @Fork(value = 1, jvmArgsPrepend = {
123         "-XX:+UseSuperWord"
124     })
125     public static class WithSuperword extends VectorReduction {
126 
127     }
128 
129     @Fork(value = 1, jvmArgsPrepend = {
130         "-XX:-UseSuperWord"
131     })
132     public static class NoSuperword extends VectorReduction {
133     }
134 
135 }
136 
137