jenkins.cc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * HashKit library
  4. *
  5. * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
  6. * Copyright (C) 2006-2009 Brian Aker All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following disclaimer
  17. * in the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * The names of its contributors may not be used to endorse or
  21. * promote products derived from this software without specific prior
  22. * written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. */
  37. /*
  38. *
  39. * By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this
  40. * code any way you wish, private, educational, or commercial. It's free.
  41. * Use for hash table lookup, or anything where one collision in 2^^32 is
  42. * acceptable. Do NOT use for cryptographic purposes.
  43. * http://burtleburtle.net/bob/hash/index.html
  44. *
  45. * Modified by Brian Pontz for libmemcached
  46. * TODO:
  47. * Add big endian support
  48. */
  49. #include <libhashkit/common.h>
  50. #define hashsize(n) ((uint32_t)1<<(n))
  51. #define hashmask(n) (hashsize(n)-1)
  52. #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
  53. #define mix(a,b,c) \
  54. { \
  55. a -= c; a ^= rot(c, 4); c += b; \
  56. b -= a; b ^= rot(a, 6); a += c; \
  57. c -= b; c ^= rot(b, 8); b += a; \
  58. a -= c; a ^= rot(c,16); c += b; \
  59. b -= a; b ^= rot(a,19); a += c; \
  60. c -= b; c ^= rot(b, 4); b += a; \
  61. }
  62. #define final(a,b,c) \
  63. { \
  64. c ^= b; c -= rot(b,14); \
  65. a ^= c; a -= rot(c,11); \
  66. b ^= a; b -= rot(a,25); \
  67. c ^= b; c -= rot(b,16); \
  68. a ^= c; a -= rot(c,4); \
  69. b ^= a; b -= rot(a,14); \
  70. c ^= b; c -= rot(b,24); \
  71. }
  72. #define JENKINS_INITVAL 13
  73. /*
  74. jenkins_hash() -- hash a variable-length key into a 32-bit value
  75. k : the key (the unaligned variable-length array of bytes)
  76. length : the length of the key, counting by bytes
  77. initval : can be any 4-byte value
  78. Returns a 32-bit value. Every bit of the key affects every bit of
  79. the return value. Two keys differing by one or two bits will have
  80. totally different hash values.
  81. The best hash table sizes are powers of 2. There is no need to do
  82. mod a prime (mod is sooo slow!). If you need less than 32 bits,
  83. use a bitmask. For example, if you need only 10 bits, do
  84. h = (h & hashmask(10));
  85. In which case, the hash table should have hashsize(10) elements.
  86. */
  87. #if __GNUC__ >= 7
  88. #pragma GCC diagnostic warning "-Wimplicit-fallthrough"
  89. #endif
  90. uint32_t hashkit_jenkins(const char *key, size_t length, void *)
  91. {
  92. uint32_t a,b,c; /* internal state */
  93. #ifndef WORDS_BIGENDIAN
  94. union { const void *ptr; size_t i; } u;
  95. u.ptr = key;
  96. #endif
  97. /* Set up the internal state */
  98. a = b = c = 0xdeadbeef + ((uint32_t)length) + JENKINS_INITVAL;
  99. #ifndef WORDS_BIGENDIAN
  100. if ((u.i & 0x3) == 0)
  101. {
  102. const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
  103. /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
  104. while (length > 12)
  105. {
  106. a += k[0];
  107. b += k[1];
  108. c += k[2];
  109. mix(a,b,c);
  110. length -= 12;
  111. k += 3;
  112. }
  113. /*----------------------------- handle the last (probably partial) block */
  114. /*
  115. * "k[2]&0xffffff" actually reads beyond the end of the string, but
  116. * then masks off the part it's not allowed to read. Because the
  117. * string is aligned, the masked-off tail is in the same word as the
  118. * rest of the string. Every machine with memory protection I've seen
  119. * does it on word boundaries, so is OK with this. But VALGRIND will
  120. * still catch it and complain. The masking trick does make the hash
  121. * noticably faster for short strings (like English words).
  122. */
  123. switch(length)
  124. {
  125. case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
  126. case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
  127. case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
  128. case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
  129. case 8 : b+=k[1]; a+=k[0]; break;
  130. case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
  131. case 6 : b+=k[1]&0xffff; a+=k[0]; break;
  132. case 5 : b+=k[1]&0xff; a+=k[0]; break;
  133. case 4 : a+=k[0]; break;
  134. case 3 : a+=k[0]&0xffffff; break;
  135. case 2 : a+=k[0]&0xffff; break;
  136. case 1 : a+=k[0]&0xff; break;
  137. case 0 : return c; /* zero length strings require no mixing */
  138. default: return c;
  139. }
  140. }
  141. else if ((u.i & 0x1) == 0)
  142. {
  143. const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
  144. const uint8_t *k8;
  145. /*--------------- all but last block: aligned reads and different mixing */
  146. while (length > 12)
  147. {
  148. a += k[0] + (((uint32_t)k[1])<<16);
  149. b += k[2] + (((uint32_t)k[3])<<16);
  150. c += k[4] + (((uint32_t)k[5])<<16);
  151. mix(a,b,c);
  152. length -= 12;
  153. k += 6;
  154. }
  155. /*----------------------------- handle the last (probably partial) block */
  156. k8 = (const uint8_t *)k;
  157. switch(length)
  158. {
  159. case 12: c+=k[4]+(((uint32_t)k[5])<<16);
  160. b+=k[2]+(((uint32_t)k[3])<<16);
  161. a+=k[0]+(((uint32_t)k[1])<<16);
  162. break;
  163. case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
  164. case 10: c+=k[4];
  165. b+=k[2]+(((uint32_t)k[3])<<16);
  166. a+=k[0]+(((uint32_t)k[1])<<16);
  167. break;
  168. case 9 : c+=k8[8]; /* fall through */
  169. case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
  170. a+=k[0]+(((uint32_t)k[1])<<16);
  171. break;
  172. case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
  173. case 6 : b+=k[2];
  174. a+=k[0]+(((uint32_t)k[1])<<16);
  175. break;
  176. case 5 : b+=k8[4]; /* fall through */
  177. case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
  178. break;
  179. case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
  180. case 2 : a+=k[0];
  181. break;
  182. case 1 : a+=k8[0];
  183. break;
  184. case 0 : return c; /* zero length requires no mixing */
  185. default: return c;
  186. }
  187. }
  188. else
  189. { /* need to read the key one byte at a time */
  190. #endif /* little endian */
  191. const uint8_t *k = (const uint8_t *)key;
  192. /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
  193. while (length > 12)
  194. {
  195. a += k[0];
  196. a += ((uint32_t)k[1])<<8;
  197. a += ((uint32_t)k[2])<<16;
  198. a += ((uint32_t)k[3])<<24;
  199. b += k[4];
  200. b += ((uint32_t)k[5])<<8;
  201. b += ((uint32_t)k[6])<<16;
  202. b += ((uint32_t)k[7])<<24;
  203. c += k[8];
  204. c += ((uint32_t)k[9])<<8;
  205. c += ((uint32_t)k[10])<<16;
  206. c += ((uint32_t)k[11])<<24;
  207. mix(a,b,c);
  208. length -= 12;
  209. k += 12;
  210. }
  211. /*-------------------------------- last block: affect all 32 bits of (c) */
  212. switch(length) /* all the case statements fall through */
  213. {
  214. case 12: c+=((uint32_t)k[11])<<24; /* fall through */
  215. case 11: c+=((uint32_t)k[10])<<16; /* fall through */
  216. case 10: c+=((uint32_t)k[9])<<8; /* fall through */
  217. case 9 : c+=k[8]; /* fall through */
  218. case 8 : b+=((uint32_t)k[7])<<24; /* fall through */
  219. case 7 : b+=((uint32_t)k[6])<<16; /* fall through */
  220. case 6 : b+=((uint32_t)k[5])<<8; /* fall through */
  221. case 5 : b+=k[4]; /* fall through */
  222. case 4 : a+=((uint32_t)k[3])<<24; /* fall through */
  223. case 3 : a+=((uint32_t)k[2])<<16; /* fall through */
  224. case 2 : a+=((uint32_t)k[1])<<8; /* fall through */
  225. case 1 : a+=k[0];
  226. break;
  227. case 0 : return c;
  228. default : return c;
  229. }
  230. #ifndef WORDS_BIGENDIAN
  231. }
  232. #endif
  233. final(a,b,c);
  234. return c;
  235. }