Blame jemalloc/include/jemalloc/internal/hash.h

Packit 345191
#ifndef JEMALLOC_INTERNAL_HASH_H
Packit 345191
#define JEMALLOC_INTERNAL_HASH_H
Packit 345191
Packit 345191
#include "jemalloc/internal/assert.h"
Packit 345191
Packit 345191
/*
Packit 345191
 * The following hash function is based on MurmurHash3, placed into the public
Packit 345191
 * domain by Austin Appleby.  See https://github.com/aappleby/smhasher for
Packit 345191
 * details.
Packit 345191
 */
Packit 345191
Packit 345191
/******************************************************************************/
Packit 345191
/* Internal implementation. */
Packit 345191
static inline uint32_t
Packit 345191
hash_rotl_32(uint32_t x, int8_t r) {
Packit 345191
	return ((x << r) | (x >> (32 - r)));
Packit 345191
}
Packit 345191
Packit 345191
static inline uint64_t
Packit 345191
hash_rotl_64(uint64_t x, int8_t r) {
Packit 345191
	return ((x << r) | (x >> (64 - r)));
Packit 345191
}
Packit 345191
Packit 345191
static inline uint32_t
Packit 345191
hash_get_block_32(const uint32_t *p, int i) {
Packit 345191
	/* Handle unaligned read. */
Packit 345191
	if (unlikely((uintptr_t)p & (sizeof(uint32_t)-1)) != 0) {
Packit 345191
		uint32_t ret;
Packit 345191
Packit 345191
		memcpy(&ret, (uint8_t *)(p + i), sizeof(uint32_t));
Packit 345191
		return ret;
Packit 345191
	}
Packit 345191
Packit 345191
	return p[i];
Packit 345191
}
Packit 345191
Packit 345191
static inline uint64_t
Packit 345191
hash_get_block_64(const uint64_t *p, int i) {
Packit 345191
	/* Handle unaligned read. */
Packit 345191
	if (unlikely((uintptr_t)p & (sizeof(uint64_t)-1)) != 0) {
Packit 345191
		uint64_t ret;
Packit 345191
Packit 345191
		memcpy(&ret, (uint8_t *)(p + i), sizeof(uint64_t));
Packit 345191
		return ret;
Packit 345191
	}
Packit 345191
Packit 345191
	return p[i];
Packit 345191
}
Packit 345191
Packit 345191
static inline uint32_t
Packit 345191
hash_fmix_32(uint32_t h) {
Packit 345191
	h ^= h >> 16;
Packit 345191
	h *= 0x85ebca6b;
Packit 345191
	h ^= h >> 13;
Packit 345191
	h *= 0xc2b2ae35;
Packit 345191
	h ^= h >> 16;
Packit 345191
Packit 345191
	return h;
Packit 345191
}
Packit 345191
Packit 345191
static inline uint64_t
Packit 345191
hash_fmix_64(uint64_t k) {
Packit 345191
	k ^= k >> 33;
Packit 345191
	k *= KQU(0xff51afd7ed558ccd);
Packit 345191
	k ^= k >> 33;
Packit 345191
	k *= KQU(0xc4ceb9fe1a85ec53);
Packit 345191
	k ^= k >> 33;
Packit 345191
Packit 345191
	return k;
Packit 345191
}
Packit 345191
Packit 345191
static inline uint32_t
Packit 345191
hash_x86_32(const void *key, int len, uint32_t seed) {
Packit 345191
	const uint8_t *data = (const uint8_t *) key;
Packit 345191
	const int nblocks = len / 4;
Packit 345191
Packit 345191
	uint32_t h1 = seed;
Packit 345191
Packit 345191
	const uint32_t c1 = 0xcc9e2d51;
Packit 345191
	const uint32_t c2 = 0x1b873593;
Packit 345191
Packit 345191
	/* body */
Packit 345191
	{
Packit 345191
		const uint32_t *blocks = (const uint32_t *) (data + nblocks*4);
Packit 345191
		int i;
Packit 345191
Packit 345191
		for (i = -nblocks; i; i++) {
Packit 345191
			uint32_t k1 = hash_get_block_32(blocks, i);
Packit 345191
Packit 345191
			k1 *= c1;
Packit 345191
			k1 = hash_rotl_32(k1, 15);
Packit 345191
			k1 *= c2;
Packit 345191
Packit 345191
			h1 ^= k1;
Packit 345191
			h1 = hash_rotl_32(h1, 13);
Packit 345191
			h1 = h1*5 + 0xe6546b64;
Packit 345191
		}
Packit 345191
	}
Packit 345191
Packit 345191
	/* tail */
Packit 345191
	{
Packit 345191
		const uint8_t *tail = (const uint8_t *) (data + nblocks*4);
Packit 345191
Packit 345191
		uint32_t k1 = 0;
Packit 345191
Packit 345191
		switch (len & 3) {
Packit 345191
		case 3: k1 ^= tail[2] << 16; JEMALLOC_FALLTHROUGH
Packit 345191
		case 2: k1 ^= tail[1] << 8; JEMALLOC_FALLTHROUGH
Packit 345191
		case 1: k1 ^= tail[0]; k1 *= c1; k1 = hash_rotl_32(k1, 15);
Packit 345191
			k1 *= c2; h1 ^= k1;
Packit 345191
		}
Packit 345191
	}
Packit 345191
Packit 345191
	/* finalization */
Packit 345191
	h1 ^= len;
Packit 345191
Packit 345191
	h1 = hash_fmix_32(h1);
Packit 345191
Packit 345191
	return h1;
Packit 345191
}
Packit 345191
Packit 345191
static inline void
Packit 345191
hash_x86_128(const void *key, const int len, uint32_t seed,
Packit 345191
    uint64_t r_out[2]) {
Packit 345191
	const uint8_t * data = (const uint8_t *) key;
Packit 345191
	const int nblocks = len / 16;
Packit 345191
Packit 345191
	uint32_t h1 = seed;
Packit 345191
	uint32_t h2 = seed;
Packit 345191
	uint32_t h3 = seed;
Packit 345191
	uint32_t h4 = seed;
Packit 345191
Packit 345191
	const uint32_t c1 = 0x239b961b;
Packit 345191
	const uint32_t c2 = 0xab0e9789;
Packit 345191
	const uint32_t c3 = 0x38b34ae5;
Packit 345191
	const uint32_t c4 = 0xa1e38b93;
Packit 345191
Packit 345191
	/* body */
Packit 345191
	{
Packit 345191
		const uint32_t *blocks = (const uint32_t *) (data + nblocks*16);
Packit 345191
		int i;
Packit 345191
Packit 345191
		for (i = -nblocks; i; i++) {
Packit 345191
			uint32_t k1 = hash_get_block_32(blocks, i*4 + 0);
Packit 345191
			uint32_t k2 = hash_get_block_32(blocks, i*4 + 1);
Packit 345191
			uint32_t k3 = hash_get_block_32(blocks, i*4 + 2);
Packit 345191
			uint32_t k4 = hash_get_block_32(blocks, i*4 + 3);
Packit 345191
Packit 345191
			k1 *= c1; k1 = hash_rotl_32(k1, 15); k1 *= c2; h1 ^= k1;
Packit 345191
Packit 345191
			h1 = hash_rotl_32(h1, 19); h1 += h2;
Packit 345191
			h1 = h1*5 + 0x561ccd1b;
Packit 345191
Packit 345191
			k2 *= c2; k2 = hash_rotl_32(k2, 16); k2 *= c3; h2 ^= k2;
Packit 345191
Packit 345191
			h2 = hash_rotl_32(h2, 17); h2 += h3;
Packit 345191
			h2 = h2*5 + 0x0bcaa747;
Packit 345191
Packit 345191
			k3 *= c3; k3 = hash_rotl_32(k3, 17); k3 *= c4; h3 ^= k3;
Packit 345191
Packit 345191
			h3 = hash_rotl_32(h3, 15); h3 += h4;
Packit 345191
			h3 = h3*5 + 0x96cd1c35;
Packit 345191
Packit 345191
			k4 *= c4; k4 = hash_rotl_32(k4, 18); k4 *= c1; h4 ^= k4;
Packit 345191
Packit 345191
			h4 = hash_rotl_32(h4, 13); h4 += h1;
Packit 345191
			h4 = h4*5 + 0x32ac3b17;
Packit 345191
		}
Packit 345191
	}
Packit 345191
Packit 345191
	/* tail */
Packit 345191
	{
Packit 345191
		const uint8_t *tail = (const uint8_t *) (data + nblocks*16);
Packit 345191
		uint32_t k1 = 0;
Packit 345191
		uint32_t k2 = 0;
Packit 345191
		uint32_t k3 = 0;
Packit 345191
		uint32_t k4 = 0;
Packit 345191
Packit 345191
		switch (len & 15) {
Packit 345191
		case 15: k4 ^= tail[14] << 16; JEMALLOC_FALLTHROUGH
Packit 345191
		case 14: k4 ^= tail[13] << 8; JEMALLOC_FALLTHROUGH
Packit 345191
		case 13: k4 ^= tail[12] << 0;
Packit 345191
			k4 *= c4; k4 = hash_rotl_32(k4, 18); k4 *= c1; h4 ^= k4;
Packit 345191
      JEMALLOC_FALLTHROUGH
Packit 345191
		case 12: k3 ^= tail[11] << 24; JEMALLOC_FALLTHROUGH
Packit 345191
		case 11: k3 ^= tail[10] << 16; JEMALLOC_FALLTHROUGH
Packit 345191
		case 10: k3 ^= tail[ 9] << 8; JEMALLOC_FALLTHROUGH
Packit 345191
		case  9: k3 ^= tail[ 8] << 0;
Packit 345191
		     k3 *= c3; k3 = hash_rotl_32(k3, 17); k3 *= c4; h3 ^= k3;
Packit 345191
         JEMALLOC_FALLTHROUGH
Packit 345191
		case  8: k2 ^= tail[ 7] << 24; JEMALLOC_FALLTHROUGH
Packit 345191
		case  7: k2 ^= tail[ 6] << 16; JEMALLOC_FALLTHROUGH
Packit 345191
		case  6: k2 ^= tail[ 5] << 8; JEMALLOC_FALLTHROUGH
Packit 345191
		case  5: k2 ^= tail[ 4] << 0;
Packit 345191
			k2 *= c2; k2 = hash_rotl_32(k2, 16); k2 *= c3; h2 ^= k2;
Packit 345191
      JEMALLOC_FALLTHROUGH
Packit 345191
		case  4: k1 ^= tail[ 3] << 24; JEMALLOC_FALLTHROUGH
Packit 345191
		case  3: k1 ^= tail[ 2] << 16; JEMALLOC_FALLTHROUGH
Packit 345191
		case  2: k1 ^= tail[ 1] << 8; JEMALLOC_FALLTHROUGH
Packit 345191
		case  1: k1 ^= tail[ 0] << 0;
Packit 345191
			k1 *= c1; k1 = hash_rotl_32(k1, 15); k1 *= c2; h1 ^= k1;
Packit 345191
      JEMALLOC_FALLTHROUGH
Packit 345191
		}
Packit 345191
	}
Packit 345191
Packit 345191
	/* finalization */
Packit 345191
	h1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len;
Packit 345191
Packit 345191
	h1 += h2; h1 += h3; h1 += h4;
Packit 345191
	h2 += h1; h3 += h1; h4 += h1;
Packit 345191
Packit 345191
	h1 = hash_fmix_32(h1);
Packit 345191
	h2 = hash_fmix_32(h2);
Packit 345191
	h3 = hash_fmix_32(h3);
Packit 345191
	h4 = hash_fmix_32(h4);
Packit 345191
Packit 345191
	h1 += h2; h1 += h3; h1 += h4;
Packit 345191
	h2 += h1; h3 += h1; h4 += h1;
Packit 345191
Packit 345191
	r_out[0] = (((uint64_t) h2) << 32) | h1;
Packit 345191
	r_out[1] = (((uint64_t) h4) << 32) | h3;
Packit 345191
}
Packit 345191
Packit 345191
static inline void
Packit 345191
hash_x64_128(const void *key, const int len, const uint32_t seed,
Packit 345191
    uint64_t r_out[2]) {
Packit 345191
	const uint8_t *data = (const uint8_t *) key;
Packit 345191
	const int nblocks = len / 16;
Packit 345191
Packit 345191
	uint64_t h1 = seed;
Packit 345191
	uint64_t h2 = seed;
Packit 345191
Packit 345191
	const uint64_t c1 = KQU(0x87c37b91114253d5);
Packit 345191
	const uint64_t c2 = KQU(0x4cf5ad432745937f);
Packit 345191
Packit 345191
	/* body */
Packit 345191
	{
Packit 345191
		const uint64_t *blocks = (const uint64_t *) (data);
Packit 345191
		int i;
Packit 345191
Packit 345191
		for (i = 0; i < nblocks; i++) {
Packit 345191
			uint64_t k1 = hash_get_block_64(blocks, i*2 + 0);
Packit 345191
			uint64_t k2 = hash_get_block_64(blocks, i*2 + 1);
Packit 345191
Packit 345191
			k1 *= c1; k1 = hash_rotl_64(k1, 31); k1 *= c2; h1 ^= k1;
Packit 345191
Packit 345191
			h1 = hash_rotl_64(h1, 27); h1 += h2;
Packit 345191
			h1 = h1*5 + 0x52dce729;
Packit 345191
Packit 345191
			k2 *= c2; k2 = hash_rotl_64(k2, 33); k2 *= c1; h2 ^= k2;
Packit 345191
Packit 345191
			h2 = hash_rotl_64(h2, 31); h2 += h1;
Packit 345191
			h2 = h2*5 + 0x38495ab5;
Packit 345191
		}
Packit 345191
	}
Packit 345191
Packit 345191
	/* tail */
Packit 345191
	{
Packit 345191
		const uint8_t *tail = (const uint8_t*)(data + nblocks*16);
Packit 345191
		uint64_t k1 = 0;
Packit 345191
		uint64_t k2 = 0;
Packit 345191
Packit 345191
		switch (len & 15) {
Packit 345191
		case 15: k2 ^= ((uint64_t)(tail[14])) << 48; JEMALLOC_FALLTHROUGH
Packit 345191
		case 14: k2 ^= ((uint64_t)(tail[13])) << 40; JEMALLOC_FALLTHROUGH
Packit 345191
		case 13: k2 ^= ((uint64_t)(tail[12])) << 32; JEMALLOC_FALLTHROUGH
Packit 345191
		case 12: k2 ^= ((uint64_t)(tail[11])) << 24; JEMALLOC_FALLTHROUGH
Packit 345191
		case 11: k2 ^= ((uint64_t)(tail[10])) << 16; JEMALLOC_FALLTHROUGH
Packit 345191
		case 10: k2 ^= ((uint64_t)(tail[ 9])) << 8;  JEMALLOC_FALLTHROUGH
Packit 345191
		case  9: k2 ^= ((uint64_t)(tail[ 8])) << 0;
Packit 345191
			k2 *= c2; k2 = hash_rotl_64(k2, 33); k2 *= c1; h2 ^= k2;
Packit 345191
			JEMALLOC_FALLTHROUGH
Packit 345191
		case  8: k1 ^= ((uint64_t)(tail[ 7])) << 56; JEMALLOC_FALLTHROUGH
Packit 345191
		case  7: k1 ^= ((uint64_t)(tail[ 6])) << 48; JEMALLOC_FALLTHROUGH
Packit 345191
		case  6: k1 ^= ((uint64_t)(tail[ 5])) << 40; JEMALLOC_FALLTHROUGH
Packit 345191
		case  5: k1 ^= ((uint64_t)(tail[ 4])) << 32; JEMALLOC_FALLTHROUGH
Packit 345191
		case  4: k1 ^= ((uint64_t)(tail[ 3])) << 24; JEMALLOC_FALLTHROUGH
Packit 345191
		case  3: k1 ^= ((uint64_t)(tail[ 2])) << 16; JEMALLOC_FALLTHROUGH
Packit 345191
		case  2: k1 ^= ((uint64_t)(tail[ 1])) << 8;  JEMALLOC_FALLTHROUGH
Packit 345191
		case  1: k1 ^= ((uint64_t)(tail[ 0])) << 0;
Packit 345191
			k1 *= c1; k1 = hash_rotl_64(k1, 31); k1 *= c2; h1 ^= k1;
Packit 345191
		}
Packit 345191
	}
Packit 345191
Packit 345191
	/* finalization */
Packit 345191
	h1 ^= len; h2 ^= len;
Packit 345191
Packit 345191
	h1 += h2;
Packit 345191
	h2 += h1;
Packit 345191
Packit 345191
	h1 = hash_fmix_64(h1);
Packit 345191
	h2 = hash_fmix_64(h2);
Packit 345191
Packit 345191
	h1 += h2;
Packit 345191
	h2 += h1;
Packit 345191
Packit 345191
	r_out[0] = h1;
Packit 345191
	r_out[1] = h2;
Packit 345191
}
Packit 345191
Packit 345191
/******************************************************************************/
Packit 345191
/* API. */
Packit 345191
static inline void
Packit 345191
hash(const void *key, size_t len, const uint32_t seed, size_t r_hash[2]) {
Packit 345191
	assert(len <= INT_MAX); /* Unfortunate implementation limitation. */
Packit 345191
Packit 345191
#if (LG_SIZEOF_PTR == 3 && !defined(JEMALLOC_BIG_ENDIAN))
Packit 345191
	hash_x64_128(key, (int)len, seed, (uint64_t *)r_hash);
Packit 345191
#else
Packit 345191
	{
Packit 345191
		uint64_t hashes[2];
Packit 345191
		hash_x86_128(key, (int)len, seed, hashes);
Packit 345191
		r_hash[0] = (size_t)hashes[0];
Packit 345191
		r_hash[1] = (size_t)hashes[1];
Packit 345191
	}
Packit 345191
#endif
Packit 345191
}
Packit 345191
Packit 345191
#endif /* JEMALLOC_INTERNAL_HASH_H */