55b52709588314a796ecd192c165f1fb8b8a626b
[awesomized/libmemcached] / libmemcached / jenkins_hash.c
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 "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 /*
42 jenkins_hash() -- hash a variable-length key into a 32-bit value
43 k : the key (the unaligned variable-length array of bytes)
44 length : the length of the key, counting by bytes
45 initval : can be any 4-byte value
46 Returns a 32-bit value. Every bit of the key affects every bit of
47 the return value. Two keys differing by one or two bits will have
48 totally different hash values.
49
50 The best hash table sizes are powers of 2. There is no need to do
51 mod a prime (mod is sooo slow!). If you need less than 32 bits,
52 use a bitmask. For example, if you need only 10 bits, do
53 h = (h & hashmask(10));
54 In which case, the hash table should have hashsize(10) elements.
55 */
56
57 uint32_t jenkins_hash(const void *key, size_t length, uint32_t initval)
58 {
59 uint32_t a,b,c; /* internal state */
60 union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
61
62 /* Set up the internal state */
63 a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
64
65 u.ptr = key;
66 if ((u.i & 0x3) == 0) {
67 const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
68 const uint8_t *k8;
69
70 /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
71 while (length > 12)
72 {
73 a += k[0];
74 b += k[1];
75 c += k[2];
76 mix(a,b,c);
77 length -= 12;
78 k += 3;
79 }
80
81 /*----------------------------- handle the last (probably partial) block */
82 /*
83 * "k[2]&0xffffff" actually reads beyond the end of the string, but
84 * then masks off the part it's not allowed to read. Because the
85 * string is aligned, the masked-off tail is in the same word as the
86 * rest of the string. Every machine with memory protection I've seen
87 * does it on word boundaries, so is OK with this. But VALGRIND will
88 * still catch it and complain. The masking trick does make the hash
89 * noticably faster for short strings (like English words).
90 */
91 switch(length)
92 {
93 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
94 case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
95 case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
96 case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
97 case 8 : b+=k[1]; a+=k[0]; break;
98 case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
99 case 6 : b+=k[1]&0xffff; a+=k[0]; break;
100 case 5 : b+=k[1]&0xff; a+=k[0]; break;
101 case 4 : a+=k[0]; break;
102 case 3 : a+=k[0]&0xffffff; break;
103 case 2 : a+=k[0]&0xffff; break;
104 case 1 : a+=k[0]&0xff; break;
105 case 0 : return c; /* zero length strings require no mixing */
106 }
107
108 } else if ((u.i & 0x1) == 0) {
109 const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
110 const uint8_t *k8;
111
112 /*--------------- all but last block: aligned reads and different mixing */
113 while (length > 12)
114 {
115 a += k[0] + (((uint32_t)k[1])<<16);
116 b += k[2] + (((uint32_t)k[3])<<16);
117 c += k[4] + (((uint32_t)k[5])<<16);
118 mix(a,b,c);
119 length -= 12;
120 k += 6;
121 }
122
123 /*----------------------------- handle the last (probably partial) block */
124 k8 = (const uint8_t *)k;
125 switch(length)
126 {
127 case 12: c+=k[4]+(((uint32_t)k[5])<<16);
128 b+=k[2]+(((uint32_t)k[3])<<16);
129 a+=k[0]+(((uint32_t)k[1])<<16);
130 break;
131 case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
132 case 10: c+=k[4];
133 b+=k[2]+(((uint32_t)k[3])<<16);
134 a+=k[0]+(((uint32_t)k[1])<<16);
135 break;
136 case 9 : c+=k8[8]; /* fall through */
137 case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
138 a+=k[0]+(((uint32_t)k[1])<<16);
139 break;
140 case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
141 case 6 : b+=k[2];
142 a+=k[0]+(((uint32_t)k[1])<<16);
143 break;
144 case 5 : b+=k8[4]; /* fall through */
145 case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
146 break;
147 case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
148 case 2 : a+=k[0];
149 break;
150 case 1 : a+=k8[0];
151 break;
152 case 0 : return c; /* zero length requires no mixing */
153 }
154
155 } else { /* need to read the key one byte at a time */
156 const uint8_t *k = (const uint8_t *)key;
157
158 /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
159 while (length > 12)
160 {
161 a += k[0];
162 a += ((uint32_t)k[1])<<8;
163 a += ((uint32_t)k[2])<<16;
164 a += ((uint32_t)k[3])<<24;
165 b += k[4];
166 b += ((uint32_t)k[5])<<8;
167 b += ((uint32_t)k[6])<<16;
168 b += ((uint32_t)k[7])<<24;
169 c += k[8];
170 c += ((uint32_t)k[9])<<8;
171 c += ((uint32_t)k[10])<<16;
172 c += ((uint32_t)k[11])<<24;
173 mix(a,b,c);
174 length -= 12;
175 k += 12;
176 }
177
178 /*-------------------------------- last block: affect all 32 bits of (c) */
179 switch(length) /* all the case statements fall through */
180 {
181 case 12: c+=((uint32_t)k[11])<<24;
182 case 11: c+=((uint32_t)k[10])<<16;
183 case 10: c+=((uint32_t)k[9])<<8;
184 case 9 : c+=k[8];
185 case 8 : b+=((uint32_t)k[7])<<24;
186 case 7 : b+=((uint32_t)k[6])<<16;
187 case 6 : b+=((uint32_t)k[5])<<8;
188 case 5 : b+=k[4];
189 case 4 : a+=((uint32_t)k[3])<<24;
190 case 3 : a+=((uint32_t)k[2])<<16;
191 case 2 : a+=((uint32_t)k[1])<<8;
192 case 1 : a+=k[0];
193 break;
194 case 0 : return c;
195 }
196 }
197
198 final(a,b,c);
199 return c;
200 }
201
202