3 * https://sites.google.com/site/murmurhash/
5 * MurmurHash3 was written by Austin Appleby, and is placed in the public
6 * domain. The author hereby disclaims copyright to this source code.
8 * Eric Wong trivially ported this to C for Ruby tdb (32-bit versions only)
13 static inline uint32_t rotl32(uint32_t x
, int8_t r
)
15 return (x
<< r
) | (x
>> (32 - r
));
18 #define ROTL32(x,y) rotl32(x,y)
20 #define BIG_CONSTANT(x) (x##LLU)
22 /* ----------------------------------------------------------------------------
23 * Block read - if your platform needs to do endian-swapping or can only
24 * handle aligned reads, do the conversion here
27 static inline uint32_t getblock(const uint32_t * p
, int i
)
32 /* ----------------------------------------------------------------------------
33 * Finalization mix - force all bits of a hash block to avalanche
36 static inline uint32_t fmix(uint32_t h
)
47 unsigned int rbtdb_murmur3a(TDB_DATA
* key
)
49 const uint8_t *data
= key
->dptr
;
50 int len
= (int)key
->dsize
;
51 const int nblocks
= len
/ 4;
52 static const uint32_t seed
;
56 static const uint32_t c1
= 0xcc9e2d51;
57 static const uint32_t c2
= 0x1b873593;
60 const uint32_t *blocks
= (const uint32_t *)(data
+ nblocks
* 4);
62 for (i
= -nblocks
; i
; i
++) {
63 uint32_t k1
= getblock(blocks
, i
);
71 h1
= h1
* 5 + 0xe6546b64;
76 const uint8_t *tail
= (const uint8_t *)(data
+ nblocks
* 4);
100 static inline uint64_t rotl64(uint64_t x
, int8_t r
)
102 return (x
<< r
) | (x
>> (64 - r
));
105 #define ROTL64(x,y) rotl64(x,y)
107 static inline uint64_t getblock64(const uint64_t * p
, int i
)
112 static inline uint64_t fmix64(uint64_t k
)
115 k
*= BIG_CONSTANT(0xff51afd7ed558ccd);
117 k
*= BIG_CONSTANT(0xc4ceb9fe1a85ec53);
123 /* this was a 128-bit hash for x86_64, but we only want 32-bits */
124 unsigned int rbtdb_murmur3f(TDB_DATA
* key
)
126 const uint8_t *data
= key
->dptr
;
127 int len
= (int)key
->dsize
;
128 const int nblocks
= len
/ 16;
129 static const uint32_t seed
;
132 uint64_t c1
= BIG_CONSTANT(0x87c37b91114253d5);
133 uint64_t c2
= BIG_CONSTANT(0x4cf5ad432745937f);
137 const uint64_t *blocks
= (const uint64_t *)(data
);
139 for (i
= 0; i
< nblocks
; i
++) {
140 uint64_t k1
= getblock64(blocks
, i
* 2 + 0);
141 uint64_t k2
= getblock64(blocks
, i
* 2 + 1);
150 h1
= h1
* 5 + 0x52dce729;
159 h2
= h2
* 5 + 0x38495ab5;
164 const uint8_t *tail
= (const uint8_t *)(data
+ nblocks
* 16);
168 #define CAST64(x) ((uint64_t)(x))
172 k2
^= CAST64(tail
[14]) << 48;
174 k2
^= CAST64(tail
[13]) << 40;
176 k2
^= CAST64(tail
[12]) << 32;
178 k2
^= CAST64(tail
[11]) << 24;
180 k2
^= CAST64(tail
[10]) << 16;
182 k2
^= CAST64(tail
[9]) << 8;
184 k2
^= CAST64(tail
[8]) << 0;
191 k1
^= CAST64(tail
[7]) << 56;
193 k1
^= CAST64(tail
[6]) << 48;
195 k1
^= CAST64(tail
[5]) << 40;
197 k1
^= CAST64(tail
[4]) << 32;
199 k1
^= CAST64(tail
[3]) << 24;
201 k1
^= CAST64(tail
[2]) << 16;
203 k1
^= CAST64(tail
[1]) << 8;
205 k1
^= CAST64(tail
[0]) << 0;
226 /* not needed for 32-bit hash */
229 return (unsigned int)h1
; /* truncate to 32-bits */