hash.c 34 KB

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