Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/compiler/rangechecks/PowerOf2SizedArraysChecks.java
41152 views
1
/*
2
* Copyright (c) 2015, 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
/**
25
* @test
26
* @bug 8003585
27
* @summary strength reduce or eliminate range checks for power-of-two sized arrays
28
*
29
* @run main/othervm -XX:CompileCommand=compileonly,compiler.rangechecks.PowerOf2SizedArraysChecks::test*
30
* -XX:-BackgroundCompilation
31
* compiler.rangechecks.PowerOf2SizedArraysChecks
32
*/
33
34
package compiler.rangechecks;
35
36
import java.util.function.BiFunction;
37
import java.util.function.Function;
38
39
public class PowerOf2SizedArraysChecks {
40
41
static void check_result(String name, int x, int m, boolean expected, boolean res) {
42
if (expected != res) {
43
throw new RuntimeException("Bad result in " + name + " for x = " + x + " m = " + m + " expected " + expected + " got " + res);
44
}
45
}
46
47
static void helper(String name, BiFunction<Integer, int[], Boolean> test, int[] x_values, int[] m_values, boolean[][] expected) {
48
for (int i = 0; i < x_values.length; i++) {
49
int x = x_values[i];
50
for (int j = 0; j < m_values.length; j++) {
51
int m = m_values[j];
52
int[] array = new int[m];
53
boolean res = test.apply(x, array);
54
check_result(name, x, m, expected[i][j], res);
55
}
56
}
57
}
58
59
static void check_result(String name, int m, boolean expected, boolean res) {
60
if (expected != res) {
61
throw new RuntimeException("Bad result in " + name + " for m = " + m + " expected " + expected + " got " + res);
62
}
63
}
64
65
static void helper2(String name, Function<int[], Boolean> test, int[] m_values, boolean[] expected) {
66
for (int j = 0; j < m_values.length; j++) {
67
int m = m_values[j];
68
int[] array = new int[m];
69
boolean res = test.apply(array);
70
check_result(name, m, expected[j], res);
71
}
72
}
73
74
// ((x & m) u<= m) is always true
75
static boolean test1(int x, int[] array) {
76
int m = array.length;
77
if ((x & m) < 0 || (x & m) > m) {
78
return false;
79
}
80
return true;
81
}
82
83
// ((x & (m - 1)) u< m) iff (m > 0)
84
static boolean test2(int x, int[] array) {
85
int m = array.length;
86
if ((x & (m-1)) < 0 || (x & (m-1)) >= m) {
87
return false;
88
}
89
return true;
90
}
91
92
static boolean test3(int x, int[] array) {
93
try {
94
int v = array[x & (array.length-1)];
95
} catch(ArrayIndexOutOfBoundsException aioobe) {
96
return false;
97
}
98
return true;
99
}
100
101
// arraylength <= 0 to arraylength u<= 0
102
static boolean test4(int[] array) {
103
if (array.length <= 0) {
104
return false;
105
}
106
return true;
107
}
108
109
// arraylength == 0 to arraylength u<= 0
110
static boolean test5(int[] array) {
111
if (array.length == 0) {
112
return false;
113
}
114
return true;
115
}
116
117
// arraylength != 0 to arraylength u> 0
118
static boolean test6(int[] array) {
119
if (array.length != 0) {
120
return false;
121
}
122
return true;
123
}
124
125
static public void main(String[] args) {
126
int[] x_values = {-10, -5, 0, 5, 8, 16, 100};
127
int[] m_values = { 16, 10, 0 };
128
129
boolean[][] test1_expected = new boolean[x_values.length][m_values.length];
130
for (int i = 0; i < x_values.length; i++) {
131
for (int j = 0; j < m_values.length; j++) {
132
test1_expected[i][j] = true;
133
}
134
}
135
136
boolean[][] test2_expected = new boolean[x_values.length][m_values.length];
137
for (int i = 0; i < x_values.length; i++) {
138
for (int j = 0; j < m_values.length; j++) {
139
test2_expected[i][j] = (m_values[j] > 0);
140
}
141
}
142
143
boolean[] test4_expected = new boolean[m_values.length];
144
for (int i = 0; i < m_values.length; i++) {
145
test4_expected[i] = (m_values[i] > 0);
146
}
147
boolean[] test5_expected = new boolean[m_values.length];
148
for (int i = 0; i < m_values.length; i++) {
149
test5_expected[i] = (m_values[i] != 0);
150
}
151
boolean[] test6_expected = new boolean[m_values.length];
152
for (int i = 0; i < m_values.length; i++) {
153
test6_expected[i] = (m_values[i] == 0);
154
}
155
156
for (int i = 0; i < 20000; i++) {
157
helper("test1", PowerOf2SizedArraysChecks::test1, x_values, m_values, test1_expected);
158
helper("test2", PowerOf2SizedArraysChecks::test2, x_values, m_values, test2_expected);
159
helper("test3", PowerOf2SizedArraysChecks::test3, x_values, m_values, test2_expected);
160
helper2("test4", PowerOf2SizedArraysChecks::test4, m_values, test4_expected);
161
helper2("test5", PowerOf2SizedArraysChecks::test5, m_values, test5_expected);
162
helper2("test6", PowerOf2SizedArraysChecks::test6, m_values, test6_expected);
163
}
164
}
165
}
166
167