blob: 79c52fa81cac9ea2dd3a1bea6e1093cd81fdb79c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_HASH_H
2#define _LINUX_HASH_H
Matthew Wilcox4e701482008-02-06 01:36:14 -08003/* Fast hashing routine for ints, longs and pointers.
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +01004 (C) 2002 Nadia Yvette Chambers, IBM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005
6/*
7 * Knuth recommends primes in approximately golden ratio to the maximum
8 * integer representable by a machine word for multiplicative hashing.
9 * Chuck Lever verified the effectiveness of this technique:
10 * http://www.citi.umich.edu/techreports/reports/citi-tr-00-1.pdf
11 *
12 * These primes are chosen to be bit-sparse, that is operations on
13 * them can use shifts and additions instead of multiplications for
14 * machines where multiplications are slow.
15 */
Matthew Wilcox4e701482008-02-06 01:36:14 -080016
17#include <asm/types.h>
Masami Hiramatsu65c10552013-03-14 20:52:30 +090018#include <linux/compiler.h>
Matthew Wilcox4e701482008-02-06 01:36:14 -080019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020/* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */
Matthew Wilcox4e701482008-02-06 01:36:14 -080021#define GOLDEN_RATIO_PRIME_32 0x9e370001UL
Linus Torvalds1da177e2005-04-16 15:20:36 -070022/* 2^63 + 2^61 - 2^57 + 2^54 - 2^51 - 2^18 + 1 */
Matthew Wilcox4e701482008-02-06 01:36:14 -080023#define GOLDEN_RATIO_PRIME_64 0x9e37fffffffc0001UL
24
25#if BITS_PER_LONG == 32
26#define GOLDEN_RATIO_PRIME GOLDEN_RATIO_PRIME_32
27#define hash_long(val, bits) hash_32(val, bits)
28#elif BITS_PER_LONG == 64
29#define hash_long(val, bits) hash_64(val, bits)
30#define GOLDEN_RATIO_PRIME GOLDEN_RATIO_PRIME_64
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#else
Matthew Wilcox4e701482008-02-06 01:36:14 -080032#error Wordsize not 32 or 64
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#endif
34
Linus Torvalds689de1d2016-05-02 12:46:42 -070035/*
36 * The above primes are actively bad for hashing, since they are
37 * too sparse. The 32-bit one is mostly ok, the 64-bit one causes
38 * real problems. Besides, the "prime" part is pointless for the
39 * multiplicative hash.
40 *
41 * Although a random odd number will do, it turns out that the golden
42 * ratio phi = (sqrt(5)-1)/2, or its negative, has particularly nice
43 * properties.
44 *
45 * These are the negative, (1 - phi) = (phi^2) = (3 - sqrt(5))/2.
46 * (See Knuth vol 3, section 6.4, exercise 9.)
47 */
48#define GOLDEN_RATIO_32 0x61C88647
49#define GOLDEN_RATIO_64 0x61C8864680B583EBull
50
Masami Hiramatsu65c10552013-03-14 20:52:30 +090051static __always_inline u64 hash_64(u64 val, unsigned int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Matthew Wilcox4e701482008-02-06 01:36:14 -080053 u64 hash = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds689de1d2016-05-02 12:46:42 -070055#if BITS_PER_LONG == 64
56 hash = hash * GOLDEN_RATIO_64;
Linus Torvalds23d0db72014-09-13 11:24:03 -070057#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 /* Sigh, gcc can't optimise this alone like it does for 32 bits. */
Matthew Wilcox4e701482008-02-06 01:36:14 -080059 u64 n = hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 n <<= 18;
61 hash -= n;
62 n <<= 33;
63 hash -= n;
64 n <<= 3;
65 hash += n;
66 n <<= 3;
67 hash -= n;
68 n <<= 4;
69 hash += n;
70 n <<= 2;
71 hash += n;
Linus Torvalds23d0db72014-09-13 11:24:03 -070072#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 /* High bits are more random, so use them. */
Matthew Wilcox4e701482008-02-06 01:36:14 -080075 return hash >> (64 - bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
Matthew Wilcox4e701482008-02-06 01:36:14 -080077
78static inline u32 hash_32(u32 val, unsigned int bits)
79{
80 /* On some cpus multiply is faster, on others gcc will do shifts */
81 u32 hash = val * GOLDEN_RATIO_PRIME_32;
82
83 /* High bits are more random, so use them. */
84 return hash >> (32 - bits);
85}
86
Ian Campbellf9918792011-08-17 13:45:09 +010087static inline unsigned long hash_ptr(const void *ptr, unsigned int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 return hash_long((unsigned long)ptr, bits);
90}
Pavel Emelyanovb14f2432012-08-08 21:52:28 +000091
92static inline u32 hash32_ptr(const void *ptr)
93{
94 unsigned long val = (unsigned long)ptr;
95
96#if BITS_PER_LONG == 64
97 val ^= (val >> 32);
98#endif
99 return (u32)val;
100}
Francesco Fusco71ae8aa2013-12-12 16:09:05 +0100101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#endif /* _LINUX_HASH_H */