Skip to content

Instantly share code, notes, and snippets.

@morn-0
Last active September 27, 2022 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morn-0/58e7febd49186ca8f8c88b643df35da6 to your computer and use it in GitHub Desktop.
Save morn-0/58e7febd49186ca8f8c88b643df35da6 to your computer and use it in GitHub Desktop.
Probably the fastest murmur3 implementation in java
/**
* @author morning
* @date 2022-09-19 09:25
*/
public class FastMurmur3 {
private static final int C1 = 0xcc9e2d51;
private static final int C2 = 0x1b873593;
private static final int DEFAULT_SEED = 0;
public static int hash32(byte[] b, int seed) {
final int len = b.length;
int h1 = seed, i = 0, newLen = len & 0xfffffffc;
while (i < newLen) {
int k1 = (b[i++] & 0xff) | ((b[i++] & 0xff) << 8) | ((b[i++] & 0xff) << 16) | (b[i++] << 24);
k1 *= C1;
k1 = ((k1 << 15) | (k1 >>> 17)) * C2;
h1 ^= k1;
h1 = ((h1 << 13) | (h1 >>> 19)) * 5 + 0xe6546b64;
}
if (i != len) {
int k1 = 0, j = 0;
do {
k1 ^= (b[i++] & 0xff) << j;
j += 8;
} while (i != len);
k1 *= C1;
k1 = ((k1 << 15) | (k1 >>> 17)) * C2;
h1 ^= k1;
}
h1 ^= len;
h1 ^= h1 >>> 16;
h1 *= 0x85ebca6b;
h1 ^= h1 >>> 13;
h1 *= 0xc2b2ae35;
h1 ^= h1 >>> 16;
return h1;
}
public static int hash32(byte[] bytes) {
return hash32(bytes, DEFAULT_SEED);
}
public static int hash32(String str) {
return hash32(str.getBytes());
}
public static int hash32(String str, int seed) {
return hash32(str.getBytes(), seed);
}
}
@morn-0
Copy link
Author

morn-0 commented Sep 27, 2022

murmur3_32

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment