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