| 1 | --- a/include/linux/jhash.h |
| 2 | +++ b/include/linux/jhash.h |
| 3 | @@ -3,80 +3,95 @@ |
| 4 | |
| 5 | /* jhash.h: Jenkins hash support. |
| 6 | * |
| 7 | - * Copyright (C) 1996 Bob Jenkins (bob_jenkins@burtleburtle.net) |
| 8 | + * Copyright (C) 2006. Bob Jenkins (bob_jenkins@burtleburtle.net) |
| 9 | * |
| 10 | * http://burtleburtle.net/bob/hash/ |
| 11 | * |
| 12 | * These are the credits from Bob's sources: |
| 13 | * |
| 14 | - * lookup2.c, by Bob Jenkins, December 1996, Public Domain. |
| 15 | - * hash(), hash2(), hash3, and mix() are externally useful functions. |
| 16 | - * Routines to test the hash are included if SELF_TEST is defined. |
| 17 | - * You can use this free for any purpose. It has no warranty. |
| 18 | + * lookup3.c, by Bob Jenkins, May 2006, Public Domain. |
| 19 | * |
| 20 | - * Copyright (C) 2003 David S. Miller (davem@redhat.com) |
| 21 | + * These are functions for producing 32-bit hashes for hash table lookup. |
| 22 | + * hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final() |
| 23 | + * are externally useful functions. Routines to test the hash are included |
| 24 | + * if SELF_TEST is defined. You can use this free for any purpose. It's in |
| 25 | + * the public domain. It has no warranty. |
| 26 | + * |
| 27 | + * Copyright (C) 2009 Jozsef Kadlecsik (kadlec@blackhole.kfki.hu) |
| 28 | * |
| 29 | * I've modified Bob's hash to be useful in the Linux kernel, and |
| 30 | - * any bugs present are surely my fault. -DaveM |
| 31 | + * any bugs present are my fault. Jozsef |
| 32 | */ |
| 33 | |
| 34 | -/* NOTE: Arguments are modified. */ |
| 35 | -#define __jhash_mix(a, b, c) \ |
| 36 | +#define __rot(x,k) (((x)<<(k)) | ((x)>>(32-(k)))) |
| 37 | + |
| 38 | +/* __jhash_mix - mix 3 32-bit values reversibly. */ |
| 39 | +#define __jhash_mix(a,b,c) \ |
| 40 | { \ |
| 41 | - a -= b; a -= c; a ^= (c>>13); \ |
| 42 | - b -= c; b -= a; b ^= (a<<8); \ |
| 43 | - c -= a; c -= b; c ^= (b>>13); \ |
| 44 | - a -= b; a -= c; a ^= (c>>12); \ |
| 45 | - b -= c; b -= a; b ^= (a<<16); \ |
| 46 | - c -= a; c -= b; c ^= (b>>5); \ |
| 47 | - a -= b; a -= c; a ^= (c>>3); \ |
| 48 | - b -= c; b -= a; b ^= (a<<10); \ |
| 49 | - c -= a; c -= b; c ^= (b>>15); \ |
| 50 | + a -= c; a ^= __rot(c, 4); c += b; \ |
| 51 | + b -= a; b ^= __rot(a, 6); a += c; \ |
| 52 | + c -= b; c ^= __rot(b, 8); b += a; \ |
| 53 | + a -= c; a ^= __rot(c,16); c += b; \ |
| 54 | + b -= a; b ^= __rot(a,19); a += c; \ |
| 55 | + c -= b; c ^= __rot(b, 4); b += a; \ |
| 56 | } |
| 57 | |
| 58 | -/* The golden ration: an arbitrary value */ |
| 59 | -#define JHASH_GOLDEN_RATIO 0x9e3779b9 |
| 60 | +/* __jhash_final - final mixing of 3 32-bit values (a,b,c) into c */ |
| 61 | +#define __jhash_final(a,b,c) \ |
| 62 | +{ \ |
| 63 | + c ^= b; c -= __rot(b,14); \ |
| 64 | + a ^= c; a -= __rot(c,11); \ |
| 65 | + b ^= a; b -= __rot(a,25); \ |
| 66 | + c ^= b; c -= __rot(b,16); \ |
| 67 | + a ^= c; a -= __rot(c,4); \ |
| 68 | + b ^= a; b -= __rot(a,14); \ |
| 69 | + c ^= b; c -= __rot(b,24); \ |
| 70 | +} |
| 71 | + |
| 72 | +/* An arbitrary initial parameter */ |
| 73 | +#define JHASH_GOLDEN_RATIO 0xdeadbeef |
| 74 | |
| 75 | /* The most generic version, hashes an arbitrary sequence |
| 76 | * of bytes. No alignment or length assumptions are made about |
| 77 | - * the input key. |
| 78 | + * the input key. The result depends on endianness. |
| 79 | */ |
| 80 | static inline u32 jhash(const void *key, u32 length, u32 initval) |
| 81 | { |
| 82 | - u32 a, b, c, len; |
| 83 | + u32 a,b,c; |
| 84 | const u8 *k = key; |
| 85 | |
| 86 | - len = length; |
| 87 | - a = b = JHASH_GOLDEN_RATIO; |
| 88 | - c = initval; |
| 89 | - |
| 90 | - while (len >= 12) { |
| 91 | - a += (k[0] +((u32)k[1]<<8) +((u32)k[2]<<16) +((u32)k[3]<<24)); |
| 92 | - b += (k[4] +((u32)k[5]<<8) +((u32)k[6]<<16) +((u32)k[7]<<24)); |
| 93 | - c += (k[8] +((u32)k[9]<<8) +((u32)k[10]<<16)+((u32)k[11]<<24)); |
| 94 | - |
| 95 | - __jhash_mix(a,b,c); |
| 96 | + /* Set up the internal state */ |
| 97 | + a = b = c = JHASH_GOLDEN_RATIO + length + initval; |
| 98 | |
| 99 | + /* all but the last block: affect some 32 bits of (a,b,c) */ |
| 100 | + while (length > 12) { |
| 101 | + a += (k[0] + ((u32)k[1]<<8) + ((u32)k[2]<<16) + ((u32)k[3]<<24)); |
| 102 | + b += (k[4] + ((u32)k[5]<<8) + ((u32)k[6]<<16) + ((u32)k[7]<<24)); |
| 103 | + c += (k[8] + ((u32)k[9]<<8) + ((u32)k[10]<<16) + ((u32)k[11]<<24)); |
| 104 | + __jhash_mix(a, b, c); |
| 105 | + length -= 12; |
| 106 | k += 12; |
| 107 | - len -= 12; |
| 108 | } |
| 109 | |
| 110 | - c += length; |
| 111 | - switch (len) { |
| 112 | - case 11: c += ((u32)k[10]<<24); |
| 113 | - case 10: c += ((u32)k[9]<<16); |
| 114 | - case 9 : c += ((u32)k[8]<<8); |
| 115 | - case 8 : b += ((u32)k[7]<<24); |
| 116 | - case 7 : b += ((u32)k[6]<<16); |
| 117 | - case 6 : b += ((u32)k[5]<<8); |
| 118 | + /* last block: affect all 32 bits of (c) */ |
| 119 | + /* all the case statements fall through */ |
| 120 | + switch (length) { |
| 121 | + case 12: c += (u32)k[11]<<24; |
| 122 | + case 11: c += (u32)k[10]<<16; |
| 123 | + case 10: c += (u32)k[9]<<8; |
| 124 | + case 9 : c += k[8]; |
| 125 | + case 8 : b += (u32)k[7]<<24; |
| 126 | + case 7 : b += (u32)k[6]<<16; |
| 127 | + case 6 : b += (u32)k[5]<<8; |
| 128 | case 5 : b += k[4]; |
| 129 | - case 4 : a += ((u32)k[3]<<24); |
| 130 | - case 3 : a += ((u32)k[2]<<16); |
| 131 | - case 2 : a += ((u32)k[1]<<8); |
| 132 | + case 4 : a += (u32)k[3]<<24; |
| 133 | + case 3 : a += (u32)k[2]<<16; |
| 134 | + case 2 : a += (u32)k[1]<<8; |
| 135 | case 1 : a += k[0]; |
| 136 | - }; |
| 137 | - |
| 138 | - __jhash_mix(a,b,c); |
| 139 | + __jhash_final(a, b, c); |
| 140 | + case 0 : |
| 141 | + break; |
| 142 | + } |
| 143 | |
| 144 | return c; |
| 145 | } |
| 146 | @@ -86,28 +101,31 @@ static inline u32 jhash(const void *key, |
| 147 | */ |
| 148 | static inline u32 jhash2(const u32 *k, u32 length, u32 initval) |
| 149 | { |
| 150 | - u32 a, b, c, len; |
| 151 | + u32 a, b, c; |
| 152 | |
| 153 | - a = b = JHASH_GOLDEN_RATIO; |
| 154 | - c = initval; |
| 155 | - len = length; |
| 156 | + /* Set up the internal state */ |
| 157 | + a = b = c = JHASH_GOLDEN_RATIO + (length<<2) + initval; |
| 158 | |
| 159 | - while (len >= 3) { |
| 160 | + /* handle most of the key */ |
| 161 | + while (length > 3) { |
| 162 | a += k[0]; |
| 163 | b += k[1]; |
| 164 | c += k[2]; |
| 165 | __jhash_mix(a, b, c); |
| 166 | - k += 3; len -= 3; |
| 167 | + length -= 3; |
| 168 | + k += 3; |
| 169 | } |
| 170 | |
| 171 | - c += length * 4; |
| 172 | - |
| 173 | - switch (len) { |
| 174 | - case 2 : b += k[1]; |
| 175 | - case 1 : a += k[0]; |
| 176 | - }; |
| 177 | - |
| 178 | - __jhash_mix(a,b,c); |
| 179 | + /* handle the last 3 u32's */ |
| 180 | + /* all the case statements fall through */ |
| 181 | + switch (length) { |
| 182 | + case 3: c += k[2]; |
| 183 | + case 2: b += k[1]; |
| 184 | + case 1: a += k[0]; |
| 185 | + __jhash_final(a, b, c); |
| 186 | + case 0: /* case 0: nothing left to add */ |
| 187 | + break; |
| 188 | + } |
| 189 | |
| 190 | return c; |
| 191 | } |
| 192 | @@ -115,29 +133,26 @@ static inline u32 jhash2(const u32 *k, u |
| 193 | |
| 194 | /* A special ultra-optimized versions that knows they are hashing exactly |
| 195 | * 3, 2 or 1 word(s). |
| 196 | - * |
| 197 | - * NOTE: In particular the "c += length; __jhash_mix(a,b,c);" normally |
| 198 | - * done at the end is not done here. |
| 199 | */ |
| 200 | static inline u32 jhash_3words(u32 a, u32 b, u32 c, u32 initval) |
| 201 | { |
| 202 | - a += JHASH_GOLDEN_RATIO; |
| 203 | - b += JHASH_GOLDEN_RATIO; |
| 204 | - c += initval; |
| 205 | + a += JHASH_GOLDEN_RATIO + initval; |
| 206 | + b += JHASH_GOLDEN_RATIO + initval; |
| 207 | + c += JHASH_GOLDEN_RATIO + initval; |
| 208 | |
| 209 | - __jhash_mix(a, b, c); |
| 210 | + __jhash_final(a, b, c); |
| 211 | |
| 212 | return c; |
| 213 | } |
| 214 | |
| 215 | static inline u32 jhash_2words(u32 a, u32 b, u32 initval) |
| 216 | { |
| 217 | - return jhash_3words(a, b, 0, initval); |
| 218 | + return jhash_3words(0, a, b, initval); |
| 219 | } |
| 220 | |
| 221 | static inline u32 jhash_1word(u32 a, u32 initval) |
| 222 | { |
| 223 | - return jhash_3words(a, 0, 0, initval); |
| 224 | + return jhash_3words(0, 0, a, initval); |
| 225 | } |
| 226 | |
| 227 | #endif /* _LINUX_JHASH_H */ |
| 228 | |