Whamcloud - gitweb
LU-9859 libcfs: make lnet_debugfs_symlink_def local to libcfs/modules.c
[fs/lustre-release.git] / libcfs / include / libcfs / util / hash.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22
23 #ifndef _LINUX_HASH_H
24 #define _LINUX_HASH_H
25 /* Fast hashing routine for ints,  longs and pointers.
26    (C) 2002 Nadia Yvette Chambers, IBM */
27
28 /*
29  * Knuth recommends primes in approximately golden ratio to the maximum
30  * integer representable by a machine word for multiplicative hashing.
31  * Chuck Lever verified the effectiveness of this technique:
32  * http://www.citi.umich.edu/techreports/reports/citi-tr-00-1.pdf
33  *
34  * These primes are chosen to be bit-sparse, that is operations on
35  * them can use shifts and additions instead of multiplications for
36  * machines where multiplications are slow.
37  */
38
39 #include <linux/types.h>
40
41 /* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */
42 #define GOLDEN_RATIO_PRIME_32 0x9e370001UL
43 /*  2^63 + 2^61 - 2^57 + 2^54 - 2^51 - 2^18 + 1 */
44 #define GOLDEN_RATIO_PRIME_64 0x9e37fffffffc0001UL
45
46 #if __BITS_PER_LONG == 32
47 #define GOLDEN_RATIO_PRIME GOLDEN_RATIO_PRIME_32
48 #define hash_long(val, bits) hash_32(val, bits)
49 #elif __BITS_PER_LONG == 64
50 #define hash_long(val, bits) hash_64(val, bits)
51 #define GOLDEN_RATIO_PRIME GOLDEN_RATIO_PRIME_64
52 #else
53 #error Wordsize not 32 or 64
54 #endif
55
56 static __always_inline __u64 hash_64(__u64 val, unsigned int bits)
57 {
58         __u64 hash = val;
59
60         /*  Sigh, gcc can't optimise this alone like it does for 32 bits. */
61         __u64 n = hash;
62         n <<= 18;
63         hash -= n;
64         n <<= 33;
65         hash -= n;
66         n <<= 3;
67         hash += n;
68         n <<= 3;
69         hash -= n;
70         n <<= 4;
71         hash += n;
72         n <<= 2;
73         hash += n;
74
75         /* High bits are more random, so use them. */
76         return hash >> (64 - bits);
77 }
78
79 static inline __u32 hash_32(__u32 val, unsigned int bits)
80 {
81         /* On some cpus multiply is faster, on others gcc will do shifts */
82         __u32 hash = val * GOLDEN_RATIO_PRIME_32;
83
84         /* High bits are more random, so use them. */
85         return hash >> (32 - bits);
86 }
87
88 static inline unsigned long hash_ptr(const void *ptr, unsigned int bits)
89 {
90         return hash_long((unsigned long)ptr, bits);
91 }
92
93 static inline __u32 hash32_ptr(const void *ptr)
94 {
95         unsigned long val = (unsigned long)ptr;
96
97 #if __BITS_PER_LONG == 64
98         val ^= (val >> 32);
99 #endif
100         return (__u32)val;
101 }
102
103 #endif /* _LINUX_HASH_H */