summaryrefslogtreecommitdiffstats
path: root/sys/dev/pci/drm/include/linux/hash.h
blob: c0898474ebc3fd3727eab0805edda6bc68af46ae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/* Public domain. */

#ifndef _LINUX_HASH_H
#define _LINUX_HASH_H

#include <sys/types.h>

/* 2^32 * ((sqrt(5) - 1) / 2) from Knuth */
#define GOLDEN_RATIO_32 0x9e3779b9

static inline uint32_t
hash_32(uint32_t val, unsigned int bits)
{
	return (val * GOLDEN_RATIO_32) >> (32 - bits);
}

/* 2^64 * ((sqrt(5) - 1) / 2) from Knuth */
#define GOLDEN_RATIO_64 0x9e3779b97f4a7c16ULL

static inline uint32_t
hash_64(uint64_t val, unsigned int bits)
{
	return (val * GOLDEN_RATIO_64) >> (64 - bits);
}

#ifdef __LP64__
#define hash_long(val, bits) hash_64(val, bits)
#else
#define hash_long(val, bits) hash_32(val, bits)
#endif

#endif