lookupa.c File Reference

lookupa.c, by Bob Jenkins, December 1996. More...

#include "lookupa.h"

Defines

#define mix(a, b, c)
#define mixc(a, b, c, d, e, f, g, h)

Functions

ub4 lookup (register ub1 *k, register ub4 length, register ub4 level)
void checksum (register ub1 *k, register ub4 len, register ub4 *state)


Detailed Description

lookupa.c, by Bob Jenkins, December 1996.

Same as lookup2.c Use this code however you wish. Public Domain. No warranty. Source is http://burtleburtle.net/bob/c/lookupa.c


Define Documentation

#define mix ( a,
b,
 ) 

Value:

{ \
  a -= b; a -= c; a ^= (c>>13); \
  b -= c; b -= a; b ^= (a<<8); \
  c -= a; c -= b; c ^= (b>>13); \
  a -= b; a -= c; a ^= (c>>12);  \
  b -= c; b -= a; b ^= (a<<16); \
  c -= a; c -= b; c ^= (b>>5); \
  a -= b; a -= c; a ^= (c>>3);  \
  b -= c; b -= a; b ^= (a<<10); \
  c -= a; c -= b; c ^= (b>>15); \
}
mix -- mix 3 32-bit values reversibly.

For every delta with one or two bit set, and the deltas of all three high bits or all three low bits, whether the original value of a,b,c is almost all zero or is uniformly distributed,

mix() was built out of 36 single-cycle latency instructions in a structure that could supported 2x parallelism, like so:

    a -= b;
    a -= c; x = (c>>13);
    b -= c; a ^= x;
    b -= a; x = (a<<8);
    c -= a; b ^= x;
    c -= b; x = (b>>13);
    ...
Unfortunately, superscalar Pentiums and Sparcs can't take advantage of that parallelism. They've also turned some of those single-cycle latency instructions into multi-cycle latency instructions. Still, this is the fastest good hash I could find. There were about 2^^68 to choose from. I only looked at a billion or so.

#define mixc ( a,
b,
c,
d,
e,
f,
g,
 ) 

Value:

{ \
   a^=b<<11; d+=a; b+=c; \
   b^=c>>2;  e+=b; c+=d; \
   c^=d<<8;  f+=c; d+=e; \
   d^=e>>16; g+=d; e+=f; \
   e^=f<<10; h+=e; f+=g; \
   f^=g>>4;  a+=f; g+=h; \
   g^=h<<8;  b+=g; h+=a; \
   h^=a>>9;  c+=h; a+=b; \
}
mixc -- mixc 8 4-bit values as quickly and thoroughly as possible.

Repeating mix() three times achieves avalanche.
Repeating mix() four times eliminates all funnels and all characteristics stronger than 2^{-11}.


Function Documentation

ub4 lookup ( register ub1 *  k,
register ub4  length,
register ub4  level 
)

lookup() -- hash a variable-length key into a 32-bit value

Parameters:
k the key (the unaligned variable-length array of bytes)
length the length of the key, counting by bytes
level can be any 4-byte valueReturns a 32-bit value. Every bit of the key affects every bit of the return value. Every 1-bit and 2-bit delta achieves avalanche. About 6len+35 instructions.
The best hash table sizes are powers of 2. There is no need to do mod a prime (mod is sooo slow!). If you need less than 32 bits, use a bitmask. For example, if you need only 10 bits, do
  h = (h & hashmask(10));
In which case, the hash table should have hashsize(10) elements.

If you are hashing n strings (ub1 **)k, do it like this:

  for (i=0, h=0; i<n; ++i) h = lookup( k[i], len[i], h);

By Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use this code any way you wish, private, educational, or commercial.

See http://burtleburtle.net/bob/hash/evahash.html Use for hash table lookup, or anything where one collision in 2^32 is acceptable. Do NOT use for cryptographic purposes.

void checksum ( register ub1 *  k,
register ub4  len,
register ub4 *  state 
)

checksum() -- hash a variable-length key into a 256-bit value

Parameters:
k the key (the unaligned variable-length array of bytes)
len the length of the key, counting by bytes
state an array of CHECKSTATE 4-byte values (256 bits)The state is the checksum. Every bit of the key affects every bit of the state. There are no funnels. About 112+6.875len instructions.
If you are hashing n strings (ub1 **)k, do it like this:
  for (i=0; i<8; ++i) state[i] = 0x9e3779b9;
  for (i=0, h=0; i<n; ++i) checksum( k[i], len[i], state);

(c) Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use this code any way you wish, private, educational, or commercial, as long as this whole comment accompanies it.

See http://burtleburtle.net/bob/hash/evahash.html Use to detect changes between revisions of documents, assuming nobody is trying to cause collisions. Do NOT use for cryptography.


Generated on Wed Jan 2 22:37:53 2008 for XHTML Negotiation Module by  doxygen 1.5.3