xref: /OK3568_Linux_fs/kernel/tools/include/linux/hash.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun #ifndef _LINUX_HASH_H
2*4882a593Smuzhiyun #define _LINUX_HASH_H
3*4882a593Smuzhiyun /* Fast hashing routine for ints,  longs and pointers.
4*4882a593Smuzhiyun    (C) 2002 Nadia Yvette Chambers, IBM */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun #include <asm/types.h>
7*4882a593Smuzhiyun #include <linux/compiler.h>
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun /*
10*4882a593Smuzhiyun  * The "GOLDEN_RATIO_PRIME" is used in ifs/btrfs/brtfs_inode.h and
11*4882a593Smuzhiyun  * fs/inode.c.  It's not actually prime any more (the previous primes
12*4882a593Smuzhiyun  * were actively bad for hashing), but the name remains.
13*4882a593Smuzhiyun  */
14*4882a593Smuzhiyun #if BITS_PER_LONG == 32
15*4882a593Smuzhiyun #define GOLDEN_RATIO_PRIME GOLDEN_RATIO_32
16*4882a593Smuzhiyun #define hash_long(val, bits) hash_32(val, bits)
17*4882a593Smuzhiyun #elif BITS_PER_LONG == 64
18*4882a593Smuzhiyun #define hash_long(val, bits) hash_64(val, bits)
19*4882a593Smuzhiyun #define GOLDEN_RATIO_PRIME GOLDEN_RATIO_64
20*4882a593Smuzhiyun #else
21*4882a593Smuzhiyun #error Wordsize not 32 or 64
22*4882a593Smuzhiyun #endif
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun /*
25*4882a593Smuzhiyun  * This hash multiplies the input by a large odd number and takes the
26*4882a593Smuzhiyun  * high bits.  Since multiplication propagates changes to the most
27*4882a593Smuzhiyun  * significant end only, it is essential that the high bits of the
28*4882a593Smuzhiyun  * product be used for the hash value.
29*4882a593Smuzhiyun  *
30*4882a593Smuzhiyun  * Chuck Lever verified the effectiveness of this technique:
31*4882a593Smuzhiyun  * http://www.citi.umich.edu/techreports/reports/citi-tr-00-1.pdf
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  * Although a random odd number will do, it turns out that the golden
34*4882a593Smuzhiyun  * ratio phi = (sqrt(5)-1)/2, or its negative, has particularly nice
35*4882a593Smuzhiyun  * properties.  (See Knuth vol 3, section 6.4, exercise 9.)
36*4882a593Smuzhiyun  *
37*4882a593Smuzhiyun  * These are the negative, (1 - phi) = phi**2 = (3 - sqrt(5))/2,
38*4882a593Smuzhiyun  * which is very slightly easier to multiply by and makes no
39*4882a593Smuzhiyun  * difference to the hash distribution.
40*4882a593Smuzhiyun  */
41*4882a593Smuzhiyun #define GOLDEN_RATIO_32 0x61C88647
42*4882a593Smuzhiyun #define GOLDEN_RATIO_64 0x61C8864680B583EBull
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun #ifdef CONFIG_HAVE_ARCH_HASH
45*4882a593Smuzhiyun /* This header may use the GOLDEN_RATIO_xx constants */
46*4882a593Smuzhiyun #include <asm/hash.h>
47*4882a593Smuzhiyun #endif
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun /*
50*4882a593Smuzhiyun  * The _generic versions exist only so lib/test_hash.c can compare
51*4882a593Smuzhiyun  * the arch-optimized versions with the generic.
52*4882a593Smuzhiyun  *
53*4882a593Smuzhiyun  * Note that if you change these, any <asm/hash.h> that aren't updated
54*4882a593Smuzhiyun  * to match need to have their HAVE_ARCH_* define values updated so the
55*4882a593Smuzhiyun  * self-test will not false-positive.
56*4882a593Smuzhiyun  */
57*4882a593Smuzhiyun #ifndef HAVE_ARCH__HASH_32
58*4882a593Smuzhiyun #define __hash_32 __hash_32_generic
59*4882a593Smuzhiyun #endif
__hash_32_generic(u32 val)60*4882a593Smuzhiyun static inline u32 __hash_32_generic(u32 val)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun 	return val * GOLDEN_RATIO_32;
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun #ifndef HAVE_ARCH_HASH_32
66*4882a593Smuzhiyun #define hash_32 hash_32_generic
67*4882a593Smuzhiyun #endif
hash_32_generic(u32 val,unsigned int bits)68*4882a593Smuzhiyun static inline u32 hash_32_generic(u32 val, unsigned int bits)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun 	/* High bits are more random, so use them. */
71*4882a593Smuzhiyun 	return __hash_32(val) >> (32 - bits);
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun #ifndef HAVE_ARCH_HASH_64
75*4882a593Smuzhiyun #define hash_64 hash_64_generic
76*4882a593Smuzhiyun #endif
hash_64_generic(u64 val,unsigned int bits)77*4882a593Smuzhiyun static __always_inline u32 hash_64_generic(u64 val, unsigned int bits)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun #if BITS_PER_LONG == 64
80*4882a593Smuzhiyun 	/* 64x64-bit multiply is efficient on all 64-bit processors */
81*4882a593Smuzhiyun 	return val * GOLDEN_RATIO_64 >> (64 - bits);
82*4882a593Smuzhiyun #else
83*4882a593Smuzhiyun 	/* Hash 64 bits using only 32x32-bit multiply. */
84*4882a593Smuzhiyun 	return hash_32((u32)val ^ __hash_32(val >> 32), bits);
85*4882a593Smuzhiyun #endif
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun 
hash_ptr(const void * ptr,unsigned int bits)88*4882a593Smuzhiyun static inline u32 hash_ptr(const void *ptr, unsigned int bits)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun 	return hash_long((unsigned long)ptr, bits);
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun /* This really should be called fold32_ptr; it does no hashing to speak of. */
hash32_ptr(const void * ptr)94*4882a593Smuzhiyun static inline u32 hash32_ptr(const void *ptr)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun 	unsigned long val = (unsigned long)ptr;
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun #if BITS_PER_LONG == 64
99*4882a593Smuzhiyun 	val ^= (val >> 32);
100*4882a593Smuzhiyun #endif
101*4882a593Smuzhiyun 	return (u32)val;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun #endif /* _LINUX_HASH_H */
105