Catalli's threaded switch
[sliver-openvswitch.git] / lib / hash.h
index 2015a63..5f6409c 100644 (file)
@@ -16,6 +16,7 @@
 #ifndef HASH_H
 #define HASH_H 1
 
+#include <stdbool.h>
 #include <stddef.h>
 #include <stdint.h>
 #include <string.h>
@@ -64,16 +65,29 @@ static inline uint32_t hash_int(uint32_t x, uint32_t basis)
     x ^= x >> 17;
     x -= x << 9;
     x ^= x << 4;
+    x += basis;
     x -= x << 3;
     x ^= x << 10;
     x ^= x >> 15;
-    return x + basis;
+    return x;
+}
+
+/* An attempt at a useful 1-bit hash function.  Has not been analyzed for
+ * quality. */
+static inline uint32_t hash_boolean(bool x, uint32_t basis)
+{
+    enum { P0 = 0xc2b73583 };   /* This is hash_int(1, 0). */
+    enum { P1 = 0xe90f1258 };   /* This is hash_int(2, 0). */
+    return (x ? P0 : P1) ^ HASH_ROT(basis, 1);
 }
 
 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);
+    uint32_t value[2];
+    BUILD_ASSERT_DECL(sizeof x == sizeof value);
+
+    memcpy(value, &x, sizeof value);
+    return hash_2words(value, basis);
 }
 
 static inline uint32_t hash_pointer(const void *p, uint32_t basis)