4 /* jhash.h: Jenkins hash support.
6 * Copyright (C) 1996 Bob Jenkins (bob_jenkins@burtleburtle.net)
8 * http://burtleburtle.net/bob/hash/
10 * These are the credits from Bob's sources:
12 * lookup2.c, by Bob Jenkins, December 1996, 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.
17 * $FreeBSD: src/sys/dev/cxgb/common/jhash.h,v 1.1 2007/05/25 09:48:19 kmacy Exp $
20 /* NOTE: Arguments are modified. */
21 #define __jhash_mix(a, b, c) \
23 a -= b; a -= c; a ^= (c>>13); \
24 b -= c; b -= a; b ^= (a<<8); \
25 c -= a; c -= b; c ^= (b>>13); \
26 a -= b; a -= c; a ^= (c>>12); \
27 b -= c; b -= a; b ^= (a<<16); \
28 c -= a; c -= b; c ^= (b>>5); \
29 a -= b; a -= c; a ^= (c>>3); \
30 b -= c; b -= a; b ^= (a<<10); \
31 c -= a; c -= b; c ^= (b>>15); \
34 /* The golden ration: an arbitrary value */
35 #define JHASH_GOLDEN_RATIO 0x9e3779b9
37 /* The most generic version, hashes an arbitrary sequence
38 * of bytes. No alignment or length assumptions are made about
41 static inline u32
jhash(const void *key
, u32 length
, u32 initval
)
47 a
= b
= JHASH_GOLDEN_RATIO
;
51 a
+= (k
[0] +((u32
)k
[1]<<8) +((u32
)k
[2]<<16) +((u32
)k
[3]<<24));
52 b
+= (k
[4] +((u32
)k
[5]<<8) +((u32
)k
[6]<<16) +((u32
)k
[7]<<24));
53 c
+= (k
[8] +((u32
)k
[9]<<8) +((u32
)k
[10]<<16)+((u32
)k
[11]<<24));
63 case 11: c
+= ((u32
)k
[10]<<24);
64 case 10: c
+= ((u32
)k
[9]<<16);
65 case 9 : c
+= ((u32
)k
[8]<<8);
66 case 8 : b
+= ((u32
)k
[7]<<24);
67 case 7 : b
+= ((u32
)k
[6]<<16);
68 case 6 : b
+= ((u32
)k
[5]<<8);
70 case 4 : a
+= ((u32
)k
[3]<<24);
71 case 3 : a
+= ((u32
)k
[2]<<16);
72 case 2 : a
+= ((u32
)k
[1]<<8);
81 /* A special optimized version that handles 1 or more of u32s.
82 * The length parameter here is the number of u32s in the key.
84 static inline u32
jhash2(u32
*k
, u32 length
, u32 initval
)
88 a
= b
= JHASH_GOLDEN_RATIO
;
113 /* A special ultra-optimized versions that knows they are hashing exactly
116 * NOTE: In partilar the "c += length; __jhash_mix(a,b,c);" normally
117 * done at the end is not done here.
119 static inline u32
jhash_3words(u32 a
, u32 b
, u32 c
, u32 initval
)
121 a
+= JHASH_GOLDEN_RATIO
;
122 b
+= JHASH_GOLDEN_RATIO
;
125 __jhash_mix(a
, b
, c
);
130 static inline u32
jhash_2words(u32 a
, u32 b
, u32 initval
)
132 return jhash_3words(a
, b
, 0, initval
);
135 static inline u32
jhash_1word(u32 a
, u32 initval
)
137 return jhash_3words(a
, 0, 0, initval
);
140 #endif /* _JHASH_H */