[ovs-dev] [PATCH 1/2] hash: Add hash_add64().

Jarno Rajahalme jrajahalme at nicira.com
Wed Dec 17 18:30:41 UTC 2014


Add support for adding 64-bit words to hashes.  This will be used by
subsequent patches.

Signed-off-by: Jarno Rajahalme <jrajahalme at nicira.com>
---
 lib/classifier-private.h |    3 +--
 lib/dpif-netdev.c        |    3 +--
 lib/hash.h               |   33 +++++++++++++++++++++++++--------
 ofproto/tunnel.c         |    4 ++--
 4 files changed, 29 insertions(+), 14 deletions(-)

diff --git a/lib/classifier-private.h b/lib/classifier-private.h
index 2522e91..17eed2c 100644
--- a/lib/classifier-private.h
+++ b/lib/classifier-private.h
@@ -236,8 +236,7 @@ miniflow_hash(const struct miniflow *flow, uint32_t basis)
         }
         p++;
     }
-    hash = hash_add(hash, hash_map);
-    hash = hash_add(hash, hash_map >> 32);
+    hash = hash_add64(hash, hash_map);
 
     return hash_finish(hash, p - values);
 }
diff --git a/lib/dpif-netdev.c b/lib/dpif-netdev.c
index a0e508c..c23decb 100644
--- a/lib/dpif-netdev.c
+++ b/lib/dpif-netdev.c
@@ -1389,8 +1389,7 @@ netdev_flow_mask_init(struct netdev_flow_key *mask,
     mask->mf.values_inline = true;
     mask->mf.map = mask_map;
 
-    hash = hash_add(hash, mask_map);
-    hash = hash_add(hash, mask_map >> 32);
+    hash = hash_add64(hash, mask_map);
 
     n = dst - mask->mf.inline_values;
 
diff --git a/lib/hash.h b/lib/hash.h
index c2820dd..4dd8312 100644
--- a/lib/hash.h
+++ b/lib/hash.h
@@ -92,6 +92,11 @@ 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);
@@ -118,8 +123,14 @@ 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)
 {
-    return hash_words_inline((uint32_t *)p, n_words * 2,
-                             (uint32_t)basis ^ basis >> 32);
+    uint32_t hash;
+    size_t i;
+
+    hash = basis ^ basis >> 32;
+    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)
@@ -140,15 +151,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__ */
@@ -159,6 +170,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
@@ -245,7 +262,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)
diff --git a/ofproto/tunnel.c b/ofproto/tunnel.c
index d079a24..5985c09 100644
--- a/ofproto/tunnel.c
+++ b/ofproto/tunnel.c
@@ -476,8 +476,8 @@ out:
 static uint32_t
 tnl_hash(struct tnl_match *match)
 {
-    BUILD_ASSERT_DECL(sizeof *match % sizeof(uint32_t) == 0);
-    return hash_words((uint32_t *) match, sizeof *match / sizeof(uint32_t), 0);
+    BUILD_ASSERT_DECL(sizeof *match % sizeof(uint64_t) == 0);
+    return hash_words64((uint64_t *)match, sizeof *match / sizeof(uint64_t), 0);
 }
 
 static struct tnl_port *
-- 
1.7.10.4




More information about the dev mailing list