Blame jenkins_hash.c

Packit 4e8bc4
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
Packit 4e8bc4
/*
Packit 4e8bc4
 * Hash table
Packit 4e8bc4
 *
Packit 4e8bc4
 * The hash function used here is by Bob Jenkins, 1996:
Packit 4e8bc4
 *    <http://burtleburtle.net/bob/hash/doobs.html>
Packit 4e8bc4
 *       "By Bob Jenkins, 1996.  bob_jenkins@burtleburtle.net.
Packit 4e8bc4
 *       You may use this code any way you wish, private, educational,
Packit 4e8bc4
 *       or commercial.  It's free."
Packit 4e8bc4
 *
Packit 4e8bc4
 */
Packit 4e8bc4
#include "memcached.h"
Packit 4e8bc4
#include "jenkins_hash.h"
Packit 4e8bc4
Packit 4e8bc4
/*
Packit 4e8bc4
 * Since the hash function does bit manipulation, it needs to know
Packit 4e8bc4
 * whether it's big or little-endian. ENDIAN_LITTLE and ENDIAN_BIG
Packit 4e8bc4
 * are set in the configure script.
Packit 4e8bc4
 */
Packit 4e8bc4
#if ENDIAN_BIG == 1
Packit 4e8bc4
# define HASH_LITTLE_ENDIAN 0
Packit 4e8bc4
# define HASH_BIG_ENDIAN 1
Packit 4e8bc4
#else
Packit 4e8bc4
# if ENDIAN_LITTLE == 1
Packit 4e8bc4
#  define HASH_LITTLE_ENDIAN 1
Packit 4e8bc4
#  define HASH_BIG_ENDIAN 0
Packit 4e8bc4
# else
Packit 4e8bc4
#  define HASH_LITTLE_ENDIAN 0
Packit 4e8bc4
#  define HASH_BIG_ENDIAN 0
Packit 4e8bc4
# endif
Packit 4e8bc4
#endif
Packit 4e8bc4
Packit 4e8bc4
#define rot(x,k) (((x)<<(k)) ^ ((x)>>(32-(k))))
Packit 4e8bc4
Packit 4e8bc4
/*
Packit 4e8bc4
-------------------------------------------------------------------------------
Packit 4e8bc4
mix -- mix 3 32-bit values reversibly.
Packit 4e8bc4
Packit 4e8bc4
This is reversible, so any information in (a,b,c) before mix() is
Packit 4e8bc4
still in (a,b,c) after mix().
Packit 4e8bc4
Packit 4e8bc4
If four pairs of (a,b,c) inputs are run through mix(), or through
Packit 4e8bc4
mix() in reverse, there are at least 32 bits of the output that
Packit 4e8bc4
are sometimes the same for one pair and different for another pair.
Packit 4e8bc4
This was tested for:
Packit 4e8bc4
* pairs that differed by one bit, by two bits, in any combination
Packit 4e8bc4
  of top bits of (a,b,c), or in any combination of bottom bits of
Packit 4e8bc4
  (a,b,c).
Packit 4e8bc4
* "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
Packit 4e8bc4
  the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
Packit 4e8bc4
  is commonly produced by subtraction) look like a single 1-bit
Packit 4e8bc4
  difference.
Packit 4e8bc4
* the base values were pseudorandom, all zero but one bit set, or
Packit 4e8bc4
  all zero plus a counter that starts at zero.
Packit 4e8bc4
Packit 4e8bc4
Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
Packit 4e8bc4
satisfy this are
Packit 4e8bc4
    4  6  8 16 19  4
Packit 4e8bc4
    9 15  3 18 27 15
Packit 4e8bc4
   14  9  3  7 17  3
Packit 4e8bc4
Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
Packit 4e8bc4
for "differ" defined as + with a one-bit base and a two-bit delta.  I
Packit 4e8bc4
used http://burtleburtle.net/bob/hash/avalanche.html to choose
Packit 4e8bc4
the operations, constants, and arrangements of the variables.
Packit 4e8bc4
Packit 4e8bc4
This does not achieve avalanche.  There are input bits of (a,b,c)
Packit 4e8bc4
that fail to affect some output bits of (a,b,c), especially of a.  The
Packit 4e8bc4
most thoroughly mixed value is c, but it doesn't really even achieve
Packit 4e8bc4
avalanche in c.
Packit 4e8bc4
Packit 4e8bc4
This allows some parallelism.  Read-after-writes are good at doubling
Packit 4e8bc4
the number of bits affected, so the goal of mixing pulls in the opposite
Packit 4e8bc4
direction as the goal of parallelism.  I did what I could.  Rotates
Packit 4e8bc4
seem to cost as much as shifts on every machine I could lay my hands
Packit 4e8bc4
on, and rotates are much kinder to the top and bottom bits, so I used
Packit 4e8bc4
rotates.
Packit 4e8bc4
-------------------------------------------------------------------------------
Packit 4e8bc4
*/
Packit 4e8bc4
#define mix(a,b,c) \
Packit 4e8bc4
{ \
Packit 4e8bc4
  a -= c;  a ^= rot(c, 4);  c += b; \
Packit 4e8bc4
  b -= a;  b ^= rot(a, 6);  a += c; \
Packit 4e8bc4
  c -= b;  c ^= rot(b, 8);  b += a; \
Packit 4e8bc4
  a -= c;  a ^= rot(c,16);  c += b; \
Packit 4e8bc4
  b -= a;  b ^= rot(a,19);  a += c; \
Packit 4e8bc4
  c -= b;  c ^= rot(b, 4);  b += a; \
Packit 4e8bc4
}
Packit 4e8bc4
Packit 4e8bc4
/*
Packit 4e8bc4
-------------------------------------------------------------------------------
Packit 4e8bc4
final -- final mixing of 3 32-bit values (a,b,c) into c
Packit 4e8bc4
Packit 4e8bc4
Pairs of (a,b,c) values differing in only a few bits will usually
Packit 4e8bc4
produce values of c that look totally different.  This was tested for
Packit 4e8bc4
* pairs that differed by one bit, by two bits, in any combination
Packit 4e8bc4
  of top bits of (a,b,c), or in any combination of bottom bits of
Packit 4e8bc4
  (a,b,c).
Packit 4e8bc4
* "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
Packit 4e8bc4
  the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
Packit 4e8bc4
  is commonly produced by subtraction) look like a single 1-bit
Packit 4e8bc4
  difference.
Packit 4e8bc4
* the base values were pseudorandom, all zero but one bit set, or
Packit 4e8bc4
  all zero plus a counter that starts at zero.
Packit 4e8bc4
Packit 4e8bc4
These constants passed:
Packit 4e8bc4
 14 11 25 16 4 14 24
Packit 4e8bc4
 12 14 25 16 4 14 24
Packit 4e8bc4
and these came close:
Packit 4e8bc4
  4  8 15 26 3 22 24
Packit 4e8bc4
 10  8 15 26 3 22 24
Packit 4e8bc4
 11  8 15 26 3 22 24
Packit 4e8bc4
-------------------------------------------------------------------------------
Packit 4e8bc4
*/
Packit 4e8bc4
#define final(a,b,c) \
Packit 4e8bc4
{ \
Packit 4e8bc4
  c ^= b; c -= rot(b,14); \
Packit 4e8bc4
  a ^= c; a -= rot(c,11); \
Packit 4e8bc4
  b ^= a; b -= rot(a,25); \
Packit 4e8bc4
  c ^= b; c -= rot(b,16); \
Packit 4e8bc4
  a ^= c; a -= rot(c,4);  \
Packit 4e8bc4
  b ^= a; b -= rot(a,14); \
Packit 4e8bc4
  c ^= b; c -= rot(b,24); \
Packit 4e8bc4
}
Packit 4e8bc4
Packit 4e8bc4
#if HASH_LITTLE_ENDIAN == 1
Packit 4e8bc4
uint32_t jenkins_hash(
Packit 4e8bc4
  const void *key,       /* the key to hash */
Packit 4e8bc4
  size_t      length)    /* length of the key */
Packit 4e8bc4
{
Packit 4e8bc4
  uint32_t a,b,c;                                          /* internal state */
Packit 4e8bc4
  union { const void *ptr; size_t i; } u;     /* needed for Mac Powerbook G4 */
Packit 4e8bc4
Packit 4e8bc4
  /* Set up the internal state */
Packit 4e8bc4
  a = b = c = 0xdeadbeef + ((uint32_t)length) + 0;
Packit 4e8bc4
Packit 4e8bc4
  u.ptr = key;
Packit 4e8bc4
  if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
Packit 4e8bc4
    const uint32_t *k = key;                           /* read 32-bit chunks */
Packit 4e8bc4
#ifdef VALGRIND
Packit 4e8bc4
    const uint8_t  *k8;
Packit 4e8bc4
#endif /* ifdef VALGRIND */
Packit 4e8bc4
Packit 4e8bc4
    /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
Packit 4e8bc4
    while (length > 12)
Packit 4e8bc4
    {
Packit 4e8bc4
      a += k[0];
Packit 4e8bc4
      b += k[1];
Packit 4e8bc4
      c += k[2];
Packit 4e8bc4
      mix(a,b,c);
Packit 4e8bc4
      length -= 12;
Packit 4e8bc4
      k += 3;
Packit 4e8bc4
    }
Packit 4e8bc4
Packit 4e8bc4
    /*----------------------------- handle the last (probably partial) block */
Packit 4e8bc4
    /*
Packit 4e8bc4
     * "k[2]&0xffffff" actually reads beyond the end of the string, but
Packit 4e8bc4
     * then masks off the part it's not allowed to read.  Because the
Packit 4e8bc4
     * string is aligned, the masked-off tail is in the same word as the
Packit 4e8bc4
     * rest of the string.  Every machine with memory protection I've seen
Packit 4e8bc4
     * does it on word boundaries, so is OK with this.  But VALGRIND will
Packit 4e8bc4
     * still catch it and complain.  The masking trick does make the hash
Packit 4e8bc4
     * noticeably faster for short strings (like English words).
Packit 4e8bc4
     */
Packit 4e8bc4
#ifndef VALGRIND
Packit 4e8bc4
Packit 4e8bc4
    switch(length)
Packit 4e8bc4
    {
Packit 4e8bc4
    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
Packit 4e8bc4
    case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
Packit 4e8bc4
    case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
Packit 4e8bc4
    case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
Packit 4e8bc4
    case 8 : b+=k[1]; a+=k[0]; break;
Packit 4e8bc4
    case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
Packit 4e8bc4
    case 6 : b+=k[1]&0xffff; a+=k[0]; break;
Packit 4e8bc4
    case 5 : b+=k[1]&0xff; a+=k[0]; break;
Packit 4e8bc4
    case 4 : a+=k[0]; break;
Packit 4e8bc4
    case 3 : a+=k[0]&0xffffff; break;
Packit 4e8bc4
    case 2 : a+=k[0]&0xffff; break;
Packit 4e8bc4
    case 1 : a+=k[0]&0xff; break;
Packit 4e8bc4
    case 0 : return c;  /* zero length strings require no mixing */
Packit 4e8bc4
    }
Packit 4e8bc4
Packit 4e8bc4
#else /* make valgrind happy */
Packit 4e8bc4
Packit 4e8bc4
    k8 = (const uint8_t *)k;
Packit 4e8bc4
    switch(length)
Packit 4e8bc4
    {
Packit 4e8bc4
    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
Packit 4e8bc4
    case 11: c+=((uint32_t)k8[10])<<16;  /* fall through */
Packit 4e8bc4
    case 10: c+=((uint32_t)k8[9])<<8;    /* fall through */
Packit 4e8bc4
    case 9 : c+=k8[8];                   /* fall through */
Packit 4e8bc4
    case 8 : b+=k[1]; a+=k[0]; break;
Packit 4e8bc4
    case 7 : b+=((uint32_t)k8[6])<<16;   /* fall through */
Packit 4e8bc4
    case 6 : b+=((uint32_t)k8[5])<<8;    /* fall through */
Packit 4e8bc4
    case 5 : b+=k8[4];                   /* fall through */
Packit 4e8bc4
    case 4 : a+=k[0]; break;
Packit 4e8bc4
    case 3 : a+=((uint32_t)k8[2])<<16;   /* fall through */
Packit 4e8bc4
    case 2 : a+=((uint32_t)k8[1])<<8;    /* fall through */
Packit 4e8bc4
    case 1 : a+=k8[0]; break;
Packit 4e8bc4
    case 0 : return c;  /* zero length strings require no mixing */
Packit 4e8bc4
    }
Packit 4e8bc4
Packit 4e8bc4
#endif /* !valgrind */
Packit 4e8bc4
Packit 4e8bc4
  } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
Packit 4e8bc4
    const uint16_t *k = key;                           /* read 16-bit chunks */
Packit 4e8bc4
    const uint8_t  *k8;
Packit 4e8bc4
Packit 4e8bc4
    /*--------------- all but last block: aligned reads and different mixing */
Packit 4e8bc4
    while (length > 12)
Packit 4e8bc4
    {
Packit 4e8bc4
      a += k[0] + (((uint32_t)k[1])<<16);
Packit 4e8bc4
      b += k[2] + (((uint32_t)k[3])<<16);
Packit 4e8bc4
      c += k[4] + (((uint32_t)k[5])<<16);
Packit 4e8bc4
      mix(a,b,c);
Packit 4e8bc4
      length -= 12;
Packit 4e8bc4
      k += 6;
Packit 4e8bc4
    }
Packit 4e8bc4
Packit 4e8bc4
    /*----------------------------- handle the last (probably partial) block */
Packit 4e8bc4
    k8 = (const uint8_t *)k;
Packit 4e8bc4
    switch(length)
Packit 4e8bc4
    {
Packit 4e8bc4
    case 12: c+=k[4]+(((uint32_t)k[5])<<16);
Packit 4e8bc4
             b+=k[2]+(((uint32_t)k[3])<<16);
Packit 4e8bc4
             a+=k[0]+(((uint32_t)k[1])<<16);
Packit 4e8bc4
             break;
Packit 4e8bc4
    case 11: c+=((uint32_t)k8[10])<<16;     /* @fallthrough */
Packit 4e8bc4
    case 10: c+=k[4];                       /* @fallthrough@ */
Packit 4e8bc4
             b+=k[2]+(((uint32_t)k[3])<<16);
Packit 4e8bc4
             a+=k[0]+(((uint32_t)k[1])<<16);
Packit 4e8bc4
             break;
Packit 4e8bc4
    case 9 : c+=k8[8];                      /* @fallthrough */
Packit 4e8bc4
    case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
Packit 4e8bc4
             a+=k[0]+(((uint32_t)k[1])<<16);
Packit 4e8bc4
             break;
Packit 4e8bc4
    case 7 : b+=((uint32_t)k8[6])<<16;      /* @fallthrough */
Packit 4e8bc4
    case 6 : b+=k[2];
Packit 4e8bc4
             a+=k[0]+(((uint32_t)k[1])<<16);
Packit 4e8bc4
             break;
Packit 4e8bc4
    case 5 : b+=k8[4];                      /* @fallthrough */
Packit 4e8bc4
    case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
Packit 4e8bc4
             break;
Packit 4e8bc4
    case 3 : a+=((uint32_t)k8[2])<<16;      /* @fallthrough */
Packit 4e8bc4
    case 2 : a+=k[0];
Packit 4e8bc4
             break;
Packit 4e8bc4
    case 1 : a+=k8[0];
Packit 4e8bc4
             break;
Packit 4e8bc4
    case 0 : return c;  /* zero length strings require no mixing */
Packit 4e8bc4
    }
Packit 4e8bc4
Packit 4e8bc4
  } else {                        /* need to read the key one byte at a time */
Packit 4e8bc4
    const uint8_t *k = key;
Packit 4e8bc4
Packit 4e8bc4
    /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
Packit 4e8bc4
    while (length > 12)
Packit 4e8bc4
    {
Packit 4e8bc4
      a += k[0];
Packit 4e8bc4
      a += ((uint32_t)k[1])<<8;
Packit 4e8bc4
      a += ((uint32_t)k[2])<<16;
Packit 4e8bc4
      a += ((uint32_t)k[3])<<24;
Packit 4e8bc4
      b += k[4];
Packit 4e8bc4
      b += ((uint32_t)k[5])<<8;
Packit 4e8bc4
      b += ((uint32_t)k[6])<<16;
Packit 4e8bc4
      b += ((uint32_t)k[7])<<24;
Packit 4e8bc4
      c += k[8];
Packit 4e8bc4
      c += ((uint32_t)k[9])<<8;
Packit 4e8bc4
      c += ((uint32_t)k[10])<<16;
Packit 4e8bc4
      c += ((uint32_t)k[11])<<24;
Packit 4e8bc4
      mix(a,b,c);
Packit 4e8bc4
      length -= 12;
Packit 4e8bc4
      k += 12;
Packit 4e8bc4
    }
Packit 4e8bc4
Packit 4e8bc4
    /*-------------------------------- last block: affect all 32 bits of (c) */
Packit 4e8bc4
    switch(length)                   /* all the case statements fall through */
Packit 4e8bc4
    {
Packit 4e8bc4
    case 12: c+=((uint32_t)k[11])<<24;
Packit 4e8bc4
    case 11: c+=((uint32_t)k[10])<<16;
Packit 4e8bc4
    case 10: c+=((uint32_t)k[9])<<8;
Packit 4e8bc4
    case 9 : c+=k[8];
Packit 4e8bc4
    case 8 : b+=((uint32_t)k[7])<<24;
Packit 4e8bc4
    case 7 : b+=((uint32_t)k[6])<<16;
Packit 4e8bc4
    case 6 : b+=((uint32_t)k[5])<<8;
Packit 4e8bc4
    case 5 : b+=k[4];
Packit 4e8bc4
    case 4 : a+=((uint32_t)k[3])<<24;
Packit 4e8bc4
    case 3 : a+=((uint32_t)k[2])<<16;
Packit 4e8bc4
    case 2 : a+=((uint32_t)k[1])<<8;
Packit 4e8bc4
    case 1 : a+=k[0];
Packit 4e8bc4
             break;
Packit 4e8bc4
    case 0 : return c;  /* zero length strings require no mixing */
Packit 4e8bc4
    }
Packit 4e8bc4
  }
Packit 4e8bc4
Packit 4e8bc4
  final(a,b,c);
Packit 4e8bc4
  return c;             /* zero length strings require no mixing */
Packit 4e8bc4
}
Packit 4e8bc4
Packit 4e8bc4
#elif HASH_BIG_ENDIAN == 1
Packit 4e8bc4
/*
Packit 4e8bc4
 * hashbig():
Packit 4e8bc4
 * This is the same as hashword() on big-endian machines.  It is different
Packit 4e8bc4
 * from hashlittle() on all machines.  hashbig() takes advantage of
Packit 4e8bc4
 * big-endian byte ordering.
Packit 4e8bc4
 */
Packit 4e8bc4
uint32_t jenkins_hash( const void *key, size_t length)
Packit 4e8bc4
{
Packit 4e8bc4
  uint32_t a,b,c;
Packit 4e8bc4
  union { const void *ptr; size_t i; } u; /* to cast key to (size_t) happily */
Packit 4e8bc4
Packit 4e8bc4
  /* Set up the internal state */
Packit 4e8bc4
  a = b = c = 0xdeadbeef + ((uint32_t)length) + 0;
Packit 4e8bc4
Packit 4e8bc4
  u.ptr = key;
Packit 4e8bc4
  if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) {
Packit 4e8bc4
    const uint32_t *k = key;                           /* read 32-bit chunks */
Packit 4e8bc4
#ifdef VALGRIND
Packit 4e8bc4
    const uint8_t  *k8;
Packit 4e8bc4
#endif /* ifdef VALGRIND */
Packit 4e8bc4
Packit 4e8bc4
    /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
Packit 4e8bc4
    while (length > 12)
Packit 4e8bc4
    {
Packit 4e8bc4
      a += k[0];
Packit 4e8bc4
      b += k[1];
Packit 4e8bc4
      c += k[2];
Packit 4e8bc4
      mix(a,b,c);
Packit 4e8bc4
      length -= 12;
Packit 4e8bc4
      k += 3;
Packit 4e8bc4
    }
Packit 4e8bc4
Packit 4e8bc4
    /*----------------------------- handle the last (probably partial) block */
Packit 4e8bc4
    /*
Packit 4e8bc4
     * "k[2]<<8" actually reads beyond the end of the string, but
Packit 4e8bc4
     * then shifts out the part it's not allowed to read.  Because the
Packit 4e8bc4
     * string is aligned, the illegal read is in the same word as the
Packit 4e8bc4
     * rest of the string.  Every machine with memory protection I've seen
Packit 4e8bc4
     * does it on word boundaries, so is OK with this.  But VALGRIND will
Packit 4e8bc4
     * still catch it and complain.  The masking trick does make the hash
Packit 4e8bc4
     * noticeably faster for short strings (like English words).
Packit 4e8bc4
     */
Packit 4e8bc4
#ifndef VALGRIND
Packit 4e8bc4
Packit 4e8bc4
    switch(length)
Packit 4e8bc4
    {
Packit 4e8bc4
    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
Packit 4e8bc4
    case 11: c+=k[2]&0xffffff00; b+=k[1]; a+=k[0]; break;
Packit 4e8bc4
    case 10: c+=k[2]&0xffff0000; b+=k[1]; a+=k[0]; break;
Packit 4e8bc4
    case 9 : c+=k[2]&0xff000000; b+=k[1]; a+=k[0]; break;
Packit 4e8bc4
    case 8 : b+=k[1]; a+=k[0]; break;
Packit 4e8bc4
    case 7 : b+=k[1]&0xffffff00; a+=k[0]; break;
Packit 4e8bc4
    case 6 : b+=k[1]&0xffff0000; a+=k[0]; break;
Packit 4e8bc4
    case 5 : b+=k[1]&0xff000000; a+=k[0]; break;
Packit 4e8bc4
    case 4 : a+=k[0]; break;
Packit 4e8bc4
    case 3 : a+=k[0]&0xffffff00; break;
Packit 4e8bc4
    case 2 : a+=k[0]&0xffff0000; break;
Packit 4e8bc4
    case 1 : a+=k[0]&0xff000000; break;
Packit 4e8bc4
    case 0 : return c;              /* zero length strings require no mixing */
Packit 4e8bc4
    }
Packit 4e8bc4
Packit 4e8bc4
#else  /* make valgrind happy */
Packit 4e8bc4
Packit 4e8bc4
    k8 = (const uint8_t *)k;
Packit 4e8bc4
    switch(length)                   /* all the case statements fall through */
Packit 4e8bc4
    {
Packit 4e8bc4
    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
Packit 4e8bc4
    case 11: c+=((uint32_t)k8[10])<<8;  /* fall through */
Packit 4e8bc4
    case 10: c+=((uint32_t)k8[9])<<16;  /* fall through */
Packit 4e8bc4
    case 9 : c+=((uint32_t)k8[8])<<24;  /* fall through */
Packit 4e8bc4
    case 8 : b+=k[1]; a+=k[0]; break;
Packit 4e8bc4
    case 7 : b+=((uint32_t)k8[6])<<8;   /* fall through */
Packit 4e8bc4
    case 6 : b+=((uint32_t)k8[5])<<16;  /* fall through */
Packit 4e8bc4
    case 5 : b+=((uint32_t)k8[4])<<24;  /* fall through */
Packit 4e8bc4
    case 4 : a+=k[0]; break;
Packit 4e8bc4
    case 3 : a+=((uint32_t)k8[2])<<8;   /* fall through */
Packit 4e8bc4
    case 2 : a+=((uint32_t)k8[1])<<16;  /* fall through */
Packit 4e8bc4
    case 1 : a+=((uint32_t)k8[0])<<24; break;
Packit 4e8bc4
    case 0 : return c;
Packit 4e8bc4
    }
Packit 4e8bc4
Packit 4e8bc4
#endif /* !VALGRIND */
Packit 4e8bc4
Packit 4e8bc4
  } else {                        /* need to read the key one byte at a time */
Packit 4e8bc4
    const uint8_t *k = key;
Packit 4e8bc4
Packit 4e8bc4
    /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
Packit 4e8bc4
    while (length > 12)
Packit 4e8bc4
    {
Packit 4e8bc4
      a += ((uint32_t)k[0])<<24;
Packit 4e8bc4
      a += ((uint32_t)k[1])<<16;
Packit 4e8bc4
      a += ((uint32_t)k[2])<<8;
Packit 4e8bc4
      a += ((uint32_t)k[3]);
Packit 4e8bc4
      b += ((uint32_t)k[4])<<24;
Packit 4e8bc4
      b += ((uint32_t)k[5])<<16;
Packit 4e8bc4
      b += ((uint32_t)k[6])<<8;
Packit 4e8bc4
      b += ((uint32_t)k[7]);
Packit 4e8bc4
      c += ((uint32_t)k[8])<<24;
Packit 4e8bc4
      c += ((uint32_t)k[9])<<16;
Packit 4e8bc4
      c += ((uint32_t)k[10])<<8;
Packit 4e8bc4
      c += ((uint32_t)k[11]);
Packit 4e8bc4
      mix(a,b,c);
Packit 4e8bc4
      length -= 12;
Packit 4e8bc4
      k += 12;
Packit 4e8bc4
    }
Packit 4e8bc4
Packit 4e8bc4
    /*-------------------------------- last block: affect all 32 bits of (c) */
Packit 4e8bc4
    switch(length)                   /* all the case statements fall through */
Packit 4e8bc4
    {
Packit 4e8bc4
    case 12: c+=k[11];
Packit 4e8bc4
    case 11: c+=((uint32_t)k[10])<<8;
Packit 4e8bc4
    case 10: c+=((uint32_t)k[9])<<16;
Packit 4e8bc4
    case 9 : c+=((uint32_t)k[8])<<24;
Packit 4e8bc4
    case 8 : b+=k[7];
Packit 4e8bc4
    case 7 : b+=((uint32_t)k[6])<<8;
Packit 4e8bc4
    case 6 : b+=((uint32_t)k[5])<<16;
Packit 4e8bc4
    case 5 : b+=((uint32_t)k[4])<<24;
Packit 4e8bc4
    case 4 : a+=k[3];
Packit 4e8bc4
    case 3 : a+=((uint32_t)k[2])<<8;
Packit 4e8bc4
    case 2 : a+=((uint32_t)k[1])<<16;
Packit 4e8bc4
    case 1 : a+=((uint32_t)k[0])<<24;
Packit 4e8bc4
             break;
Packit 4e8bc4
    case 0 : return c;
Packit 4e8bc4
    }
Packit 4e8bc4
  }
Packit 4e8bc4
Packit 4e8bc4
  final(a,b,c);
Packit 4e8bc4
  return c;
Packit 4e8bc4
}
Packit 4e8bc4
#else /* HASH_XXX_ENDIAN == 1 */
Packit 4e8bc4
#error Must define HASH_BIG_ENDIAN or HASH_LITTLE_ENDIAN
Packit 4e8bc4
#endif /* HASH_XXX_ENDIAN == 1 */