2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
16 #include "libhashkit/common.h"
18 #define hashsize(n) ((uint32_t) 1 << (n))
19 #define hashmask(n) (hashsize(n) - 1)
20 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
22 #define mix(a, b, c) \
44 #define final(a, b, c) \
62 #define JENKINS_INITVAL 13
65 jenkins_hash() -- hash a variable-length key into a 32-bit value
66 k : the key (the unaligned variable-length array of bytes)
67 length : the length of the key, counting by bytes
68 initval : can be any 4-byte value
69 Returns a 32-bit value. Every bit of the key affects every bit of
70 the return value. Two keys differing by one or two bits will have
71 totally different hash values.
73 The best hash table sizes are powers of 2. There is no need to do
74 mod a prime (mod is sooo slow!). If you need less than 32 bits,
75 use a bitmask. For example, if you need only 10 bits, do
76 h = (h & hashmask(10));
77 In which case, the hash table should have hashsize(10) elements.
81 __attribute__((no_sanitize_address
, no_sanitize("address")))
84 hashkit_jenkins(const char *key
, size_t length
, void *) {
85 uint32_t a
, b
, c
; /* internal state */
94 /* Set up the internal state */
95 a
= b
= c
= 0xdeadbeef + ((uint32_t) length
) + JENKINS_INITVAL
;
98 if ((u
.i
& 0x3) == 0) {
99 const uint32_t *k
= (const uint32_t *) key
; /* read 32-bit chunks */
101 /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
102 while (length
> 12) {
111 /*----------------------------- handle the last (probably partial) block */
113 * "k[2]&0xffffff" actually reads beyond the end of the string, but
114 * then masks off the part it's not allowed to read. Because the
115 * string is aligned, the masked-off tail is in the same word as the
116 * rest of the string. Every machine with memory protection I've seen
117 * does it on word boundaries, so is OK with this. But VALGRIND will
118 * still catch it and complain. The masking trick does make the hash
119 * noticably faster for short strings (like English words).
128 c
+= k
[2] & 0xffffff;
147 b
+= k
[1] & 0xffffff;
162 a
+= k
[0] & 0xffffff;
171 return c
; /* zero length strings require no mixing */
176 } else if ((u
.i
& 0x1) == 0) {
177 const uint16_t *k
= (const uint16_t *) key
; /* read 16-bit chunks */
180 /*--------------- all but last block: aligned reads and different mixing */
181 while (length
> 12) {
182 a
+= k
[0] + (((uint32_t) k
[1]) << 16);
183 b
+= k
[2] + (((uint32_t) k
[3]) << 16);
184 c
+= k
[4] + (((uint32_t) k
[5]) << 16);
190 /*----------------------------- handle the last (probably partial) block */
191 k8
= (const uint8_t *) k
;
194 c
+= k
[4] + (((uint32_t) k
[5]) << 16);
195 b
+= k
[2] + (((uint32_t) k
[3]) << 16);
196 a
+= k
[0] + (((uint32_t) k
[1]) << 16);
199 c
+= ((uint32_t) k8
[10]) << 16;
203 b
+= k
[2] + (((uint32_t) k
[3]) << 16);
204 a
+= k
[0] + (((uint32_t) k
[1]) << 16);
210 b
+= k
[2] + (((uint32_t) k
[3]) << 16);
211 a
+= k
[0] + (((uint32_t) k
[1]) << 16);
214 b
+= ((uint32_t) k8
[6]) << 16;
218 a
+= k
[0] + (((uint32_t) k
[1]) << 16);
224 a
+= k
[0] + (((uint32_t) k
[1]) << 16);
227 a
+= ((uint32_t) k8
[2]) << 16;
236 return c
; /* zero length requires no mixing */
241 } else { /* need to read the key one byte at a time */
242 #endif /* little endian */
243 const uint8_t *k
= (const uint8_t *) key
;
245 /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
246 while (length
> 12) {
248 a
+= ((uint32_t) k
[1]) << 8;
249 a
+= ((uint32_t) k
[2]) << 16;
250 a
+= ((uint32_t) k
[3]) << 24;
252 b
+= ((uint32_t) k
[5]) << 8;
253 b
+= ((uint32_t) k
[6]) << 16;
254 b
+= ((uint32_t) k
[7]) << 24;
256 c
+= ((uint32_t) k
[9]) << 8;
257 c
+= ((uint32_t) k
[10]) << 16;
258 c
+= ((uint32_t) k
[11]) << 24;
264 /*-------------------------------- last block: affect all 32 bits of (c) */
265 switch (length
) /* all the case statements fall through */ {
267 c
+= ((uint32_t) k
[11]) << 24;
270 c
+= ((uint32_t) k
[10]) << 16;
273 c
+= ((uint32_t) k
[9]) << 8;
279 b
+= ((uint32_t) k
[7]) << 24;
282 b
+= ((uint32_t) k
[6]) << 16;
285 b
+= ((uint32_t) k
[5]) << 8;
291 a
+= ((uint32_t) k
[3]) << 24;
294 a
+= ((uint32_t) k
[2]) << 16;
297 a
+= ((uint32_t) k
[1]) << 8;