hash: Implement hash function for "double" values.
authorBen Pfaff <blp@nicira.com>
Thu, 15 Oct 2009 00:03:55 +0000 (17:03 -0700)
committerBen Pfaff <blp@nicira.com>
Wed, 4 Nov 2009 23:00:30 +0000 (15:00 -0800)
This will be used by the configuration database, which can store real
numbers.

lib/hash.c
lib/hash.h

index f1daa6b..63b4784 100644 (file)
@@ -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)
index 4ad14af..2015a63 100644 (file)
@@ -19,6 +19,7 @@
 #include <stddef.h>
 #include <stdint.h>
 #include <string.h>
+#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