rpm 5.2.1

- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

rpmio/lookup3.c
Go to the documentation of this file.

    00001 /* -------------------------------------------------------------------- */
    00002 /*
    00003  * lookup3.c, by Bob Jenkins, May 2006, Public Domain.
    00004  *
    00005  * These are functions for producing 32-bit hashes for hash table lookup.
    00006  * jlu32w(), jlu32l(), jlu32lpair(), jlu32b(), _JLU3_MIX(), and _JLU3_FINAL()
    00007  * are externally useful functions.  Routines to test the hash are included
    00008  * if SELF_TEST is defined.  You can use this free for any purpose.  It's in
    00009  * the public domain.  It has no warranty.
    00010  *
    00011  * You probably want to use jlu32l().  jlu32l() and jlu32b()
    00012  * hash byte arrays.  jlu32l() is is faster than jlu32b() on
    00013  * little-endian machines.  Intel and AMD are little-endian machines.
    00014  * On second thought, you probably want jlu32lpair(), which is identical to
    00015  * jlu32l() except it returns two 32-bit hashes for the price of one.
    00016  * You could implement jlu32bpair() if you wanted but I haven't bothered here.
    00017  *
    00018  * If you want to find a hash of, say, exactly 7 integers, do
    00019  *   a = i1;  b = i2;  c = i3;
    00020  *   _JLU3_MIX(a,b,c);
    00021  *   a += i4; b += i5; c += i6;
    00022  *   _JLU3_MIX(a,b,c);
    00023  *   a += i7;
    00024  *   _JLU3_FINAL(a,b,c);
    00025  * then use c as the hash value.  If you have a variable size array of
    00026  * 4-byte integers to hash, use jlu32w().  If you have a byte array (like
    00027  * a character string), use jlu32l().  If you have several byte arrays, or
    00028  * a mix of things, see the comments above jlu32l().
    00029  *
    00030  * Why is this so big?  I read 12 bytes at a time into 3 4-byte integers,
    00031  * then mix those integers.  This is fast (you can do a lot more thorough
    00032  * mixing with 12*3 instructions on 3 integers than you can with 3 instructions
    00033  * on 1 byte), but shoehorning those bytes into integers efficiently is messy.
    00034 */
    00035 /* -------------------------------------------------------------------- */
    00036
    00037 #include "system.h"
    00038 #include "rpmiotypes.h"
    00039 #include "debug.h"
    00040
    00041 #if defined(_JLU3_SELFTEST)
    00042 # define _JLU3_jlu32w           1
    00043 # define _JLU3_jlu32l           1
    00044 # define _JLU3_jlu32lpair       1
    00045 # define _JLU3_jlu32b           1
    00046 #endif
    00047
    00048 /*@-redef@*/
    00049 /*@unchecked@*/
    00050 static const union _dbswap {
    00051     const rpmuint32_t ui;
    00052     const unsigned char uc[4];
    00053 } endian = { .ui = 0x11223344 };
    00054 # define HASH_LITTLE_ENDIAN     (endian.uc[0] == (unsigned char) 0x44)
    00055 # define HASH_BIG_ENDIAN        (endian.uc[0] == (unsigned char) 0x11)
    00056 /*@=redef@*/
    00057
    00058 #ifndef ROTL32
    00059 # define ROTL32(x, s) (((x) << (s)) | ((x) >> (32 - (s))))
    00060 #endif
    00061
    00062 /* NOTE: The _size parameter should be in bytes. */
    00063 #define _JLU3_INIT(_h, _size)   (0xdeadbeef + ((rpmuint32_t)(_size)) + (_h))
    00064
    00065 /* -------------------------------------------------------------------- */
    00066 /*
    00067  * _JLU3_MIX -- mix 3 32-bit values reversibly.
    00068  *
    00069  * This is reversible, so any information in (a,b,c) before _JLU3_MIX() is
    00070  * still in (a,b,c) after _JLU3_MIX().
    00071  *
    00072  * If four pairs of (a,b,c) inputs are run through _JLU3_MIX(), or through
    00073  * _JLU3_MIX() in reverse, there are at least 32 bits of the output that
    00074  * are sometimes the same for one pair and different for another pair.
    00075  * This was tested for:
    00076  * * pairs that differed by one bit, by two bits, in any combination
    00077  *   of top bits of (a,b,c), or in any combination of bottom bits of
    00078  *   (a,b,c).
    00079  * * "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
    00080  *   the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
    00081  *   is commonly produced by subtraction) look like a single 1-bit
    00082  *   difference.
    00083  * * the base values were pseudorandom, all zero but one bit set, or
    00084  *   all zero plus a counter that starts at zero.
    00085  *
    00086  * Some k values for my "a-=c; a^=ROTL32(c,k); c+=b;" arrangement that
    00087  * satisfy this are
    00088  *     4  6  8 16 19  4
    00089  *     9 15  3 18 27 15
    00090  *    14  9  3  7 17  3
    00091  * Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
    00092  * for "differ" defined as + with a one-bit base and a two-bit delta.  I
    00093  * used http://burtleburtle.net/bob/hash/avalanche.html to choose
    00094  * the operations, constants, and arrangements of the variables.
    00095  *
    00096  * This does not achieve avalanche.  There are input bits of (a,b,c)
    00097  * that fail to affect some output bits of (a,b,c), especially of a.  The
    00098  * most thoroughly mixed value is c, but it doesn't really even achieve
    00099  * avalanche in c.
    00100  *
    00101  * This allows some parallelism.  Read-after-writes are good at doubling
    00102  * the number of bits affected, so the goal of mixing pulls in the opposite
    00103  * direction as the goal of parallelism.  I did what I could.  Rotates
    00104  * seem to cost as much as shifts on every machine I could lay my hands
    00105  * on, and rotates are much kinder to the top and bottom bits, so I used
    00106  * rotates.
    00107  */
    00108 /* -------------------------------------------------------------------- */
    00109 #define _JLU3_MIX(a,b,c) \
    00110 { \
    00111   a -= c;  a ^= ROTL32(c, 4);  c += b; \
    00112   b -= a;  b ^= ROTL32(a, 6);  a += c; \
    00113   c -= b;  c ^= ROTL32(b, 8);  b += a; \
    00114   a -= c;  a ^= ROTL32(c,16);  c += b; \
    00115   b -= a;  b ^= ROTL32(a,19);  a += c; \
    00116   c -= b;  c ^= ROTL32(b, 4);  b += a; \
    00117 }
    00118
    00119 /* -------------------------------------------------------------------- */
    00143 /* -------------------------------------------------------------------- */
    00144 #define _JLU3_FINAL(a,b,c) \
    00145 { \
    00146   c ^= b; c -= ROTL32(b,14); \
    00147   a ^= c; a -= ROTL32(c,11); \
    00148   b ^= a; b -= ROTL32(a,25); \
    00149   c ^= b; c -= ROTL32(b,16); \
    00150   a ^= c; a -= ROTL32(c,4);  \
    00151   b ^= a; b -= ROTL32(a,14); \
    00152   c ^= b; c -= ROTL32(b,24); \
    00153 }
    00154
    00155 #if defined(_JLU3_jlu32w)
    00156 rpmuint32_t jlu32w(rpmuint32_t h, /*@null@*/ const rpmuint32_t *k, size_t size)
    00157         /*@*/;
    00158 /* -------------------------------------------------------------------- */
    00175 /* -------------------------------------------------------------------- */
    00176 rpmuint32_t jlu32w(rpmuint32_t h, const rpmuint32_t *k, size_t size)
    00177 {
    00178     rpmuint32_t a = _JLU3_INIT(h, (size * sizeof(*k)));
    00179     rpmuint32_t b = a;
    00180     rpmuint32_t c = a;
    00181
    00182     if (k == NULL)
    00183         goto exit;
    00184
    00185     /*----------------------------------------------- handle most of the key */
    00186     while (size > 3) {
    00187         a += k[0];
    00188         b += k[1];
    00189         c += k[2];
    00190         _JLU3_MIX(a,b,c);
    00191         size -= 3;
    00192         k += 3;
    00193     }
    00194
    00195     /*----------------------------------------- handle the last 3 rpmuint32_t's */
    00196     switch (size) {
    00197     case 3 : c+=k[2];
    00198     case 2 : b+=k[1];
    00199     case 1 : a+=k[0];
    00200         _JLU3_FINAL(a,b,c);
    00201         /*@fallthrough@*/
    00202     case 0:
    00203         break;
    00204     }
    00205     /*---------------------------------------------------- report the result */
    00206 exit:
    00207     return c;
    00208 }
    00209 #endif  /* defined(_JLU3_jlu32w) */
    00210
    00211 #if defined(_JLU3_jlu32l)
    00212 rpmuint32_t jlu32l(rpmuint32_t h, const void *key, size_t size)
    00213         /*@*/;
    00214 /* -------------------------------------------------------------------- */
    00215 /*
    00216  * jlu32l() -- hash a variable-length key into a 32-bit value
    00217  *   h       : can be any 4-byte value
    00218  *   k       : the key (the unaligned variable-length array of bytes)
    00219  *   size    : the size of the key, counting by bytes
    00220  * Returns a 32-bit value.  Every bit of the key affects every bit of
    00221  * the return value.  Two keys differing by one or two bits will have
    00222  * totally different hash values.
    00223  *
    00224  * The best hash table sizes are powers of 2.  There is no need to do
    00225  * mod a prime (mod is sooo slow!).  If you need less than 32 bits,
    00226  * use a bitmask.  For example, if you need only 10 bits, do
    00227  *   h = (h & hashmask(10));
    00228  * In which case, the hash table should have hashsize(10) elements.
    00229  *
    00230  * If you are hashing n strings (rpmuint8_t **)k, do it like this:
    00231  *   for (i=0, h=0; i<n; ++i) h = jlu32l(h, k[i], len[i]);
    00232  *
    00233  * By Bob Jenkins, 2006.  bob_jenkins@burtleburtle.net.  You may use this
    00234  * code any way you wish, private, educational, or commercial.  It's free.
    00235  *
    00236  * Use for hash table lookup, or anything where one collision in 2^^32 is
    00237  * acceptable.  Do NOT use for cryptographic purposes.
    00238  *
    00239  * @param h             the previous hash, or an arbitrary value
    00240  * @param *k            the key, an array of rpmuint8_t values
    00241  * @param size          the size of the key
    00242  * @return              the lookup3 hash
    00243  */
    00244 /* -------------------------------------------------------------------- */
    00245 rpmuint32_t jlu32l(rpmuint32_t h, const void *key, size_t size)
    00246 {
    00247     union { const void *ptr; size_t i; } u;
    00248     rpmuint32_t a = _JLU3_INIT(h, size);
    00249     rpmuint32_t b = a;
    00250     rpmuint32_t c = a;
    00251
    00252     if (key == NULL)
    00253         goto exit;
    00254
    00255     u.ptr = key;
    00256     if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
    00257         const rpmuint32_t *k = (const rpmuint32_t *)key;        /* read 32-bit chunks */
    00258 #ifdef  VALGRIND
    00259         const rpmuint8_t  *k8;
    00260 #endif
    00261
    00262     /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
    00263         while (size > 12) {
    00264             a += k[0];
    00265             b += k[1];
    00266             c += k[2];
    00267             _JLU3_MIX(a,b,c);
    00268             size -= 12;
    00269             k += 3;
    00270         }
    00271
    00272         /*------------------------- handle the last (probably partial) block */
    00273         /*
    00274          * "k[2]&0xffffff" actually reads beyond the end of the string, but
    00275          * then masks off the part it's not allowed to read.  Because the
    00276          * string is aligned, the masked-off tail is in the same word as the
    00277          * rest of the string.  Every machine with memory protection I've seen
    00278          * does it on word boundaries, so is OK with this.  But VALGRIND will
    00279          * still catch it and complain.  The masking trick does make the hash
    00280          * noticably faster for short strings (like English words).
    00281          */
    00282 #ifndef VALGRIND
    00283
    00284         switch (size) {
    00285         case 12:        c += k[2]; b+=k[1]; a+=k[0]; break;
    00286         case 11:        c += k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
    00287         case 10:        c += k[2]&0xffff; b+=k[1]; a+=k[0]; break;
    00288         case  9:        c += k[2]&0xff; b+=k[1]; a+=k[0]; break;
    00289         case  8:        b += k[1]; a+=k[0]; break;
    00290         case  7:        b += k[1]&0xffffff; a+=k[0]; break;
    00291         case  6:        b += k[1]&0xffff; a+=k[0]; break;
    00292         case  5:        b += k[1]&0xff; a+=k[0]; break;
    00293         case  4:        a += k[0]; break;
    00294         case  3:        a += k[0]&0xffffff; break;
    00295         case  2:        a += k[0]&0xffff; break;
    00296         case  1:        a += k[0]&0xff; break;
    00297         case  0:        goto exit;
    00298         }
    00299
    00300 #else /* make valgrind happy */
    00301
    00302         k8 = (const rpmuint8_t *)k;
    00303         switch (size) {
    00304         case 12:        c += k[2]; b+=k[1]; a+=k[0]     break;
    00305         case 11:        c += ((rpmuint32_t)k8[10])<<16; /*@fallthrough@*/
    00306         case 10:        c += ((rpmuint32_t)k8[9])<<8;   /*@fallthrough@*/
    00307         case  9:        c += k8[8];                     /*@fallthrough@*/
    00308         case  8:        b += k[1]; a+=k[0];             break;
    00309         case  7:        b += ((rpmuint32_t)k8[6])<<16;  /*@fallthrough@*/
    00310         case  6:        b += ((rpmuint32_t)k8[5])<<8;   /*@fallthrough@*/
    00311         case  5:        b += k8[4];                     /*@fallthrough@*/
    00312         case  4:        a += k[0];                      break;
    00313         case  3:        a += ((rpmuint32_t)k8[2])<<16;  /*@fallthrough@*/
    00314         case  2:        a += ((rpmuint32_t)k8[1])<<8;   /*@fallthrough@*/
    00315         case  1:        a += k8[0];                     break;
    00316         case  0:        goto exit;
    00317         }
    00318
    00319 #endif /* !valgrind */
    00320
    00321     } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
    00322         const rpmuint16_t *k = (const rpmuint16_t *)key;        /* read 16-bit chunks */
    00323         const rpmuint8_t  *k8;
    00324
    00325         /*----------- all but last block: aligned reads and different mixing */
    00326         while (size > 12) {
    00327             a += k[0] + (((rpmuint32_t)k[1])<<16);
    00328             b += k[2] + (((rpmuint32_t)k[3])<<16);
    00329             c += k[4] + (((rpmuint32_t)k[5])<<16);
    00330             _JLU3_MIX(a,b,c);
    00331             size -= 12;
    00332             k += 6;
    00333         }
    00334
    00335         /*------------------------- handle the last (probably partial) block */
    00336         k8 = (const rpmuint8_t *)k;
    00337         switch (size) {
    00338         case 12:
    00339             c += k[4]+(((rpmuint32_t)k[5])<<16);
    00340             b += k[2]+(((rpmuint32_t)k[3])<<16);
    00341             a += k[0]+(((rpmuint32_t)k[1])<<16);
    00342             break;
    00343         case 11:
    00344             c += ((rpmuint32_t)k8[10])<<16;
    00345             /*@fallthrough@*/
    00346         case 10:
    00347             c += (rpmuint32_t)k[4];
    00348             b += k[2]+(((rpmuint32_t)k[3])<<16);
    00349             a += k[0]+(((rpmuint32_t)k[1])<<16);
    00350             break;
    00351         case  9:
    00352             c += (rpmuint32_t)k8[8];
    00353             /*@fallthrough@*/
    00354         case  8:
    00355             b += k[2]+(((rpmuint32_t)k[3])<<16);
    00356             a += k[0]+(((rpmuint32_t)k[1])<<16);
    00357             break;
    00358         case  7:
    00359             b += ((rpmuint32_t)k8[6])<<16;
    00360             /*@fallthrough@*/
    00361         case  6:
    00362             b += (rpmuint32_t)k[2];
    00363             a += k[0]+(((rpmuint32_t)k[1])<<16);
    00364             break;
    00365         case  5:
    00366             b += (rpmuint32_t)k8[4];
    00367             /*@fallthrough@*/
    00368         case  4:
    00369             a += k[0]+(((rpmuint32_t)k[1])<<16);
    00370             break;
    00371         case  3:
    00372             a += ((rpmuint32_t)k8[2])<<16;
    00373             /*@fallthrough@*/
    00374         case  2:
    00375             a += (rpmuint32_t)k[0];
    00376             break;
    00377         case  1:
    00378             a += (rpmuint32_t)k8[0];
    00379             break;
    00380         case  0:
    00381             goto exit;
    00382         }
    00383
    00384     } else {            /* need to read the key one byte at a time */
    00385         const rpmuint8_t *k = (const rpmuint8_t *)key;
    00386
    00387         /*----------- all but the last block: affect some 32 bits of (a,b,c) */
    00388         while (size > 12) {
    00389             a += (rpmuint32_t)k[0];
    00390             a += ((rpmuint32_t)k[1])<<8;
    00391             a += ((rpmuint32_t)k[2])<<16;
    00392             a += ((rpmuint32_t)k[3])<<24;
    00393             b += (rpmuint32_t)k[4];
    00394             b += ((rpmuint32_t)k[5])<<8;
    00395             b += ((rpmuint32_t)k[6])<<16;
    00396             b += ((rpmuint32_t)k[7])<<24;
    00397             c += (rpmuint32_t)k[8];
    00398             c += ((rpmuint32_t)k[9])<<8;
    00399             c += ((rpmuint32_t)k[10])<<16;
    00400             c += ((rpmuint32_t)k[11])<<24;
    00401             _JLU3_MIX(a,b,c);
    00402             size -= 12;
    00403             k += 12;
    00404         }
    00405
    00406         /*---------------------------- last block: affect all 32 bits of (c) */
    00407         switch (size) {
    00408         case 12:        c += ((rpmuint32_t)k[11])<<24;  /*@fallthrough@*/
    00409         case 11:        c += ((rpmuint32_t)k[10])<<16;  /*@fallthrough@*/
    00410         case 10:        c += ((rpmuint32_t)k[9])<<8;    /*@fallthrough@*/
    00411         case  9:        c += (rpmuint32_t)k[8];         /*@fallthrough@*/
    00412         case  8:        b += ((rpmuint32_t)k[7])<<24;   /*@fallthrough@*/
    00413         case  7:        b += ((rpmuint32_t)k[6])<<16;   /*@fallthrough@*/
    00414         case  6:        b += ((rpmuint32_t)k[5])<<8;    /*@fallthrough@*/
    00415         case  5:        b += (rpmuint32_t)k[4];         /*@fallthrough@*/
    00416         case  4:        a += ((rpmuint32_t)k[3])<<24;   /*@fallthrough@*/
    00417         case  3:        a += ((rpmuint32_t)k[2])<<16;   /*@fallthrough@*/
    00418         case  2:        a += ((rpmuint32_t)k[1])<<8;    /*@fallthrough@*/
    00419         case  1:        a += (rpmuint32_t)k[0];
    00420             break;
    00421         case  0:
    00422             goto exit;
    00423         }
    00424     }
    00425
    00426     _JLU3_FINAL(a,b,c);
    00427
    00428 exit:
    00429     return c;
    00430 }
    00431 #endif  /* defined(_JLU3_jlu32l) */
    00432
    00433 #if defined(_JLU3_jlu32lpair)
    00434 void jlu32lpair(/*@null@*/ const void *key, size_t size,
    00435                 rpmuint32_t *pc, rpmuint32_t *pb)
    00436         /*@modifies *pc, *pb@*/;
    00453 void jlu32lpair(const void *key, size_t size, rpmuint32_t *pc, rpmuint32_t *pb)
    00454 {
    00455     union { const void *ptr; size_t i; } u;
    00456     rpmuint32_t a = _JLU3_INIT(*pc, size);
    00457     rpmuint32_t b = a;
    00458     rpmuint32_t c = a;
    00459
    00460     if (key == NULL)
    00461         goto exit;
    00462
    00463     c += *pb;   /* Add the secondary hash. */
    00464
    00465     u.ptr = key;
    00466     if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
    00467         const rpmuint32_t *k = (const rpmuint32_t *)key;        /* read 32-bit chunks */
    00468 #ifdef  VALGRIND
    00469         const rpmuint8_t  *k8;
    00470 #endif
    00471
    00472         /*-- all but last block: aligned reads and affect 32 bits of (a,b,c) */
    00473         while (size > 12) {
    00474             a += k[0];
    00475             b += k[1];
    00476             c += k[2];
    00477             _JLU3_MIX(a,b,c);
    00478             size -= 12;
    00479             k += 3;
    00480         }
    00481         /*------------------------- handle the last (probably partial) block */
    00482         /*
    00483          * "k[2]&0xffffff" actually reads beyond the end of the string, but
    00484          * then masks off the part it's not allowed to read.  Because the
    00485          * string is aligned, the masked-off tail is in the same word as the
    00486          * rest of the string.  Every machine with memory protection I've seen
    00487          * does it on word boundaries, so is OK with this.  But VALGRIND will
    00488          * still catch it and complain.  The masking trick does make the hash
    00489          * noticably faster for short strings (like English words).
    00490          */
    00491 #ifndef VALGRIND
    00492
    00493         switch (size) {
    00494         case 12:        c += k[2]; b+=k[1]; a+=k[0]; break;
    00495         case 11:        c += k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
    00496         case 10:        c += k[2]&0xffff; b+=k[1]; a+=k[0]; break;
    00497         case  9:        c += k[2]&0xff; b+=k[1]; a+=k[0]; break;
    00498         case  8:        b += k[1]; a+=k[0]; break;
    00499         case  7:        b += k[1]&0xffffff; a+=k[0]; break;
    00500         case  6:        b += k[1]&0xffff; a+=k[0]; break;
    00501         case  5:        b += k[1]&0xff; a+=k[0]; break;
    00502         case  4:        a += k[0]; break;
    00503         case  3:        a += k[0]&0xffffff; break;
    00504         case  2:        a += k[0]&0xffff; break;
    00505         case  1:        a += k[0]&0xff; break;
    00506         case  0:        goto exit;
    00507         }
    00508
    00509 #else /* make valgrind happy */
    00510
    00511         k8 = (const rpmuint8_t *)k;
    00512         switch (size) {
    00513         case 12:        c += k[2]; b+=k[1]; a+=k[0];    break;
    00514         case 11:        c += ((rpmuint32_t)k8[10])<<16; /*@fallthrough@*/
    00515         case 10:        c += ((rpmuint32_t)k8[9])<<8;   /*@fallthrough@*/
    00516         case  9:        c += k8[8];                     /*@fallthrough@*/
    00517         case  8:        b += k[1]; a+=k[0];             break;
    00518         case  7:        b += ((rpmuint32_t)k8[6])<<16;  /*@fallthrough@*/
    00519         case  6:        b += ((rpmuint32_t)k8[5])<<8;   /*@fallthrough@*/
    00520         case  5:        b += k8[4];                     /*@fallthrough@*/
    00521         case  4:        a += k[0];                      break;
    00522         case  3:        a += ((rpmuint32_t)k8[2])<<16;  /*@fallthrough@*/
    00523         case  2:        a += ((rpmuint32_t)k8[1])<<8;   /*@fallthrough@*/
    00524         case  1:        a += k8[0];                     break;
    00525         case  0:        goto exit;
    00526         }
    00527
    00528 #endif /* !valgrind */
    00529
    00530     } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
    00531         const rpmuint16_t *k = (const rpmuint16_t *)key;        /* read 16-bit chunks */
    00532         const rpmuint8_t  *k8;
    00533
    00534         /*----------- all but last block: aligned reads and different mixing */
    00535         while (size > 12) {
    00536             a += k[0] + (((rpmuint32_t)k[1])<<16);
    00537             b += k[2] + (((rpmuint32_t)k[3])<<16);
    00538             c += k[4] + (((rpmuint32_t)k[5])<<16);
    00539             _JLU3_MIX(a,b,c);
    00540             size -= 12;
    00541             k += 6;
    00542         }
    00543
    00544         /*------------------------- handle the last (probably partial) block */
    00545         k8 = (const rpmuint8_t *)k;
    00546         switch (size) {
    00547         case 12:
    00548             c += k[4]+(((rpmuint32_t)k[5])<<16);
    00549             b += k[2]+(((rpmuint32_t)k[3])<<16);
    00550             a += k[0]+(((rpmuint32_t)k[1])<<16);
    00551             break;
    00552         case 11:
    00553             c += ((rpmuint32_t)k8[10])<<16;
    00554             /*@fallthrough@*/
    00555         case 10:
    00556             c += k[4];
    00557             b += k[2]+(((rpmuint32_t)k[3])<<16);
    00558             a += k[0]+(((rpmuint32_t)k[1])<<16);
    00559             break;
    00560         case  9:
    00561             c += k8[8];
    00562             /*@fallthrough@*/
    00563         case  8:
    00564             b += k[2]+(((rpmuint32_t)k[3])<<16);
    00565             a += k[0]+(((rpmuint32_t)k[1])<<16);
    00566             break;
    00567         case  7:
    00568             b += ((rpmuint32_t)k8[6])<<16;
    00569             /*@fallthrough@*/
    00570         case  6:
    00571             b += k[2];
    00572             a += k[0]+(((rpmuint32_t)k[1])<<16);
    00573             break;
    00574         case  5:
    00575             b += k8[4];
    00576             /*@fallthrough@*/
    00577         case  4:
    00578             a += k[0]+(((rpmuint32_t)k[1])<<16);
    00579             break;
    00580         case  3:
    00581             a += ((rpmuint32_t)k8[2])<<16;
    00582             /*@fallthrough@*/
    00583         case  2:
    00584             a += k[0];
    00585             break;
    00586         case  1:
    00587             a += k8[0];
    00588             break;
    00589         case  0:
    00590             goto exit;
    00591         }
    00592
    00593     } else {            /* need to read the key one byte at a time */
    00594         const rpmuint8_t *k = (const rpmuint8_t *)key;
    00595
    00596         /*----------- all but the last block: affect some 32 bits of (a,b,c) */
    00597         while (size > 12) {
    00598             a += k[0];
    00599             a += ((rpmuint32_t)k[1])<<8;
    00600             a += ((rpmuint32_t)k[2])<<16;
    00601             a += ((rpmuint32_t)k[3])<<24;
    00602             b += k[4];
    00603             b += ((rpmuint32_t)k[5])<<8;
    00604             b += ((rpmuint32_t)k[6])<<16;
    00605             b += ((rpmuint32_t)k[7])<<24;
    00606             c += k[8];
    00607             c += ((rpmuint32_t)k[9])<<8;
    00608             c += ((rpmuint32_t)k[10])<<16;
    00609             c += ((rpmuint32_t)k[11])<<24;
    00610             _JLU3_MIX(a,b,c);
    00611             size -= 12;
    00612             k += 12;
    00613         }
    00614
    00615         /*---------------------------- last block: affect all 32 bits of (c) */
    00616         switch (size) {
    00617         case 12:        c += ((rpmuint32_t)k[11])<<24;  /*@fallthrough@*/
    00618         case 11:        c += ((rpmuint32_t)k[10])<<16;  /*@fallthrough@*/
    00619         case 10:        c += ((rpmuint32_t)k[9])<<8;    /*@fallthrough@*/
    00620         case  9:        c += k[8];                      /*@fallthrough@*/
    00621         case  8:        b += ((rpmuint32_t)k[7])<<24;   /*@fallthrough@*/
    00622         case  7:        b += ((rpmuint32_t)k[6])<<16;   /*@fallthrough@*/
    00623         case  6:        b += ((rpmuint32_t)k[5])<<8;    /*@fallthrough@*/
    00624         case  5:        b += k[4];                      /*@fallthrough@*/
    00625         case  4:        a += ((rpmuint32_t)k[3])<<24;   /*@fallthrough@*/
    00626         case  3:        a += ((rpmuint32_t)k[2])<<16;   /*@fallthrough@*/
    00627         case  2:        a += ((rpmuint32_t)k[1])<<8;    /*@fallthrough@*/
    00628         case  1:        a += k[0];                      /*@fallthrough@*/
    00629             break;
    00630         case  0:
    00631             goto exit;
    00632         }
    00633     }
    00634
    00635     _JLU3_FINAL(a,b,c);
    00636
    00637 exit:
    00638     *pc = c;
    00639     *pb = b;
    00640     return;
    00641 }
    00642 #endif  /* defined(_JLU3_jlu32lpair) */
    00643
    00644 #if defined(_JLU3_jlu32b)
    00645 rpmuint32_t jlu32b(rpmuint32_t h, /*@null@*/ const void *key, size_t size)
    00646         /*@*/;
    00647 /*
    00648  * jlu32b():
    00649  * This is the same as jlu32w() on big-endian machines.  It is different
    00650  * from jlu32l() on all machines.  jlu32b() takes advantage of
    00651  * big-endian byte ordering.
    00652  *
    00653  * @param h             the previous hash, or an arbitrary value
    00654  * @param *k            the key, an array of rpmuint8_t values
    00655  * @param size          the size of the key
    00656  * @return              the lookup3 hash
    00657  */
    00658 rpmuint32_t jlu32b(rpmuint32_t h, const void *key, size_t size)
    00659 {
    00660     union { const void *ptr; size_t i; } u;
    00661     rpmuint32_t a = _JLU3_INIT(h, size);
    00662     rpmuint32_t b = a;
    00663     rpmuint32_t c = a;
    00664
    00665     if (key == NULL)
    00666         return h;
    00667
    00668     u.ptr = key;
    00669     if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) {
    00670         const rpmuint32_t *k = (const rpmuint32_t *)key;        /* read 32-bit chunks */
    00671 #ifdef  VALGRIND
    00672         const rpmuint8_t  *k8;
    00673 #endif
    00674
    00675         /*-- all but last block: aligned reads and affect 32 bits of (a,b,c) */
    00676         while (size > 12) {
    00677             a += k[0];
    00678             b += k[1];
    00679             c += k[2];
    00680             _JLU3_MIX(a,b,c);
    00681             size -= 12;
    00682             k += 3;
    00683         }
    00684
    00685         /*------------------------- handle the last (probably partial) block */
    00686         /*
    00687          * "k[2]<<8" actually reads beyond the end of the string, but
    00688          * then shifts out the part it's not allowed to read.  Because the
    00689          * string is aligned, the illegal read is in the same word as the
    00690          * rest of the string.  Every machine with memory protection I've seen
    00691          * does it on word boundaries, so is OK with this.  But VALGRIND will
    00692          * still catch it and complain.  The masking trick does make the hash
    00693          * noticably faster for short strings (like English words).
    00694          */
    00695 #ifndef VALGRIND
    00696
    00697         switch (size) {
    00698         case 12:        c += k[2]; b+=k[1]; a+=k[0]; break;
    00699         case 11:        c += k[2]&0xffffff00; b+=k[1]; a+=k[0]; break;
    00700         case 10:        c += k[2]&0xffff0000; b+=k[1]; a+=k[0]; break;
    00701         case  9:        c += k[2]&0xff000000; b+=k[1]; a+=k[0]; break;
    00702         case  8:        b += k[1]; a+=k[0]; break;
    00703         case  7:        b += k[1]&0xffffff00; a+=k[0]; break;
    00704         case  6:        b += k[1]&0xffff0000; a+=k[0]; break;
    00705         case  5:        b += k[1]&0xff000000; a+=k[0]; break;
    00706         case  4:        a += k[0]; break;
    00707         case  3:        a += k[0]&0xffffff00; break;
    00708         case  2:        a += k[0]&0xffff0000; break;
    00709         case  1:        a += k[0]&0xff000000; break;
    00710         case  0:        goto exit;
    00711     }
    00712
    00713 #else  /* make valgrind happy */
    00714
    00715         k8 = (const rpmuint8_t *)k;
    00716         switch (size) { /* all the case statements fall through */
    00717         case 12:        c += k[2]; b+=k[1]; a+=k[0];    break;
    00718         case 11:        c += ((rpmuint32_t)k8[10])<<8;  /*@fallthrough@*/
    00719         case 10:        c += ((rpmuint32_t)k8[9])<<16;  /*@fallthrough@*/
    00720         case  9:        c += ((rpmuint32_t)k8[8])<<24;  /*@fallthrough@*/
    00721         case  8:        b += k[1]; a+=k[0];             break;
    00722         case  7:        b += ((rpmuint32_t)k8[6])<<8;   /*@fallthrough@*/
    00723         case  6:        b += ((rpmuint32_t)k8[5])<<16;  /*@fallthrough@*/
    00724         case  5:        b += ((rpmuint32_t)k8[4])<<24;  /*@fallthrough@*/
    00725         case  4:        a += k[0];                      break;
    00726         case  3:        a += ((rpmuint32_t)k8[2])<<8;   /*@fallthrough@*/
    00727         case  2:        a += ((rpmuint32_t)k8[1])<<16;  /*@fallthrough@*/
    00728         case  1:        a += ((rpmuint32_t)k8[0])<<24;  break;
    00729         case  0:        goto exit;
    00730     }
    00731
    00732 #endif /* !VALGRIND */
    00733
    00734     } else {                        /* need to read the key one byte at a time */
    00735         const rpmuint8_t *k = (const rpmuint8_t *)key;
    00736
    00737         /*----------- all but the last block: affect some 32 bits of (a,b,c) */
    00738         while (size > 12) {
    00739             a += ((rpmuint32_t)k[0])<<24;
    00740             a += ((rpmuint32_t)k[1])<<16;
    00741             a += ((rpmuint32_t)k[2])<<8;
    00742             a += ((rpmuint32_t)k[3]);
    00743             b += ((rpmuint32_t)k[4])<<24;
    00744             b += ((rpmuint32_t)k[5])<<16;
    00745             b += ((rpmuint32_t)k[6])<<8;
    00746             b += ((rpmuint32_t)k[7]);
    00747             c += ((rpmuint32_t)k[8])<<24;
    00748             c += ((rpmuint32_t)k[9])<<16;
    00749             c += ((rpmuint32_t)k[10])<<8;
    00750             c += ((rpmuint32_t)k[11]);
    00751             _JLU3_MIX(a,b,c);
    00752             size -= 12;
    00753             k += 12;
    00754         }
    00755
    00756         /*---------------------------- last block: affect all 32 bits of (c) */
    00757         switch (size) { /* all the case statements fall through */
    00758         case 12:        c += k[11];                     /*@fallthrough@*/
    00759         case 11:        c += ((rpmuint32_t)k[10])<<8;   /*@fallthrough@*/
    00760         case 10:        c += ((rpmuint32_t)k[9])<<16;   /*@fallthrough@*/
    00761         case  9:        c += ((rpmuint32_t)k[8])<<24;   /*@fallthrough@*/
    00762         case  8:        b += k[7];                      /*@fallthrough@*/
    00763         case  7:        b += ((rpmuint32_t)k[6])<<8;    /*@fallthrough@*/
    00764         case  6:        b += ((rpmuint32_t)k[5])<<16;   /*@fallthrough@*/
    00765         case  5:        b += ((rpmuint32_t)k[4])<<24;   /*@fallthrough@*/
    00766         case  4:        a += k[3];                      /*@fallthrough@*/
    00767         case  3:        a += ((rpmuint32_t)k[2])<<8;    /*@fallthrough@*/
    00768         case  2:        a += ((rpmuint32_t)k[1])<<16;   /*@fallthrough@*/
    00769         case  1:        a += ((rpmuint32_t)k[0])<<24;   /*@fallthrough@*/
    00770             break;
    00771         case  0:
    00772             goto exit;
    00773         }
    00774     }
    00775
    00776     _JLU3_FINAL(a,b,c);
    00777
    00778 exit:
    00779     return c;
    00780 }
    00781 #endif  /* defined(_JLU3_jlu32b) */
    00782
    00783 #if defined(_JLU3_SELFTEST)
    00784
    00785 /* used for timings */
    00786 static void driver1(void)
    00787         /*@*/
    00788 {
    00789     rpmuint8_t buf[256];
    00790     rpmuint32_t i;
    00791     rpmuint32_t h=0;
    00792     time_t a,z;
    00793
    00794     time(&a);
    00795     for (i=0; i<256; ++i) buf[i] = 'x';
    00796     for (i=0; i<1; ++i) {
    00797         h = jlu32l(h, &buf[0], sizeof(buf[0]));
    00798     }
    00799     time(&z);
    00800     if (z-a > 0) printf("time %d %.8x\n", (int)(z-a), h);
    00801 }
    00802
    00803 /* check that every input bit changes every output bit half the time */
    00804 #define HASHSTATE 1
    00805 #define HASHLEN   1
    00806 #define MAXPAIR 60
    00807 #define MAXLEN  70
    00808 static void driver2(void)
    00809         /*@*/
    00810 {
    00811     rpmuint8_t qa[MAXLEN+1], qb[MAXLEN+2], *a = &qa[0], *b = &qb[1];
    00812     rpmuint32_t c[HASHSTATE], d[HASHSTATE], i=0, j=0, k, l, m=0, z;
    00813     rpmuint32_t e[HASHSTATE],f[HASHSTATE],g[HASHSTATE],h[HASHSTATE];
    00814     rpmuint32_t x[HASHSTATE],y[HASHSTATE];
    00815     rpmuint32_t hlen;
    00816
    00817     printf("No more than %d trials should ever be needed \n",MAXPAIR/2);
    00818     for (hlen=0; hlen < MAXLEN; ++hlen) {
    00819         z=0;
    00820         for (i=0; i<hlen; ++i) {        /*-------------- for each input byte, */
    00821             for (j=0; j<8; ++j) {       /*--------------- for each input bit, */
    00822                 for (m=1; m<8; ++m) {   /*--- for serveral possible initvals, */
    00823                     for (l=0; l<HASHSTATE; ++l)
    00824                         e[l]=f[l]=g[l]=h[l]=x[l]=y[l]=~((rpmuint32_t)0);
    00825
    00826                     /* check that every output bit is affected by that input bit */
    00827                     for (k=0; k<MAXPAIR; k+=2) {
    00828                         rpmuint32_t finished=1;
    00829                         /* keys have one bit different */
    00830                         for (l=0; l<hlen+1; ++l) {a[l] = b[l] = (rpmuint8_t)0;}
    00831                         /* have a and b be two keys differing in only one bit */
    00832                         a[i] ^= (k<<j);
    00833                         a[i] ^= (k>>(8-j));
    00834                         c[0] = jlu32l(m, a, hlen);
    00835                         b[i] ^= ((k+1)<<j);
    00836                         b[i] ^= ((k+1)>>(8-j));
    00837                         d[0] = jlu32l(m, b, hlen);
    00838                         /* check every bit is 1, 0, set, and not set at least once */
    00839                         for (l=0; l<HASHSTATE; ++l) {
    00840                             e[l] &= (c[l]^d[l]);
    00841                             f[l] &= ~(c[l]^d[l]);
    00842                             g[l] &= c[l];
    00843                             h[l] &= ~c[l];
    00844                             x[l] &= d[l];
    00845                             y[l] &= ~d[l];
    00846                             if (e[l]|f[l]|g[l]|h[l]|x[l]|y[l]) finished=0;
    00847                         }
    00848                         if (finished) break;
    00849                     }
    00850                     if (k>z) z=k;
    00851                     if (k == MAXPAIR) {
    00852                         printf("Some bit didn't change: ");
    00853                         printf("%.8x %.8x %.8x %.8x %.8x %.8x  ",
    00854                                 e[0],f[0],g[0],h[0],x[0],y[0]);
    00855                         printf("i %d j %d m %d len %d\n", i, j, m, hlen);
    00856                     }
    00857                     if (z == MAXPAIR) goto done;
    00858                 }
    00859             }
    00860         }
    00861    done:
    00862         if (z < MAXPAIR) {
    00863             printf("Mix success  %2d bytes  %2d initvals  ",i,m);
    00864             printf("required  %d  trials\n", z/2);
    00865         }
    00866     }
    00867     printf("\n");
    00868 }
    00869
    00870 /* Check for reading beyond the end of the buffer and alignment problems */
    00871 static void driver3(void)
    00872         /*@*/
    00873 {
    00874     rpmuint8_t buf[MAXLEN+20], *b;
    00875     rpmuint32_t len;
    00876     rpmuint8_t q[] = "This is the time for all good men to come to the aid of their country...";
    00877     rpmuint32_t h;
    00878     rpmuint8_t qq[] = "xThis is the time for all good men to come to the aid of their country...";
    00879     rpmuint32_t i;
    00880     rpmuint8_t qqq[] = "xxThis is the time for all good men to come to the aid of their country...";
    00881     rpmuint32_t j;
    00882     rpmuint8_t qqqq[] = "xxxThis is the time for all good men to come to the aid of their country...";
    00883     rpmuint32_t ref,x,y;
    00884     rpmuint8_t *p;
    00885     rpmuint32_t m = 13;
    00886
    00887     printf("Endianness.  These lines should all be the same (for values filled in):\n");
    00888     printf("%.8x                            %.8x                            %.8x\n",
    00889         jlu32w(m, (const rpmuint32_t *)q, (sizeof(q)-1)/4),
    00890         jlu32w(m, (const rpmuint32_t *)q, (sizeof(q)-5)/4),
    00891         jlu32w(m, (const rpmuint32_t *)q, (sizeof(q)-9)/4));
    00892     p = q;
    00893     printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
    00894         jlu32l(m, p, sizeof(q)-1), jlu32l(m, p, sizeof(q)-2),
    00895         jlu32l(m, p, sizeof(q)-3), jlu32l(m, p, sizeof(q)-4),
    00896         jlu32l(m, p, sizeof(q)-5), jlu32l(m, p, sizeof(q)-6),
    00897         jlu32l(m, p, sizeof(q)-7), jlu32l(m, p, sizeof(q)-8),
    00898         jlu32l(m, p, sizeof(q)-9), jlu32l(m, p, sizeof(q)-10),
    00899         jlu32l(m, p, sizeof(q)-11), jlu32l(m, p, sizeof(q)-12));
    00900     p = &qq[1];
    00901     printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
    00902         jlu32l(m, p, sizeof(q)-1), jlu32l(m, p, sizeof(q)-2),
    00903         jlu32l(m, p, sizeof(q)-3), jlu32l(m, p, sizeof(q)-4),
    00904         jlu32l(m, p, sizeof(q)-5), jlu32l(m, p, sizeof(q)-6),
    00905         jlu32l(m, p, sizeof(q)-7), jlu32l(m, p, sizeof(q)-8),
    00906         jlu32l(m, p, sizeof(q)-9), jlu32l(m, p, sizeof(q)-10),
    00907         jlu32l(m, p, sizeof(q)-11), jlu32l(m, p, sizeof(q)-12));
    00908     p = &qqq[2];
    00909     printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
    00910         jlu32l(m, p, sizeof(q)-1), jlu32l(m, p, sizeof(q)-2),
    00911         jlu32l(m, p, sizeof(q)-3), jlu32l(m, p, sizeof(q)-4),
    00912         jlu32l(m, p, sizeof(q)-5), jlu32l(m, p, sizeof(q)-6),
    00913         jlu32l(m, p, sizeof(q)-7), jlu32l(m, p, sizeof(q)-8),
    00914         jlu32l(m, p, sizeof(q)-9), jlu32l(m, p, sizeof(q)-10),
    00915         jlu32l(m, p, sizeof(q)-11), jlu32l(m, p, sizeof(q)-12));
    00916     p = &qqqq[3];
    00917     printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
    00918         jlu32l(m, p, sizeof(q)-1), jlu32l(m, p, sizeof(q)-2),
    00919         jlu32l(m, p, sizeof(q)-3), jlu32l(m, p, sizeof(q)-4),
    00920         jlu32l(m, p, sizeof(q)-5), jlu32l(m, p, sizeof(q)-6),
    00921         jlu32l(m, p, sizeof(q)-7), jlu32l(m, p, sizeof(q)-8),
    00922         jlu32l(m, p, sizeof(q)-9), jlu32l(m, p, sizeof(q)-10),
    00923         jlu32l(m, p, sizeof(q)-11), jlu32l(m, p, sizeof(q)-12));
    00924     printf("\n");
    00925     for (h=0, b=buf+1; h<8; ++h, ++b) {
    00926         for (i=0; i<MAXLEN; ++i) {
    00927             len = i;
    00928             for (j=0; j<i; ++j)
    00929                 *(b+j)=0;
    00930
    00931             /* these should all be equal */
    00932             m = 1;
    00933             ref = jlu32l(m, b, len);
    00934             *(b+i)=(rpmuint8_t)~0;
    00935             *(b-1)=(rpmuint8_t)~0;
    00936             x = jlu32l(m, b, len);
    00937             y = jlu32l(m, b, len);
    00938             if ((ref != x) || (ref != y))
    00939                 printf("alignment error: %.8x %.8x %.8x %d %d\n",ref,x,y, h, i);
    00940         }
    00941     }
    00942 }
    00943
    00944 /* check for problems with nulls */
    00945 static void driver4(void)
    00946         /*@*/
    00947 {
    00948     rpmuint8_t buf[1];
    00949     rpmuint32_t h;
    00950     rpmuint32_t i;
    00951     rpmuint32_t state[HASHSTATE];
    00952
    00953     buf[0] = ~0;
    00954     for (i=0; i<HASHSTATE; ++i)
    00955         state[i] = 1;
    00956     printf("These should all be different\n");
    00957     h = 0;
    00958     for (i=0; i<8; ++i) {
    00959         h = jlu32l(h, buf, 0);
    00960         printf("%2ld  0-byte strings, hash is  %.8x\n", (long)i, h);
    00961     }
    00962 }
    00963
    00964
    00965 int main(int argc, char ** argv)
    00966 {
    00967     driver1();  /* test that the key is hashed: used for timings */
    00968     driver2();  /* test that whole key is hashed thoroughly */
    00969     driver3();  /* test that nothing but the key is hashed */
    00970     driver4();  /* test hashing multiple buffers (all buffers are null) */
    00971     return 1;
    00972 }
    00973
    00974 #endif  /* _JLU3_SELFTEST */