1 /*
2  * Copyright (c) 2018, Red Hat Inc. 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 8204479
27  * @summary Bitwise AND on byte value sometimes produces wrong result
28  *
29  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-TieredCompilation
30  *      -XX:-UseOnStackReplacement -XX:-BackgroundCompilation -Xcomp -XX:-Inline
31  *      compiler.c2.TestUnsignedByteCompare
32  */
33 
34 package compiler.c2;
35 
36 public class TestUnsignedByteCompare {
37 
38     static int p, n;
39 
report(byte[] ba, int i, boolean failed)40     static void report(byte[] ba, int i, boolean failed) {
41         // Enable for debugging:
42         // System.out.println((failed ? "Failed" : "Passed") + " with: " + ba[i] + " at " + i);
43     }
44 
m1(byte[] ba)45     static void m1(byte[] ba) {
46         for (int i = 0; i < ba.length; i++) {
47             if ((ba[i] & 0xFF) < 0x10) {
48                p++;
49                report(ba, i, true);
50             } else {
51                n++;
52                report(ba, i, false);
53             }
54         }
55     }
56 
m2(byte[] ba)57     static void m2(byte[] ba) {
58         for (int i = 0; i < ba.length; i++) {
59             if (((ba[i] & 0xFF) & 0x80) < 0) {
60                p++;
61                report(ba, i, true);
62             } else {
63                n++;
64                report(ba, i, false);
65             }
66         }
67     }
68 
main(String[] args)69     static public void main(String[] args) {
70         final int tries = 1_000;
71         final int count = 1_000;
72 
73         byte[] ba = new byte[count];
74 
75         for (int i = 0; i < count; i++) {
76             int v = -(i % 126 + 1);
77             ba[i] = (byte)v;
78         }
79 
80         for (int t = 0; t < tries; t++) {
81             m1(ba);
82             if (p != 0) {
83                 throw new IllegalStateException("m1 error: p = " + p + ", n = " + n);
84             }
85         }
86 
87         for (int t = 0; t < tries; t++) {
88             m2(ba);
89             if (p != 0) {
90                 throw new IllegalStateException("m2 error: p = " + p + ", n = " + n);
91             }
92         }
93     }
94 }
95