2 * The following hash function is based on MurmurHash3, placed into the public
3 * domain by Austin Appleby. See http://code.google.com/p/smhasher/ for
6 /******************************************************************************/
7 #ifdef JEMALLOC_H_TYPES
9 #endif /* JEMALLOC_H_TYPES */
10 /******************************************************************************/
11 #ifdef JEMALLOC_H_STRUCTS
13 #endif /* JEMALLOC_H_STRUCTS */
14 /******************************************************************************/
15 #ifdef JEMALLOC_H_EXTERNS
17 #endif /* JEMALLOC_H_EXTERNS */
18 /******************************************************************************/
19 #ifdef JEMALLOC_H_INLINES
21 #ifndef JEMALLOC_ENABLE_INLINE
22 void hash(const void *key, size_t len, const uint32_t seed,
26 #if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_HASH_C_))
27 /******************************************************************************/
28 /* Internal implementation. */
29 JEMALLOC_INLINE uint32_t
30 hash_rotl_32(uint32_t x, int8_t r)
33 return (x << r) | (x >> (32 - r));
36 JEMALLOC_INLINE uint64_t
37 hash_rotl_64(uint64_t x, int8_t r)
39 return (x << r) | (x >> (64 - r));
42 JEMALLOC_INLINE uint32_t
43 hash_get_block_32(const uint32_t *p, int i)
49 JEMALLOC_INLINE uint64_t
50 hash_get_block_64(const uint64_t *p, int i)
56 JEMALLOC_INLINE uint32_t
57 hash_fmix_32(uint32_t h)
69 JEMALLOC_INLINE uint64_t
70 hash_fmix_64(uint64_t k)
74 k *= QU(0xff51afd7ed558ccdLLU);
76 k *= QU(0xc4ceb9fe1a85ec53LLU);
82 JEMALLOC_INLINE uint32_t
83 hash_x86_32(const void *key, int len, uint32_t seed)
85 const uint8_t *data = (const uint8_t *) key;
86 const int nblocks = len / 4;
90 const uint32_t c1 = 0xcc9e2d51;
91 const uint32_t c2 = 0x1b873593;
95 const uint32_t *blocks = (const uint32_t *) (data + nblocks*4);
98 for (i = -nblocks; i; i++) {
99 uint32_t k1 = hash_get_block_32(blocks, i);
102 k1 = hash_rotl_32(k1, 15);
106 h1 = hash_rotl_32(h1, 13);
107 h1 = h1*5 + 0xe6546b64;
113 const uint8_t *tail = (const uint8_t *) (data + nblocks*4);
118 case 3: k1 ^= tail[2] << 16;
119 case 2: k1 ^= tail[1] << 8;
120 case 1: k1 ^= tail[0]; k1 *= c1; k1 = hash_rotl_32(k1, 15);
128 h1 = hash_fmix_32(h1);
133 UNUSED JEMALLOC_INLINE void
134 hash_x86_128(const void *key, const int len, uint32_t seed,
137 const uint8_t * data = (const uint8_t *) key;
138 const int nblocks = len / 16;
145 const uint32_t c1 = 0x239b961b;
146 const uint32_t c2 = 0xab0e9789;
147 const uint32_t c3 = 0x38b34ae5;
148 const uint32_t c4 = 0xa1e38b93;
152 const uint32_t *blocks = (const uint32_t *) (data + nblocks*16);
155 for (i = -nblocks; i; i++) {
156 uint32_t k1 = hash_get_block_32(blocks, i*4 + 0);
157 uint32_t k2 = hash_get_block_32(blocks, i*4 + 1);
158 uint32_t k3 = hash_get_block_32(blocks, i*4 + 2);
159 uint32_t k4 = hash_get_block_32(blocks, i*4 + 3);
161 k1 *= c1; k1 = hash_rotl_32(k1, 15); k1 *= c2; h1 ^= k1;
163 h1 = hash_rotl_32(h1, 19); h1 += h2;
164 h1 = h1*5 + 0x561ccd1b;
166 k2 *= c2; k2 = hash_rotl_32(k2, 16); k2 *= c3; h2 ^= k2;
168 h2 = hash_rotl_32(h2, 17); h2 += h3;
169 h2 = h2*5 + 0x0bcaa747;
171 k3 *= c3; k3 = hash_rotl_32(k3, 17); k3 *= c4; h3 ^= k3;
173 h3 = hash_rotl_32(h3, 15); h3 += h4;
174 h3 = h3*5 + 0x96cd1c35;
176 k4 *= c4; k4 = hash_rotl_32(k4, 18); k4 *= c1; h4 ^= k4;
178 h4 = hash_rotl_32(h4, 13); h4 += h1;
179 h4 = h4*5 + 0x32ac3b17;
185 const uint8_t *tail = (const uint8_t *) (data + nblocks*16);
192 case 15: k4 ^= tail[14] << 16;
193 case 14: k4 ^= tail[13] << 8;
194 case 13: k4 ^= tail[12] << 0;
195 k4 *= c4; k4 = hash_rotl_32(k4, 18); k4 *= c1; h4 ^= k4;
197 case 12: k3 ^= tail[11] << 24;
198 case 11: k3 ^= tail[10] << 16;
199 case 10: k3 ^= tail[ 9] << 8;
200 case 9: k3 ^= tail[ 8] << 0;
201 k3 *= c3; k3 = hash_rotl_32(k3, 17); k3 *= c4; h3 ^= k3;
203 case 8: k2 ^= tail[ 7] << 24;
204 case 7: k2 ^= tail[ 6] << 16;
205 case 6: k2 ^= tail[ 5] << 8;
206 case 5: k2 ^= tail[ 4] << 0;
207 k2 *= c2; k2 = hash_rotl_32(k2, 16); k2 *= c3; h2 ^= k2;
209 case 4: k1 ^= tail[ 3] << 24;
210 case 3: k1 ^= tail[ 2] << 16;
211 case 2: k1 ^= tail[ 1] << 8;
212 case 1: k1 ^= tail[ 0] << 0;
213 k1 *= c1; k1 = hash_rotl_32(k1, 15); k1 *= c2; h1 ^= k1;
218 h1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len;
220 h1 += h2; h1 += h3; h1 += h4;
221 h2 += h1; h3 += h1; h4 += h1;
223 h1 = hash_fmix_32(h1);
224 h2 = hash_fmix_32(h2);
225 h3 = hash_fmix_32(h3);
226 h4 = hash_fmix_32(h4);
228 h1 += h2; h1 += h3; h1 += h4;
229 h2 += h1; h3 += h1; h4 += h1;
231 r_out[0] = (((uint64_t) h2) << 32) | h1;
232 r_out[1] = (((uint64_t) h4) << 32) | h3;
235 UNUSED JEMALLOC_INLINE void
236 hash_x64_128(const void *key, const int len, const uint32_t seed,
239 const uint8_t *data = (const uint8_t *) key;
240 const int nblocks = len / 16;
245 const uint64_t c1 = QU(0x87c37b91114253d5LLU);
246 const uint64_t c2 = QU(0x4cf5ad432745937fLLU);
250 const uint64_t *blocks = (const uint64_t *) (data);
253 for (i = 0; i < nblocks; i++) {
254 uint64_t k1 = hash_get_block_64(blocks, i*2 + 0);
255 uint64_t k2 = hash_get_block_64(blocks, i*2 + 1);
257 k1 *= c1; k1 = hash_rotl_64(k1, 31); k1 *= c2; h1 ^= k1;
259 h1 = hash_rotl_64(h1, 27); h1 += h2;
260 h1 = h1*5 + 0x52dce729;
262 k2 *= c2; k2 = hash_rotl_64(k2, 33); k2 *= c1; h2 ^= k2;
264 h2 = hash_rotl_64(h2, 31); h2 += h1;
265 h2 = h2*5 + 0x38495ab5;
271 const uint8_t *tail = (const uint8_t*)(data + nblocks*16);
276 case 15: k2 ^= ((uint64_t)(tail[14])) << 48;
277 case 14: k2 ^= ((uint64_t)(tail[13])) << 40;
278 case 13: k2 ^= ((uint64_t)(tail[12])) << 32;
279 case 12: k2 ^= ((uint64_t)(tail[11])) << 24;
280 case 11: k2 ^= ((uint64_t)(tail[10])) << 16;
281 case 10: k2 ^= ((uint64_t)(tail[ 9])) << 8;
282 case 9: k2 ^= ((uint64_t)(tail[ 8])) << 0;
283 k2 *= c2; k2 = hash_rotl_64(k2, 33); k2 *= c1; h2 ^= k2;
285 case 8: k1 ^= ((uint64_t)(tail[ 7])) << 56;
286 case 7: k1 ^= ((uint64_t)(tail[ 6])) << 48;
287 case 6: k1 ^= ((uint64_t)(tail[ 5])) << 40;
288 case 5: k1 ^= ((uint64_t)(tail[ 4])) << 32;
289 case 4: k1 ^= ((uint64_t)(tail[ 3])) << 24;
290 case 3: k1 ^= ((uint64_t)(tail[ 2])) << 16;
291 case 2: k1 ^= ((uint64_t)(tail[ 1])) << 8;
292 case 1: k1 ^= ((uint64_t)(tail[ 0])) << 0;
293 k1 *= c1; k1 = hash_rotl_64(k1, 31); k1 *= c2; h1 ^= k1;
298 h1 ^= len; h2 ^= len;
303 h1 = hash_fmix_64(h1);
304 h2 = hash_fmix_64(h2);
314 /******************************************************************************/
317 hash(const void *key, size_t len, const uint32_t seed, size_t r_hash[2])
319 #if (LG_SIZEOF_PTR == 3)
320 hash_x64_128(key, len, seed, (uint64_t *)r_hash);
323 hash_x86_128(key, len, seed, hashes);
324 r_hash[0] = (size_t)hashes[0];
325 r_hash[1] = (size_t)hashes[1];
330 #endif /* JEMALLOC_H_INLINES */
331 /******************************************************************************/