Defines | |
#define | _c_mix(a, b, c) |
#define | _c_mix64(a, b, c) |
#define | c_hashmask(n) (xhashsize(n) - 1) |
#define | c_hashsize(n) ((uint8_t) 1 << (n)) |
Functions | |
static uint32_t | c_jhash (const uint8_t *k, uint32_t length, uint32_t initval) |
static uint64_t | c_jhash64 (const uint8_t *k, uint64_t length, uint64_t intval) |
#define _c_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, If _c_mix() is run forward or backward, at least 32 bits in a,b,c have at least 1/4 probability of changing. If _c_mix() is run forward, every bit of c will change between 1/3 and 2/3 of the time. (Well, 22/100 and 78/100 for some 2-bit deltas.) _c_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.
Definition at line 58 of file c_jhash.h.
Referenced by c_jhash().
#define _c_mix64 | ( | a, | |||
b, | |||||
c | ) |
Value:
{ \ a -= b; a -= c; a ^= (c>>43); \ b -= c; b -= a; b ^= (a<<9); \ c -= a; c -= b; c ^= (b>>8); \ a -= b; a -= c; a ^= (c>>38); \ b -= c; b -= a; b ^= (a<<23); \ c -= a; c -= b; c ^= (b>>5); \ a -= b; a -= c; a ^= (c>>35); \ b -= c; b -= a; b ^= (a<<49); \ c -= a; c -= b; c ^= (b>>11); \ a -= b; a -= c; a ^= (c>>12); \ b -= c; b -= a; b ^= (a<<18); \ c -= a; c -= b; c ^= (b>>22); \ }
_c_mix64() takes 48 machine instructions, but only 24 cycles on a superscalar machine (like Intel's new MMX architecture). It requires 4 64-bit registers for 4::2 parallelism. All 1-bit deltas, all 2-bit deltas, all deltas composed of top bits of (a,b,c), and all deltas of bottom bits were tested. All deltas were tested both on random keys and on keys that were nearly all zero. These deltas all cause every bit of c to change between 1/3 and 2/3 of the time (well, only 113/400 to 287/400 of the time for some 2-bit delta). These deltas all cause at least 80 bits to change among (a,b,c) when the _c_mix is run either forward or backward (yes it is reversible). This implies that a hash using _c_mix64 has no funnels. There may be characteristics with 3-bit deltas or bigger, I didn't test for those.
Definition at line 89 of file c_jhash.h.
Referenced by c_jhash64().
static uint32_t c_jhash | ( | const uint8_t * | k, | |
uint32_t | length, | |||
uint32_t | initval | |||
) | [inline, static] |
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)); In which case, the hash table should have hashsize(10) elements.
Use for hash table lookup, or anything where one collision in 2^32 is acceptable. Do NOT use for cryptographic purposes.
k | The key (the unaligned variable-length array of bytes). | |
length | The length of the key, counting by bytes. | |
initval | Initial value, can be any 4-byte value. |
Definition at line 127 of file c_jhash.h.
References _c_mix.
static uint64_t c_jhash64 | ( | const uint8_t * | k, | |
uint64_t | length, | |||
uint64_t | intval | |||
) | [inline, static] |
hash a variable-length key into a 64-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 64 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.
Use for hash table lookup, or anything where one collision in 2^^64 is acceptable. Do NOT use for cryptographic purposes.
k | The key (the unaligned variable-length array of bytes). | |
length | The length of the key, counting by bytes. | |
intval | Initial value, can be any 8-byte value. |
Definition at line 186 of file c_jhash.h.
References _c_mix64.