5f85ad62a220c5b04f0708c5f37776c1e2155ad7
[awesomized/libmemcached] / libhashkit / jenkins.cc
1 /*
2 *
3 * By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this
4 * code any way you wish, private, educational, or commercial. It's free.
5 * Use for hash table lookup, or anything where one collision in 2^^32 is
6 * acceptable. Do NOT use for cryptographic purposes.
7 * http://burtleburtle.net/bob/hash/index.html
8 *
9 * Modified by Brian Pontz for libmemcached
10 * TODO:
11 * Add big endian support
12 */
13
14 #include <libhashkit/common.h>
15
16 #define hashsize(n) ((uint32_t)1<<(n))
17 #define hashmask(n) (hashsize(n)-1)
18 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
19
20 #define mix(a,b,c) \
21 { \
22 a -= c; a ^= rot(c, 4); c += b; \
23 b -= a; b ^= rot(a, 6); a += c; \
24 c -= b; c ^= rot(b, 8); b += a; \
25 a -= c; a ^= rot(c,16); c += b; \
26 b -= a; b ^= rot(a,19); a += c; \
27 c -= b; c ^= rot(b, 4); b += a; \
28 }
29
30 #define final(a,b,c) \
31 { \
32 c ^= b; c -= rot(b,14); \
33 a ^= c; a -= rot(c,11); \
34 b ^= a; b -= rot(a,25); \
35 c ^= b; c -= rot(b,16); \
36 a ^= c; a -= rot(c,4); \
37 b ^= a; b -= rot(a,14); \
38 c ^= b; c -= rot(b,24); \
39 }
40
41 #define JENKINS_INITVAL 13
42
43 /*
44 jenkins_hash() -- hash a variable-length key into a 32-bit value
45 k : the key (the unaligned variable-length array of bytes)
46 length : the length of the key, counting by bytes
47 initval : can be any 4-byte value
48 Returns a 32-bit value. Every bit of the key affects every bit of
49 the return value. Two keys differing by one or two bits will have
50 totally different hash values.
51
52 The best hash table sizes are powers of 2. There is no need to do
53 mod a prime (mod is sooo slow!). If you need less than 32 bits,
54 use a bitmask. For example, if you need only 10 bits, do
55 h = (h & hashmask(10));
56 In which case, the hash table should have hashsize(10) elements.
57 */
58
59 uint32_t hashkit_jenkins(const char *key, size_t length, void *)
60 {
61 uint32_t a,b,c; /* internal state */
62 union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
63
64 /* Set up the internal state */
65 a = b = c = 0xdeadbeef + ((uint32_t)length) + JENKINS_INITVAL;
66
67 u.ptr = key;
68 #ifndef WORDS_BIGENDIAN
69 if ((u.i & 0x3) == 0)
70 {
71 const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
72
73 /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
74 while (length > 12)
75 {
76 a += k[0];
77 b += k[1];
78 c += k[2];
79 mix(a,b,c);
80 length -= 12;
81 k += 3;
82 }
83
84 /*----------------------------- handle the last (probably partial) block */
85 /*
86 * "k[2]&0xffffff" actually reads beyond the end of the string, but
87 * then masks off the part it's not allowed to read. Because the
88 * string is aligned, the masked-off tail is in the same word as the
89 * rest of the string. Every machine with memory protection I've seen
90 * does it on word boundaries, so is OK with this. But VALGRIND will
91 * still catch it and complain. The masking trick does make the hash
92 * noticably faster for short strings (like English words).
93 */
94 switch(length)
95 {
96 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
97 case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
98 case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
99 case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
100 case 8 : b+=k[1]; a+=k[0]; break;
101 case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
102 case 6 : b+=k[1]&0xffff; a+=k[0]; break;
103 case 5 : b+=k[1]&0xff; a+=k[0]; break;
104 case 4 : a+=k[0]; break;
105 case 3 : a+=k[0]&0xffffff; break;
106 case 2 : a+=k[0]&0xffff; break;
107 case 1 : a+=k[0]&0xff; break;
108 case 0 : return c; /* zero length strings require no mixing */
109 default: return c;
110 }
111
112 }
113 else if ((u.i & 0x1) == 0)
114 {
115 const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
116 const uint8_t *k8;
117
118 /*--------------- all but last block: aligned reads and different mixing */
119 while (length > 12)
120 {
121 a += k[0] + (((uint32_t)k[1])<<16);
122 b += k[2] + (((uint32_t)k[3])<<16);
123 c += k[4] + (((uint32_t)k[5])<<16);
124 mix(a,b,c);
125 length -= 12;
126 k += 6;
127 }
128
129 /*----------------------------- handle the last (probably partial) block */
130 k8 = (const uint8_t *)k;
131 switch(length)
132 {
133 case 12: c+=k[4]+(((uint32_t)k[5])<<16);
134 b+=k[2]+(((uint32_t)k[3])<<16);
135 a+=k[0]+(((uint32_t)k[1])<<16);
136 break;
137 case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
138 case 10: c+=k[4];
139 b+=k[2]+(((uint32_t)k[3])<<16);
140 a+=k[0]+(((uint32_t)k[1])<<16);
141 break;
142 case 9 : c+=k8[8]; /* fall through */
143 case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
144 a+=k[0]+(((uint32_t)k[1])<<16);
145 break;
146 case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
147 case 6 : b+=k[2];
148 a+=k[0]+(((uint32_t)k[1])<<16);
149 break;
150 case 5 : b+=k8[4]; /* fall through */
151 case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
152 break;
153 case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
154 case 2 : a+=k[0];
155 break;
156 case 1 : a+=k8[0];
157 break;
158 case 0 : return c; /* zero length requires no mixing */
159 default: return c;
160 }
161
162 }
163 else
164 { /* need to read the key one byte at a time */
165 #endif /* little endian */
166 const uint8_t *k = (const uint8_t *)key;
167
168 /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
169 while (length > 12)
170 {
171 a += k[0];
172 a += ((uint32_t)k[1])<<8;
173 a += ((uint32_t)k[2])<<16;
174 a += ((uint32_t)k[3])<<24;
175 b += k[4];
176 b += ((uint32_t)k[5])<<8;
177 b += ((uint32_t)k[6])<<16;
178 b += ((uint32_t)k[7])<<24;
179 c += k[8];
180 c += ((uint32_t)k[9])<<8;
181 c += ((uint32_t)k[10])<<16;
182 c += ((uint32_t)k[11])<<24;
183 mix(a,b,c);
184 length -= 12;
185 k += 12;
186 }
187
188 /*-------------------------------- last block: affect all 32 bits of (c) */
189 switch(length) /* all the case statements fall through */
190 {
191 case 12: c+=((uint32_t)k[11])<<24;
192 case 11: c+=((uint32_t)k[10])<<16;
193 case 10: c+=((uint32_t)k[9])<<8;
194 case 9 : c+=k[8];
195 case 8 : b+=((uint32_t)k[7])<<24;
196 case 7 : b+=((uint32_t)k[6])<<16;
197 case 6 : b+=((uint32_t)k[5])<<8;
198 case 5 : b+=k[4];
199 case 4 : a+=((uint32_t)k[3])<<24;
200 case 3 : a+=((uint32_t)k[2])<<16;
201 case 2 : a+=((uint32_t)k[1])<<8;
202 case 1 : a+=k[0];
203 break;
204 case 0 : return c;
205 default : return c;
206 }
207 #ifndef WORDS_BIGENDIAN
208 }
209 #endif
210
211 final(a,b,c);
212 return c;
213 }