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