Skip to content

Instantly share code, notes, and snippets.

@codewings
Created February 18, 2014 07:59
Show Gist options
  • Save codewings/9066486 to your computer and use it in GitHub Desktop.
Save codewings/9066486 to your computer and use it in GitHub Desktop.
def bytes_to_int32(bytes):
assert len(bytes) == 4
return sum((b << (k * 8) for k, b in enumerate(bytes)))
def MurmurHash64B(data, seed):
m = 0x5bd1e995
r = 24
MASK = 0xFFFFFFFF
data_as_bytes = bytearray(data)
length = len(data_as_bytes) & MASK
h1 = (seed & MASK) ^ length;
h2 = 0;
index = 0
while(length >= 8):
k1 = bytes_to_int32(data_as_bytes[index:index+4])
k1 = (k1 * m) & MASK; k1 = k1 ^ ((k1 >> r) & MASK); k1 = (k1 * m) & MASK;
h1 = (h1 * m) & MASK; h1 = h1 ^ k1;
length -= 4;
index += 4;
k2 = bytes_to_int32(data_as_bytes[index:index+4])
k2 = (k2 * m) & MASK; k2 = k2 ^ ((k2 >> r) & MASK); k2 = (k2 * m) & MASK;
h2 = (h2 * m) & MASK; h2 = h2 ^ k2;
length -= 4;
index += 4;
if(length >= 4):
k3 = bytes_to_int32(data_as_bytes[index:index+4])
k3 = (k3 * m) & MASK; k3 = k3 ^ ((k3 >> r) & MASK); k3 = (k3 * m) & MASK;
h1 = (h1 * m) & MASK; h1 = h1 ^ k3;
length -= 4;
index += 4;
if(length == 3):
h2 = h2 ^ ((data_as_bytes[index+2] << 16) & MASK )
h2 = h2 ^ ((data_as_bytes[index+1] << 8 ) & MASK )
h2 = h2 ^ (data_as_bytes[index+0])
h2 = (h2 * m) & MASK;
if(length == 2):
h2 = h2 ^ ((data_as_bytes[index+1] << 8 ) & MASK )
h2 = h2 ^ (data_as_bytes[index+0])
h2 = (h2 * m) & MASK;
if(length == 1):
h2 = h2 ^ (data_as_bytes[index+0])
h2 = (h2 * m) & MASK;
h1 = h1 ^ ((h2 >> 18) & MASK ); h1 = (h1 * m) & MASK;
h2 = h2 ^ ((h1 >> 22) & MASK ); h2 = (h2 * m) & MASK;
h1 = h1 ^ ((h2 >> 17) & MASK ); h1 = (h1 * m) & MASK;
h2 = h2 ^ ((h1 >> 19) & MASK ); h2 = (h2 * m) & MASK;
h = h1;
h = ((h << 32) & 0xFFFFFFFFFFFFFFFF) | h2;
return h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment