Add a naive FNV-1a hash series

This commit is contained in:
Danny Robson 2011-10-29 15:06:39 +11:00
parent dc202db67e
commit d57269178a
2 changed files with 45 additions and 0 deletions

View File

@ -46,3 +46,44 @@ util::wang64 (uint64_t key) {
}
// 32: 2^ 24 + 2^8 + 0x93
// 64: 2^ 40 + 2^8 + 0xb3
// 128: 2^ 88 + 2^8 + 0x3B
// 256: 2^168 + 2^8 + 0x63
// 512: 2^344 + 2^8 + 0x57
// 1024: 2^680 + 2^8 + 0x8D
static const uint32_t FNV_1a_32_PRIME = 16777619U;
static const uint64_t FNV_1a_64_PRIME = 1099511628211U;
// Bias is the FNV-0 hash of "chongo <Landon Curt Noll> /\\../\\"
static const uint32_t FNV_1a_32_BIAS = 2166136261U;
static const uint64_t FNV_1a_64_BIAS = 14695981039346656037U;
uint32_t
util::fnv1a32 (const void *_data, size_t size) {
const uint8_t *data = static_cast<const uint8_t*> (_data);
uint32_t result = FNV_1a_32_BIAS;
for (size_t i = 0; i < size; ++i) {
result ^= data[i];
result *= FNV_1a_32_PRIME;
}
return result;
}
uint64_t
util::fnv1a64 (const void *_data, size_t size) {
const uint8_t *data = static_cast<const uint8_t*> (_data);
uint64_t result = FNV_1a_64_BIAS;
for (size_t i = 0; i < size; ++i) {
result ^= data[i];
result *= FNV_1a_64_PRIME;
}
return result;
}

View File

@ -29,6 +29,10 @@ namespace util {
uint32_t wang32 (uint32_t key);
uint64_t wang64 (uint64_t key);
// Fast and general hashing using FNV-1a
uint32_t fnv1a32 (const void *, size_t);
uint64_t fnv1a64 (const void *, size_t);
// General hashes for when you really just don't care about implementation
inline uint32_t hash (uint32_t key) { return wang32 (key); }
inline uint64_t hash (uint64_t key) { return wang64 (key); }