#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) |
Same as lookup2.c Use this code however you wish. Public Domain. No warranty. Source is http://burtleburtle.net/bob/c/lookupa.c
#define mix | ( | a, | |||
b, | |||||
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,
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); ...
#define mixc | ( | a, | |||
b, | |||||
c, | |||||
d, | |||||
e, | |||||
f, | |||||
g, | |||||
h | ) |
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.
Repeating mix() four times eliminates all funnels and all characteristics stronger than 2^{-11}.
ub4 lookup | ( | register ub1 * | k, | |
register ub4 | length, | |||
register ub4 | level | |||
) |
lookup() -- hash a variable-length key into a 32-bit value
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. |
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.
void checksum | ( | register ub1 * | k, | |
register ub4 | len, | |||
register ub4 * | state | |||
) |
checksum() -- hash a variable-length key into a 256-bit value
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. |
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.