Skip to content

Murmur Hash 2 for Arduino

December 12, 2012

I made modification from the official murmur2 code because the official code assumes 32 bit math whereas Arduino’s math is 16 bit.

// Same as MurmurHash2, but endian- and alignment-neutral.
// Half the speed though, alas.
uint32_t MurmurHashNeutral2 ( const void * key, uint32_t len, uint32_t seed )
{
 const uint32_t m = 0x5bd1e995;
 const uint32_t r = 24;
uint32_t h = seed ^ len;
const unsigned char * data = (const unsigned char *)key;
while(len >= 4)
 {
 uint32_t k;
k = (uint32_t)data[0];
 k |= (uint32_t)data[1] << 8;
 k |= (uint32_t)data[2] << 16;
 k |= (uint32_t)data[3] << 24;
k *= m; 
 k ^= k >> r; 
 k *= m;
h *= m;
 h ^= k;
data += 4;
 len -= 4;
 }

 switch(len)
 {
 case 3: h ^= (uint32_t)data[2] << 16;
 case 2: h ^= (uint32_t)data[1] << 8;
 case 1: h ^= (uint32_t)data[0];
 h *= m;
 };
h ^= h >> 13;
 h *= m;
 h ^= h >> 15;
return h;
}
Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: