这是murmur3的32位版本,输出为32位整数,具有良好的均匀分布特性,可方便用于hash table等场景。 根据测试,以rand()函数生成随机数作为输入,并且seed(种子)设为0的情况下,输出1000个[0,1000]以内整数(%1000 取余处理), 分布比较均匀。
C语言代码实现如下:
`
uint32_t murmur3_32(const void* key, int len, uint32_t seed) { if (key == NULL) return 0; const uint32_t c1 = 0xcc9e2d51; const uint32_t c2 = 0x1b873593; const uint32_t r1 = 15; const uint32_t r2 = 13; const uint32_t m = 5; const uint32_t n = 0xe6546b64;
uint32_t hash = seed;
const int nblocks = len / 4;
const uint32_t* blocks = (const uint32_t*)key;
int i = 0;
uint32_t k = 0;
for (i = 0;i<nblocks;i++) {
k = blocks[i];
k *= c1;
k = (k << r1) | (k >> (32 - r1));
k *= c2;
hash ^= k;
hash = ((hash << r2) | (hash >> (32 - r2))) * m + n;
}
const uint8_t* tail = (const uint8_t*)key + (nblocks * 4);
uint32_t k1 = 0;
switch (len&3) {
case 3:
k1 ^= tail[2] << 16;
case 2:
k1 ^= tail[1] << 8;
case 1:
k1 ^= tail[0];
k1 *= c1;
k1 = (k1 << r1) | (k >> (32 - r1));
k1 *= c2;
hash ^= k1;
}
hash ^= len;
hash ^= (hash >> 16);
hash *= 0x85ebca6b;
hash ^= (hash >> 13);
hash *= 0xc2b2ae35;
hash ^= (hash >> 16);
return hash;
} `