Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/micro/org/openjdk/bench/vm/compiler/MaxMinOptimizeTest.java
41161 views
1
/*
2
* Copyright (c) 2021, Huawei Technologies Co. Ltd. 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.Benchmark;
26
import org.openjdk.jmh.annotations.*;
27
28
import java.util.Random;
29
import java.util.concurrent.TimeUnit;
30
import org.openjdk.jmh.infra.Blackhole;
31
32
@BenchmarkMode({Mode.AverageTime})
33
@OutputTimeUnit(TimeUnit.MICROSECONDS)
34
@State(Scope.Thread)
35
public class MaxMinOptimizeTest {
36
private static final int COUNT = 100000;
37
38
private float[] floats_a = new float[COUNT];
39
private float[] floats_b = new float[COUNT];
40
private double[] doubles_a = new double[COUNT];
41
private double[] doubles_b = new double[COUNT];
42
43
private Random r = new Random();
44
45
@Setup
46
public void init() {
47
for (int i=0; i<COUNT; i++) {
48
floats_a[i] = r.nextFloat();
49
floats_b[i] = r.nextFloat();
50
doubles_a[i] = r.nextDouble();
51
doubles_b[i] = r.nextDouble();
52
}
53
}
54
55
@Benchmark
56
public void fAdd(Blackhole bh) {
57
float sum = 0;
58
for (int i=0; i<COUNT; i++)
59
sum += fAddBench(floats_a[i], floats_b[i]);
60
bh.consume(sum);
61
}
62
63
@Benchmark
64
public void fMul(Blackhole bh) {
65
float sum = 0;
66
for (int i=0; i<COUNT; i++)
67
sum += fMulBench(floats_a[i], floats_b[i]);
68
bh.consume(sum);
69
}
70
71
@Benchmark
72
public void fMax(Blackhole bh) {
73
float sum = 0;
74
for (int i=0; i<COUNT; i++)
75
sum += fMaxBench(floats_a[i], floats_b[i]);
76
bh.consume(sum);
77
}
78
79
@Benchmark
80
public void fMin(Blackhole bh) {
81
float sum = 0;
82
for (int i=0; i<COUNT; i++)
83
sum += fMinBench(floats_a[i], floats_b[i]);
84
bh.consume(sum);
85
}
86
87
private float fAddBench(float a, float b) {
88
return Math.max(a, b) + Math.min(a, b);
89
}
90
91
private float fMulBench(float a, float b) {
92
return Math.max(a, b) * Math.min(a, b);
93
}
94
95
private float fMaxBench(float a, float b) {
96
return Math.max(Math.max(a, b), Math.min(a, b));
97
}
98
99
private float fMinBench(float a, float b) {
100
return Math.min(Math.max(a, b), Math.min(a, b));
101
}
102
103
104
@Benchmark
105
public void dAdd(Blackhole bh) {
106
double sum = 0;
107
for (int i=0; i<COUNT; i++)
108
sum += dAddBench(doubles_a[i], doubles_b[i]);
109
bh.consume(sum);
110
}
111
112
@Benchmark
113
public void dMul(Blackhole bh) {
114
double sum = 0;
115
for (int i=0; i<COUNT; i++)
116
sum += dMulBench(doubles_a[i], doubles_b[i]);
117
bh.consume(sum);
118
}
119
120
@Benchmark
121
public void dMax(Blackhole bh) {
122
double sum = 0;
123
for (int i=0; i<COUNT; i++)
124
sum += dMaxBench(doubles_a[i], doubles_b[i]);
125
bh.consume(sum);
126
}
127
128
@Benchmark
129
public void dMin(Blackhole bh) {
130
double sum = 0;
131
for (int i=0; i<COUNT; i++)
132
sum += dMinBench(doubles_a[i], doubles_b[i]);
133
bh.consume(sum);
134
}
135
136
private double dAddBench(double a, double b) {
137
return Math.max(a, b) + Math.min(a, b);
138
}
139
140
private double dMulBench(double a, double b) {
141
return Math.max(a, b) * Math.min(a, b);
142
}
143
144
private double dMaxBench(double a, double b) {
145
return Math.max(Math.max(a, b), Math.min(a, b));
146
}
147
148
private double dMinBench(double a, double b) {
149
return Math.min(Math.max(a, b), Math.min(a, b));
150
}
151
}
152
153