Makefile.am: Clean flake8-check too.
[cascardo/ovs.git] / lib / hash.h
index f8bbada..114a419 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2009, 2010, 2012, 2013 Nicira, Inc.
+ * Copyright (c) 2008, 2009, 2010, 2012, 2013, 2014, 2016 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -33,11 +33,15 @@ hash_rot(uint32_t x, int k)
 }
 
 uint32_t hash_bytes(const void *, size_t n_bytes, uint32_t basis);
+/* The hash input must be a word larger than 128 bits. */
+void hash_bytes128(const void *_, size_t n_bytes, uint32_t basis,
+                   ovs_u128 *out);
 
 static inline uint32_t hash_int(uint32_t x, uint32_t basis);
 static inline uint32_t hash_2words(uint32_t, uint32_t);
-static inline uint32_t hash_uint64(uint64_t);
-static inline uint32_t hash_uint64_basis(uint64_t x, uint32_t basis);
+static inline uint32_t hash_uint64(const uint64_t);
+static inline uint32_t hash_uint64_basis(const uint64_t x,
+                                         const uint32_t basis);
 uint32_t hash_3words(uint32_t, uint32_t, uint32_t);
 
 static inline uint32_t hash_boolean(bool x, uint32_t basis);
@@ -71,9 +75,8 @@ static inline uint32_t mhash_add(uint32_t hash, uint32_t data)
     return hash * 5 + 0xe6546b64;
 }
 
-static inline uint32_t mhash_finish(uint32_t hash, uint32_t n_bytes)
+static inline uint32_t mhash_finish(uint32_t hash)
 {
-    hash ^= n_bytes;
     hash ^= hash >> 16;
     hash *= 0x85ebca6b;
     hash ^= hash >> 13;
@@ -83,16 +86,21 @@ static inline uint32_t mhash_finish(uint32_t hash, uint32_t n_bytes)
 }
 
 #if !(defined(__SSE4_2__) && defined(__x86_64__))
-/* Mhash-based implemantation. */
+/* Mhash-based implementation. */
 
 static inline uint32_t hash_add(uint32_t hash, uint32_t data)
 {
     return mhash_add(hash, data);
 }
 
+static inline uint32_t hash_add64(uint32_t hash, uint64_t data)
+{
+    return hash_add(hash_add(hash, data), data >> 32);
+}
+
 static inline uint32_t hash_finish(uint32_t hash, uint32_t final)
 {
-    return mhash_finish(hash, final);
+    return mhash_finish(hash ^ final);
 }
 
 /* Returns the hash of the 'n' 32-bit words at 'p', starting from 'basis'.
@@ -114,10 +122,16 @@ hash_words_inline(const uint32_t p[], size_t n_words, uint32_t basis)
 }
 
 static inline uint32_t
-hash_words64_inline(const uint64_t p[], size_t n_words, uint64_t basis)
+hash_words64_inline(const uint64_t p[], size_t n_words, uint32_t basis)
 {
-    return hash_words_inline((uint32_t *)p, n_words * 2,
-                             (uint32_t)basis ^ basis >> 32);
+    uint32_t hash;
+    size_t i;
+
+    hash = basis;
+    for (i = 0; i < n_words; i++) {
+        hash = hash_add64(hash, p[i]);
+    }
+    return hash_finish(hash, n_words * 8);
 }
 
 static inline uint32_t hash_pointer(const void *p, uint32_t basis)
@@ -138,15 +152,15 @@ static inline uint32_t hash_2words(uint32_t x, uint32_t y)
     return hash_finish(hash_add(hash_add(x, 0), y), 8);
 }
 
-static inline uint32_t hash_uint64(const uint64_t x)
+static inline uint32_t hash_uint64_basis(const uint64_t x,
+                                         const uint32_t basis)
 {
-    return hash_2words((uint32_t)(x >> 32), (uint32_t)x);
+    return hash_finish(hash_add64(basis, x), 8);
 }
 
-static inline uint32_t hash_uint64_basis(const uint64_t x,
-                                         const uint32_t basis)
+static inline uint32_t hash_uint64(const uint64_t x)
 {
-    return hash_3words((uint32_t)(x >> 32), (uint32_t)x, basis);
+    return hash_uint64_basis(x, 0);
 }
 
 #else /* __SSE4_2__ && __x86_64__ */
@@ -157,6 +171,12 @@ static inline uint32_t hash_add(uint32_t hash, uint32_t data)
     return _mm_crc32_u32(hash, data);
 }
 
+/* Add the halves of 'data' in the memory order. */
+static inline uint32_t hash_add64(uint32_t hash, uint64_t data)
+{
+    return _mm_crc32_u64(hash, data);
+}
+
 static inline uint32_t hash_finish(uint64_t hash, uint64_t final)
 {
     /* The finishing multiplier 0x805204f3 has been experimentally
@@ -213,10 +233,10 @@ hash_words_inline(const uint32_t p_[], size_t n_words, uint32_t basis)
 /* A simpler version for 64-bit data.
  * 'n_words' is the count of 64-bit words, basis is 64 bits. */
 static inline uint32_t
-hash_words64_inline(const uint64_t p[], size_t n_words, uint64_t basis)
+hash_words64_inline(const uint64_t p[], size_t n_words, uint32_t basis)
 {
-    uint64_t hash1 = (uint32_t)basis;
-    uint64_t hash2 = basis >> 32;
+    uint64_t hash1 = basis;
+    uint64_t hash2 = 0;
     uint64_t hash3 = n_words;
     const uint64_t *endp = p + n_words;
     const uint64_t *limit = endp - 3;
@@ -243,7 +263,7 @@ static inline uint32_t hash_uint64_basis(const uint64_t x,
                                          const uint32_t basis)
 {
     /* '23' chosen to mix bits enough for the test-hash to pass. */
-    return hash_finish(_mm_crc32_u64(basis, x), 23);
+    return hash_finish(hash_add64(basis, x), 23);
 }
 
 static inline uint32_t hash_uint64(const uint64_t x)
@@ -263,7 +283,7 @@ static inline uint32_t hash_pointer(const void *p, uint32_t basis)
 #endif
 
 uint32_t hash_words__(const uint32_t p[], size_t n_words, uint32_t basis);
-uint32_t hash_words64__(const uint64_t p[], size_t n_words, uint64_t basis);
+uint32_t hash_words64__(const uint64_t p[], size_t n_words, uint32_t basis);
 
 /* Inline the larger hash functions only when 'n_words' is known to be
  * compile-time constant. */
@@ -279,7 +299,7 @@ hash_words(const uint32_t p[], size_t n_words, uint32_t basis)
 }
 
 static inline uint32_t
-hash_words64(const uint64_t p[], size_t n_words, uint64_t basis)
+hash_words64(const uint64_t p[], size_t n_words, uint32_t basis)
 {
     if (__builtin_constant_p(n_words)) {
         return hash_words64_inline(p, n_words, basis);
@@ -297,12 +317,24 @@ hash_words(const uint32_t p[], size_t n_words, uint32_t basis)
 }
 
 static inline uint32_t
-hash_words64(const uint64_t p[], size_t n_words, uint64_t basis)
+hash_words64(const uint64_t p[], size_t n_words, uint32_t basis)
 {
     return hash_words64__(p, n_words, basis);
 }
 #endif
 
+static inline uint32_t
+hash_bytes32(const uint32_t p[], size_t n_bytes, uint32_t basis)
+{
+    return hash_words(p, n_bytes / 4, basis);
+}
+
+static inline uint32_t
+hash_bytes64(const uint64_t p[], size_t n_bytes, uint32_t basis)
+{
+    return hash_words64(p, n_bytes / 8, basis);
+}
+
 static inline uint32_t hash_string(const char *s, uint32_t basis)
 {
     return hash_bytes(s, strlen(s), basis);