-
Notifications
You must be signed in to change notification settings - Fork 129
/
XxHash64.java
324 lines (264 loc) · 8.8 KB
/
XxHash64.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.airlift.slice;
import java.io.IOException;
import java.io.InputStream;
import static io.airlift.slice.JvmUtils.unsafe;
import static java.lang.Long.rotateLeft;
import static java.lang.Math.min;
import static java.util.Objects.checkFromIndexSize;
import static sun.misc.Unsafe.ARRAY_BYTE_BASE_OFFSET;
public final class XxHash64
{
private static final long PRIME64_1 = 0x9E3779B185EBCA87L;
private static final long PRIME64_2 = 0xC2B2AE3D27D4EB4FL;
private static final long PRIME64_3 = 0x165667B19E3779F9L;
private static final long PRIME64_4 = 0x85EBCA77C2b2AE63L;
private static final long PRIME64_5 = 0x27D4EB2F165667C5L;
private static final long DEFAULT_SEED = 0;
private final long seed;
private static final long BUFFER_ADDRESS = ARRAY_BYTE_BASE_OFFSET;
private final byte[] buffer = new byte[32];
private int bufferSize;
private long bodyLength;
private long v1;
private long v2;
private long v3;
private long v4;
public XxHash64()
{
this(DEFAULT_SEED);
}
public XxHash64(long seed)
{
this.seed = seed;
this.v1 = seed + PRIME64_1 + PRIME64_2;
this.v2 = seed + PRIME64_2;
this.v3 = seed;
this.v4 = seed - PRIME64_1;
}
public XxHash64 update(byte[] data)
{
return update(data, 0, data.length);
}
public XxHash64 update(byte[] data, int offset, int length)
{
checkFromIndexSize(offset, length, data.length);
updateHash(data, ARRAY_BYTE_BASE_OFFSET + offset, length);
return this;
}
public XxHash64 update(Slice data)
{
return update(data, 0, data.length());
}
public XxHash64 update(Slice data, int offset, int length)
{
checkFromIndexSize(offset, length, data.length());
updateHash(data.byteArray(), (long) data.byteArrayOffset() + ARRAY_BYTE_BASE_OFFSET + offset, length);
return this;
}
public long hash()
{
long hash;
if (bodyLength > 0) {
hash = computeBody();
}
else {
hash = seed + PRIME64_5;
}
hash += bodyLength + bufferSize;
return updateTail(hash, buffer, BUFFER_ADDRESS, 0, bufferSize);
}
private long computeBody()
{
long hash = rotateLeft(v1, 1) + rotateLeft(v2, 7) + rotateLeft(v3, 12) + rotateLeft(v4, 18);
hash = update(hash, v1);
hash = update(hash, v2);
hash = update(hash, v3);
hash = update(hash, v4);
return hash;
}
private void updateHash(byte[] base, long address, int length)
{
if (bufferSize > 0) {
int available = min(32 - bufferSize, length);
unsafe.copyMemory(base, address, buffer, BUFFER_ADDRESS + bufferSize, available);
bufferSize += available;
address += available;
length -= available;
if (bufferSize == 32) {
updateBody(buffer, BUFFER_ADDRESS, bufferSize);
bufferSize = 0;
}
}
if (length >= 32) {
int index = updateBody(base, address, length);
address += index;
length -= index;
}
if (length > 0) {
unsafe.copyMemory(base, address, buffer, BUFFER_ADDRESS, length);
bufferSize = length;
}
}
private int updateBody(byte[] base, long address, int length)
{
int remaining = length;
while (remaining >= 32) {
v1 = mix(v1, unsafe.getLong(base, address));
v2 = mix(v2, unsafe.getLong(base, address + 8));
v3 = mix(v3, unsafe.getLong(base, address + 16));
v4 = mix(v4, unsafe.getLong(base, address + 24));
address += 32;
remaining -= 32;
}
int index = length - remaining;
bodyLength += index;
return index;
}
public static long hash(long value)
{
return hash(DEFAULT_SEED, value);
}
public static long hash(long seed, long value)
{
long hash = seed + PRIME64_5 + SizeOf.SIZE_OF_LONG;
hash = updateTail(hash, value);
hash = finalShuffle(hash);
return hash;
}
public static long hash(InputStream in)
throws IOException
{
return hash(DEFAULT_SEED, in);
}
public static long hash(long seed, InputStream in)
throws IOException
{
XxHash64 hash = new XxHash64(seed);
byte[] buffer = new byte[8192];
while (true) {
int length = in.read(buffer);
if (length == -1) {
break;
}
hash.update(buffer, 0, length);
}
return hash.hash();
}
public static long hash(Slice data)
{
return hash(data, 0, data.length());
}
public static long hash(long seed, Slice data)
{
return hash(seed, data, 0, data.length());
}
public static long hash(Slice data, int offset, int length)
{
return hash(DEFAULT_SEED, data, offset, length);
}
public static long hash(long seed, Slice data, int offset, int length)
{
checkFromIndexSize(offset, length, data.length());
byte[] base = data.byteArray();
final long address = (long) data.byteArrayOffset() + ARRAY_BYTE_BASE_OFFSET + offset;
long hash;
if (length >= 32) {
hash = updateBody(seed, base, address, length);
}
else {
hash = seed + PRIME64_5;
}
hash += length;
// round to the closest 32 byte boundary
// this is the point up to which updateBody() processed
int index = length & 0xFFFFFFE0;
return updateTail(hash, base, address, index, length);
}
private static long updateTail(long hash, byte[] base, long address, int index, int length)
{
while (index <= length - 8) {
hash = updateTail(hash, unsafe.getLong(base, address + index));
index += 8;
}
if (index <= length - 4) {
hash = updateTail(hash, unsafe.getInt(base, address + index));
index += 4;
}
while (index < length) {
hash = updateTail(hash, unsafe.getByte(base, address + index));
index++;
}
hash = finalShuffle(hash);
return hash;
}
private static long updateBody(long seed, byte[] base, long address, int length)
{
long v1 = seed + PRIME64_1 + PRIME64_2;
long v2 = seed + PRIME64_2;
long v3 = seed;
long v4 = seed - PRIME64_1;
int remaining = length;
while (remaining >= 32) {
v1 = mix(v1, unsafe.getLong(base, address));
v2 = mix(v2, unsafe.getLong(base, address + 8));
v3 = mix(v3, unsafe.getLong(base, address + 16));
v4 = mix(v4, unsafe.getLong(base, address + 24));
address += 32;
remaining -= 32;
}
long hash = rotateLeft(v1, 1) + rotateLeft(v2, 7) + rotateLeft(v3, 12) + rotateLeft(v4, 18);
hash = update(hash, v1);
hash = update(hash, v2);
hash = update(hash, v3);
hash = update(hash, v4);
return hash;
}
private static long mix(long current, long value)
{
return rotateLeft(current + value * PRIME64_2, 31) * PRIME64_1;
}
private static long update(long hash, long value)
{
long temp = hash ^ mix(0, value);
return temp * PRIME64_1 + PRIME64_4;
}
private static long updateTail(long hash, long value)
{
long temp = hash ^ mix(0, value);
return rotateLeft(temp, 27) * PRIME64_1 + PRIME64_4;
}
private static long updateTail(long hash, int value)
{
long unsigned = value & 0xFFFF_FFFFL;
long temp = hash ^ (unsigned * PRIME64_1);
return rotateLeft(temp, 23) * PRIME64_2 + PRIME64_3;
}
private static long updateTail(long hash, byte value)
{
int unsigned = value & 0xFF;
long temp = hash ^ (unsigned * PRIME64_5);
return rotateLeft(temp, 11) * PRIME64_1;
}
private static long finalShuffle(long hash)
{
hash ^= hash >>> 33;
hash *= PRIME64_2;
hash ^= hash >>> 29;
hash *= PRIME64_3;
hash ^= hash >>> 32;
return hash;
}
}