diff mbox

[ovs-dev,1/2] hash: New helper functions for adding words in a buffer to a hash.

Message ID 20170607163703.3638-1-blp@ovn.org
State Superseded
Headers show

Commit Message

Ben Pfaff June 7, 2017, 4:37 p.m. UTC
These will receive their first user (outside of hash.h) in the following
commit.

Signed-off-by: Ben Pfaff <blp@ovn.org>
---
 lib/hash.h | 66 +++++++++++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 48 insertions(+), 18 deletions(-)

Comments

Darrell Ball June 9, 2017, 3:35 a.m. UTC | #1
Acked-by: Darrell Ball <dlu998@gmail.com>

On 6/7/17, 9:37 AM, "ovs-dev-bounces@openvswitch.org on behalf of Ben Pfaff" <ovs-dev-bounces@openvswitch.org on behalf of blp@ovn.org> wrote:

    These will receive their first user (outside of hash.h) in the following
    commit.
    
    Signed-off-by: Ben Pfaff <blp@ovn.org>
    ---
     lib/hash.h | 66 +++++++++++++++++++++++++++++++++++++++++++++-----------------
     1 file changed, 48 insertions(+), 18 deletions(-)
    
    diff --git a/lib/hash.h b/lib/hash.h
    index d68ed759a7e9..7dffeaa9cacc 100644
    --- a/lib/hash.h
    +++ b/lib/hash.h
    @@ -90,6 +90,15 @@ static inline uint32_t mhash_finish(uint32_t hash)
         return hash;
     }
     
    +static inline uint32_t hash_add(uint32_t hash, uint32_t data);
    +static inline uint32_t hash_add64(uint32_t hash, uint64_t data);
    +static inline uint32_t hash_finish(uint32_t hash, uint32_t final);
    +
    +static inline uint32_t hash_add_words(uint32_t, const uint32_t *, size_t);
    +static inline uint32_t hash_add_words64(uint32_t, const uint64_t *, size_t);
    +static inline uint32_t hash_add_bytes32(uint32_t, const uint32_t *, size_t);
    +static inline uint32_t hash_add_bytes64(uint32_t, const uint64_t *, size_t);
    +
     #if !(defined(__SSE4_2__) && defined(__x86_64__))
     /* Mhash-based implementation. */
     
    @@ -114,29 +123,15 @@ static inline uint32_t hash_finish(uint32_t hash, uint32_t final)
      * This is inlined for the compiler to have access to the 'n_words', which
      * in many cases is a constant. */
     static inline uint32_t
    -hash_words_inline(const uint32_t p[], size_t n_words, uint32_t basis)
    +hash_words_inline(const uint32_t *p, size_t n_words, uint32_t basis)
     {
    -    uint32_t hash;
    -    size_t i;
    -
    -    hash = basis;
    -    for (i = 0; i < n_words; i++) {
    -        hash = hash_add(hash, p[i]);
    -    }
    -    return hash_finish(hash, n_words * 4);
    +    return hash_finish(hash_add_words(basis, p, n_words), n_words * 4);
     }
     
     static inline uint32_t
    -hash_words64_inline(const uint64_t p[], size_t n_words, uint32_t basis)
    +hash_words64_inline(const uint64_t *p, size_t n_words, uint32_t basis)
     {
    -    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);
    +    return hash_finish(hash_add_words64(basis, p, n_words), n_words * 8);
     }
     
     static inline uint32_t hash_pointer(const void *p, uint32_t basis)
    @@ -358,6 +353,41 @@ static inline uint32_t hash_boolean(bool x, uint32_t basis)
         const uint32_t P1 = 0xe90f1258;   /* This is hash_int(2, 0). */
         return (x ? P0 : P1) ^ hash_rot(basis, 1);
     }
    +?
    +/* Helper functions for calling hash_add() for several 32- or 64-bit words in a
    + * buffer.  These are not hash functions by themselves, since they need
    + * hash_finish() to be called, so if you are looking for a full hash function
    + * see hash_words(), etc. */
    +
    +static inline uint32_t
    +hash_add_words(uint32_t hash, const uint32_t *p, size_t n_words)
    +{
    +    for (size_t i = 0; i < n_words; i++) {
    +        hash = hash_add(hash, p[i]);
    +    }
    +    return hash;
    +}
    +
    +static inline uint32_t
    +hash_add_words64(uint32_t hash, const uint64_t *p, size_t n_words)
    +{
    +    for (size_t i = 0; i < n_words; i++) {
    +        hash = hash_add64(hash, p[i]);
    +    }
    +    return hash;
    +}
    +
    +static inline uint32_t
    +hash_add_bytes32(uint32_t hash, const uint32_t *p, size_t n_bytes)
    +{
    +    return hash_add_words(hash, p, n_bytes / 4);
    +}
    +
    +static inline uint32_t
    +hash_add_bytes64(uint32_t hash, const uint64_t *p, size_t n_bytes)
    +{
    +    return hash_add_words64(hash, p, n_bytes / 8);
    +}
     
     #ifdef __cplusplus
     }
    -- 
    2.10.2
    
    _______________________________________________
    dev mailing list
    dev@openvswitch.org
    https://urldefense.proofpoint.com/v2/url?u=https-3A__mail.openvswitch.org_mailman_listinfo_ovs-2Ddev&d=DwICAg&c=uilaK90D4TOVoH58JNXRgQ&r=BVhFA09CGX7JQ5Ih-uZnsw&m=kee8Fq6OyF_ZAvSD3rLTSW2pqsffYCxrE2AWREKAgI8&s=DHXO3ZX4LzeJ7Lj69uoFPRako5mgJbzIiiB6-qsJHFg&e=
diff mbox

Patch

diff --git a/lib/hash.h b/lib/hash.h
index d68ed759a7e9..7dffeaa9cacc 100644
--- a/lib/hash.h
+++ b/lib/hash.h
@@ -90,6 +90,15 @@  static inline uint32_t mhash_finish(uint32_t hash)
     return hash;
 }
 
+static inline uint32_t hash_add(uint32_t hash, uint32_t data);
+static inline uint32_t hash_add64(uint32_t hash, uint64_t data);
+static inline uint32_t hash_finish(uint32_t hash, uint32_t final);
+
+static inline uint32_t hash_add_words(uint32_t, const uint32_t *, size_t);
+static inline uint32_t hash_add_words64(uint32_t, const uint64_t *, size_t);
+static inline uint32_t hash_add_bytes32(uint32_t, const uint32_t *, size_t);
+static inline uint32_t hash_add_bytes64(uint32_t, const uint64_t *, size_t);
+
 #if !(defined(__SSE4_2__) && defined(__x86_64__))
 /* Mhash-based implementation. */
 
@@ -114,29 +123,15 @@  static inline uint32_t hash_finish(uint32_t hash, uint32_t final)
  * This is inlined for the compiler to have access to the 'n_words', which
  * in many cases is a constant. */
 static inline uint32_t
-hash_words_inline(const uint32_t p[], size_t n_words, uint32_t basis)
+hash_words_inline(const uint32_t *p, size_t n_words, uint32_t basis)
 {
-    uint32_t hash;
-    size_t i;
-
-    hash = basis;
-    for (i = 0; i < n_words; i++) {
-        hash = hash_add(hash, p[i]);
-    }
-    return hash_finish(hash, n_words * 4);
+    return hash_finish(hash_add_words(basis, p, n_words), n_words * 4);
 }
 
 static inline uint32_t
-hash_words64_inline(const uint64_t p[], size_t n_words, uint32_t basis)
+hash_words64_inline(const uint64_t *p, size_t n_words, uint32_t basis)
 {
-    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);
+    return hash_finish(hash_add_words64(basis, p, n_words), n_words * 8);
 }
 
 static inline uint32_t hash_pointer(const void *p, uint32_t basis)
@@ -358,6 +353,41 @@  static inline uint32_t hash_boolean(bool x, uint32_t basis)
     const uint32_t P1 = 0xe90f1258;   /* This is hash_int(2, 0). */
     return (x ? P0 : P1) ^ hash_rot(basis, 1);
 }
+
+/* Helper functions for calling hash_add() for several 32- or 64-bit words in a
+ * buffer.  These are not hash functions by themselves, since they need
+ * hash_finish() to be called, so if you are looking for a full hash function
+ * see hash_words(), etc. */
+
+static inline uint32_t
+hash_add_words(uint32_t hash, const uint32_t *p, size_t n_words)
+{
+    for (size_t i = 0; i < n_words; i++) {
+        hash = hash_add(hash, p[i]);
+    }
+    return hash;
+}
+
+static inline uint32_t
+hash_add_words64(uint32_t hash, const uint64_t *p, size_t n_words)
+{
+    for (size_t i = 0; i < n_words; i++) {
+        hash = hash_add64(hash, p[i]);
+    }
+    return hash;
+}
+
+static inline uint32_t
+hash_add_bytes32(uint32_t hash, const uint32_t *p, size_t n_bytes)
+{
+    return hash_add_words(hash, p, n_bytes / 4);
+}
+
+static inline uint32_t
+hash_add_bytes64(uint32_t hash, const uint64_t *p, size_t n_bytes)
+{
+    return hash_add_words64(hash, p, n_bytes / 8);
+}
 
 #ifdef __cplusplus
 }