1 #ifndef _LINUX_IPSET_JHASH_H
2 #define _LINUX_IPSET_JHASH_H
4 /* This is a copy of linux/jhash.h but the types u32/u8 are changed
5 * to __u32/__u8 so that the header file can be included into
6 * userspace code as well. Jozsef Kadlecsik (kadlec@blackhole.kfki.hu)
9 /* jhash.h: Jenkins hash support.
11 * Copyright (C) 1996 Bob Jenkins (bob_jenkins@burtleburtle.net)
13 * http://burtleburtle.net/bob/hash/
15 * These are the credits from Bob's sources:
17 * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
18 * hash(), hash2(), hash3, and mix() are externally useful functions.
19 * Routines to test the hash are included if SELF_TEST is defined.
20 * You can use this free for any purpose. It has no warranty.
22 * Copyright (C) 2003 David S. Miller (davem@redhat.com)
24 * I've modified Bob's hash to be useful in the Linux kernel, and
25 * any bugs present are surely my fault. -DaveM
28 /* NOTE: Arguments are modified. */
29 #define __jhash_mix(a, b, c) \
31 a -= b; a -= c; a ^= (c>>13); \
32 b -= c; b -= a; b ^= (a<<8); \
33 c -= a; c -= b; c ^= (b>>13); \
34 a -= b; a -= c; a ^= (c>>12); \
35 b -= c; b -= a; b ^= (a<<16); \
36 c -= a; c -= b; c ^= (b>>5); \
37 a -= b; a -= c; a ^= (c>>3); \
38 b -= c; b -= a; b ^= (a<<10); \
39 c -= a; c -= b; c ^= (b>>15); \
42 /* The golden ration: an arbitrary value */
43 #define JHASH_GOLDEN_RATIO 0x9e3779b9
45 /* The most generic version, hashes an arbitrary sequence
46 * of bytes. No alignment or length assumptions are made about
49 static inline __u32 jhash(void *key, __u32 length, __u32 initval)
55 a = b = JHASH_GOLDEN_RATIO;
59 a += (k[0] +((__u32)k[1]<<8) +((__u32)k[2]<<16) +((__u32)k[3]<<24));
60 b += (k[4] +((__u32)k[5]<<8) +((__u32)k[6]<<16) +((__u32)k[7]<<24));
61 c += (k[8] +((__u32)k[9]<<8) +((__u32)k[10]<<16)+((__u32)k[11]<<24));
71 case 11: c += ((__u32)k[10]<<24);
72 case 10: c += ((__u32)k[9]<<16);
73 case 9 : c += ((__u32)k[8]<<8);
74 case 8 : b += ((__u32)k[7]<<24);
75 case 7 : b += ((__u32)k[6]<<16);
76 case 6 : b += ((__u32)k[5]<<8);
78 case 4 : a += ((__u32)k[3]<<24);
79 case 3 : a += ((__u32)k[2]<<16);
80 case 2 : a += ((__u32)k[1]<<8);
89 /* A special optimized version that handles 1 or more of __u32s.
90 * The length parameter here is the number of __u32s in the key.
92 static inline __u32 jhash2(__u32 *k, __u32 length, __u32 initval)
96 a = b = JHASH_GOLDEN_RATIO;
104 __jhash_mix(a, b, c);
121 /* A special ultra-optimized versions that knows they are hashing exactly
124 * NOTE: In partilar the "c += length; __jhash_mix(a,b,c);" normally
125 * done at the end is not done here.
127 static inline __u32 jhash_3words(__u32 a, __u32 b, __u32 c, __u32 initval)
129 a += JHASH_GOLDEN_RATIO;
130 b += JHASH_GOLDEN_RATIO;
133 __jhash_mix(a, b, c);
138 static inline __u32 jhash_2words(__u32 a, __u32 b, __u32 initval)
140 return jhash_3words(a, b, 0, initval);
143 static inline __u32 jhash_1word(__u32 a, __u32 initval)
145 return jhash_3words(a, 0, 0, initval);
148 #endif /* _LINUX_IPSET_JHASH_H */