1 #ifndef _LINUX_HASH_H
2 #define _LINUX_HASH_H
3 
4 #include <inttypes.h>
5 #include "arch/arch.h"
6 #include "compiler/compiler.h"
7 
8 /* Fast hashing routine for a long.
9    (C) 2002 William Lee Irwin III, IBM */
10 
11 /*
12  * Knuth recommends primes in approximately golden ratio to the maximum
13  * integer representable by a machine word for multiplicative hashing.
14  * Chuck Lever verified the effectiveness of this technique:
15  * http://www.citi.umich.edu/techreports/reports/citi-tr-00-1.pdf
16  *
17  * These primes are chosen to be bit-sparse, that is operations on
18  * them can use shifts and additions instead of multiplications for
19  * machines where multiplications are slow.
20  */
21 
22 #if BITS_PER_LONG == 32
23 /* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */
24 #define GOLDEN_RATIO_PRIME 0x9e370001UL
25 #elif BITS_PER_LONG == 64
26 /*  2^63 + 2^61 - 2^57 + 2^54 - 2^51 - 2^18 + 1 */
27 #define GOLDEN_RATIO_PRIME 0x9e37fffffffc0001UL
28 #else
29 #error Define GOLDEN_RATIO_PRIME for your wordsize.
30 #endif
31 
32 /*
33  * The above primes are actively bad for hashing, since they are
34  * too sparse. The 32-bit one is mostly ok, the 64-bit one causes
35  * real problems. Besides, the "prime" part is pointless for the
36  * multiplicative hash.
37  *
38  * Although a random odd number will do, it turns out that the golden
39  * ratio phi = (sqrt(5)-1)/2, or its negative, has particularly nice
40  * properties.
41  *
42  * These are the negative, (1 - phi) = (phi^2) = (3 - sqrt(5))/2.
43  * (See Knuth vol 3, section 6.4, exercise 9.)
44  */
45 #define GOLDEN_RATIO_32 0x61C88647
46 #define GOLDEN_RATIO_64 0x61C8864680B583EBull
47 
__hash_long(uint64_t val)48 static inline unsigned long __hash_long(uint64_t val)
49 {
50 	uint64_t hash = val;
51 
52 #if BITS_PER_LONG == 64
53 	hash *= GOLDEN_RATIO_64;
54 #else
55 	/*  Sigh, gcc can't optimise this alone like it does for 32 bits. */
56 	uint64_t n = hash;
57 	n <<= 18;
58 	hash -= n;
59 	n <<= 33;
60 	hash -= n;
61 	n <<= 3;
62 	hash += n;
63 	n <<= 3;
64 	hash -= n;
65 	n <<= 4;
66 	hash += n;
67 	n <<= 2;
68 	hash += n;
69 #endif
70 
71 	return hash;
72 }
73 
hash_long(unsigned long val,unsigned int bits)74 static inline unsigned long hash_long(unsigned long val, unsigned int bits)
75 {
76 	/* High bits are more random, so use them. */
77 	return __hash_long(val) >> (BITS_PER_LONG - bits);
78 }
79 
__hash_u64(uint64_t val)80 static inline uint64_t __hash_u64(uint64_t val)
81 {
82 	return val * GOLDEN_RATIO_64;
83 }
84 
hash_ptr(void * ptr,unsigned int bits)85 static inline unsigned long hash_ptr(void *ptr, unsigned int bits)
86 {
87 	return hash_long((uintptr_t)ptr, bits);
88 }
89 
90 /*
91  * Bob Jenkins jhash
92  */
93 
94 #define JHASH_INITVAL	GOLDEN_RATIO_32
95 
rol32(uint32_t word,uint32_t shift)96 static inline uint32_t rol32(uint32_t word, uint32_t shift)
97 {
98 	return (word << shift) | (word >> (32 - shift));
99 }
100 
101 /* __jhash_mix -- mix 3 32-bit values reversibly. */
102 #define __jhash_mix(a, b, c)			\
103 {						\
104 	a -= c;  a ^= rol32(c, 4);  c += b;	\
105 	b -= a;  b ^= rol32(a, 6);  a += c;	\
106 	c -= b;  c ^= rol32(b, 8);  b += a;	\
107 	a -= c;  a ^= rol32(c, 16); c += b;	\
108 	b -= a;  b ^= rol32(a, 19); a += c;	\
109 	c -= b;  c ^= rol32(b, 4);  b += a;	\
110 }
111 
112 /* __jhash_final - final mixing of 3 32-bit values (a,b,c) into c */
113 #define __jhash_final(a, b, c)			\
114 {						\
115 	c ^= b; c -= rol32(b, 14);		\
116 	a ^= c; a -= rol32(c, 11);		\
117 	b ^= a; b -= rol32(a, 25);		\
118 	c ^= b; c -= rol32(b, 16);		\
119 	a ^= c; a -= rol32(c, 4);		\
120 	b ^= a; b -= rol32(a, 14);		\
121 	c ^= b; c -= rol32(b, 24);		\
122 }
123 
jhash(const void * key,uint32_t length,uint32_t initval)124 static inline uint32_t jhash(const void *key, uint32_t length, uint32_t initval)
125 {
126 	const uint8_t *k = key;
127 	uint32_t a, b, c;
128 
129 	/* Set up the internal state */
130 	a = b = c = JHASH_INITVAL + length + initval;
131 
132 	/* All but the last block: affect some 32 bits of (a,b,c) */
133 	while (length > 12) {
134 		a += *k;
135 		b += *(k + 4);
136 		c += *(k + 8);
137 		__jhash_mix(a, b, c);
138 		length -= 12;
139 		k += 12;
140 	}
141 
142 	/* Last block: affect all 32 bits of (c) */
143 	/* All the case statements fall through */
144 	switch (length) {
145 	case 12: c += (uint32_t) k[11] << 24;	fallthrough;
146 	case 11: c += (uint32_t) k[10] << 16;	fallthrough;
147 	case 10: c += (uint32_t) k[9] << 8;	fallthrough;
148 	case 9:  c += k[8];			fallthrough;
149 	case 8:  b += (uint32_t) k[7] << 24;	fallthrough;
150 	case 7:  b += (uint32_t) k[6] << 16;	fallthrough;
151 	case 6:  b += (uint32_t) k[5] << 8;	fallthrough;
152 	case 5:  b += k[4];			fallthrough;
153 	case 4:  a += (uint32_t) k[3] << 24;	fallthrough;
154 	case 3:  a += (uint32_t) k[2] << 16;	fallthrough;
155 	case 2:  a += (uint32_t) k[1] << 8;	fallthrough;
156 	case 1:  a += k[0];
157 		 __jhash_final(a, b, c);
158 		 fallthrough;
159 	case 0: /* Nothing left to add */
160 		break;
161 	}
162 
163 	return c;
164 }
165 
166 #endif /* _LINUX_HASH_H */
167