[ovs-dev] [PATCH 1/2] hash: New helper functions for adding words in a buffer to a hash.
Ben Pfaff
blp at ovn.org
Wed Jun 7 16:37:02 UTC 2017
These will receive their first user (outside of hash.h) in the following
commit.
Signed-off-by: Ben Pfaff <blp at 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
More information about the dev
mailing list