11da177e4SLinus Torvalds #ifndef _LINUX_HASH_H
21da177e4SLinus Torvalds #define _LINUX_HASH_H
34e701482SMatthew Wilcox /* Fast hashing routine for ints, longs and pointers.
46d49e352SNadia Yvette Chambers (C) 2002 Nadia Yvette Chambers, IBM */
51da177e4SLinus Torvalds
64e701482SMatthew Wilcox #include <asm/types.h>
765c10553SMasami Hiramatsu #include <linux/compiler.h>
84e701482SMatthew Wilcox
9ef703f49SGeorge Spelvin /*
10ef703f49SGeorge Spelvin * The "GOLDEN_RATIO_PRIME" is used in ifs/btrfs/brtfs_inode.h and
11ef703f49SGeorge Spelvin * fs/inode.c. It's not actually prime any more (the previous primes
12ef703f49SGeorge Spelvin * were actively bad for hashing), but the name remains.
13ef703f49SGeorge Spelvin */
144e701482SMatthew Wilcox #if BITS_PER_LONG == 32
15ef703f49SGeorge Spelvin #define GOLDEN_RATIO_PRIME GOLDEN_RATIO_32
164e701482SMatthew Wilcox #define hash_long(val, bits) hash_32(val, bits)
174e701482SMatthew Wilcox #elif BITS_PER_LONG == 64
184e701482SMatthew Wilcox #define hash_long(val, bits) hash_64(val, bits)
19ef703f49SGeorge Spelvin #define GOLDEN_RATIO_PRIME GOLDEN_RATIO_64
201da177e4SLinus Torvalds #else
214e701482SMatthew Wilcox #error Wordsize not 32 or 64
221da177e4SLinus Torvalds #endif
231da177e4SLinus Torvalds
24689de1d6SLinus Torvalds /*
25ef703f49SGeorge Spelvin * This hash multiplies the input by a large odd number and takes the
26ef703f49SGeorge Spelvin * high bits. Since multiplication propagates changes to the most
27ef703f49SGeorge Spelvin * significant end only, it is essential that the high bits of the
28ef703f49SGeorge Spelvin * product be used for the hash value.
29ef703f49SGeorge Spelvin *
30ef703f49SGeorge Spelvin * Chuck Lever verified the effectiveness of this technique:
31ef703f49SGeorge Spelvin * http://www.citi.umich.edu/techreports/reports/citi-tr-00-1.pdf
32689de1d6SLinus Torvalds *
33689de1d6SLinus Torvalds * Although a random odd number will do, it turns out that the golden
34689de1d6SLinus Torvalds * ratio phi = (sqrt(5)-1)/2, or its negative, has particularly nice
35ef703f49SGeorge Spelvin * properties. (See Knuth vol 3, section 6.4, exercise 9.)
36689de1d6SLinus Torvalds *
37ef703f49SGeorge Spelvin * These are the negative, (1 - phi) = phi**2 = (3 - sqrt(5))/2,
38ef703f49SGeorge Spelvin * which is very slightly easier to multiply by and makes no
39ef703f49SGeorge Spelvin * difference to the hash distribution.
40689de1d6SLinus Torvalds */
41689de1d6SLinus Torvalds #define GOLDEN_RATIO_32 0x61C88647
42689de1d6SLinus Torvalds #define GOLDEN_RATIO_64 0x61C8864680B583EBull
43689de1d6SLinus Torvalds
44468a9428SGeorge Spelvin #ifdef CONFIG_HAVE_ARCH_HASH
45468a9428SGeorge Spelvin /* This header may use the GOLDEN_RATIO_xx constants */
46468a9428SGeorge Spelvin #include <asm/hash.h>
47468a9428SGeorge Spelvin #endif
48ef703f49SGeorge Spelvin
49468a9428SGeorge Spelvin /*
50468a9428SGeorge Spelvin * The _generic versions exist only so lib/test_hash.c can compare
51468a9428SGeorge Spelvin * the arch-optimized versions with the generic.
52468a9428SGeorge Spelvin *
53468a9428SGeorge Spelvin * Note that if you change these, any <asm/hash.h> that aren't updated
54468a9428SGeorge Spelvin * to match need to have their HAVE_ARCH_* define values updated so the
55468a9428SGeorge Spelvin * self-test will not false-positive.
56468a9428SGeorge Spelvin */
57468a9428SGeorge Spelvin #ifndef HAVE_ARCH__HASH_32
58468a9428SGeorge Spelvin #define __hash_32 __hash_32_generic
59468a9428SGeorge Spelvin #endif
__hash_32_generic(u32 val)60468a9428SGeorge Spelvin static inline u32 __hash_32_generic(u32 val)
611da177e4SLinus Torvalds {
62ef703f49SGeorge Spelvin return val * GOLDEN_RATIO_32;
634e701482SMatthew Wilcox }
644e701482SMatthew Wilcox
hash_32(u32 val,unsigned int bits)65*fd0a1462SIsabella Basso static inline u32 hash_32(u32 val, unsigned int bits)
664e701482SMatthew Wilcox {
674e701482SMatthew Wilcox /* High bits are more random, so use them. */
68ef703f49SGeorge Spelvin return __hash_32(val) >> (32 - bits);
69ef703f49SGeorge Spelvin }
70ef703f49SGeorge Spelvin
71468a9428SGeorge Spelvin #ifndef HAVE_ARCH_HASH_64
72468a9428SGeorge Spelvin #define hash_64 hash_64_generic
73468a9428SGeorge Spelvin #endif
hash_64_generic(u64 val,unsigned int bits)74468a9428SGeorge Spelvin static __always_inline u32 hash_64_generic(u64 val, unsigned int bits)
75ef703f49SGeorge Spelvin {
76ef703f49SGeorge Spelvin #if BITS_PER_LONG == 64
77ef703f49SGeorge Spelvin /* 64x64-bit multiply is efficient on all 64-bit processors */
78ef703f49SGeorge Spelvin return val * GOLDEN_RATIO_64 >> (64 - bits);
79ef703f49SGeorge Spelvin #else
80ef703f49SGeorge Spelvin /* Hash 64 bits using only 32x32-bit multiply. */
81ef703f49SGeorge Spelvin return hash_32((u32)val ^ __hash_32(val >> 32), bits);
82ef703f49SGeorge Spelvin #endif
831da177e4SLinus Torvalds }
841da177e4SLinus Torvalds
hash_ptr(const void * ptr,unsigned int bits)8592d56774SGeorge Spelvin static inline u32 hash_ptr(const void *ptr, unsigned int bits)
861da177e4SLinus Torvalds {
871da177e4SLinus Torvalds return hash_long((unsigned long)ptr, bits);
881da177e4SLinus Torvalds }
89b14f243aSPavel Emelyanov
90ef703f49SGeorge Spelvin /* This really should be called fold32_ptr; it does no hashing to speak of. */
hash32_ptr(const void * ptr)91b14f243aSPavel Emelyanov static inline u32 hash32_ptr(const void *ptr)
92b14f243aSPavel Emelyanov {
93b14f243aSPavel Emelyanov unsigned long val = (unsigned long)ptr;
94b14f243aSPavel Emelyanov
95b14f243aSPavel Emelyanov #if BITS_PER_LONG == 64
96b14f243aSPavel Emelyanov val ^= (val >> 32);
97b14f243aSPavel Emelyanov #endif
98b14f243aSPavel Emelyanov return (u32)val;
99b14f243aSPavel Emelyanov }
10071ae8aacSFrancesco Fusco
1011da177e4SLinus Torvalds #endif /* _LINUX_HASH_H */
102