lookup3.c 31 KB

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