From ad09f3e93adac5d1d4ce098ce4cfbfe1bb440218 Mon Sep 17 00:00:00 2001 From: Marko Kreen Date: Thu, 6 Dec 2007 09:18:15 +0000 Subject: [PATCH] int/ptr hash function --- include/hash.h | 7 +++++++ src/hash.c | 22 +++++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/include/hash.h b/include/hash.h index 3a82476..fb2e67f 100644 --- a/include/hash.h +++ b/include/hash.h @@ -1,3 +1,10 @@ uint32_t lookup3_hash(const void *data, size_t len); +uint32_t hash32(uint32_t v); + +static inline uint32_t ptr_hash32(const void *ptr) +{ + return hash32((uint32_t)(long)ptr); +} + diff --git a/src/hash.c b/src/hash.c index 80179a8..5c9a736 100644 --- a/src/hash.c +++ b/src/hash.c @@ -16,6 +16,9 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include "system.h" +#include "hash.h" + /* * A simple version of Bob Jenkins' lookup3.c hash. * @@ -26,9 +29,6 @@ * Actual difference varies as it depends on cpu/compiler/libc details. */ -#include "system.h" -#include "hash.h" - /* rotate uint32 */ #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k)))) @@ -84,3 +84,19 @@ done: return c; } + +/* + * Reversible integer hash function by Thomas Wang. + */ + +uint32_t hash32(uint32_t v) +{ + v = ~v + (v << 15); + v = v ^ (v >> 12); + v = v + (v << 2); + v = v ^ (v >> 4); + v = v * 2057; + v = v ^ (v >> 16); + return v; +} + -- 2.40.0