Allow make from the exec directory.
[openais.git] / exec / jhash.h
blobcf768762f78af021f6c419c06203a60150bc29cf
1 #ifndef _LINUX_JHASH_H
2 #define _LINUX_JHASH_H
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 * Copyright (C) 2003 David S. Miller (davem@redhat.com)
19 * I've modified Bob's hash to be useful in the Linux kernel, and
20 * any bugs present are surely my fault. -DaveM
23 typedef uint32_t u32;
24 typedef uint8_t u8;
26 /* NOTE: Arguments are modified. */
27 #define __jhash_mix(a, b, c) \
28 { \
29 a -= b; a -= c; a ^= (c>>13); \
30 b -= c; b -= a; b ^= (a<<8); \
31 c -= a; c -= b; c ^= (b>>13); \
32 a -= b; a -= c; a ^= (c>>12); \
33 b -= c; b -= a; b ^= (a<<16); \
34 c -= a; c -= b; c ^= (b>>5); \
35 a -= b; a -= c; a ^= (c>>3); \
36 b -= c; b -= a; b ^= (a<<10); \
37 c -= a; c -= b; c ^= (b>>15); \
40 /* The golden ration: an arbitrary value */
41 #define JHASH_GOLDEN_RATIO 0x9e3779b9
43 /* The most generic version, hashes an arbitrary sequence
44 * of bytes. No alignment or length assumptions are made about
45 * the input key.
47 static inline u32 jhash(const void *key, u32 length, u32 initval)
49 u32 a, b, c, len;
50 const u8 *k = key;
52 len = length;
53 a = b = JHASH_GOLDEN_RATIO;
54 c = initval;
56 while (len >= 12) {
57 a += (k[0] +((u32)k[1]<<8) +((u32)k[2]<<16) +((u32)k[3]<<24));
58 b += (k[4] +((u32)k[5]<<8) +((u32)k[6]<<16) +((u32)k[7]<<24));
59 c += (k[8] +((u32)k[9]<<8) +((u32)k[10]<<16)+((u32)k[11]<<24));
61 __jhash_mix(a,b,c);
63 k += 12;
64 len -= 12;
67 c += length;
68 switch (len) {
69 case 11: c += ((u32)k[10]<<24);
70 case 10: c += ((u32)k[9]<<16);
71 case 9 : c += ((u32)k[8]<<8);
72 case 8 : b += ((u32)k[7]<<24);
73 case 7 : b += ((u32)k[6]<<16);
74 case 6 : b += ((u32)k[5]<<8);
75 case 5 : b += k[4];
76 case 4 : a += ((u32)k[3]<<24);
77 case 3 : a += ((u32)k[2]<<16);
78 case 2 : a += ((u32)k[1]<<8);
79 case 1 : a += k[0];
82 __jhash_mix(a,b,c);
84 return c;
87 /* A special optimized version that handles 1 or more of u32s.
88 * The length parameter here is the number of u32s in the key.
90 static inline u32 jhash2(u32 *k, u32 length, u32 initval)
92 u32 a, b, c, len;
94 a = b = JHASH_GOLDEN_RATIO;
95 c = initval;
96 len = length;
98 while (len >= 3) {
99 a += k[0];
100 b += k[1];
101 c += k[2];
102 __jhash_mix(a, b, c);
103 k += 3; len -= 3;
106 c += length * 4;
108 switch (len) {
109 case 2 : b += k[1];
110 case 1 : a += k[0];
113 __jhash_mix(a,b,c);
115 return c;
119 /* A special ultra-optimized versions that knows they are hashing exactly
120 * 3, 2 or 1 word(s).
122 * NOTE: In partilar the "c += length; __jhash_mix(a,b,c);" normally
123 * done at the end is not done here.
125 static inline u32 jhash_3words(u32 a, u32 b, u32 c, u32 initval)
127 a += JHASH_GOLDEN_RATIO;
128 b += JHASH_GOLDEN_RATIO;
129 c += initval;
131 __jhash_mix(a, b, c);
133 return c;
136 static inline u32 jhash_2words(u32 a, u32 b, u32 initval)
138 return jhash_3words(a, b, 0, initval);
141 static inline u32 jhash_1word(u32 a, u32 initval)
143 return jhash_3words(a, 0, 0, initval);
146 #endif /* _LINUX_JHASH_H */