lookup3.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. /*
  2. -------------------------------------------------------------------------------
  3. lookup3.c, by Bob Jenkins, May 2006, Public Domain.
  4. Original: http://burtleburtle.net/bob/c/lookup3.c
  5. Modified by Russ Rew for adaption in netCDF.
  6. - Make use of Paul Hsieh's pstdint.h, if stdint.h not available.
  7. - Declare unused functions static to keep global namespace clean.
  8. - Provide function hash_fast() that uses either hashlittle() or
  9. hashbig(), depending on endianness.
  10. - Because portability is more important than speed for netCDF use,
  11. we define VALGRIND to skip "#ifndef VALGRIND" code, so reads of
  12. strings don't access extra bytes after end of string. This may
  13. slow it down enough to justify a simpler hash, but blame me, not
  14. original author!
  15. These are functions for producing 32-bit hashes for hash table lookup.
  16. hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
  17. are externally useful functions. Routines to test the hash are included
  18. if SELF_TEST is defined. You can use this free for any purpose. It's in
  19. the public domain. It has no warranty.
  20. You probably want to use hashlittle(). hashlittle() and hashbig()
  21. hash byte arrays. hashlittle() is is faster than hashbig() on
  22. little-endian machines. Intel and AMD are little-endian machines.
  23. On second thought, you probably want hashlittle2(), which is identical to
  24. hashlittle() except it returns two 32-bit hashes for the price of one.
  25. You could implement hashbig2() if you wanted but I haven't bothered here.
  26. If you want to find a hash of, say, exactly 7 integers, do
  27. a = i1; b = i2; c = i3;
  28. mix(a,b,c);
  29. a += i4; b += i5; c += i6;
  30. mix(a,b,c);
  31. a += i7;
  32. final(a,b,c);
  33. then use c as the hash value. If you have a variable length array of
  34. 4-byte integers to hash, use hashword(). If you have a byte array (like
  35. a character string), use hashlittle(). If you have several byte arrays, or
  36. a mix of things, see the comments above hashlittle().
  37. Why is this so big? I read 12 bytes at a time into 3 4-byte integers,
  38. then mix those integers. This is fast (you can do a lot more thorough
  39. mixing with 12*3 instructions on 3 integers than you can with 3 instructions
  40. on 1 byte), but shoehorning those bytes into integers efficiently is messy.
  41. -------------------------------------------------------------------------------
  42. */
  43. /* #define SELF_TEST 1 */
  44. #include <config.h>
  45. #include <stdio.h> /* defines printf for tests */
  46. #include <time.h> /* defines time_t for timings in the test */
  47. #ifndef HAVE_STDINT_H
  48. # include "pstdint.h" /* attempts to define uint32_t etc portably */
  49. #else
  50. # include <stdint.h>
  51. #endif /* HAVE_STDINT_H */
  52. #ifdef HAVE_SYS_PARAM_H
  53. #include <sys/param.h> /* attempt to define endianness */
  54. #endif /* HAVE_SYS_PARAM_H */
  55. #ifdef linux
  56. # include <endian.h> /* attempt to define endianness */
  57. #endif
  58. #define VALGRIND /* added by Russ Rew, for portability over speed */
  59. #ifndef WORDS_BIGENDIAN /* from config.h */
  60. #define HASH_LITTLE_ENDIAN 1
  61. #define HASH_BIG_ENDIAN 0
  62. #else
  63. #define HASH_LITTLE_ENDIAN 0
  64. #define HASH_BIG_ENDIAN 1
  65. #endif
  66. #define hashsize(n) ((uint32_t)1<<(n))
  67. #define hashmask(n) (hashsize(n)-1)
  68. #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
  69. /*
  70. -------------------------------------------------------------------------------
  71. mix -- mix 3 32-bit values reversibly.
  72. This is reversible, so any information in (a,b,c) before mix() is
  73. still in (a,b,c) after mix().
  74. If four pairs of (a,b,c) inputs are run through mix(), or through
  75. mix() in reverse, there are at least 32 bits of the output that
  76. are sometimes the same for one pair and different for another pair.
  77. This was tested for:
  78. * pairs that differed by one bit, by two bits, in any combination
  79. of top bits of (a,b,c), or in any combination of bottom bits of
  80. (a,b,c).
  81. * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
  82. the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
  83. is commonly produced by subtraction) look like a single 1-bit
  84. difference.
  85. * the base values were pseudorandom, all zero but one bit set, or
  86. all zero plus a counter that starts at zero.
  87. Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
  88. satisfy this are
  89. 4 6 8 16 19 4
  90. 9 15 3 18 27 15
  91. 14 9 3 7 17 3
  92. Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
  93. for "differ" defined as + with a one-bit base and a two-bit delta. I
  94. used http://burtleburtle.net/bob/hash/avalanche.html to choose
  95. the operations, constants, and arrangements of the variables.
  96. This does not achieve avalanche. There are input bits of (a,b,c)
  97. that fail to affect some output bits of (a,b,c), especially of a. The
  98. most thoroughly mixed value is c, but it doesn't really even achieve
  99. avalanche in c.
  100. This allows some parallelism. Read-after-writes are good at doubling
  101. the number of bits affected, so the goal of mixing pulls in the opposite
  102. direction as the goal of parallelism. I did what I could. Rotates
  103. seem to cost as much as shifts on every machine I could lay my hands
  104. on, and rotates are much kinder to the top and bottom bits, so I used
  105. rotates.
  106. -------------------------------------------------------------------------------
  107. */
  108. #define mix(a,b,c) \
  109. { \
  110. a -= c; a ^= rot(c, 4); c += b; \
  111. b -= a; b ^= rot(a, 6); a += c; \
  112. c -= b; c ^= rot(b, 8); b += a; \
  113. a -= c; a ^= rot(c,16); c += b; \
  114. b -= a; b ^= rot(a,19); a += c; \
  115. c -= b; c ^= rot(b, 4); b += a; \
  116. }
  117. /*
  118. -------------------------------------------------------------------------------
  119. final -- final mixing of 3 32-bit values (a,b,c) into c
  120. Pairs of (a,b,c) values differing in only a few bits will usually
  121. produce values of c that look totally different. This was tested for
  122. * pairs that differed by one bit, by two bits, in any combination
  123. of top bits of (a,b,c), or in any combination of bottom bits of
  124. (a,b,c).
  125. * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
  126. the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
  127. is commonly produced by subtraction) look like a single 1-bit
  128. difference.
  129. * the base values were pseudorandom, all zero but one bit set, or
  130. all zero plus a counter that starts at zero.
  131. These constants passed:
  132. 14 11 25 16 4 14 24
  133. 12 14 25 16 4 14 24
  134. and these came close:
  135. 4 8 15 26 3 22 24
  136. 10 8 15 26 3 22 24
  137. 11 8 15 26 3 22 24
  138. -------------------------------------------------------------------------------
  139. */
  140. #define final(a,b,c) \
  141. { \
  142. c ^= b; c -= rot(b,14); \
  143. a ^= c; a -= rot(c,11); \
  144. b ^= a; b -= rot(a,25); \
  145. c ^= b; c -= rot(b,16); \
  146. a ^= c; a -= rot(c,4); \
  147. b ^= a; b -= rot(a,14); \
  148. c ^= b; c -= rot(b,24); \
  149. }
  150. /*
  151. --------------------------------------------------------------------
  152. This works on all machines. To be useful, it requires
  153. -- that the key be an array of uint32_t's, and
  154. -- that the length be the number of uint32_t's in the key
  155. The function hashword() is identical to hashlittle() on little-endian
  156. machines, and identical to hashbig() on big-endian machines,
  157. except that the length has to be measured in uint32_ts rather than in
  158. bytes. hashlittle() is more complicated than hashword() only because
  159. hashlittle() has to dance around fitting the key bytes into registers.
  160. --------------------------------------------------------------------
  161. */
  162. #ifdef SELF_TEST
  163. static
  164. uint32_t hashword(
  165. const uint32_t *k, /* the key, an array of uint32_t values */
  166. size_t length, /* the length of the key, in uint32_ts */
  167. uint32_t initval) /* the previous hash, or an arbitrary value */
  168. {
  169. uint32_t a,b,c;
  170. /* Set up the internal state */
  171. a = b = c = 0xdeadbeef + (((uint32_t)length)<<2) + initval;
  172. /*------------------------------------------------- handle most of the key */
  173. while (length > 3)
  174. {
  175. a += k[0];
  176. b += k[1];
  177. c += k[2];
  178. mix(a,b,c);
  179. length -= 3;
  180. k += 3;
  181. }
  182. /*------------------------------------------- handle the last 3 uint32_t's */
  183. switch(length) /* all the case statements fall through */
  184. {
  185. case 3 : c+=k[2];
  186. case 2 : b+=k[1];
  187. case 1 : a+=k[0];
  188. final(a,b,c);
  189. case 0: /* case 0: nothing left to add */
  190. break;
  191. }
  192. /*------------------------------------------------------ report the result */
  193. return c;
  194. }
  195. /*
  196. --------------------------------------------------------------------
  197. hashword2() -- same as hashword(), but take two seeds and return two
  198. 32-bit values. pc and pb must both be nonnull, and *pc and *pb must
  199. both be initialized with seeds. If you pass in (*pb)==0, the output
  200. (*pc) will be the same as the return value from hashword().
  201. --------------------------------------------------------------------
  202. */
  203. static
  204. void hashword2 (
  205. const uint32_t *k, /* the key, an array of uint32_t values */
  206. size_t length, /* the length of the key, in uint32_ts */
  207. uint32_t *pc, /* IN: seed OUT: primary hash value */
  208. uint32_t *pb) /* IN: more seed OUT: secondary hash value */
  209. {
  210. uint32_t a,b,c;
  211. /* Set up the internal state */
  212. a = b = c = 0xdeadbeef + ((uint32_t)(length<<2)) + *pc;
  213. c += *pb;
  214. /*------------------------------------------------- handle most of the key */
  215. while (length > 3)
  216. {
  217. a += k[0];
  218. b += k[1];
  219. c += k[2];
  220. mix(a,b,c);
  221. length -= 3;
  222. k += 3;
  223. }
  224. /*------------------------------------------- handle the last 3 uint32_t's */
  225. switch(length) /* all the case statements fall through */
  226. {
  227. case 3 : c+=k[2];
  228. case 2 : b+=k[1];
  229. case 1 : a+=k[0];
  230. final(a,b,c);
  231. case 0: /* case 0: nothing left to add */
  232. break;
  233. }
  234. /*------------------------------------------------------ report the result */
  235. *pc=c; *pb=b;
  236. }
  237. /*
  238. * hashlittle2: return 2 32-bit hash values
  239. *
  240. * This is identical to hashlittle(), except it returns two 32-bit hash
  241. * values instead of just one. This is good enough for hash table
  242. * lookup with 2^^64 buckets, or if you want a second hash if you're not
  243. * happy with the first, or if you want a probably-unique 64-bit ID for
  244. * the key. *pc is better mixed than *pb, so use *pc first. If you want
  245. * a 64-bit value do something like "*pc + (((uint64_t)*pb)<<32)".
  246. */
  247. static void
  248. hashlittle2(
  249. const void *key, /* the key to hash */
  250. size_t length, /* length of the key */
  251. uint32_t *pc, /* IN: primary initval, OUT: primary hash */
  252. uint32_t *pb) /* IN: secondary initval, OUT: secondary hash */
  253. {
  254. uint32_t a,b,c; /* internal state */
  255. union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
  256. /* Set up the internal state */
  257. a = b = c = 0xdeadbeef + ((uint32_t)length) + *pc;
  258. c += *pb;
  259. u.ptr = key;
  260. if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
  261. const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
  262. const uint8_t *k8;
  263. /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
  264. while (length > 12)
  265. {
  266. a += k[0];
  267. b += k[1];
  268. c += k[2];
  269. mix(a,b,c);
  270. length -= 12;
  271. k += 3;
  272. }
  273. /*----------------------------- handle the last (probably partial) block */
  274. /*
  275. * "k[2]&0xffffff" actually reads beyond the end of the string, but
  276. * then masks off the part it's not allowed to read. Because the
  277. * string is aligned, the masked-off tail is in the same word as the
  278. * rest of the string. Every machine with memory protection I've seen
  279. * does it on word boundaries, so is OK with this. But VALGRIND will
  280. * still catch it and complain. The masking trick does make the hash
  281. * noticably faster for short strings (like English words).
  282. */
  283. #ifndef VALGRIND
  284. switch(length)
  285. {
  286. case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
  287. case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
  288. case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
  289. case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
  290. case 8 : b+=k[1]; a+=k[0]; break;
  291. case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
  292. case 6 : b+=k[1]&0xffff; a+=k[0]; break;
  293. case 5 : b+=k[1]&0xff; a+=k[0]; break;
  294. case 4 : a+=k[0]; break;
  295. case 3 : a+=k[0]&0xffffff; break;
  296. case 2 : a+=k[0]&0xffff; break;
  297. case 1 : a+=k[0]&0xff; break;
  298. case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
  299. }
  300. #else /* make valgrind happy */
  301. k8 = (const uint8_t *)k;
  302. switch(length)
  303. {
  304. case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
  305. case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
  306. case 10: c+=((uint32_t)k8[9])<<8; /* fall through */
  307. case 9 : c+=k8[8]; /* fall through */
  308. case 8 : b+=k[1]; a+=k[0]; break;
  309. case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
  310. case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */
  311. case 5 : b+=k8[4]; /* fall through */
  312. case 4 : a+=k[0]; break;
  313. case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
  314. case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */
  315. case 1 : a+=k8[0]; break;
  316. case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
  317. }
  318. #endif /* !valgrind */
  319. } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
  320. const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
  321. const uint8_t *k8;
  322. /*--------------- all but last block: aligned reads and different mixing */
  323. while (length > 12)
  324. {
  325. a += k[0] + (((uint32_t)k[1])<<16);
  326. b += k[2] + (((uint32_t)k[3])<<16);
  327. c += k[4] + (((uint32_t)k[5])<<16);
  328. mix(a,b,c);
  329. length -= 12;
  330. k += 6;
  331. }
  332. /*----------------------------- handle the last (probably partial) block */
  333. k8 = (const uint8_t *)k;
  334. switch(length)
  335. {
  336. case 12: c+=k[4]+(((uint32_t)k[5])<<16);
  337. b+=k[2]+(((uint32_t)k[3])<<16);
  338. a+=k[0]+(((uint32_t)k[1])<<16);
  339. break;
  340. case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
  341. case 10: c+=k[4];
  342. b+=k[2]+(((uint32_t)k[3])<<16);
  343. a+=k[0]+(((uint32_t)k[1])<<16);
  344. break;
  345. case 9 : c+=k8[8]; /* fall through */
  346. case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
  347. a+=k[0]+(((uint32_t)k[1])<<16);
  348. break;
  349. case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
  350. case 6 : b+=k[2];
  351. a+=k[0]+(((uint32_t)k[1])<<16);
  352. break;
  353. case 5 : b+=k8[4]; /* fall through */
  354. case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
  355. break;
  356. case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
  357. case 2 : a+=k[0];
  358. break;
  359. case 1 : a+=k8[0];
  360. break;
  361. case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
  362. }
  363. } else { /* need to read the key one byte at a time */
  364. const uint8_t *k = (const uint8_t *)key;
  365. /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
  366. while (length > 12)
  367. {
  368. a += k[0];
  369. a += ((uint32_t)k[1])<<8;
  370. a += ((uint32_t)k[2])<<16;
  371. a += ((uint32_t)k[3])<<24;
  372. b += k[4];
  373. b += ((uint32_t)k[5])<<8;
  374. b += ((uint32_t)k[6])<<16;
  375. b += ((uint32_t)k[7])<<24;
  376. c += k[8];
  377. c += ((uint32_t)k[9])<<8;
  378. c += ((uint32_t)k[10])<<16;
  379. c += ((uint32_t)k[11])<<24;
  380. mix(a,b,c);
  381. length -= 12;
  382. k += 12;
  383. }
  384. /*-------------------------------- last block: affect all 32 bits of (c) */
  385. switch(length) /* all the case statements fall through */
  386. {
  387. case 12: c+=((uint32_t)k[11])<<24;
  388. case 11: c+=((uint32_t)k[10])<<16;
  389. case 10: c+=((uint32_t)k[9])<<8;
  390. case 9 : c+=k[8];
  391. case 8 : b+=((uint32_t)k[7])<<24;
  392. case 7 : b+=((uint32_t)k[6])<<16;
  393. case 6 : b+=((uint32_t)k[5])<<8;
  394. case 5 : b+=k[4];
  395. case 4 : a+=((uint32_t)k[3])<<24;
  396. case 3 : a+=((uint32_t)k[2])<<16;
  397. case 2 : a+=((uint32_t)k[1])<<8;
  398. case 1 : a+=k[0];
  399. break;
  400. case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
  401. }
  402. }
  403. final(a,b,c);
  404. *pc=c; *pb=b;
  405. }
  406. #endif /*SELF_TEST*/
  407. #ifdef WORDS_BIGENDIAN
  408. /*
  409. * hashbig():
  410. * This is the same as hashword() on big-endian machines. It is different
  411. * from hashlittle() on all machines. hashbig() takes advantage of
  412. * big-endian byte ordering.
  413. */
  414. static uint32_t
  415. hashbig( const void *key, size_t length, uint32_t initval)
  416. {
  417. uint32_t a,b,c;
  418. union { const void *ptr; size_t i; } u; /* to cast key to (size_t) happily */
  419. /* Set up the internal state */
  420. a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
  421. u.ptr = key;
  422. if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) {
  423. const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
  424. const uint8_t *k8;
  425. /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
  426. while (length > 12)
  427. {
  428. a += k[0];
  429. b += k[1];
  430. c += k[2];
  431. mix(a,b,c);
  432. length -= 12;
  433. k += 3;
  434. }
  435. /*----------------------------- handle the last (probably partial) block */
  436. /*
  437. * "k[2]<<8" actually reads beyond the end of the string, but
  438. * then shifts out the part it's not allowed to read. Because the
  439. * string is aligned, the illegal read is in the same word as the
  440. * rest of the string. Every machine with memory protection I've seen
  441. * does it on word boundaries, so is OK with this. But VALGRIND will
  442. * still catch it and complain. The masking trick does make the hash
  443. * noticably faster for short strings (like English words).
  444. */
  445. #ifndef VALGRIND
  446. switch(length)
  447. {
  448. case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
  449. case 11: c+=k[2]&0xffffff00; b+=k[1]; a+=k[0]; break;
  450. case 10: c+=k[2]&0xffff0000; b+=k[1]; a+=k[0]; break;
  451. case 9 : c+=k[2]&0xff000000; b+=k[1]; a+=k[0]; break;
  452. case 8 : b+=k[1]; a+=k[0]; break;
  453. case 7 : b+=k[1]&0xffffff00; a+=k[0]; break;
  454. case 6 : b+=k[1]&0xffff0000; a+=k[0]; break;
  455. case 5 : b+=k[1]&0xff000000; a+=k[0]; break;
  456. case 4 : a+=k[0]; break;
  457. case 3 : a+=k[0]&0xffffff00; break;
  458. case 2 : a+=k[0]&0xffff0000; break;
  459. case 1 : a+=k[0]&0xff000000; break;
  460. case 0 : return c; /* zero length strings require no mixing */
  461. }
  462. #else /* make valgrind happy */
  463. k8 = (const uint8_t *)k;
  464. switch(length) /* all the case statements fall through */
  465. {
  466. case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
  467. case 11: c+=((uint32_t)k8[10])<<8; /* fall through */
  468. case 10: c+=((uint32_t)k8[9])<<16; /* fall through */
  469. case 9 : c+=((uint32_t)k8[8])<<24; /* fall through */
  470. case 8 : b+=k[1]; a+=k[0]; break;
  471. case 7 : b+=((uint32_t)k8[6])<<8; /* fall through */
  472. case 6 : b+=((uint32_t)k8[5])<<16; /* fall through */
  473. case 5 : b+=((uint32_t)k8[4])<<24; /* fall through */
  474. case 4 : a+=k[0]; break;
  475. case 3 : a+=((uint32_t)k8[2])<<8; /* fall through */
  476. case 2 : a+=((uint32_t)k8[1])<<16; /* fall through */
  477. case 1 : a+=((uint32_t)k8[0])<<24; break;
  478. case 0 : return c;
  479. }
  480. #endif /* !VALGRIND */
  481. } else { /* need to read the key one byte at a time */
  482. const uint8_t *k = (const uint8_t *)key;
  483. /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
  484. while (length > 12)
  485. {
  486. a += ((uint32_t)k[0])<<24;
  487. a += ((uint32_t)k[1])<<16;
  488. a += ((uint32_t)k[2])<<8;
  489. a += ((uint32_t)k[3]);
  490. b += ((uint32_t)k[4])<<24;
  491. b += ((uint32_t)k[5])<<16;
  492. b += ((uint32_t)k[6])<<8;
  493. b += ((uint32_t)k[7]);
  494. c += ((uint32_t)k[8])<<24;
  495. c += ((uint32_t)k[9])<<16;
  496. c += ((uint32_t)k[10])<<8;
  497. c += ((uint32_t)k[11]);
  498. mix(a,b,c);
  499. length -= 12;
  500. k += 12;
  501. }
  502. /*-------------------------------- last block: affect all 32 bits of (c) */
  503. switch(length) /* all the case statements fall through */
  504. {
  505. case 12: c+=k[11];
  506. case 11: c+=((uint32_t)k[10])<<8;
  507. case 10: c+=((uint32_t)k[9])<<16;
  508. case 9 : c+=((uint32_t)k[8])<<24;
  509. case 8 : b+=k[7];
  510. case 7 : b+=((uint32_t)k[6])<<8;
  511. case 6 : b+=((uint32_t)k[5])<<16;
  512. case 5 : b+=((uint32_t)k[4])<<24;
  513. case 4 : a+=k[3];
  514. case 3 : a+=((uint32_t)k[2])<<8;
  515. case 2 : a+=((uint32_t)k[1])<<16;
  516. case 1 : a+=((uint32_t)k[0])<<24;
  517. break;
  518. case 0 : return c;
  519. }
  520. }
  521. final(a,b,c);
  522. return c;
  523. }
  524. #endif /*WORDS_BIGENDIAN*/
  525. /*
  526. -------------------------------------------------------------------------------
  527. hashlittle() -- hash a variable-length key into a 32-bit value
  528. k : the key (the unaligned variable-length array of bytes)
  529. length : the length of the key, counting by bytes
  530. initval : can be any 4-byte value
  531. Returns a 32-bit value. Every bit of the key affects every bit of
  532. the return value. Two keys differing by one or two bits will have
  533. totally different hash values.
  534. The best hash table sizes are powers of 2. There is no need to do
  535. mod a prime (mod is sooo slow!). If you need less than 32 bits,
  536. use a bitmask. For example, if you need only 10 bits, do
  537. h = (h & hashmask(10));
  538. In which case, the hash table should have hashsize(10) elements.
  539. If you are hashing n strings (uint8_t **)k, do it like this:
  540. for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
  541. By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this
  542. code any way you wish, private, educational, or commercial. It's free.
  543. Use for hash table lookup, or anything where one collision in 2^^32 is
  544. acceptable. Do NOT use for cryptographic purposes.
  545. -------------------------------------------------------------------------------
  546. */
  547. static uint32_t
  548. hashlittle( const void *key, size_t length, uint32_t initval)
  549. {
  550. uint32_t a,b,c; /* internal state */
  551. union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
  552. /* Set up the internal state */
  553. a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
  554. u.ptr = key;
  555. if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
  556. const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
  557. const uint8_t *k8;
  558. /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
  559. while (length > 12)
  560. {
  561. a += k[0];
  562. b += k[1];
  563. c += k[2];
  564. mix(a,b,c);
  565. length -= 12;
  566. k += 3;
  567. }
  568. /*----------------------------- handle the last (probably partial) block */
  569. /*
  570. * "k[2]&0xffffff" actually reads beyond the end of the string, but
  571. * then masks off the part it's not allowed to read. Because the
  572. * string is aligned, the masked-off tail is in the same word as the
  573. * rest of the string. Every machine with memory protection I've seen
  574. * does it on word boundaries, so is OK with this. But VALGRIND will
  575. * still catch it and complain. The masking trick does make the hash
  576. * noticably faster for short strings (like English words).
  577. */
  578. #ifndef VALGRIND
  579. switch(length)
  580. {
  581. case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
  582. case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
  583. case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
  584. case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
  585. case 8 : b+=k[1]; a+=k[0]; break;
  586. case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
  587. case 6 : b+=k[1]&0xffff; a+=k[0]; break;
  588. case 5 : b+=k[1]&0xff; a+=k[0]; break;
  589. case 4 : a+=k[0]; break;
  590. case 3 : a+=k[0]&0xffffff; break;
  591. case 2 : a+=k[0]&0xffff; break;
  592. case 1 : a+=k[0]&0xff; break;
  593. case 0 : return c; /* zero length strings require no mixing */
  594. }
  595. #else /* make valgrind happy */
  596. k8 = (const uint8_t *)k;
  597. switch(length)
  598. {
  599. case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
  600. case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
  601. case 10: c+=((uint32_t)k8[9])<<8; /* fall through */
  602. case 9 : c+=k8[8]; /* fall through */
  603. case 8 : b+=k[1]; a+=k[0]; break;
  604. case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
  605. case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */
  606. case 5 : b+=k8[4]; /* fall through */
  607. case 4 : a+=k[0]; break;
  608. case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
  609. case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */
  610. case 1 : a+=k8[0]; break;
  611. case 0 : return c;
  612. }
  613. #endif /* !valgrind */
  614. } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
  615. const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
  616. const uint8_t *k8;
  617. /*--------------- all but last block: aligned reads and different mixing */
  618. while (length > 12)
  619. {
  620. a += k[0] + (((uint32_t)k[1])<<16);
  621. b += k[2] + (((uint32_t)k[3])<<16);
  622. c += k[4] + (((uint32_t)k[5])<<16);
  623. mix(a,b,c);
  624. length -= 12;
  625. k += 6;
  626. }
  627. /*----------------------------- handle the last (probably partial) block */
  628. k8 = (const uint8_t *)k;
  629. switch(length)
  630. {
  631. case 12: c+=k[4]+(((uint32_t)k[5])<<16);
  632. b+=k[2]+(((uint32_t)k[3])<<16);
  633. a+=k[0]+(((uint32_t)k[1])<<16);
  634. break;
  635. case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
  636. case 10: c+=k[4];
  637. b+=k[2]+(((uint32_t)k[3])<<16);
  638. a+=k[0]+(((uint32_t)k[1])<<16);
  639. break;
  640. case 9 : c+=k8[8]; /* fall through */
  641. case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
  642. a+=k[0]+(((uint32_t)k[1])<<16);
  643. break;
  644. case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
  645. case 6 : b+=k[2];
  646. a+=k[0]+(((uint32_t)k[1])<<16);
  647. break;
  648. case 5 : b+=k8[4]; /* fall through */
  649. case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
  650. break;
  651. case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
  652. case 2 : a+=k[0];
  653. break;
  654. case 1 : a+=k8[0];
  655. break;
  656. case 0 : return c; /* zero length requires no mixing */
  657. }
  658. } else { /* need to read the key one byte at a time */
  659. const uint8_t *k = (const uint8_t *)key;
  660. /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
  661. while (length > 12)
  662. {
  663. a += k[0];
  664. a += ((uint32_t)k[1])<<8;
  665. a += ((uint32_t)k[2])<<16;
  666. a += ((uint32_t)k[3])<<24;
  667. b += k[4];
  668. b += ((uint32_t)k[5])<<8;
  669. b += ((uint32_t)k[6])<<16;
  670. b += ((uint32_t)k[7])<<24;
  671. c += k[8];
  672. c += ((uint32_t)k[9])<<8;
  673. c += ((uint32_t)k[10])<<16;
  674. c += ((uint32_t)k[11])<<24;
  675. mix(a,b,c);
  676. length -= 12;
  677. k += 12;
  678. }
  679. /*-------------------------------- last block: affect all 32 bits of (c) */
  680. switch(length) /* all the case statements fall through */
  681. {
  682. case 12: c+=((uint32_t)k[11])<<24;
  683. case 11: c+=((uint32_t)k[10])<<16;
  684. case 10: c+=((uint32_t)k[9])<<8;
  685. case 9 : c+=k[8];
  686. case 8 : b+=((uint32_t)k[7])<<24;
  687. case 7 : b+=((uint32_t)k[6])<<16;
  688. case 6 : b+=((uint32_t)k[5])<<8;
  689. case 5 : b+=k[4];
  690. case 4 : a+=((uint32_t)k[3])<<24;
  691. case 3 : a+=((uint32_t)k[2])<<16;
  692. case 2 : a+=((uint32_t)k[1])<<8;
  693. case 1 : a+=k[0];
  694. break;
  695. case 0 : return c;
  696. }
  697. }
  698. final(a,b,c);
  699. return c;
  700. }
  701. /*
  702. * hash_fast(key, length, initval)
  703. * Wrapper that calls either hashlittle or hashbig, depending on endianness.
  704. */
  705. uint32_t
  706. hash_fast( const void *key, size_t length) {
  707. #define NC_ARBITRARY_UINT (992099683U)
  708. #ifndef WORDS_BIGENDIAN
  709. return hashlittle(key, length, NC_ARBITRARY_UINT);
  710. #else
  711. return hashbig(key, length, NC_ARBITRARY_UINT);
  712. #endif
  713. }
  714. #ifdef SELF_TEST
  715. /* used for timings */
  716. void driver1()
  717. {
  718. uint8_t buf[256];
  719. uint32_t i;
  720. uint32_t h=0;
  721. time_t a,z;
  722. time(&a);
  723. for (i=0; i<256; ++i) buf[i] = 'x';
  724. for (i=0; i<1; ++i)
  725. {
  726. h = hashlittle(&buf[0],1,h);
  727. }
  728. time(&z);
  729. if (z-a > 0) printf("time %d %.8x\n", z-a, h);
  730. }
  731. /* check that every input bit changes every output bit half the time */
  732. #define HASHSTATE 1
  733. #define HASHLEN 1
  734. #define MAXPAIR 60
  735. #define MAXLEN 70
  736. void driver2()
  737. {
  738. uint8_t qa[MAXLEN+1], qb[MAXLEN+2], *a = &qa[0], *b = &qb[1];
  739. uint32_t c[HASHSTATE], d[HASHSTATE], i=0, j=0, k, l, m=0, z;
  740. uint32_t e[HASHSTATE],f[HASHSTATE],g[HASHSTATE],h[HASHSTATE];
  741. uint32_t x[HASHSTATE],y[HASHSTATE];
  742. uint32_t hlen;
  743. printf("No more than %d trials should ever be needed \n",MAXPAIR/2);
  744. for (hlen=0; hlen < MAXLEN; ++hlen)
  745. {
  746. z=0;
  747. for (i=0; i<hlen; ++i) /*----------------------- for each input byte, */
  748. {
  749. for (j=0; j<8; ++j) /*------------------------ for each input bit, */
  750. {
  751. for (m=1; m<8; ++m) /*------------ for serveral possible initvals, */
  752. {
  753. for (l=0; l<HASHSTATE; ++l)
  754. e[l]=f[l]=g[l]=h[l]=x[l]=y[l]=~((uint32_t)0);
  755. /*---- check that every output bit is affected by that input bit */
  756. for (k=0; k<MAXPAIR; k+=2)
  757. {
  758. uint32_t finished=1;
  759. /* keys have one bit different */
  760. for (l=0; l<hlen+1; ++l) {a[l] = b[l] = (uint8_t)0;}
  761. /* have a and b be two keys differing in only one bit */
  762. a[i] ^= (k<<j);
  763. a[i] ^= (k>>(8-j));
  764. c[0] = hashlittle(a, hlen, m);
  765. b[i] ^= ((k+1)<<j);
  766. b[i] ^= ((k+1)>>(8-j));
  767. d[0] = hashlittle(b, hlen, m);
  768. /* check every bit is 1, 0, set, and not set at least once */
  769. for (l=0; l<HASHSTATE; ++l)
  770. {
  771. e[l] &= (c[l]^d[l]);
  772. f[l] &= ~(c[l]^d[l]);
  773. g[l] &= c[l];
  774. h[l] &= ~c[l];
  775. x[l] &= d[l];
  776. y[l] &= ~d[l];
  777. if (e[l]|f[l]|g[l]|h[l]|x[l]|y[l]) finished=0;
  778. }
  779. if (finished) break;
  780. }
  781. if (k>z) z=k;
  782. if (k==MAXPAIR)
  783. {
  784. printf("Some bit didn't change: ");
  785. printf("%.8x %.8x %.8x %.8x %.8x %.8x ",
  786. e[0],f[0],g[0],h[0],x[0],y[0]);
  787. printf("i %d j %d m %d len %d\n", i, j, m, hlen);
  788. }
  789. if (z==MAXPAIR) goto done;
  790. }
  791. }
  792. }
  793. done:
  794. if (z < MAXPAIR)
  795. {
  796. printf("Mix success %2d bytes %2d initvals ",i,m);
  797. printf("required %d trials\n", z/2);
  798. }
  799. }
  800. printf("\n");
  801. }
  802. /* Check for reading beyond the end of the buffer and alignment problems */
  803. void driver3()
  804. {
  805. uint8_t buf[MAXLEN+20], *b;
  806. uint32_t len;
  807. uint8_t q[] = "This is the time for all good men to come to the aid of their country...";
  808. uint32_t h;
  809. uint8_t qq[] = "xThis is the time for all good men to come to the aid of their country...";
  810. uint32_t i;
  811. uint8_t qqq[] = "xxThis is the time for all good men to come to the aid of their country...";
  812. uint32_t j;
  813. uint8_t qqqq[] = "xxxThis is the time for all good men to come to the aid of their country...";
  814. uint32_t ref,x,y;
  815. uint8_t *p;
  816. printf("Endianness. These lines should all be the same (for values filled in):\n");
  817. printf("%.8x %.8x %.8x\n",
  818. hashword((const uint32_t *)q, (sizeof(q)-1)/4, 13),
  819. hashword((const uint32_t *)q, (sizeof(q)-5)/4, 13),
  820. hashword((const uint32_t *)q, (sizeof(q)-9)/4, 13));
  821. p = q;
  822. printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
  823. hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
  824. hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
  825. hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
  826. hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
  827. hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
  828. hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
  829. p = &qq[1];
  830. printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
  831. hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
  832. hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
  833. hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
  834. hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
  835. hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
  836. hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
  837. p = &qqq[2];
  838. printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
  839. hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
  840. hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
  841. hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
  842. hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
  843. hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
  844. hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
  845. p = &qqqq[3];
  846. printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
  847. hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
  848. hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
  849. hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
  850. hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
  851. hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
  852. hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
  853. printf("\n");
  854. /* check that hashlittle2 and hashlittle produce the same results */
  855. i=47; j=0;
  856. hashlittle2(q, sizeof(q), &i, &j);
  857. if (hashlittle(q, sizeof(q), 47) != i)
  858. printf("hashlittle2 and hashlittle mismatch\n");
  859. /* check that hashword2 and hashword produce the same results */
  860. len = 0xdeadbeef;
  861. i=47, j=0;
  862. hashword2(&len, 1, &i, &j);
  863. if (hashword(&len, 1, 47) != i)
  864. printf("hashword2 and hashword mismatch %x %x\n",
  865. i, hashword(&len, 1, 47));
  866. /* check hashlittle doesn't read before or after the ends of the string */
  867. for (h=0, b=buf+1; h<8; ++h, ++b)
  868. {
  869. for (i=0; i<MAXLEN; ++i)
  870. {
  871. len = i;
  872. for (j=0; j<i; ++j) *(b+j)=0;
  873. /* these should all be equal */
  874. ref = hashlittle(b, len, (uint32_t)1);
  875. *(b+i)=(uint8_t)~0;
  876. *(b-1)=(uint8_t)~0;
  877. x = hashlittle(b, len, (uint32_t)1);
  878. y = hashlittle(b, len, (uint32_t)1);
  879. if ((ref != x) || (ref != y))
  880. {
  881. printf("alignment error: %.8x %.8x %.8x %d %d\n",ref,x,y,
  882. h, i);
  883. }
  884. }
  885. }
  886. }
  887. /* check for problems with nulls */
  888. void driver4()
  889. {
  890. uint8_t buf[1];
  891. uint32_t h,i,state[HASHSTATE];
  892. buf[0] = ~0;
  893. for (i=0; i<HASHSTATE; ++i) state[i] = 1;
  894. printf("These should all be different\n");
  895. for (i=0, h=0; i<8; ++i)
  896. {
  897. h = hashlittle(buf, 0, h);
  898. printf("%2ld 0-byte strings, hash is %.8x\n", i, h);
  899. }
  900. }
  901. void driver5()
  902. {
  903. uint32_t b,c;
  904. b=0, c=0, hashlittle2("", 0, &c, &b);
  905. printf("hash is %.8lx %.8lx\n", c, b); /* deadbeef deadbeef */
  906. b=0xdeadbeef, c=0, hashlittle2("", 0, &c, &b);
  907. printf("hash is %.8lx %.8lx\n", c, b); /* bd5b7dde deadbeef */
  908. b=0xdeadbeef, c=0xdeadbeef, hashlittle2("", 0, &c, &b);
  909. printf("hash is %.8lx %.8lx\n", c, b); /* 9c093ccd bd5b7dde */
  910. b=0, c=0, hashlittle2("Four score and seven years ago", 30, &c, &b);
  911. printf("hash is %.8lx %.8lx\n", c, b); /* 17770551 ce7226e6 */
  912. b=1, c=0, hashlittle2("Four score and seven years ago", 30, &c, &b);
  913. printf("hash is %.8lx %.8lx\n", c, b); /* e3607cae bd371de4 */
  914. b=0, c=1, hashlittle2("Four score and seven years ago", 30, &c, &b);
  915. printf("hash is %.8lx %.8lx\n", c, b); /* cd628161 6cbea4b3 */
  916. c = hashlittle("Four score and seven years ago", 30, 0);
  917. printf("hash is %.8lx\n", c); /* 17770551 */
  918. c = hashlittle("Four score and seven years ago", 30, 1);
  919. printf("hash is %.8lx\n", c); /* cd628161 */
  920. }
  921. #endif /* SELF_TEST */