jenkins_hash.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * Taken from http://burtleburtle.net/bob/c/lookup3.c
  3. * $FreeBSD$
  4. */
  5. #include <sys/hash.h>
  6. #include <machine/endian.h>
  7. /*
  8. -------------------------------------------------------------------------------
  9. lookup3.c, by Bob Jenkins, May 2006, Public Domain.
  10. These are functions for producing 32-bit hashes for hash table lookup.
  11. hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
  12. are externally useful functions. Routines to test the hash are included
  13. if SELF_TEST is defined. You can use this free for any purpose. It's in
  14. the public domain. It has no warranty.
  15. You probably want to use hashlittle(). hashlittle() and hashbig()
  16. hash byte arrays. hashlittle() is faster than hashbig() on
  17. little-endian machines. Intel and AMD are little-endian machines.
  18. On second thought, you probably want hashlittle2(), which is identical to
  19. hashlittle() except it returns two 32-bit hashes for the price of one.
  20. You could implement hashbig2() if you wanted but I haven't bothered here.
  21. If you want to find a hash of, say, exactly 7 integers, do
  22. a = i1; b = i2; c = i3;
  23. mix(a,b,c);
  24. a += i4; b += i5; c += i6;
  25. mix(a,b,c);
  26. a += i7;
  27. final(a,b,c);
  28. then use c as the hash value. If you have a variable length array of
  29. 4-byte integers to hash, use hashword(). If you have a byte array (like
  30. a character string), use hashlittle(). If you have several byte arrays, or
  31. a mix of things, see the comments above hashlittle().
  32. Why is this so big? I read 12 bytes at a time into 3 4-byte integers,
  33. then mix those integers. This is fast (you can do a lot more thorough
  34. mixing with 12*3 instructions on 3 integers than you can with 3 instructions
  35. on 1 byte), but shoehorning those bytes into integers efficiently is messy.
  36. -------------------------------------------------------------------------------
  37. */
  38. #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
  39. /*
  40. -------------------------------------------------------------------------------
  41. mix -- mix 3 32-bit values reversibly.
  42. This is reversible, so any information in (a,b,c) before mix() is
  43. still in (a,b,c) after mix().
  44. If four pairs of (a,b,c) inputs are run through mix(), or through
  45. mix() in reverse, there are at least 32 bits of the output that
  46. are sometimes the same for one pair and different for another pair.
  47. This was tested for:
  48. * pairs that differed by one bit, by two bits, in any combination
  49. of top bits of (a,b,c), or in any combination of bottom bits of
  50. (a,b,c).
  51. * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
  52. the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
  53. is commonly produced by subtraction) look like a single 1-bit
  54. difference.
  55. * the base values were pseudorandom, all zero but one bit set, or
  56. all zero plus a counter that starts at zero.
  57. Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
  58. satisfy this are
  59. 4 6 8 16 19 4
  60. 9 15 3 18 27 15
  61. 14 9 3 7 17 3
  62. Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
  63. for "differ" defined as + with a one-bit base and a two-bit delta. I
  64. used http://burtleburtle.net/bob/hash/avalanche.html to choose
  65. the operations, constants, and arrangements of the variables.
  66. This does not achieve avalanche. There are input bits of (a,b,c)
  67. that fail to affect some output bits of (a,b,c), especially of a. The
  68. most thoroughly mixed value is c, but it doesn't really even achieve
  69. avalanche in c.
  70. This allows some parallelism. Read-after-writes are good at doubling
  71. the number of bits affected, so the goal of mixing pulls in the opposite
  72. direction as the goal of parallelism. I did what I could. Rotates
  73. seem to cost as much as shifts on every machine I could lay my hands
  74. on, and rotates are much kinder to the top and bottom bits, so I used
  75. rotates.
  76. -------------------------------------------------------------------------------
  77. */
  78. #define mix(a,b,c) \
  79. { \
  80. a -= c; a ^= rot(c, 4); c += b; \
  81. b -= a; b ^= rot(a, 6); a += c; \
  82. c -= b; c ^= rot(b, 8); b += a; \
  83. a -= c; a ^= rot(c,16); c += b; \
  84. b -= a; b ^= rot(a,19); a += c; \
  85. c -= b; c ^= rot(b, 4); b += a; \
  86. }
  87. /*
  88. -------------------------------------------------------------------------------
  89. final -- final mixing of 3 32-bit values (a,b,c) into c
  90. Pairs of (a,b,c) values differing in only a few bits will usually
  91. produce values of c that look totally different. This was tested for
  92. * pairs that differed by one bit, by two bits, in any combination
  93. of top bits of (a,b,c), or in any combination of bottom bits of
  94. (a,b,c).
  95. * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
  96. the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
  97. is commonly produced by subtraction) look like a single 1-bit
  98. difference.
  99. * the base values were pseudorandom, all zero but one bit set, or
  100. all zero plus a counter that starts at zero.
  101. These constants passed:
  102. 14 11 25 16 4 14 24
  103. 12 14 25 16 4 14 24
  104. and these came close:
  105. 4 8 15 26 3 22 24
  106. 10 8 15 26 3 22 24
  107. 11 8 15 26 3 22 24
  108. -------------------------------------------------------------------------------
  109. */
  110. #define final(a,b,c) \
  111. { \
  112. c ^= b; c -= rot(b,14); \
  113. a ^= c; a -= rot(c,11); \
  114. b ^= a; b -= rot(a,25); \
  115. c ^= b; c -= rot(b,16); \
  116. a ^= c; a -= rot(c,4); \
  117. b ^= a; b -= rot(a,14); \
  118. c ^= b; c -= rot(b,24); \
  119. }
  120. /*
  121. --------------------------------------------------------------------
  122. This works on all machines. To be useful, it requires
  123. -- that the key be an array of uint32_t's, and
  124. -- that the length be the number of uint32_t's in the key
  125. The function hashword() is identical to hashlittle() on little-endian
  126. machines, and identical to hashbig() on big-endian machines,
  127. except that the length has to be measured in uint32_ts rather than in
  128. bytes. hashlittle() is more complicated than hashword() only because
  129. hashlittle() has to dance around fitting the key bytes into registers.
  130. --------------------------------------------------------------------
  131. */
  132. uint32_t jenkins_hash32(
  133. const uint32_t *k, /* the key, an array of uint32_t values */
  134. size_t length, /* the length of the key, in uint32_ts */
  135. uint32_t initval) /* the previous hash, or an arbitrary value */
  136. {
  137. uint32_t a,b,c;
  138. /* Set up the internal state */
  139. a = b = c = 0xdeadbeef + (((uint32_t)length)<<2) + initval;
  140. /*------------------------------------------------- handle most of the key */
  141. while (length > 3)
  142. {
  143. a += k[0];
  144. b += k[1];
  145. c += k[2];
  146. mix(a,b,c);
  147. length -= 3;
  148. k += 3;
  149. }
  150. /*------------------------------------------- handle the last 3 uint32_t's */
  151. switch(length) /* all the case statements fall through */
  152. {
  153. case 3 : c+=k[2];
  154. case 2 : b+=k[1];
  155. case 1 : a+=k[0];
  156. final(a,b,c);
  157. case 0: /* case 0: nothing left to add */
  158. break;
  159. }
  160. /*------------------------------------------------------ report the result */
  161. return c;
  162. }
  163. #if BYTE_ORDER == LITTLE_ENDIAN
  164. /*
  165. -------------------------------------------------------------------------------
  166. hashlittle() -- hash a variable-length key into a 32-bit value
  167. k : the key (the unaligned variable-length array of bytes)
  168. length : the length of the key, counting by bytes
  169. initval : can be any 4-byte value
  170. Returns a 32-bit value. Every bit of the key affects every bit of
  171. the return value. Two keys differing by one or two bits will have
  172. totally different hash values.
  173. The best hash table sizes are powers of 2. There is no need to do
  174. mod a prime (mod is sooo slow!). If you need less than 32 bits,
  175. use a bitmask. For example, if you need only 10 bits, do
  176. h = (h & hashmask(10));
  177. In which case, the hash table should have hashsize(10) elements.
  178. If you are hashing n strings (uint8_t **)k, do it like this:
  179. for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
  180. By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this
  181. code any way you wish, private, educational, or commercial. It's free.
  182. Use for hash table lookup, or anything where one collision in 2^^32 is
  183. acceptable. Do NOT use for cryptographic purposes.
  184. -------------------------------------------------------------------------------
  185. */
  186. uint32_t jenkins_hash( const void *key, size_t length, uint32_t initval)
  187. {
  188. uint32_t a,b,c; /* internal state */
  189. union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
  190. /* Set up the internal state */
  191. a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
  192. u.ptr = key;
  193. if ((u.i & 0x3) == 0) {
  194. const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
  195. /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
  196. while (length > 12)
  197. {
  198. a += k[0];
  199. b += k[1];
  200. c += k[2];
  201. mix(a,b,c);
  202. length -= 12;
  203. k += 3;
  204. }
  205. /*----------------------------- handle the last (probably partial) block */
  206. /*
  207. * "k[2]&0xffffff" actually reads beyond the end of the string, but
  208. * then masks off the part it's not allowed to read. Because the
  209. * string is aligned, the masked-off tail is in the same word as the
  210. * rest of the string. Every machine with memory protection I've seen
  211. * does it on word boundaries, so is OK with this. But VALGRIND will
  212. * still catch it and complain. The masking trick does make the hash
  213. * noticably faster for short strings (like English words).
  214. */
  215. switch(length)
  216. {
  217. case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
  218. case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
  219. case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
  220. case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
  221. case 8 : b+=k[1]; a+=k[0]; break;
  222. case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
  223. case 6 : b+=k[1]&0xffff; a+=k[0]; break;
  224. case 5 : b+=k[1]&0xff; a+=k[0]; break;
  225. case 4 : a+=k[0]; break;
  226. case 3 : a+=k[0]&0xffffff; break;
  227. case 2 : a+=k[0]&0xffff; break;
  228. case 1 : a+=k[0]&0xff; break;
  229. case 0 : return c; /* zero length strings require no mixing */
  230. }
  231. } else if ((u.i & 0x1) == 0) {
  232. const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
  233. const uint8_t *k8;
  234. /*--------------- all but last block: aligned reads and different mixing */
  235. while (length > 12)
  236. {
  237. a += k[0] + (((uint32_t)k[1])<<16);
  238. b += k[2] + (((uint32_t)k[3])<<16);
  239. c += k[4] + (((uint32_t)k[5])<<16);
  240. mix(a,b,c);
  241. length -= 12;
  242. k += 6;
  243. }
  244. /*----------------------------- handle the last (probably partial) block */
  245. k8 = (const uint8_t *)k;
  246. switch(length)
  247. {
  248. case 12: c+=k[4]+(((uint32_t)k[5])<<16);
  249. b+=k[2]+(((uint32_t)k[3])<<16);
  250. a+=k[0]+(((uint32_t)k[1])<<16);
  251. break;
  252. case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
  253. case 10: c+=k[4];
  254. b+=k[2]+(((uint32_t)k[3])<<16);
  255. a+=k[0]+(((uint32_t)k[1])<<16);
  256. break;
  257. case 9 : c+=k8[8]; /* fall through */
  258. case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
  259. a+=k[0]+(((uint32_t)k[1])<<16);
  260. break;
  261. case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
  262. case 6 : b+=k[2];
  263. a+=k[0]+(((uint32_t)k[1])<<16);
  264. break;
  265. case 5 : b+=k8[4]; /* fall through */
  266. case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
  267. break;
  268. case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
  269. case 2 : a+=k[0];
  270. break;
  271. case 1 : a+=k8[0];
  272. break;
  273. case 0 : return c; /* zero length requires no mixing */
  274. }
  275. } else { /* need to read the key one byte at a time */
  276. const uint8_t *k = (const uint8_t *)key;
  277. /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
  278. while (length > 12)
  279. {
  280. a += k[0];
  281. a += ((uint32_t)k[1])<<8;
  282. a += ((uint32_t)k[2])<<16;
  283. a += ((uint32_t)k[3])<<24;
  284. b += k[4];
  285. b += ((uint32_t)k[5])<<8;
  286. b += ((uint32_t)k[6])<<16;
  287. b += ((uint32_t)k[7])<<24;
  288. c += k[8];
  289. c += ((uint32_t)k[9])<<8;
  290. c += ((uint32_t)k[10])<<16;
  291. c += ((uint32_t)k[11])<<24;
  292. mix(a,b,c);
  293. length -= 12;
  294. k += 12;
  295. }
  296. /*-------------------------------- last block: affect all 32 bits of (c) */
  297. switch(length) /* all the case statements fall through */
  298. {
  299. case 12: c+=((uint32_t)k[11])<<24;
  300. case 11: c+=((uint32_t)k[10])<<16;
  301. case 10: c+=((uint32_t)k[9])<<8;
  302. case 9 : c+=k[8];
  303. case 8 : b+=((uint32_t)k[7])<<24;
  304. case 7 : b+=((uint32_t)k[6])<<16;
  305. case 6 : b+=((uint32_t)k[5])<<8;
  306. case 5 : b+=k[4];
  307. case 4 : a+=((uint32_t)k[3])<<24;
  308. case 3 : a+=((uint32_t)k[2])<<16;
  309. case 2 : a+=((uint32_t)k[1])<<8;
  310. case 1 : a+=k[0];
  311. break;
  312. case 0 : return c;
  313. }
  314. }
  315. final(a,b,c);
  316. return c;
  317. }
  318. #else /* !(BYTE_ORDER == LITTLE_ENDIAN) */
  319. /*
  320. * hashbig():
  321. * This is the same as hashword() on big-endian machines. It is different
  322. * from hashlittle() on all machines. hashbig() takes advantage of
  323. * big-endian byte ordering.
  324. */
  325. uint32_t jenkins_hash( const void *key, size_t length, uint32_t initval)
  326. {
  327. uint32_t a,b,c;
  328. union { const void *ptr; size_t i; } u; /* to cast key to (size_t) happily */
  329. /* Set up the internal state */
  330. a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
  331. u.ptr = key;
  332. if ((u.i & 0x3) == 0) {
  333. const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
  334. /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
  335. while (length > 12)
  336. {
  337. a += k[0];
  338. b += k[1];
  339. c += k[2];
  340. mix(a,b,c);
  341. length -= 12;
  342. k += 3;
  343. }
  344. /*----------------------------- handle the last (probably partial) block */
  345. /*
  346. * "k[2]<<8" actually reads beyond the end of the string, but
  347. * then shifts out the part it's not allowed to read. Because the
  348. * string is aligned, the illegal read is in the same word as the
  349. * rest of the string. Every machine with memory protection I've seen
  350. * does it on word boundaries, so is OK with this. But VALGRIND will
  351. * still catch it and complain. The masking trick does make the hash
  352. * noticably faster for short strings (like English words).
  353. */
  354. switch(length)
  355. {
  356. case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
  357. case 11: c+=k[2]&0xffffff00; b+=k[1]; a+=k[0]; break;
  358. case 10: c+=k[2]&0xffff0000; b+=k[1]; a+=k[0]; break;
  359. case 9 : c+=k[2]&0xff000000; b+=k[1]; a+=k[0]; break;
  360. case 8 : b+=k[1]; a+=k[0]; break;
  361. case 7 : b+=k[1]&0xffffff00; a+=k[0]; break;
  362. case 6 : b+=k[1]&0xffff0000; a+=k[0]; break;
  363. case 5 : b+=k[1]&0xff000000; a+=k[0]; break;
  364. case 4 : a+=k[0]; break;
  365. case 3 : a+=k[0]&0xffffff00; break;
  366. case 2 : a+=k[0]&0xffff0000; break;
  367. case 1 : a+=k[0]&0xff000000; break;
  368. case 0 : return c; /* zero length strings require no mixing */
  369. }
  370. } else { /* need to read the key one byte at a time */
  371. const uint8_t *k = (const uint8_t *)key;
  372. /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
  373. while (length > 12)
  374. {
  375. a += ((uint32_t)k[0])<<24;
  376. a += ((uint32_t)k[1])<<16;
  377. a += ((uint32_t)k[2])<<8;
  378. a += ((uint32_t)k[3]);
  379. b += ((uint32_t)k[4])<<24;
  380. b += ((uint32_t)k[5])<<16;
  381. b += ((uint32_t)k[6])<<8;
  382. b += ((uint32_t)k[7]);
  383. c += ((uint32_t)k[8])<<24;
  384. c += ((uint32_t)k[9])<<16;
  385. c += ((uint32_t)k[10])<<8;
  386. c += ((uint32_t)k[11]);
  387. mix(a,b,c);
  388. length -= 12;
  389. k += 12;
  390. }
  391. /*-------------------------------- last block: affect all 32 bits of (c) */
  392. switch(length) /* all the case statements fall through */
  393. {
  394. case 12: c+=k[11];
  395. case 11: c+=((uint32_t)k[10])<<8;
  396. case 10: c+=((uint32_t)k[9])<<16;
  397. case 9 : c+=((uint32_t)k[8])<<24;
  398. case 8 : b+=k[7];
  399. case 7 : b+=((uint32_t)k[6])<<8;
  400. case 6 : b+=((uint32_t)k[5])<<16;
  401. case 5 : b+=((uint32_t)k[4])<<24;
  402. case 4 : a+=k[3];
  403. case 3 : a+=((uint32_t)k[2])<<8;
  404. case 2 : a+=((uint32_t)k[1])<<16;
  405. case 1 : a+=((uint32_t)k[0])<<24;
  406. break;
  407. case 0 : return c;
  408. }
  409. }
  410. final(a,b,c);
  411. return c;
  412. }
  413. #endif