diff --git a/Makefile.am b/Makefile.am index 2ff37e65..9db11916 100644 --- a/Makefile.am +++ b/Makefile.am @@ -29,6 +29,8 @@ UTIL_FILES = \ fwd.hpp \ guid.cpp \ guid.hpp \ + hash.cpp \ + hash.hpp \ io.cpp \ io.hpp \ ip.cpp \ diff --git a/hash.cpp b/hash.cpp new file mode 100644 index 00000000..aaac446e --- /dev/null +++ b/hash.cpp @@ -0,0 +1,53 @@ +/* + * This file is part of libgim. + * + * Waif is free software: you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * Waif is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with libgim. If not, see . + * + * Copyright 2010 Danny Robson + */ + +#include "hash.hpp" + + +// Thomas Wang's 32 bit mixing function +uint32_t +hash (uint32_t key) { + uint32_t c2 = 0x27d4eb2d; // a prime or an odd constant + key = (key ^ 61) ^ (key >> 16); + key = key + (key << 3); + key = key ^ (key >> 4); + key = key * c2; + key = key ^ (key >> 15); + return key; +} + + +// Thomas Wang's 64 bit mixing function +uint64_t +hash (uint64_t key) { + key = (~key) + (key << 21); // key = (key << 21) - key - 1; + key = key ^ (key >> 24); + key = (key + (key << 3)) + (key << 8); // key * 265 + key = key ^ (key >> 14); + key = (key + (key << 2)) + (key << 4); // key * 21 + key = key ^ (key >> 28); + key = key + (key << 31); + + return key; +} + + +uintptr_t +hash (void *const key) + { return hash ((uintptr_t)key); } diff --git a/hash.hpp b/hash.hpp new file mode 100644 index 00000000..f6df3498 --- /dev/null +++ b/hash.hpp @@ -0,0 +1,29 @@ +/* + * This file is part of libgim. + * + * Waif is free software: you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * Waif is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with libgim. If not, see . + * + * Copyright 2010 Danny Robson + */ + +#ifndef __UTIL_HASH_HPP +#define __UTIL_HASH_HPP + +#include + +uint32_t hash (uint32_t key); +uint64_t hash (uint64_t key); +uintptr_t hash (void * const); + +#endif