-
Notifications
You must be signed in to change notification settings - Fork 0
/
MurmurHashV3.java
369 lines (303 loc) · 7.31 KB
/
MurmurHashV3.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package ie.ucd.murmur;
/**
* Murmur hash 3.0.
*
* The Murmur hash is a relative fast hash function from
* https://code.google.com/p/smhasher/ for platforms with efficient
* multiplication.
*
* This is a re-implementation of the original C++ code plus some additional
* features.
*
* Public domain.
*
* @author Viliam Holub
* @version 1.0
*
*/
public final class MurmurHashV3 {
/**
* Generates 128 bit hash from byte array with default seed value.
*
* @param data byte array to hash
* @param length length of the array to hash
* @return 128 bit hash of the given string
*/
public static long[] hash128( final byte[] data, int length) {
return hash128( data, 0, length, 0x9ee73d188796670eL);
}
/**
* Generates 128 bit hash from a string.
*
* @param text string to hash
* @return 128 bit hash of the given string
*/
public static long hash128_64( final String text) {
return hash128_64( text, 0, text.length(), 0x9ee73d188796670eL);
}
/**
* Generates 128 bit hash from a string.
*
* @param text string to hash
* @param salt initial salt
* @return 128 bit hash of the given string
*/
public static long hash128_64( final String text, long salt) {
return hash128_64( text, 0, text.length(), salt);
}
static int getblock32( byte[] data, int offset, int index) {
int i4 = offset + index * 4;
return (data[ i4 + 0] & 0xff) + ((data[ i4 + 1] & 0xff) << 8)
+ ((data[ i4 + 2] & 0xff) << 16)
+ ((data[ i4 + 3] & 0xff) << 24);
}
static long getblock( byte[] data, int offset, int index) {
int i8 = offset + index / 8;
return ((long) data[ i8 + 0] & 0xff)
+ (((long) data[ i8 + 1] & 0xff) << 8)
+ (((long) data[ i8 + 2] & 0xff) << 16)
+ (((long) data[ i8 + 3] & 0xff) << 24)
+ (((long) data[ i8 + 4] & 0xff) << 32)
+ (((long) data[ i8 + 5] & 0xff) << 40)
+ (((long) data[ i8 + 6] & 0xff) << 48)
+ (((long) data[ i8 + 7] & 0xff) << 56);
}
static long getblock( String data, int offset, int index) {
int i8 = offset + index / 8 / 2;
return ((long) data.charAt( i8 + 0) & 0xffff)
+ (((long) data.charAt( i8 + 1) & 0xffff) << 16)
+ (((long) data.charAt( i8 + 2) & 0xffff) << 32)
+ (((long) data.charAt( i8 + 3) & 0xffff) << 48);
}
/**
* Rotate left.
*
* @param val value
* @param s shift
* @return value rotated left by {@code s}
*/
static int rotl32( int val, int s) {
return ((val << s) | (val >>> (32 - s)));
}
/**
* Rotate left.
*
* @param val value
* @param s shift
* @return value rotated left by {@code s}
*/
static long rotl64( long val, int s) {
return ((val << s) | (val >>> (64 - s)));
}
/**
* Finalization mix - force all bits of a hash block to avalanche.
*
* @param h value
* @return hashed value
*/
public static long fmix( long h) {
h ^= h >>> 33;
h *= 0xff51afd7ed558ccdL;
h ^= h >>> 33;
h *= 0xc4ceb9fe1a85ec53L;
h ^= h >>> 33;
return h;
}
public static int hash32( byte[] key, int offset, int length, int seed) {
final int nblocks = length / 4;
int h1 = seed;
int c1 = 0xcc9e2d51;
int c2 = 0x1b873593;
// Body
for (int i = 0; i < nblocks; i++) {
int k1 = getblock32( key, offset, i);
k1 *= c1;
k1 = rotl32( k1, 15);
k1 *= c2;
h1 ^= k1;
h1 = rotl32( h1, 13);
h1 = h1 * 5 + 0xe6546b64;
}
// Tail
int tail = offset + nblocks * 4;
int k1 = 0;
switch (length & 3) {
case 3:
k1 ^= ((int) key[ tail + 2]) << 16;
case 2:
k1 ^= ((int) key[ tail + 1]) << 8;
case 1:
k1 ^= ((int) key[ tail]);
k1 *= c1;
k1 = rotl32( k1, 15);
k1 *= c2;
h1 ^= k1;
};
// Finalization
h1 ^= length;
return (int) fmix( h1);
}
/**
* Murmur hash 128 bits.
*
* @param data byte array of data
* @param offset starting offset
* @param length length of data
* @param seed initial seed
* @return hash in two longs
*/
public static long[] hash128( byte[] data, int offset, int length, long seed) {
final int nblocks = length / 16; // Process as 128-bit blocks.
long h1 = seed;
long h2 = seed;
long c1 = 0x87c37b91114253d5L;
long c2 = 0x4cf5ad432745937fL;
// Body
for (int i = 0; i < nblocks; i++) {
long k1 = getblock( data, offset, i * 2 + 0);
long k2 = getblock( data, offset, i * 2 + 1);
k1 *= c1;
k1 = rotl64( k1, 31);
k1 *= c2;
h1 ^= k1;
h1 = rotl64( h1, 27);
h1 += h2;
h1 = h1 * 5 + 0x52dce729;
k2 *= c2;
k2 = rotl64( k2, 33);
k2 *= c1;
h2 ^= k2;
h2 = rotl64( h2, 31);
h2 += h1;
h2 = h2 * 5 + 0x38495ab5;
}
// Tail
int tail = offset + nblocks * 16;
long k1 = 0;
long k2 = 0;
switch (length & 15) {
case 15:
k2 ^= ((long) data[ tail + 14]) << 48;
case 14:
k2 ^= ((long) data[ tail + 13]) << 40;
case 13:
k2 ^= ((long) data[ tail + 12]) << 32;
case 12:
k2 ^= ((long) data[ tail + 11]) << 24;
case 11:
k2 ^= ((long) data[ tail + 10]) << 16;
case 10:
k2 ^= ((long) data[ tail + 9]) << 8;
case 9:
k2 ^= ((long) data[ tail + 8]) << 0;
k2 *= c2;
k2 = rotl64( k2, 33);
k2 *= c1;
h2 ^= k2;
case 8:
k1 ^= ((long) data[ tail + 7]) << 56;
case 7:
k1 ^= ((long) data[ tail + 6]) << 48;
case 6:
k1 ^= ((long) data[ tail + 5]) << 40;
case 5:
k1 ^= ((long) data[ tail + 4]) << 32;
case 4:
k1 ^= ((long) data[ tail + 3]) << 24;
case 3:
k1 ^= ((long) data[ tail + 2]) << 16;
case 2:
k1 ^= ((long) data[ tail + 1]) << 8;
case 1:
k1 ^= ((long) data[ tail]);
k1 *= c1;
k1 = rotl64( k1, 31);
k1 *= c2;
h1 ^= k1;
};
// Finalization
h1 ^= length;
h2 ^= length;
h1 += h2;
h2 += h1;
h1 = fmix( h1);
h2 = fmix( h2);
h1 += h2;
h2 += h1;
return new long[] { h1, h2};
}
/**
* Generates 128 bit hash from a substring.
*
* @param text string to hash
* @param from starting index
* @param length length of the substring to hash
* @param seed seed
* @return 128 bit hash of the given array
*/
public static long hash128_64( final String text, int from, int length,
long seed) {
final int nblocks = length * 2 / 16; // Process as 128-bit blocks.
long h1 = seed;
long h2 = seed;
long c1 = 0x87c37b91114253d5L;
long c2 = 0x4cf5ad432745937fL;
// Body
for (int i = 0; i < nblocks; i++) {
long k1 = getblock( text, from, i * 2 + 0);
long k2 = getblock( text, from, i * 2 + 1);
k1 *= c1;
k1 = rotl64( k1, 31);
k1 *= c2;
h1 ^= k1;
h1 = rotl64( h1, 27);
h1 += h2;
h1 = h1 * 5 + 0x52dce729;
k2 *= c2;
k2 = rotl64( k2, 33);
k2 *= c1;
h2 ^= k2;
h2 = rotl64( h2, 31);
h2 += h1;
h2 = h2 * 5 + 0x38495ab5;
}
// Tail
final int tail = from + (nblocks * 16 / 2);
long k1 = 0;
long k2 = 0;
switch (length & (15 / 2)) {
case 7:
k2 ^= ((long) text.charAt( tail + 6)) << 32;
case 6:
k2 ^= ((long) text.charAt( tail + 5)) << 16;
case 5:
k2 ^= ((long) text.charAt( tail + 4)) << 0;
k2 *= c2;
k2 = rotl64( k2, 33);
k2 *= c1;
h2 ^= k2;
case 4:
k1 ^= ((long) text.charAt( tail + 3)) << 48;
case 3:
k1 ^= ((long) text.charAt( tail + 2)) << 32;
case 2:
k1 ^= ((long) text.charAt( tail + 1)) << 16;
case 1:
k1 ^= ((long) text.charAt( tail)) << 0;
k1 *= c1;
k1 = rotl64( k1, 31);
k1 *= c2;
h1 ^= k1;
};
// Finalization
h1 ^= length * 2;
h2 ^= length * 2;
h1 += h2;
h2 += h1;
h1 = fmix( h1);
h2 = fmix( h2);
h1 += h2;
h2 += h1;
return h1 ^ h2;
}
}