Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/compiler/codegen/Test6896617.java
41149 views
1
/*
2
* Copyright (c) 2013, 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
24
/*
25
* @test
26
* @key randomness
27
* @bug 6896617
28
* @summary Optimize sun.nio.cs.ISO_8859_1$Encode.encodeArrayLoop() with SSE instructions on x86
29
* @library /test/lib
30
* @modules java.base/jdk.internal.misc
31
* java.base/sun.nio.cs
32
* java.management
33
*
34
* @ignore 8193479
35
* @run main/othervm/timeout=1200 -Xbatch -Xmx256m compiler.codegen.Test6896617
36
*/
37
38
package compiler.codegen;
39
40
import jdk.test.lib.Utils;
41
42
import java.nio.ByteBuffer;
43
import java.nio.CharBuffer;
44
import java.nio.charset.Charset;
45
import java.nio.charset.CharsetDecoder;
46
import java.nio.charset.CharsetEncoder;
47
import java.nio.charset.CodingErrorAction;
48
import java.util.Arrays;
49
import java.util.Random;
50
51
public class Test6896617 {
52
final static int SIZE = 256;
53
54
public static void main(String[] args) {
55
String csn = "ISO-8859-1";
56
Charset cs = Charset.forName(csn);
57
CharsetEncoder enc = cs.newEncoder();
58
enc.onMalformedInput(CodingErrorAction.REPLACE)
59
.onUnmappableCharacter(CodingErrorAction.REPLACE);
60
CharsetDecoder dec = cs.newDecoder();
61
dec.onMalformedInput(CodingErrorAction.REPLACE)
62
.onUnmappableCharacter(CodingErrorAction.REPLACE);
63
64
byte repl = (byte)'?';
65
enc.replaceWith(new byte[] { repl });
66
67
// Use internal API for tests.
68
sun.nio.cs.ArrayEncoder arrenc = (sun.nio.cs.ArrayEncoder)enc;
69
sun.nio.cs.ArrayDecoder arrdec = (sun.nio.cs.ArrayDecoder)dec;
70
71
// Populate char[] with chars which can be encoded by ISO_8859_1 (<= 0xFF)
72
Random rnd = Utils.getRandomInstance();
73
int maxchar = 0xFF;
74
char[] a = new char[SIZE];
75
byte[] b = new byte[SIZE];
76
char[] at = new char[SIZE];
77
byte[] bt = new byte[SIZE];
78
for (int i = 0; i < SIZE; i++) {
79
char c = (char) rnd.nextInt(maxchar);
80
if (!enc.canEncode(c)) {
81
System.out.printf("Something wrong: can't encode c=%03x\n", (int)c);
82
System.exit(97);
83
}
84
a[i] = c;
85
b[i] = (byte)c;
86
at[i] = (char)-1;
87
bt[i] = (byte)-1;
88
}
89
if (arrenc.encode(a, 0, SIZE, bt) != SIZE || !Arrays.equals(b, bt)) {
90
System.out.println("Something wrong: ArrayEncoder.encode failed");
91
System.exit(97);
92
}
93
if (arrdec.decode(b, 0, SIZE, at) != SIZE || !Arrays.equals(a, at)) {
94
System.out.println("Something wrong: ArrayDecoder.decode failed");
95
System.exit(97);
96
}
97
for (int i = 0; i < SIZE; i++) {
98
at[i] = (char)-1;
99
bt[i] = (byte)-1;
100
}
101
102
ByteBuffer bb = ByteBuffer.wrap(b);
103
CharBuffer ba = CharBuffer.wrap(a);
104
ByteBuffer bbt = ByteBuffer.wrap(bt);
105
CharBuffer bat = CharBuffer.wrap(at);
106
if (!enc.encode(ba, bbt, true).isUnderflow() || !Arrays.equals(b, bt)) {
107
System.out.println("Something wrong: Encoder.encode failed");
108
System.exit(97);
109
}
110
if (!dec.decode(bb, bat, true).isUnderflow() || !Arrays.equals(a, at)) {
111
System.out.println("Something wrong: Decoder.decode failed");
112
System.exit(97);
113
}
114
for (int i = 0; i < SIZE; i++) {
115
at[i] = (char)-1;
116
bt[i] = (byte)-1;
117
}
118
119
// Warm up
120
boolean failed = false;
121
int result = 0;
122
for (int i = 0; i < 10000; i++) {
123
result += arrenc.encode(a, 0, SIZE, bt);
124
result -= arrdec.decode(b, 0, SIZE, at);
125
}
126
for (int i = 0; i < 10000; i++) {
127
result += arrenc.encode(a, 0, SIZE, bt);
128
result -= arrdec.decode(b, 0, SIZE, at);
129
}
130
for (int i = 0; i < 10000; i++) {
131
result += arrenc.encode(a, 0, SIZE, bt);
132
result -= arrdec.decode(b, 0, SIZE, at);
133
}
134
if (result != 0 || !Arrays.equals(b, bt) || !Arrays.equals(a, at)) {
135
failed = true;
136
System.out.println("Failed: ArrayEncoder.encode char[" + SIZE + "] and ArrayDecoder.decode byte[" + SIZE + "]");
137
}
138
for (int i = 0; i < SIZE; i++) {
139
at[i] = (char)-1;
140
bt[i] = (byte)-1;
141
}
142
143
boolean is_underflow = true;
144
for (int i = 0; i < 10000; i++) {
145
ba.clear(); bb.clear(); bat.clear(); bbt.clear();
146
boolean enc_res = enc.encode(ba, bbt, true).isUnderflow();
147
boolean dec_res = dec.decode(bb, bat, true).isUnderflow();
148
is_underflow = is_underflow && enc_res && dec_res;
149
}
150
for (int i = 0; i < SIZE; i++) {
151
at[i] = (char)-1;
152
bt[i] = (byte)-1;
153
}
154
for (int i = 0; i < 10000; i++) {
155
ba.clear(); bb.clear(); bat.clear(); bbt.clear();
156
boolean enc_res = enc.encode(ba, bbt, true).isUnderflow();
157
boolean dec_res = dec.decode(bb, bat, true).isUnderflow();
158
is_underflow = is_underflow && enc_res && dec_res;
159
}
160
for (int i = 0; i < SIZE; i++) {
161
at[i] = (char)-1;
162
bt[i] = (byte)-1;
163
}
164
for (int i = 0; i < 10000; i++) {
165
ba.clear(); bb.clear(); bat.clear(); bbt.clear();
166
boolean enc_res = enc.encode(ba, bbt, true).isUnderflow();
167
boolean dec_res = dec.decode(bb, bat, true).isUnderflow();
168
is_underflow = is_underflow && enc_res && dec_res;
169
}
170
if (!is_underflow || !Arrays.equals(b, bt) || !Arrays.equals(a, at)) {
171
failed = true;
172
System.out.println("Failed: Encoder.encode char[" + SIZE + "] and Decoder.decode byte[" + SIZE + "]");
173
}
174
175
// Test encoder with different source and destination sizes
176
System.out.println("Testing different source and destination sizes");
177
for (int i = 1; i <= SIZE; i++) {
178
for (int j = 1; j <= SIZE; j++) {
179
bt = new byte[j];
180
// very source's SIZE
181
result = arrenc.encode(a, 0, i, bt);
182
int l = Math.min(i, j);
183
if (result != l) {
184
failed = true;
185
System.out.println("Failed: encode char[" + i + "] to byte[" + j + "]: result = " + result + ", expected " + l);
186
}
187
for (int k = 0; k < l; k++) {
188
if (bt[k] != b[k]) {
189
failed = true;
190
System.out.println("Failed: encoded byte[" + k + "] (" + bt[k] + ") != " + b[k]);
191
}
192
}
193
// very source's offset
194
int sz = SIZE - i + 1;
195
result = arrenc.encode(a, i-1, sz, bt);
196
l = Math.min(sz, j);
197
if (result != l) {
198
failed = true;
199
System.out.println("Failed: encode char[" + sz + "] to byte[" + j + "]: result = " + result + ", expected " + l);
200
}
201
for (int k = 0; k < l; k++) {
202
if (bt[k] != b[i+k-1]) {
203
failed = true;
204
System.out.println("Failed: encoded byte[" + k + "] (" + bt[k] + ") != " + b[i+k-1]);
205
}
206
}
207
}
208
}
209
210
// Test encoder with char > 0xFF
211
System.out.println("Testing big char");
212
213
byte orig = (byte)'A';
214
bt = new byte[SIZE];
215
for (int i = 1; i <= SIZE; i++) {
216
for (int j = 0; j < i; j++) {
217
a[j] += 0x100;
218
// make sure to replace a different byte
219
bt[j] = orig;
220
result = arrenc.encode(a, 0, i, bt);
221
if (result != i) {
222
failed = true;
223
System.out.println("Failed: encode char[" + i + "] to byte[" + i + "]: result = " + result + ", expected " + i);
224
}
225
if (bt[j] != repl) {
226
failed = true;
227
System.out.println("Failed: encoded replace byte[" + j + "] (" + bt[j] + ") != " + repl);
228
}
229
bt[j] = b[j]; // Restore to compare whole array
230
for (int k = 0; k < i; k++) {
231
if (bt[k] != b[k]) {
232
failed = true;
233
System.out.println("Failed: encoded byte[" + k + "] (" + bt[k] + ") != " + b[k]);
234
}
235
}
236
a[j] -= 0x100; // Restore
237
}
238
}
239
240
// Test sun.nio.cs.ISO_8859_1$Encode.encodeArrayLoop() performance.
241
242
int itrs = Integer.getInteger("iterations", 1000000);
243
int size = Integer.getInteger("size", 256);
244
a = new char[size];
245
b = new byte[size];
246
bt = new byte[size];
247
for (int i = 0; i < size; i++) {
248
char c = (char) rnd.nextInt(maxchar);
249
if (!enc.canEncode(c)) {
250
System.out.printf("Something wrong: can't encode c=%03x\n", (int)c);
251
System.exit(97);
252
}
253
a[i] = c;
254
b[i] = (byte)-1;
255
bt[i] = (byte)c;
256
}
257
ba = CharBuffer.wrap(a);
258
bb = ByteBuffer.wrap(b);
259
boolean enc_res = enc.encode(ba, bb, true).isUnderflow();
260
if (!enc_res || !Arrays.equals(b, bt)) {
261
failed = true;
262
System.out.println("Failed 1: Encoder.encode char[" + size + "]");
263
}
264
for (int i = 0; i < size; i++) {
265
b[i] = (byte)-1;
266
}
267
268
// Make sure to recompile method if needed before performance run.
269
for (int i = 0; i < 10000; i++) {
270
ba.clear(); bb.clear();
271
enc_res = enc_res && enc.encode(ba, bb, true).isUnderflow();
272
}
273
for (int i = 0; i < size; i++) {
274
b[i] = (byte)-1;
275
}
276
for (int i = 0; i < 10000; i++) {
277
ba.clear(); bb.clear();
278
enc_res = enc_res && enc.encode(ba, bb, true).isUnderflow();
279
}
280
if (!enc_res || !Arrays.equals(b, bt)) {
281
failed = true;
282
System.out.println("Failed 2: Encoder.encode char[" + size + "]");
283
}
284
for (int i = 0; i < size; i++) {
285
b[i] = (byte)-1;
286
}
287
288
System.out.println("Testing ISO_8859_1$Encode.encodeArrayLoop() performance");
289
long start = System.currentTimeMillis();
290
for (int i = 0; i < itrs; i++) {
291
ba.clear(); bb.clear();
292
enc_res = enc_res && enc.encode(ba, bb, true).isUnderflow();
293
}
294
long end = System.currentTimeMillis();
295
if (!enc_res || !Arrays.equals(b, bt)) {
296
failed = true;
297
System.out.println("Failed 3: Encoder.encode char[" + size + "]");
298
} else {
299
System.out.println("size: " + size + " time: " + (end - start));
300
}
301
302
// Test sun.nio.cs.ISO_8859_1$Encode.encode() performance.
303
304
// Make sure to recompile method if needed before performance run.
305
result = 0;
306
for (int i = 0; i < size; i++) {
307
b[i] = (byte)-1;
308
}
309
for (int i = 0; i < 10000; i++) {
310
result += arrenc.encode(a, 0, size, b);
311
}
312
for (int i = 0; i < size; i++) {
313
b[i] = (byte)-1;
314
}
315
for (int i = 0; i < 10000; i++) {
316
result += arrenc.encode(a, 0, size, b);
317
}
318
if (result != size*20000 || !Arrays.equals(b, bt)) {
319
failed = true;
320
System.out.println("Failed 1: ArrayEncoder.encode char[" + SIZE + "]");
321
}
322
for (int i = 0; i < size; i++) {
323
b[i] = (byte)-1;
324
}
325
326
System.out.println("Testing ISO_8859_1$Encode.encode() performance");
327
result = 0;
328
start = System.currentTimeMillis();
329
for (int i = 0; i < itrs; i++) {
330
result += arrenc.encode(a, 0, size, b);
331
}
332
end = System.currentTimeMillis();
333
if (!Arrays.equals(b, bt)) {
334
failed = true;
335
System.out.println("Failed 2: ArrayEncoder.encode char[" + size + "]");
336
} else {
337
System.out.println("size: " + size + " time: " + (end - start));
338
}
339
340
if (failed) {
341
System.out.println("FAILED");
342
System.exit(97);
343
}
344
System.out.println("PASSED");
345
}
346
}
347
348