From cce1d8bd8e326c93579a6ff5d037fe3a60a39f86 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Wed, 14 Oct 2009 17:03:55 -0700 Subject: [PATCH] hash: Implement hash function for "double" values. This will be used by the configuration database, which can store real numbers. --- lib/hash.c | 15 +++++++++++++++ lib/hash.h | 8 ++++++++ 2 files changed, 23 insertions(+) diff --git a/lib/hash.c b/lib/hash.c index f1daa6ba..63b4784f 100644 --- a/lib/hash.c +++ b/lib/hash.c @@ -52,6 +52,21 @@ hash_words(const uint32_t *p, size_t n, uint32_t basis) return c; } +/* Returns the hash of the pair of aligned 32-bit words at 'p', starting from + * 'basis'. */ +uint32_t +hash_2words(const uint32_t *p, uint32_t basis) +{ + uint32_t a, b, c; + + a = b = c = 0xdeadbeef + (2 << 2) + basis; + b += p[1]; + a += p[0]; + HASH_FINAL(a, b, c); + + return c; +} + /* Returns the hash of the 'n' bytes at 'p', starting from 'basis'. */ uint32_t hash_bytes(const void *p_, size_t n, uint32_t basis) diff --git a/lib/hash.h b/lib/hash.h index 4ad14aff..2015a639 100644 --- a/lib/hash.h +++ b/lib/hash.h @@ -19,6 +19,7 @@ #include #include #include +#include "util.h" /* This is the public domain lookup3 hash by Bob Jenkins from * http://burtleburtle.net/bob/c/lookup3.c, modified for style. */ @@ -47,6 +48,7 @@ } while (0) uint32_t hash_words(const uint32_t *, size_t n_word, uint32_t basis); +uint32_t hash_2words(const uint32_t *, uint32_t basis); uint32_t hash_bytes(const void *, size_t n_bytes, uint32_t basis); static inline uint32_t hash_string(const char *s, uint32_t basis) @@ -68,6 +70,12 @@ static inline uint32_t hash_int(uint32_t x, uint32_t basis) return x + basis; } +static inline uint32_t hash_double(double x, uint32_t basis) +{ + BUILD_ASSERT_DECL(sizeof x == 8); + return hash_2words((const uint32_t *) &x, basis); +} + static inline uint32_t hash_pointer(const void *p, uint32_t basis) { /* Often pointers are hashed simply by casting to integer type, but that -- 2.30.2