hash.c 36 KB

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