4 /* jhash.h: Jenkins hash support.
6 * Copyright (C) 2006. Bob Jenkins (bob_jenkins@burtleburtle.net)
8 * http://burtleburtle.net/bob/hash/
10 * These are the credits from Bob's sources:
12 * lookup3.c, by Bob Jenkins, May 2006, Public Domain.
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.
20 * Copyright (C) 2009 Jozsef Kadlecsik (kadlec@blackhole.kfki.hu)
22 * I've modified Bob's hash to be useful in the Linux kernel, and
23 * any bugs present are my fault. Jozsef
26 #define __rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
28 /* __jhash_mix - mix 3 32-bit values reversibly. */
29 #define __jhash_mix(a,b,c) \
31 a -= c; a ^= __rot(c, 4); c += b; \
32 b -= a; b ^= __rot(a, 6); a += c; \
33 c -= b; c ^= __rot(b, 8); b += a; \
34 a -= c; a ^= __rot(c,16); c += b; \
35 b -= a; b ^= __rot(a,19); a += c; \
36 c -= b; c ^= __rot(b, 4); b += a; \
39 /* __jhash_final - final mixing of 3 32-bit values (a,b,c) into c */
40 #define __jhash_final(a,b,c) \
42 c ^= b; c -= __rot(b,14); \
43 a ^= c; a -= __rot(c,11); \
44 b ^= a; b -= __rot(a,25); \
45 c ^= b; c -= __rot(b,16); \
46 a ^= c; a -= __rot(c,4); \
47 b ^= a; b -= __rot(a,14); \
48 c ^= b; c -= __rot(b,24); \
51 /* The golden ration: an arbitrary value */
52 #define JHASH_GOLDEN_RATIO 0xdeadbeef
54 /* The most generic version, hashes an arbitrary sequence
55 * of bytes. No alignment or length assumptions are made about
56 * the input key. The result depends on endianness.
58 static inline u32 jhash(const void *key, u32 length, u32 initval)
63 /* Set up the internal state */
64 a = b = c = JHASH_GOLDEN_RATIO + length + initval;
66 /* all but the last block: affect some 32 bits of (a,b,c) */
68 a += (k[0] + ((u32)k[1]<<8) + ((u32)k[2]<<16) + ((u32)k[3]<<24));
69 b += (k[4] + ((u32)k[5]<<8) + ((u32)k[6]<<16) + ((u32)k[7]<<24));
70 c += (k[8] + ((u32)k[9]<<8) + ((u32)k[10]<<16) + ((u32)k[11]<<24));
76 /* last block: affect all 32 bits of (c) */
77 /* all the case statements fall through */
79 case 12: c += (u32)k[11]<<24;
80 case 11: c += (u32)k[10]<<16;
81 case 10: c += (u32)k[9]<<8;
83 case 8 : b += (u32)k[7]<<24;
84 case 7 : b += (u32)k[6]<<16;
85 case 6 : b += (u32)k[5]<<8;
87 case 4 : a += (u32)k[3]<<24;
88 case 3 : a += (u32)k[2]<<16;
89 case 2 : a += (u32)k[1]<<8;
91 __jhash_final(a, b, c);
99 /* A special optimized version that handles 1 or more of u32s.
100 * The length parameter here is the number of u32s in the key.
102 static inline u32 jhash2(u32 *k, u32 length, u32 initval)
106 /* Set up the internal state */
107 a = b = c = JHASH_GOLDEN_RATIO + (length<<2) + initval;
109 /* handle most of the key */
114 __jhash_mix(a, b, c);
119 /* handle the last 3 u32's */
120 /* all the case statements fall through */
125 __jhash_final(a, b, c);
126 case 0: /* case 0: nothing left to add */
133 /* A special ultra-optimized versions that knows they are hashing exactly
136 static inline u32 jhash_3words(u32 a, u32 b, u32 c, u32 initval)
138 a += JHASH_GOLDEN_RATIO + initval;
139 b += JHASH_GOLDEN_RATIO + initval;
140 c += JHASH_GOLDEN_RATIO + initval;
142 __jhash_final(a, b, c);
147 static inline u32 jhash_2words(u32 a, u32 b, u32 initval)
149 return jhash_3words(0, a, b, initval);
152 static inline u32 jhash_1word(u32 a, u32 initval)
154 return jhash_3words(0, 0, a, initval);
157 #endif /* _LINUX_JHASH_H */