/
MurmurHash3.java
227 lines (193 loc) · 6.75 KB
/
MurmurHash3.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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.common.hash;
import org.elasticsearch.common.util.ByteUtils;
import java.math.BigInteger;
import java.util.Objects;
/**
* MurmurHash3 hashing functions.
*/
public enum MurmurHash3 {
;
/**
* A 128-bits hash.
*/
public static class Hash128 {
/** lower 64 bits part **/
public long h1;
/** higher 64 bits part **/
public long h2;
public byte[] getBytes() {
byte[] hash = new byte[16];
getBytes(hash, 0);
return hash;
}
public void getBytes(byte[] bytes, int offset) {
ByteUtils.writeLongBE(h1, bytes, offset);
ByteUtils.writeLongBE(h2, bytes, offset + 8);
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null || getClass() != other.getClass()) {
return false;
}
Hash128 that = (Hash128) other;
return Objects.equals(this.h1, that.h1) && Objects.equals(this.h2, that.h2);
}
@Override
public int hashCode() {
return Objects.hash(h1, h2);
}
@Override
public String toString() {
byte[] longBytes = new byte[17];
getBytes(longBytes, 1);
BigInteger bi = new BigInteger(longBytes);
return "0x" + bi.toString(16);
}
}
static class IntermediateResult {
int offset;
long h1;
long h2;
IntermediateResult(int offset, long h1, long h2) {
this.offset = offset;
this.h1 = h1;
this.h2 = h2;
}
}
private static long C1 = 0x87c37b91114253d5L;
private static long C2 = 0x4cf5ad432745937fL;
protected static long fmix(long k) {
k ^= k >>> 33;
k *= 0xff51afd7ed558ccdL;
k ^= k >>> 33;
k *= 0xc4ceb9fe1a85ec53L;
k ^= k >>> 33;
return k;
}
/**
* Compute the hash of the MurmurHash3_x64_128 hashing function.
*
* Note, this hashing function might be used to persist hashes, so if the way hashes are computed
* changes for some reason, it needs to be addressed (like in BloomFilter and MurmurHashField).
*/
@SuppressWarnings("fallthrough") // Intentionally uses fallthrough to implement a well known hashing algorithm
public static Hash128 hash128(byte[] key, int offset, int length, long seed, Hash128 hash) {
long h1 = seed;
long h2 = seed;
if (length >= 16) {
IntermediateResult result = intermediateHash(key, offset, length, h1, h2);
h1 = result.h1;
h2 = result.h2;
offset = result.offset;
}
return finalizeHash(hash, key, offset, length, h1, h2);
}
static IntermediateResult intermediateHash(byte[] key, int offset, int length, long h1, long h2) {
final int len16 = length & 0xFFFFFFF0; // higher multiple of 16 that is lower than or equal to length
final int end = offset + len16;
for (int i = offset; i < end; i += 16) {
long k1 = ByteUtils.readLongLE(key, i);
long k2 = ByteUtils.readLongLE(key, i + 8);
k1 *= C1;
k1 = Long.rotateLeft(k1, 31);
k1 *= C2;
h1 ^= k1;
h1 = Long.rotateLeft(h1, 27);
h1 += h2;
h1 = h1 * 5 + 0x52dce729;
k2 *= C2;
k2 = Long.rotateLeft(k2, 33);
k2 *= C1;
h2 ^= k2;
h2 = Long.rotateLeft(h2, 31);
h2 += h1;
h2 = h2 * 5 + 0x38495ab5;
}
// Advance offset to the unprocessed tail of the data.
offset = end;
return new IntermediateResult(offset, h1, h2);
}
@SuppressWarnings("fallthrough") // Intentionally uses fallthrough to implement a well known hashing algorithm
static Hash128 finalizeHash(Hash128 hash, byte[] remainder, int offset, int length, long h1, long h2) {
long k1 = 0;
long k2 = 0;
switch (length & 15) {
case 15:
k2 ^= (remainder[offset + 14] & 0xFFL) << 48;
case 14:
k2 ^= (remainder[offset + 13] & 0xFFL) << 40;
case 13:
k2 ^= (remainder[offset + 12] & 0xFFL) << 32;
case 12:
k2 ^= (remainder[offset + 11] & 0xFFL) << 24;
case 11:
k2 ^= (remainder[offset + 10] & 0xFFL) << 16;
case 10:
k2 ^= (remainder[offset + 9] & 0xFFL) << 8;
case 9:
k2 ^= (remainder[offset + 8] & 0xFFL) << 0;
k2 *= C2;
k2 = Long.rotateLeft(k2, 33);
k2 *= C1;
h2 ^= k2;
case 8:
k1 ^= (remainder[offset + 7] & 0xFFL) << 56;
case 7:
k1 ^= (remainder[offset + 6] & 0xFFL) << 48;
case 6:
k1 ^= (remainder[offset + 5] & 0xFFL) << 40;
case 5:
k1 ^= (remainder[offset + 4] & 0xFFL) << 32;
case 4:
k1 ^= (remainder[offset + 3] & 0xFFL) << 24;
case 3:
k1 ^= (remainder[offset + 2] & 0xFFL) << 16;
case 2:
k1 ^= (remainder[offset + 1] & 0xFFL) << 8;
case 1:
k1 ^= (remainder[offset] & 0xFFL);
k1 *= C1;
k1 = Long.rotateLeft(k1, 31);
k1 *= C2;
h1 ^= k1;
}
h1 ^= length;
h2 ^= length;
h1 += h2;
h2 += h1;
h1 = fmix(h1);
h2 = fmix(h2);
h1 += h2;
h2 += h1;
hash.h1 = h1;
hash.h2 = h2;
return hash;
}
/**
* A 64-bit variant which accepts a long to hash, and returns the 64bit long hash.
* This is useful if the input is already in long (or smaller) format and you don't
* need the full 128b width and flexibility of
* {@link MurmurHash3#hash128(byte[], int, int, long, Hash128)}
*
* Given the limited nature of this variant, it should be faster than the 128b version
* when you only need 128b (many fewer instructions)
*/
public static long murmur64(long h) {
h ^= h >>> 33;
h *= 0xff51afd7ed558ccdL;
h ^= h >>> 33;
h *= 0xc4ceb9fe1a85ec53L;
h ^= h >>> 33;
return h;
}
}