1#ifndef _LINUX_HASH_H
2#define _LINUX_HASH_H
3
4
5
6
7
8
9
10
11
12
13
14
15
16#if BITS_PER_LONG == 32
17
18#define GOLDEN_RATIO_PRIME 0x9e370001UL
19#elif BITS_PER_LONG == 64
20
21#define GOLDEN_RATIO_PRIME 0x9e37fffffffc0001UL
22#else
23#error Define GOLDEN_RATIO_PRIME for your wordsize.
24#endif
25
26static inline unsigned long hash_long(unsigned long val, unsigned int bits)
27{
28 unsigned long hash = val;
29
30#if BITS_PER_LONG == 64
31
32 unsigned long n = hash;
33 n <<= 18;
34 hash -= n;
35 n <<= 33;
36 hash -= n;
37 n <<= 3;
38 hash += n;
39 n <<= 3;
40 hash -= n;
41 n <<= 4;
42 hash += n;
43 n <<= 2;
44 hash += n;
45#else
46
47 hash *= GOLDEN_RATIO_PRIME;
48#endif
49
50
51 return hash >> (BITS_PER_LONG - bits);
52}
53
54static inline unsigned long hash_ptr(void *ptr, unsigned int bits)
55{
56 return hash_long((unsigned long)ptr, bits);
57}
58#endif
59