lookup3.c 34 KB

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