7 /* Fast hashing routine for a long.
8 (C) 2002 William Lee Irwin III, IBM */
11 * Knuth recommends primes in approximately golden ratio to the maximum
12 * integer representable by a machine word for multiplicative hashing.
13 * Chuck Lever verified the effectiveness of this technique:
14 * http://www.citi.umich.edu/techreports/reports/citi-tr-00-1.pdf
16 * These primes are chosen to be bit-sparse, that is operations on
17 * them can use shifts and additions instead of multiplications for
18 * machines where multiplications are slow.
21 #if BITS_PER_LONG == 32
22 /* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */
23 #define GOLDEN_RATIO_PRIME 0x9e370001UL
24 #elif BITS_PER_LONG == 64
25 /* 2^63 + 2^61 - 2^57 + 2^54 - 2^51 - 2^18 + 1 */
26 #define GOLDEN_RATIO_PRIME 0x9e37fffffffc0001UL
28 #error Define GOLDEN_RATIO_PRIME for your wordsize.
31 static inline unsigned long __hash_long(unsigned long val)
33 unsigned long hash = val;
35 #if BITS_PER_LONG == 64
36 /* Sigh, gcc can't optimise this alone like it does for 32 bits. */
37 unsigned long n = hash;
51 /* On some cpus multiply is faster, on others gcc will do shifts */
52 hash *= GOLDEN_RATIO_PRIME;
58 static inline unsigned long hash_long(unsigned long val, unsigned int bits)
60 /* High bits are more random, so use them. */
61 return __hash_long(val) >> (BITS_PER_LONG - bits);
64 static inline unsigned long hash_ptr(void *ptr, unsigned int bits)
66 return hash_long((uintptr_t)ptr, bits);
73 #define JHASH_INITVAL GOLDEN_RATIO_PRIME
75 static inline uint32_t rol32(uint32_t word, uint32_t shift)
77 return (word << shift) | (word >> (32 - shift));
80 /* __jhash_mix -- mix 3 32-bit values reversibly. */
81 #define __jhash_mix(a, b, c) \
83 a -= c; a ^= rol32(c, 4); c += b; \
84 b -= a; b ^= rol32(a, 6); a += c; \
85 c -= b; c ^= rol32(b, 8); b += a; \
86 a -= c; a ^= rol32(c, 16); c += b; \
87 b -= a; b ^= rol32(a, 19); a += c; \
88 c -= b; c ^= rol32(b, 4); b += a; \
91 /* __jhash_final - final mixing of 3 32-bit values (a,b,c) into c */
92 #define __jhash_final(a, b, c) \
94 c ^= b; c -= rol32(b, 14); \
95 a ^= c; a -= rol32(c, 11); \
96 b ^= a; b -= rol32(a, 25); \
97 c ^= b; c -= rol32(b, 16); \
98 a ^= c; a -= rol32(c, 4); \
99 b ^= a; b -= rol32(a, 14); \
100 c ^= b; c -= rol32(b, 24); \
103 static inline uint32_t jhash(const void *key, uint32_t length, uint32_t initval)
105 const uint8_t *k = key;
108 /* Set up the internal state */
109 a = b = c = JHASH_INITVAL + length + initval;
111 /* All but the last block: affect some 32 bits of (a,b,c) */
112 while (length > 12) {
116 __jhash_mix(a, b, c);
121 /* Last block: affect all 32 bits of (c) */
122 /* All the case statements fall through */
124 case 12: c += (uint32_t) k[11] << 24;
125 case 11: c += (uint32_t) k[10] << 16;
126 case 10: c += (uint32_t) k[9] << 8;
128 case 8: b += (uint32_t) k[7] << 24;
129 case 7: b += (uint32_t) k[6] << 16;
130 case 6: b += (uint32_t) k[5] << 8;
132 case 4: a += (uint32_t) k[3] << 24;
133 case 3: a += (uint32_t) k[2] << 16;
134 case 2: a += (uint32_t) k[1] << 8;
136 __jhash_final(a, b, c);
137 case 0: /* Nothing left to add */
144 #endif /* _LINUX_HASH_H */