28
28
import jdk .internal .misc .ScopedMemoryAccess ;
29
29
import jdk .internal .util .Architecture ;
30
30
import jdk .internal .util .ArraysSupport ;
31
- import jdk .internal .util .ByteArrayLittleEndian ;
32
31
import jdk .internal .vm .annotation .ForceInline ;
33
32
import jdk .internal .vm .annotation .Stable ;
34
33
@@ -50,6 +49,7 @@ public final class SegmentBulkOperations {
50
49
private SegmentBulkOperations () {}
51
50
52
51
private static final ScopedMemoryAccess SCOPED_MEMORY_ACCESS = ScopedMemoryAccess .getScopedMemoryAccess ();
52
+ private static final long LONG_MASK = ~7L ; // The last three bits are zero
53
53
54
54
// All the threshold values below MUST be a power of two and should preferably be
55
55
// greater or equal to 2^3.
@@ -75,21 +75,21 @@ public static MemorySegment fill(AbstractMemorySegmentImpl dst, byte value) {
75
75
int offset = 0 ;
76
76
// 0...0X...X000
77
77
final int limit = (int ) (dst .length & (NATIVE_THRESHOLD_FILL - 8 ));
78
- for (; offset < limit ; offset += 8 ) {
78
+ for (; offset < limit ; offset += Long . BYTES ) {
79
79
SCOPED_MEMORY_ACCESS .putLongUnaligned (dst .sessionImpl (), dst .unsafeGetBase (), dst .unsafeGetOffset () + offset , longValue , !Architecture .isLittleEndian ());
80
80
}
81
81
int remaining = (int ) dst .length - limit ;
82
82
// 0...0X00
83
- if (remaining >= 4 ) {
83
+ if (remaining >= Integer . BYTES ) {
84
84
SCOPED_MEMORY_ACCESS .putIntUnaligned (dst .sessionImpl (), dst .unsafeGetBase (), dst .unsafeGetOffset () + offset , (int ) longValue , !Architecture .isLittleEndian ());
85
- offset += 4 ;
86
- remaining -= 4 ;
85
+ offset += Integer . BYTES ;
86
+ remaining -= Integer . BYTES ;
87
87
}
88
88
// 0...00X0
89
- if (remaining >= 2 ) {
89
+ if (remaining >= Short . BYTES ) {
90
90
SCOPED_MEMORY_ACCESS .putShortUnaligned (dst .sessionImpl (), dst .unsafeGetBase (), dst .unsafeGetOffset () + offset , (short ) longValue , !Architecture .isLittleEndian ());
91
- offset += 2 ;
92
- remaining -= 2 ;
91
+ offset += Short . BYTES ;
92
+ remaining -= Short . BYTES ;
93
93
}
94
94
// 0...000X
95
95
if (remaining == 1 ) {
@@ -123,26 +123,26 @@ public static void copy(AbstractMemorySegmentImpl src, long srcOffset,
123
123
// is an overlap, we could tolerate one particular direction of overlap (but not the other).
124
124
125
125
// 0...0X...X000
126
- final int limit = (int ) (size & (NATIVE_THRESHOLD_COPY - 8 ));
126
+ final int limit = (int ) (size & (NATIVE_THRESHOLD_COPY - Long . BYTES ));
127
127
int offset = 0 ;
128
- for (; offset < limit ; offset += 8 ) {
128
+ for (; offset < limit ; offset += Long . BYTES ) {
129
129
final long v = SCOPED_MEMORY_ACCESS .getLongUnaligned (src .sessionImpl (), src .unsafeGetBase (), src .unsafeGetOffset () + srcOffset + offset , !Architecture .isLittleEndian ());
130
130
SCOPED_MEMORY_ACCESS .putLongUnaligned (dst .sessionImpl (), dst .unsafeGetBase (), dst .unsafeGetOffset () + dstOffset + offset , v , !Architecture .isLittleEndian ());
131
131
}
132
132
int remaining = (int ) size - offset ;
133
133
// 0...0X00
134
- if (remaining >= 4 ) {
134
+ if (remaining >= Integer . BYTES ) {
135
135
final int v = SCOPED_MEMORY_ACCESS .getIntUnaligned (src .sessionImpl (), src .unsafeGetBase (),src .unsafeGetOffset () + srcOffset + offset , !Architecture .isLittleEndian ());
136
136
SCOPED_MEMORY_ACCESS .putIntUnaligned (dst .sessionImpl (), dst .unsafeGetBase (), dst .unsafeGetOffset () + dstOffset + offset , v , !Architecture .isLittleEndian ());
137
- offset += 4 ;
138
- remaining -= 4 ;
137
+ offset += Integer . BYTES ;
138
+ remaining -= Integer . BYTES ;
139
139
}
140
140
// 0...00X0
141
- if (remaining >= 2 ) {
141
+ if (remaining >= Short . BYTES ) {
142
142
final short v = SCOPED_MEMORY_ACCESS .getShortUnaligned (src .sessionImpl (), src .unsafeGetBase (), src .unsafeGetOffset () + srcOffset + offset , !Architecture .isLittleEndian ());
143
143
SCOPED_MEMORY_ACCESS .putShortUnaligned (dst .sessionImpl (), dst .unsafeGetBase (), dst .unsafeGetOffset () + dstOffset + offset , v , !Architecture .isLittleEndian ());
144
- offset += 2 ;
145
- remaining -=2 ;
144
+ offset += Short . BYTES ;
145
+ remaining -= Short . BYTES ;
146
146
}
147
147
// 0...000X
148
148
if (remaining == 1 ) {
@@ -202,9 +202,9 @@ public static int contentHash(AbstractMemorySegmentImpl segment, long fromOffset
202
202
return 1 ;
203
203
}
204
204
int result = 1 ;
205
- final long longBytes = length & (( 1L << 62 ) - 8 ) ;
205
+ final long longBytes = length & LONG_MASK ;
206
206
final long limit = fromOffset + longBytes ;
207
- for (; fromOffset < limit ; fromOffset += 8 ) {
207
+ for (; fromOffset < limit ; fromOffset += Long . BYTES ) {
208
208
long val = SCOPED_MEMORY_ACCESS .getLongUnaligned (segment .sessionImpl (), segment .unsafeGetBase (), segment .unsafeGetOffset () + fromOffset , !Architecture .isLittleEndian ());
209
209
result = result * POWERS_OF_31 [7 ]
210
210
+ ((byte ) (val >>> 56 )) * POWERS_OF_31 [6 ]
@@ -218,24 +218,24 @@ public static int contentHash(AbstractMemorySegmentImpl segment, long fromOffset
218
218
}
219
219
int remaining = (int ) (length - longBytes );
220
220
// 0...0X00
221
- if (remaining >= 4 ) {
221
+ if (remaining >= Integer . BYTES ) {
222
222
int val = SCOPED_MEMORY_ACCESS .getIntUnaligned (segment .sessionImpl (), segment .unsafeGetBase (), segment .unsafeGetOffset () + fromOffset , !Architecture .isLittleEndian ());
223
223
result = result * POWERS_OF_31 [3 ]
224
224
+ ((byte ) (val >>> 24 )) * POWERS_OF_31 [2 ]
225
225
+ ((byte ) (val >>> 16 )) * POWERS_OF_31 [1 ]
226
226
+ ((byte ) (val >>> 8 )) * POWERS_OF_31 [0 ]
227
227
+ ((byte ) val );
228
- fromOffset += 4 ;
229
- remaining -= 4 ;
228
+ fromOffset += Integer . BYTES ;
229
+ remaining -= Integer . BYTES ;
230
230
}
231
231
// 0...00X0
232
- if (remaining >= 2 ) {
232
+ if (remaining >= Short . BYTES ) {
233
233
short val = SCOPED_MEMORY_ACCESS .getShortUnaligned (segment .sessionImpl (), segment .unsafeGetBase (), segment .unsafeGetOffset () + fromOffset , !Architecture .isLittleEndian ());
234
234
result = result * POWERS_OF_31 [1 ]
235
235
+ ((byte ) (val >>> 8 )) * POWERS_OF_31 [0 ]
236
236
+ ((byte ) val );
237
- fromOffset += 2 ;
238
- remaining -= 2 ;
237
+ fromOffset += Short . BYTES ;
238
+ remaining -= Short . BYTES ;
239
239
}
240
240
// 0...000X
241
241
if (remaining == 1 ) {
@@ -288,7 +288,7 @@ private static long mismatch(AbstractMemorySegmentImpl src, long srcFromOffset,
288
288
long start , int length , boolean srcAndDstBytesDiffer ) {
289
289
int offset = 0 ;
290
290
final int limit = length & (NATIVE_THRESHOLD_MISMATCH - 8 );
291
- for (; offset < limit ; offset += 8 ) {
291
+ for (; offset < limit ; offset += Long . BYTES ) {
292
292
final long s = SCOPED_MEMORY_ACCESS .getLongUnaligned (src .sessionImpl (), src .unsafeGetBase (), src .unsafeGetOffset () + srcFromOffset + offset , false );
293
293
final long d = SCOPED_MEMORY_ACCESS .getLongUnaligned (dst .sessionImpl (), dst .unsafeGetBase (), dst .unsafeGetOffset () + dstFromOffset + offset , false );
294
294
if (s != d ) {
@@ -298,24 +298,24 @@ private static long mismatch(AbstractMemorySegmentImpl src, long srcFromOffset,
298
298
int remaining = length - offset ;
299
299
300
300
// 0...0X00
301
- if (remaining >= 4 ) {
301
+ if (remaining >= Integer . BYTES ) {
302
302
final int s = SCOPED_MEMORY_ACCESS .getIntUnaligned (src .sessionImpl (), src .unsafeGetBase (), src .unsafeGetOffset () + srcFromOffset + offset , false );
303
303
final int d = SCOPED_MEMORY_ACCESS .getIntUnaligned (dst .sessionImpl (), dst .unsafeGetBase (), dst .unsafeGetOffset () + dstFromOffset + offset , false );
304
304
if (s != d ) {
305
305
return start + offset + mismatch (s , d );
306
306
}
307
- offset += 4 ;
308
- remaining -= 4 ;
307
+ offset += Integer . BYTES ;
308
+ remaining -= Integer . BYTES ;
309
309
}
310
310
// 0...00X0
311
- if (remaining >= 2 ) {
311
+ if (remaining >= Short . BYTES ) {
312
312
final short s = SCOPED_MEMORY_ACCESS .getShortUnaligned (src .sessionImpl (), src .unsafeGetBase (), src .unsafeGetOffset () + srcFromOffset + offset , false );
313
313
final short d = SCOPED_MEMORY_ACCESS .getShortUnaligned (dst .sessionImpl (), dst .unsafeGetBase (), dst .unsafeGetOffset () + dstFromOffset + offset , false );
314
314
if (s != d ) {
315
315
return start + offset + mismatch (s , d );
316
316
}
317
- offset += 2 ;
318
- remaining -= 2 ;
317
+ offset += Short . BYTES ;
318
+ remaining -= Short . BYTES ;
319
319
}
320
320
// 0...000X
321
321
if (remaining == 1 ) {
0 commit comments