Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/java/nio/BufferMismatch.java
41152 views
1
/*
2
* Copyright (c) 2017, 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
package java.nio;
26
27
import jdk.internal.misc.ScopedMemoryAccess;
28
import jdk.internal.util.ArraysSupport;
29
30
/**
31
* Mismatch methods for buffers
32
*/
33
final class BufferMismatch {
34
35
static final ScopedMemoryAccess SCOPED_MEMORY_ACCESS = ScopedMemoryAccess.getScopedMemoryAccess();
36
37
static int mismatch(ByteBuffer a, int aOff, ByteBuffer b, int bOff, int length) {
38
int i = 0;
39
if (length > 7) {
40
if (a.get(aOff) != b.get(bOff))
41
return 0;
42
i = SCOPED_MEMORY_ACCESS.vectorizedMismatch(a.scope(), b.scope(),
43
a.base(), a.address + aOff,
44
b.base(), b.address + bOff,
45
length,
46
ArraysSupport.LOG2_ARRAY_BYTE_INDEX_SCALE);
47
if (i >= 0) return i;
48
i = length - ~i;
49
}
50
for (; i < length; i++) {
51
if (a.get(aOff + i) != b.get(bOff + i))
52
return i;
53
}
54
return -1;
55
}
56
57
static int mismatch(CharBuffer a, int aOff, CharBuffer b, int bOff, int length) {
58
int i = 0;
59
// Ensure only heap or off-heap buffer instances use the
60
// vectorized mismatch. If either buffer is a StringCharBuffer
61
// (order is null) then the slow path is taken
62
if (length > 3 && a.charRegionOrder() == b.charRegionOrder()
63
&& a.charRegionOrder() != null && b.charRegionOrder() != null) {
64
if (a.get(aOff) != b.get(bOff))
65
return 0;
66
i = SCOPED_MEMORY_ACCESS.vectorizedMismatch(a.scope(), b.scope(),
67
a.base(), a.address + (aOff << ArraysSupport.LOG2_ARRAY_CHAR_INDEX_SCALE),
68
b.base(), b.address + (bOff << ArraysSupport.LOG2_ARRAY_CHAR_INDEX_SCALE),
69
length,
70
ArraysSupport.LOG2_ARRAY_CHAR_INDEX_SCALE);
71
if (i >= 0) return i;
72
i = length - ~i;
73
}
74
for (; i < length; i++) {
75
if (a.get(aOff + i) != b.get(bOff + i))
76
return i;
77
}
78
return -1;
79
}
80
81
static int mismatch(ShortBuffer a, int aOff, ShortBuffer b, int bOff, int length) {
82
int i = 0;
83
if (length > 3 && a.order() == b.order()) {
84
if (a.get(aOff) != b.get(bOff))
85
return 0;
86
i = SCOPED_MEMORY_ACCESS.vectorizedMismatch(a.scope(), b.scope(),
87
a.base(), a.address + (aOff << ArraysSupport.LOG2_ARRAY_SHORT_INDEX_SCALE),
88
b.base(), b.address + (bOff << ArraysSupport.LOG2_ARRAY_SHORT_INDEX_SCALE),
89
length,
90
ArraysSupport.LOG2_ARRAY_SHORT_INDEX_SCALE);
91
if (i >= 0) return i;
92
i = length - ~i;
93
}
94
for (; i < length; i++) {
95
if (a.get(aOff + i) != b.get(bOff + i))
96
return i;
97
}
98
return -1;
99
}
100
101
static int mismatch(IntBuffer a, int aOff, IntBuffer b, int bOff, int length) {
102
int i = 0;
103
if (length > 1 && a.order() == b.order()) {
104
if (a.get(aOff) != b.get(bOff))
105
return 0;
106
i = SCOPED_MEMORY_ACCESS.vectorizedMismatch(a.scope(), b.scope(),
107
a.base(), a.address + (aOff << ArraysSupport.LOG2_ARRAY_INT_INDEX_SCALE),
108
b.base(), b.address + (bOff << ArraysSupport.LOG2_ARRAY_INT_INDEX_SCALE),
109
length,
110
ArraysSupport.LOG2_ARRAY_INT_INDEX_SCALE);
111
if (i >= 0) return i;
112
i = length - ~i;
113
}
114
for (; i < length; i++) {
115
if (a.get(aOff + i) != b.get(bOff + i))
116
return i;
117
}
118
return -1;
119
}
120
121
static int mismatch(FloatBuffer a, int aOff, FloatBuffer b, int bOff, int length) {
122
int i = 0;
123
if (length > 1 && a.order() == b.order()) {
124
if (Float.floatToRawIntBits(a.get(aOff)) == Float.floatToRawIntBits(b.get(bOff))) {
125
i = SCOPED_MEMORY_ACCESS.vectorizedMismatch(a.scope(), b.scope(),
126
a.base(), a.address + (aOff << ArraysSupport.LOG2_ARRAY_FLOAT_INDEX_SCALE),
127
b.base(), b.address + (bOff << ArraysSupport.LOG2_ARRAY_FLOAT_INDEX_SCALE),
128
length,
129
ArraysSupport.LOG2_ARRAY_FLOAT_INDEX_SCALE);
130
}
131
// Mismatched
132
if (i >= 0) {
133
// Check if mismatch is not associated with two NaN values; and
134
// is not associated with +0 and -0
135
float av = a.get(aOff + i);
136
float bv = b.get(bOff + i);
137
if (av != bv && (!Float.isNaN(av) || !Float.isNaN(bv)))
138
return i;
139
140
// Fall back to slow mechanism
141
// ISSUE: Consider looping over vectorizedMismatch adjusting ranges
142
// However, requires that returned value be relative to input ranges
143
i++;
144
}
145
// Matched
146
else {
147
i = length - ~i;
148
}
149
}
150
for (; i < length; i++) {
151
float av = a.get(aOff + i);
152
float bv = b.get(bOff + i);
153
if (av != bv && (!Float.isNaN(av) || !Float.isNaN(bv)))
154
return i;
155
}
156
return -1;
157
}
158
159
static int mismatch(LongBuffer a, int aOff, LongBuffer b, int bOff, int length) {
160
int i = 0;
161
if (length > 0 && a.order() == b.order()) {
162
if (a.get(aOff) != b.get(bOff))
163
return 0;
164
i = SCOPED_MEMORY_ACCESS.vectorizedMismatch(a.scope(), b.scope(),
165
a.base(), a.address + (aOff << ArraysSupport.LOG2_ARRAY_LONG_INDEX_SCALE),
166
b.base(), b.address + (bOff << ArraysSupport.LOG2_ARRAY_LONG_INDEX_SCALE),
167
length,
168
ArraysSupport.LOG2_ARRAY_LONG_INDEX_SCALE);
169
return i >= 0 ? i : -1;
170
}
171
for (; i < length; i++) {
172
if (a.get(aOff + i) != b.get(bOff + i))
173
return i;
174
}
175
return -1;
176
}
177
178
static int mismatch(DoubleBuffer a, int aOff, DoubleBuffer b, int bOff, int length) {
179
int i = 0;
180
if (length > 0 && a.order() == b.order()) {
181
if (Double.doubleToRawLongBits(a.get(aOff)) == Double.doubleToRawLongBits(b.get(bOff))) {
182
i = SCOPED_MEMORY_ACCESS.vectorizedMismatch(a.scope(), b.scope(),
183
a.base(), a.address + (aOff << ArraysSupport.LOG2_ARRAY_DOUBLE_INDEX_SCALE),
184
b.base(), b.address + (bOff << ArraysSupport.LOG2_ARRAY_DOUBLE_INDEX_SCALE),
185
length,
186
ArraysSupport.LOG2_ARRAY_DOUBLE_INDEX_SCALE);
187
}
188
// Mismatched
189
if (i >= 0) {
190
// Check if mismatch is not associated with two NaN values; and
191
// is not associated with +0 and -0
192
double av = a.get(aOff + i);
193
double bv = b.get(bOff + i);
194
if (av != bv && (!Double.isNaN(av) || !Double.isNaN(bv)))
195
return i;
196
197
// Fall back to slow mechanism
198
// ISSUE: Consider looping over vectorizedMismatch adjusting ranges
199
// However, requires that returned value be relative to input ranges
200
i++;
201
}
202
// Matched
203
else {
204
return -1;
205
}
206
}
207
for (; i < length; i++) {
208
double av = a.get(aOff + i);
209
double bv = b.get(bOff + i);
210
if (av != bv && (!Double.isNaN(av) || !Double.isNaN(bv)))
211
return i;
212
}
213
return -1;
214
}
215
}
216
217