#include "lookupa.h"
Defines | |
#define | mix(a, b, c) |
mix -- mix 3 32-bit values reversibly. More... | |
#define | mixc(a, b, c, d, e, f, g, h) |
mixc -- mixc 8 4-bit values as quickly and thoroughly as possible. More... | |
Functions | |
ub4 | lookup (register ub1 *k, register ub4 length, register ub4 level) |
lookup() -- hash a variable-length key into a 32-bit value. More... | |
void | checksum (register ub1 *k, register ub4 len, register ub4 *state) |
checksum() -- hash a variable-length key into a 256-bit value. More... |
Same as lookup2.c Use this code however you wish. Public Domain. No warranty. Source is http://burtleburtle.net/bob/c/lookupa.c
|
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); \ } 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,
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); ... |
|
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; \ }
Repeating mix() three times achieves avalanche. |
|
lookup() -- hash a variable-length key into a 32-bit value.
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)); 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. |
|
checksum() -- hash a variable-length key into a 256-bit value.
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. |