aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux/jhash.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/linux/jhash.h')
-rw-r--r--include/linux/jhash.h183
1 files changed, 105 insertions, 78 deletions
diff --git a/include/linux/jhash.h b/include/linux/jhash.h
index 2a2f99fbcb16..47cb09edec1a 100644
--- a/include/linux/jhash.h
+++ b/include/linux/jhash.h
@@ -3,129 +3,156 @@
3 3
4/* jhash.h: Jenkins hash support. 4/* jhash.h: Jenkins hash support.
5 * 5 *
6 * Copyright (C) 1996 Bob Jenkins (bob_jenkins@burtleburtle.net) 6 * Copyright (C) 2006. Bob Jenkins (bob_jenkins@burtleburtle.net)
7 * 7 *
8 * http://burtleburtle.net/bob/hash/ 8 * http://burtleburtle.net/bob/hash/
9 * 9 *
10 * These are the credits from Bob's sources: 10 * These are the credits from Bob's sources:
11 * 11 *
12 * lookup2.c, by Bob Jenkins, December 1996, Public Domain. 12 * lookup3.c, by Bob Jenkins, May 2006, Public Domain.
13 * hash(), hash2(), hash3, and mix() are externally useful functions.
14 * Routines to test the hash are included if SELF_TEST is defined.
15 * You can use this free for any purpose. It has no warranty.
16 * 13 *
17 * Copyright (C) 2003 David S. Miller (davem@redhat.com) 14 * These are functions for producing 32-bit hashes for hash table lookup.
15 * hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
16 * are externally useful functions. Routines to test the hash are included
17 * if SELF_TEST is defined. You can use this free for any purpose. It's in
18 * the public domain. It has no warranty.
19 *
20 * Copyright (C) 2009-2010 Jozsef Kadlecsik (kadlec@blackhole.kfki.hu)
18 * 21 *
19 * I've modified Bob's hash to be useful in the Linux kernel, and 22 * I've modified Bob's hash to be useful in the Linux kernel, and
20 * any bugs present are surely my fault. -DaveM 23 * any bugs present are my fault.
24 * Jozsef
21 */ 25 */
26#include <linux/bitops.h>
27#include <linux/unaligned/packed_struct.h>
28
29/* Best hash sizes are of power of two */
30#define jhash_size(n) ((u32)1<<(n))
31/* Mask the hash value, i.e (value & jhash_mask(n)) instead of (value % n) */
32#define jhash_mask(n) (jhash_size(n)-1)
33
34/* __jhash_mix -- mix 3 32-bit values reversibly. */
35#define __jhash_mix(a, b, c) \
36{ \
37 a -= c; a ^= rol32(c, 4); c += b; \
38 b -= a; b ^= rol32(a, 6); a += c; \
39 c -= b; c ^= rol32(b, 8); b += a; \
40 a -= c; a ^= rol32(c, 16); c += b; \
41 b -= a; b ^= rol32(a, 19); a += c; \
42 c -= b; c ^= rol32(b, 4); b += a; \
43}
22 44
23/* NOTE: Arguments are modified. */ 45/* __jhash_final - final mixing of 3 32-bit values (a,b,c) into c */
24#define __jhash_mix(a, b, c) \ 46#define __jhash_final(a, b, c) \
25{ \ 47{ \
26 a -= b; a -= c; a ^= (c>>13); \ 48 c ^= b; c -= rol32(b, 14); \
27 b -= c; b -= a; b ^= (a<<8); \ 49 a ^= c; a -= rol32(c, 11); \
28 c -= a; c -= b; c ^= (b>>13); \ 50 b ^= a; b -= rol32(a, 25); \
29 a -= b; a -= c; a ^= (c>>12); \ 51 c ^= b; c -= rol32(b, 16); \
30 b -= c; b -= a; b ^= (a<<16); \ 52 a ^= c; a -= rol32(c, 4); \
31 c -= a; c -= b; c ^= (b>>5); \ 53 b ^= a; b -= rol32(a, 14); \
32 a -= b; a -= c; a ^= (c>>3); \ 54 c ^= b; c -= rol32(b, 24); \
33 b -= c; b -= a; b ^= (a<<10); \
34 c -= a; c -= b; c ^= (b>>15); \
35} 55}
36 56
37/* The golden ration: an arbitrary value */ 57/* An arbitrary initial parameter */
38#define JHASH_GOLDEN_RATIO 0x9e3779b9 58#define JHASH_INITVAL 0xdeadbeef
39 59
40/* The most generic version, hashes an arbitrary sequence 60/* jhash - hash an arbitrary key
41 * of bytes. No alignment or length assumptions are made about 61 * @k: sequence of bytes as key
42 * the input key. 62 * @length: the length of the key
63 * @initval: the previous hash, or an arbitray value
64 *
65 * The generic version, hashes an arbitrary sequence of bytes.
66 * No alignment or length assumptions are made about the input key.
67 *
68 * Returns the hash value of the key. The result depends on endianness.
43 */ 69 */
44static inline u32 jhash(const void *key, u32 length, u32 initval) 70static inline u32 jhash(const void *key, u32 length, u32 initval)
45{ 71{
46 u32 a, b, c, len; 72 u32 a, b, c;
47 const u8 *k = key; 73 const u8 *k = key;
48 74
49 len = length; 75 /* Set up the internal state */
50 a = b = JHASH_GOLDEN_RATIO; 76 a = b = c = JHASH_INITVAL + length + initval;
51 c = initval;
52
53 while (len >= 12) {
54 a += (k[0] +((u32)k[1]<<8) +((u32)k[2]<<16) +((u32)k[3]<<24));
55 b += (k[4] +((u32)k[5]<<8) +((u32)k[6]<<16) +((u32)k[7]<<24));
56 c += (k[8] +((u32)k[9]<<8) +((u32)k[10]<<16)+((u32)k[11]<<24));
57
58 __jhash_mix(a,b,c);
59 77
78 /* All but the last block: affect some 32 bits of (a,b,c) */
79 while (length > 12) {
80 a += __get_unaligned_cpu32(k);
81 b += __get_unaligned_cpu32(k + 4);
82 c += __get_unaligned_cpu32(k + 8);
83 __jhash_mix(a, b, c);
84 length -= 12;
60 k += 12; 85 k += 12;
61 len -= 12;
62 } 86 }
63 87 /* Last block: affect all 32 bits of (c) */
64 c += length; 88 /* All the case statements fall through */
65 switch (len) { 89 switch (length) {
66 case 11: c += ((u32)k[10]<<24); 90 case 12: c += (u32)k[11]<<24;
67 case 10: c += ((u32)k[9]<<16); 91 case 11: c += (u32)k[10]<<16;
68 case 9 : c += ((u32)k[8]<<8); 92 case 10: c += (u32)k[9]<<8;
69 case 8 : b += ((u32)k[7]<<24); 93 case 9: c += k[8];
70 case 7 : b += ((u32)k[6]<<16); 94 case 8: b += (u32)k[7]<<24;
71 case 6 : b += ((u32)k[5]<<8); 95 case 7: b += (u32)k[6]<<16;
72 case 5 : b += k[4]; 96 case 6: b += (u32)k[5]<<8;
73 case 4 : a += ((u32)k[3]<<24); 97 case 5: b += k[4];
74 case 3 : a += ((u32)k[2]<<16); 98 case 4: a += (u32)k[3]<<24;
75 case 2 : a += ((u32)k[1]<<8); 99 case 3: a += (u32)k[2]<<16;
76 case 1 : a += k[0]; 100 case 2: a += (u32)k[1]<<8;
77 }; 101 case 1: a += k[0];
78 102 __jhash_final(a, b, c);
79 __jhash_mix(a,b,c); 103 case 0: /* Nothing left to add */
104 break;
105 }
80 106
81 return c; 107 return c;
82} 108}
83 109
84/* A special optimized version that handles 1 or more of u32s. 110/* jhash2 - hash an array of u32's
85 * The length parameter here is the number of u32s in the key. 111 * @k: the key which must be an array of u32's
112 * @length: the number of u32's in the key
113 * @initval: the previous hash, or an arbitray value
114 *
115 * Returns the hash value of the key.
86 */ 116 */
87static inline u32 jhash2(const u32 *k, u32 length, u32 initval) 117static inline u32 jhash2(const u32 *k, u32 length, u32 initval)
88{ 118{
89 u32 a, b, c, len; 119 u32 a, b, c;
90 120
91 a = b = JHASH_GOLDEN_RATIO; 121 /* Set up the internal state */
92 c = initval; 122 a = b = c = JHASH_INITVAL + (length<<2) + initval;
93 len = length;
94 123
95 while (len >= 3) { 124 /* Handle most of the key */
125 while (length > 3) {
96 a += k[0]; 126 a += k[0];
97 b += k[1]; 127 b += k[1];
98 c += k[2]; 128 c += k[2];
99 __jhash_mix(a, b, c); 129 __jhash_mix(a, b, c);
100 k += 3; len -= 3; 130 length -= 3;
131 k += 3;
101 } 132 }
102 133
103 c += length * 4; 134 /* Handle the last 3 u32's: all the case statements fall through */
104 135 switch (length) {
105 switch (len) { 136 case 3: c += k[2];
106 case 2 : b += k[1]; 137 case 2: b += k[1];
107 case 1 : a += k[0]; 138 case 1: a += k[0];
108 }; 139 __jhash_final(a, b, c);
109 140 case 0: /* Nothing left to add */
110 __jhash_mix(a,b,c); 141 break;
142 }
111 143
112 return c; 144 return c;
113} 145}
114 146
115 147
116/* A special ultra-optimized versions that knows they are hashing exactly 148/* jhash_3words - hash exactly 3, 2 or 1 word(s) */
117 * 3, 2 or 1 word(s).
118 *
119 * NOTE: In partilar the "c += length; __jhash_mix(a,b,c);" normally
120 * done at the end is not done here.
121 */
122static inline u32 jhash_3words(u32 a, u32 b, u32 c, u32 initval) 149static inline u32 jhash_3words(u32 a, u32 b, u32 c, u32 initval)
123{ 150{
124 a += JHASH_GOLDEN_RATIO; 151 a += JHASH_INITVAL;
125 b += JHASH_GOLDEN_RATIO; 152 b += JHASH_INITVAL;
126 c += initval; 153 c += initval;
127 154
128 __jhash_mix(a, b, c); 155 __jhash_final(a, b, c);
129 156
130 return c; 157 return c;
131} 158}