Skip to content

Commit e52156d

Browse files
author
Vladimir Ivanov
committed
8255067: Restore Copyright line in file modified by 8253191
Reviewed-by: kvn, shade
1 parent c520469 commit e52156d

File tree

2 files changed

+138
-44
lines changed

2 files changed

+138
-44
lines changed
Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, Red Hat Inc. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,67 +23,72 @@
2323

2424
/*
2525
* @test
26-
* @bug 8204479 8253191
26+
* @bug 8204479
27+
* @summary Bitwise AND on byte value sometimes produces wrong result
2728
*
28-
* @library /test/lib
29-
* @modules java.base/jdk.internal.vm.annotation
30-
*
31-
* @run main/bootclasspath/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-TieredCompilation compiler.c2.TestUnsignedByteCompare
29+
* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-TieredCompilation
30+
* -XX:-UseOnStackReplacement -XX:-BackgroundCompilation -Xcomp -XX:-Inline
31+
* compiler.c2.TestUnsignedByteCompare
3232
*/
33-
package compiler.c2;
3433

35-
import java.lang.invoke.*;
36-
import jdk.internal.vm.annotation.DontInline;
37-
import jdk.test.lib.Asserts;
34+
package compiler.c2;
3835

3936
public class TestUnsignedByteCompare {
4037

41-
@DontInline static boolean testByteGT0(byte[] val) { return (val[0] & mask()) > 0; }
42-
@DontInline static boolean testByteGE0(byte[] val) { return (val[0] & mask()) >= 0; }
43-
@DontInline static boolean testByteEQ0(byte[] val) { return (val[0] & mask()) == 0; }
44-
@DontInline static boolean testByteNE0(byte[] val) { return (val[0] & mask()) != 0; }
45-
@DontInline static boolean testByteLE0(byte[] val) { return (val[0] & mask()) <= 0; }
46-
@DontInline static boolean testByteLT0(byte[] val) { return (val[0] & mask()) < 0; }
38+
static int p, n;
4739

48-
static void testValue(byte b) {
49-
byte[] bs = new byte[] { b };
50-
Asserts.assertEquals(((b & mask()) > 0), testByteGT0(bs), errorMessage(b, "GT0"));
51-
Asserts.assertEquals(((b & mask()) >= 0), testByteGE0(bs), errorMessage(b, "GE0"));
52-
Asserts.assertEquals(((b & mask()) == 0), testByteEQ0(bs), errorMessage(b, "EQ0"));
53-
Asserts.assertEquals(((b & mask()) != 0), testByteNE0(bs), errorMessage(b, "NE0"));
54-
Asserts.assertEquals(((b & mask()) <= 0), testByteLE0(bs), errorMessage(b, "LE0"));
55-
Asserts.assertEquals(((b & mask()) < 0), testByteLT0(bs), errorMessage(b, "LT0"));
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);
5643
}
5744

58-
public static void main(String[] args) {
59-
for (int mask = 0; mask <= 0xFF; mask++) {
60-
setMask(mask);
61-
for (int i = 0; i < 20_000; i++) {
62-
testValue((byte) i);
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);
6353
}
6454
}
65-
System.out.println("TEST PASSED");
6655
}
6756

68-
static String errorMessage(byte b, String type) {
69-
return String.format("%s: val=0x%x mask=0x%x", type, b, mask());
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+
}
7067
}
7168

72-
// Mutable mask as a compile-time constant.
69+
static public void main(String[] args) {
70+
final int tries = 1_000;
71+
final int count = 1_000;
7372

74-
private static final CallSite MASK_CS = new MutableCallSite(MethodType.methodType(int.class));
75-
private static final MethodHandle MASK_MH = MASK_CS.dynamicInvoker();
73+
byte[] ba = new byte[count];
7674

77-
static int mask() {
78-
try {
79-
return (int) MASK_MH.invokeExact();
80-
} catch (Throwable t) {
81-
throw new InternalError(t); // should NOT happen
75+
for (int i = 0; i < count; i++) {
76+
int v = -(i % 126 + 1);
77+
ba[i] = (byte)v;
8278
}
83-
}
8479

85-
static void setMask(int mask) {
86-
MethodHandle constant = MethodHandles.constant(int.class, mask);
87-
MASK_CS.setTarget(constant);
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+
}
8893
}
8994
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
24+
/*
25+
* @test
26+
* @bug 8253191
27+
*
28+
* @library /test/lib
29+
* @modules java.base/jdk.internal.vm.annotation
30+
*
31+
* @run main/bootclasspath/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-TieredCompilation compiler.c2.TestUnsignedByteCompare1
32+
*/
33+
package compiler.c2;
34+
35+
import java.lang.invoke.*;
36+
import jdk.internal.vm.annotation.DontInline;
37+
import jdk.test.lib.Asserts;
38+
39+
public class TestUnsignedByteCompare1 {
40+
41+
@DontInline static boolean testByteGT0(byte[] val) { return (val[0] & mask()) > 0; }
42+
@DontInline static boolean testByteGE0(byte[] val) { return (val[0] & mask()) >= 0; }
43+
@DontInline static boolean testByteEQ0(byte[] val) { return (val[0] & mask()) == 0; }
44+
@DontInline static boolean testByteNE0(byte[] val) { return (val[0] & mask()) != 0; }
45+
@DontInline static boolean testByteLE0(byte[] val) { return (val[0] & mask()) <= 0; }
46+
@DontInline static boolean testByteLT0(byte[] val) { return (val[0] & mask()) < 0; }
47+
48+
static void testValue(byte b) {
49+
byte[] bs = new byte[] { b };
50+
Asserts.assertEquals(((b & mask()) > 0), testByteGT0(bs), errorMessage(b, "GT0"));
51+
Asserts.assertEquals(((b & mask()) >= 0), testByteGE0(bs), errorMessage(b, "GE0"));
52+
Asserts.assertEquals(((b & mask()) == 0), testByteEQ0(bs), errorMessage(b, "EQ0"));
53+
Asserts.assertEquals(((b & mask()) != 0), testByteNE0(bs), errorMessage(b, "NE0"));
54+
Asserts.assertEquals(((b & mask()) <= 0), testByteLE0(bs), errorMessage(b, "LE0"));
55+
Asserts.assertEquals(((b & mask()) < 0), testByteLT0(bs), errorMessage(b, "LT0"));
56+
}
57+
58+
public static void main(String[] args) {
59+
for (int mask = 0; mask <= 0xFF; mask++) {
60+
setMask(mask);
61+
for (int i = 0; i < 20_000; i++) {
62+
testValue((byte) i);
63+
}
64+
}
65+
System.out.println("TEST PASSED");
66+
}
67+
68+
static String errorMessage(byte b, String type) {
69+
return String.format("%s: val=0x%x mask=0x%x", type, b, mask());
70+
}
71+
72+
// Mutable mask as a compile-time constant.
73+
74+
private static final CallSite MASK_CS = new MutableCallSite(MethodType.methodType(int.class));
75+
private static final MethodHandle MASK_MH = MASK_CS.dynamicInvoker();
76+
77+
static int mask() {
78+
try {
79+
return (int) MASK_MH.invokeExact();
80+
} catch (Throwable t) {
81+
throw new InternalError(t); // should NOT happen
82+
}
83+
}
84+
85+
static void setMask(int mask) {
86+
MethodHandle constant = MethodHandles.constant(int.class, mask);
87+
MASK_CS.setTarget(constant);
88+
}
89+
}

0 commit comments

Comments
 (0)