Taken from: http://burtleburtle.net/bob/hash/
#include <config.h>
#include <stdint.h>
Go to the source code of this file.
Functions | |
uint32_t | hashword (const uint32_t *k, size_t length, uint32_t initval) |
This works on all machines. | |
uint32_t | hashlittle (const void *key, size_t length, uint32_t initval) |
hashlittle() -- hash a variable-length key into a 32-bit value | |
uint32_t | hashbig (const void *key, size_t length, uint32_t initval) |
hashbig(): This is the same as hashword() on big-endian machines. |
|
This works on all machines. To be useful, it requires -- that the key be an array of uint32's, and -- that all your machines have the same endianness, and -- that the length be the number of uint32's in the key The function hashword() is identical to hashlittle() on little-endian machines, and identical to hashbig() on big-endian machines, except that the length has to be measured in uint32s rather than in bytes. hashlittle() is more complicated than hashword() only because hashlittle() has to dance around fitting the key bytes into registers.
|
|
hashlittle() -- 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. If you are hashing n strings (uint8 **)k, do it like this: for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h); By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this code any way you wish, private, educational, or commercial. It's free. Use for hash table lookup, or anything where one collision in 2^^32 is acceptable. Do NOT use for cryptographic purposes. |
|
hashbig(): This is the same as hashword() on big-endian machines. It is different from hashlittle() on all machines. hashbig() takes advantage of big-endian byte ordering. |