logswan

Fast Web log analyzer using probabilistic data structures
Log | Files | Refs | README | LICENSE

MurmurHash3.c (1247B)


      1 /*-----------------------------------------------------------------------------
      2  MurmurHash3 was written by Austin Appleby, and is placed in the public
      3  domain. The author hereby disclaims copyright to this source code.
      4 */
      5 
      6 #include "MurmurHash3.h"
      7 
      8 #define	ROTL32(x, r)	((x) << (r)) | ((x) >> (32 - (r)))
      9 
     10 uint32_t MurmurHash3_x86_32(const void *key, uint32_t len, uint32_t seed) {
     11 	const uint8_t *data = (const uint8_t *)key;
     12 	const int32_t nblocks = (int32_t)len / 4;
     13 
     14 	uint32_t h1 = seed;
     15 	int i;
     16 
     17 	const uint32_t c1 = 0xcc9e2d51;
     18 	const uint32_t c2 = 0x1b873593;
     19 	const uint32_t *blocks = (const uint32_t *)(data + nblocks * 4);
     20 
     21 	for(i = -nblocks; i; i++)
     22 	{
     23 		uint32_t k1 = blocks[i];
     24 
     25 		k1 *= c1;
     26 		k1 = ROTL32(k1, 15);
     27 		k1 *= c2;
     28 
     29 		h1 ^= k1;
     30 		h1 = ROTL32(h1, 13);
     31 		h1 = h1 * 5 + 0xe6546b64;
     32 	}
     33 
     34 	const uint8_t * tail = (const uint8_t *)(data + nblocks * 4);
     35 
     36 	uint32_t k1 = 0;
     37 
     38 	switch(len & 3)
     39 	{
     40 		case 3:
     41 			k1 ^= (uint32_t)tail[2] << 16;	/* FALLTHROUGH */
     42 		case 2:
     43 			k1 ^= (uint32_t)tail[1] << 8;	/* FALLTHROUGH */
     44 		case 1:
     45 			k1 ^= tail[0];
     46 			k1 *= c1;
     47 			k1 = ROTL32(k1, 15);
     48 			k1 *= c2;
     49 			h1 ^= k1;
     50 	};
     51 
     52 	h1 ^= len;
     53 
     54 	h1 ^= h1 >> 16;
     55 	h1 *= 0x85ebca6b;
     56 	h1 ^= h1 >> 13;
     57 	h1 *= 0xc2b2ae35;
     58 	h1 ^= h1 >> 16;
     59 
     60 	return h1;
     61 }