Use hash algorithm name in its function's symbol

This commit is contained in:
Danny Robson 2011-10-29 15:05:49 +11:00
parent 796a3d3639
commit dc202db67e
2 changed files with 20 additions and 10 deletions

View File

@ -20,9 +20,8 @@
#include "hash.hpp" #include "hash.hpp"
// Thomas Wang's 32 bit mixing function
uint32_t uint32_t
hash (uint32_t key) { util::wang32 (uint32_t key) {
uint32_t c2 = 0x27d4eb2d; // a prime or an odd constant uint32_t c2 = 0x27d4eb2d; // a prime or an odd constant
key = (key ^ 61) ^ (key >> 16); key = (key ^ 61) ^ (key >> 16);
key = key + (key << 3); key = key + (key << 3);
@ -33,9 +32,8 @@ hash (uint32_t key) {
} }
// Thomas Wang's 64 bit mixing function
uint64_t uint64_t
hash (uint64_t key) { util::wang64 (uint64_t key) {
key = (~key) + (key << 21); // key = (key << 21) - key - 1; key = (~key) + (key << 21); // key = (key << 21) - key - 1;
key = key ^ (key >> 24); key = key ^ (key >> 24);
key = (key + (key << 3)) + (key << 8); // key * 265 key = (key + (key << 3)) + (key << 8); // key * 265
@ -48,6 +46,3 @@ hash (uint64_t key) {
} }
uintptr_t
hash (void *const key)
{ return hash ((uintptr_t)key); }

View File

@ -21,9 +21,24 @@
#define __UTIL_HASH_HPP #define __UTIL_HASH_HPP
#include <cstdint> #include <cstdint>
#include <cstdlib>
// All hashes are unsuitable for cryptographic operations nnless noted.
namespace util {
// Fast integer mixing operations by Thomas Wang.
uint32_t wang32 (uint32_t key);
uint64_t wang64 (uint64_t key);
// 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); }
inline uintptr_t hash (const void *key) {
return sizeof (uintptr_t) == 32 ? wang32 (reinterpret_cast<uintptr_t> (key)) :
wang64 (reinterpret_cast<uintptr_t> (key));
}
}
uint32_t hash (uint32_t key);
uint64_t hash (uint64_t key);
uintptr_t hash (void * const);
#endif #endif