-
Notifications
You must be signed in to change notification settings - Fork 0
/
Murmur3.java
308 lines (230 loc) · 7.76 KB
/
Murmur3.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
package com.hashfunctions.murmur;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class Murmur3 extends MurmurConstants {
private static final int X86_32_C1 = 0xcc9e2d51;
private static final int X86_32_C2 = 0x1b873593;
private static long X64_128_C1 = 0x87c37b91114253d5L;
private static long X64_128_C2 = 0x4cf5ad432745937fL;
public static long hash_32(final byte[] data, int length, long seed) {
final int nblocks = length >> 2;
long hash = seed;
//----------
// body
for(int i = 0; i < nblocks; i++) {
final int i4 = i << 2;
long k1 = (data[i4] & UNSIGNED_MASK);
k1 |= (data[i4 + 1] & UNSIGNED_MASK) << 8;
k1 |= (data[i4 + 2] & UNSIGNED_MASK) << 16;
k1 |= (data[i4 + 3] & UNSIGNED_MASK) << 24;
// int k1 = (data[i4] & 0xff) + ((data[i4 + 1] & 0xff) << 8) + ((data[i4 + 2] & 0xff) << 16) + ((data[i4 + 3] & 0xff) << 24);
k1 = (k1 * X86_32_C1) & UINT_MASK;
k1 = rotl32(k1, 15);
k1 = (k1 * X86_32_C2) & UINT_MASK;
hash ^= k1;
hash = rotl32(hash,13);
hash = (((hash * 5) & UINT_MASK) + 0xe6546b64l) & UINT_MASK;
}
//----------
// tail
// Advance offset to the unprocessed tail of the data.
int offset = (nblocks << 2); // nblocks * 2;
long k1 = 0;
switch (length & 3) {
case 3:
k1 ^= (data[offset + 2] << 16) & UINT_MASK;
case 2:
k1 ^= (data[offset + 1] << 8) & UINT_MASK;
case 1:
k1 ^= data[offset];
k1 = (k1 * X86_32_C1) & UINT_MASK;
k1 = rotl32(k1, 15);
k1 = (k1 * X86_32_C2) & UINT_MASK;
hash ^= k1;
}
// ----------
// finalization
hash ^= length;
hash = fmix32(hash);
return hash;
}
public static long hash_32(String data, long seed) {
int length = data.length();
final int nblocks = length >> 2;
long hash = seed;
//----------
// body
for(int i = 0; i < nblocks; i++) {
final int i4 = i << 2;
long k1 = (data.charAt(i4) & UNSIGNED_MASK);
k1 |= (data.charAt(i4 + 1) & UNSIGNED_MASK) << 8;
k1 |= (data.charAt(i4 + 2) & UNSIGNED_MASK) << 16;
k1 |= (data.charAt(i4 + 3) & UNSIGNED_MASK) << 24;
// int k1 = (data[i4] & 0xff) + ((data[i4 + 1] & 0xff) << 8) + ((data[i4 + 2] & 0xff) << 16) + ((data[i4 + 3] & 0xff) << 24);
k1 = (k1 * X86_32_C1) & UINT_MASK;
k1 = rotl32(k1, 15);
k1 = (k1 * X86_32_C2) & UINT_MASK;
hash ^= k1;
hash = rotl32(hash,13);
hash = (((hash * 5) & UINT_MASK) + 0xe6546b64l) & UINT_MASK;
}
//----------
// tail
// Advance offset to the unprocessed tail of the data.
int offset = (nblocks << 2); // nblocks * 2;
long k1 = 0;
switch (length & 3) {
case 3:
k1 ^= (data.charAt(offset + 2) << 16) & UINT_MASK;
case 2:
k1 ^= (data.charAt(offset + 1) << 8) & UINT_MASK;
case 1:
k1 ^= data.charAt(offset);
k1 = (k1 * X86_32_C1) & UINT_MASK;
k1 = rotl32(k1, 15);
k1 = (k1 * X86_32_C2) & UINT_MASK;
hash ^= k1;
}
// ----------
// finalization
hash ^= length;
hash = fmix32(hash);
return hash;
}
public static long[] hash_128(final byte[] data, final int length, final long seed) {
long h1 = seed;
long h2 = seed;
ByteBuffer buffer = ByteBuffer.wrap(data);
buffer.order(ByteOrder.LITTLE_ENDIAN);
while(buffer.remaining() >= 16) {
long k1 = buffer.getLong();
long k2 = buffer.getLong();
h1 ^= mixK1(k1);
h1 = Long.rotateLeft(h1, 27);
h1 += h2;
h1 = h1 * 5 + 0x52dce729;
h2 ^= mixK2(k2);
h2 = Long.rotateLeft(h2, 31);
h2 += h1;
h2 = h2 * 5 + 0x38495ab5;
}
// ----------
// tail
// Advance offset to the unprocessed tail of the data.
// offset += (nblocks << 4); // nblocks * 16;
buffer.compact();
buffer.flip();
final int remaining = buffer.remaining();
if(remaining > 0) {
long k1 = 0;
long k2 = 0;
switch (buffer.remaining()) {
case 15:
k2 ^= (long) (buffer.get(14) & UNSIGNED_MASK) << 48;
case 14:
k2 ^= (long) (buffer.get(13) & UNSIGNED_MASK) << 40;
case 13:
k2 ^= (long) (buffer.get(12) & UNSIGNED_MASK) << 32;
case 12:
k2 ^= (long) (buffer.get(11) & UNSIGNED_MASK) << 24;
case 11:
k2 ^= (long) (buffer.get(10) & UNSIGNED_MASK) << 16;
case 10:
k2 ^= (long) (buffer.get(9) & UNSIGNED_MASK) << 8;
case 9:
k2 ^= (long) (buffer.get(8) & UNSIGNED_MASK);
case 8:
k1 ^= buffer.getLong();
break;
case 7:
k1 ^= (long) (buffer.get(6) & UNSIGNED_MASK) << 48;
case 6:
k1 ^= (long) (buffer.get(5) & UNSIGNED_MASK) << 40;
case 5:
k1 ^= (long) (buffer.get(4) & UNSIGNED_MASK) << 32;
case 4:
k1 ^= (long) (buffer.get(3) & UNSIGNED_MASK) << 24;
case 3:
k1 ^= (long) (buffer.get(2) & UNSIGNED_MASK) << 16;
case 2:
k1 ^= (long) (buffer.get(1) & UNSIGNED_MASK) << 8;
case 1:
k1 ^= (long) (buffer.get(0) & UNSIGNED_MASK);
break;
default:
throw new AssertionError("Code should not reach here!");
}
// mix
h1 ^= mixK1(k1);
h2 ^= mixK2(k2);
}
// ----------
// finalization
h1 ^= length;
h2 ^= length;
h1 += h2;
h2 += h1;
h1 = fmix64(h1);
h2 = fmix64(h2);
h1 += h2;
h2 += h1;
return (new long[] { h1, h2 });
}
private static long mixK1(long k1) {
k1 *= X64_128_C1;
k1 = Long.rotateLeft(k1, 31);
k1 *= X64_128_C2;
return k1;
}
private static long mixK2(long k2) {
k2 *= X64_128_C2;
k2 = Long.rotateLeft(k2, 33);
k2 *= X64_128_C1;
return k2;
}
/**
* Rotate left for 32 bits.
*
* @param original
* @param shift
* @return
*/
private static long rotl32(long original, int shift) {
return ((original << shift) & UINT_MASK) | ((original >>> (32 - shift)) & UINT_MASK);
}
/**
* Rotate left for 64 bits.
*
* @param original
* @param shift
* @return
*/
/**
* fmix function for 32 bits.
*
* @param h
* @return
*/
private static long fmix32(long h) {
h ^= (h >> 16) & UINT_MASK;
h = (h * 0x85ebca6bl) & UINT_MASK;
h ^= (h >> 13) & UINT_MASK;
h = (h * 0xc2b2ae35) & UINT_MASK;
h ^= (h >> 16) & UINT_MASK;
return h;
}
/**
* fmix function for 64 bits.
*
* @param k
* @return
*/
private static long fmix64(long k) {
k ^= k >>> 33;
k *= 0xff51afd7ed558ccdL;
k ^= k >>> 33;
k *= 0xc4ceb9fe1a85ec53L;
k ^= k >>> 33;
return k;
}
}