Add includes (thanks yurivict, #44) and revise HighwayHashCat interface to only require single allocation
diff --git a/highwayhash/highwayhash_target.cc b/highwayhash/highwayhash_target.cc index 625530f..74022f6 100644 --- a/highwayhash/highwayhash_target.cc +++ b/highwayhash/highwayhash_target.cc
@@ -62,39 +62,36 @@ template <TargetBits Target> void HighwayHashCat<Target>::operator()(const HHKey& key, - const char* const* HH_RESTRICT fragments, - const size_t* HH_RESTRICT sizes, + const StringView* HH_RESTRICT fragments, const size_t num_fragments, HHResult64* HH_RESTRICT hash) const { HighwayHashCatT<Target> cat(key); for (size_t i = 0; i < num_fragments; ++i) { - cat.Append(fragments[i], sizes[i]); + cat.Append(fragments[i].data, fragments[i].num_bytes); } cat.Finalize(hash); } template <TargetBits Target> void HighwayHashCat<Target>::operator()(const HHKey& key, - const char* const* HH_RESTRICT fragments, - const size_t* HH_RESTRICT sizes, + const StringView* HH_RESTRICT fragments, const size_t num_fragments, HHResult128* HH_RESTRICT hash) const { HighwayHashCatT<Target> cat(key); for (size_t i = 0; i < num_fragments; ++i) { - cat.Append(fragments[i], sizes[i]); + cat.Append(fragments[i].data, fragments[i].num_bytes); } cat.Finalize(hash); } template <TargetBits Target> void HighwayHashCat<Target>::operator()(const HHKey& key, - const char* const* HH_RESTRICT fragments, - const size_t* HH_RESTRICT sizes, + const StringView* HH_RESTRICT fragments, const size_t num_fragments, HHResult256* HH_RESTRICT hash) const { HighwayHashCatT<Target> cat(key); for (size_t i = 0; i < num_fragments; ++i) { - cat.Append(fragments[i], sizes[i]); + cat.Append(fragments[i].data, fragments[i].num_bytes); } cat.Finalize(hash); }
diff --git a/highwayhash/highwayhash_target.h b/highwayhash/highwayhash_target.h index 72e2df2..f354c34 100644 --- a/highwayhash/highwayhash_target.h +++ b/highwayhash/highwayhash_target.h
@@ -56,25 +56,33 @@ const size_t size, HHResult256* HH_RESTRICT hash) const; }; +// Replacement for C++17 std::string_view that avoids dependencies. +// A struct requires fewer allocations when calling HighwayHashCat with +// non-const "num_fragments". +struct StringView { + const char* data; // not necessarily aligned/padded + size_t num_bytes; // nonzero +}; + // Note: this interface avoids dispatch overhead per fragment. template <TargetBits Target> struct HighwayHashCat { - // Stores a 64/128/256 bit hash of all fragments (of given count and sizes) - // using the HighwayHashCatT implementation for "Target". The hash result is - // identical to HighwayHash of the flattened data, regardless of Target. + // Stores a 64/128/256 bit hash of all "num_fragments" "fragments" using the + // HighwayHashCatT implementation for "Target". The hash result is identical + // to HighwayHash of the flattened data, regardless of Target. // // "key" is a (randomly generated or hard-coded) HHKey. - // "fragments" are "num_fragments" pointers to the (unaligned) data to hash. - // "sizes" are "num_fragments" values indicating the number of bytes. + // "fragments" contain unaligned pointers and the number of valid bytes. + // "num_fragments" indicates the number of entries in "fragments". // "hash" is a HHResult* (either 64, 128 or 256 bits). - void operator()(const HHKey& key, const char* const* HH_RESTRICT fragments, - const size_t* HH_RESTRICT sizes, const size_t num_fragments, + void operator()(const HHKey& key, const StringView* HH_RESTRICT fragments, + const size_t num_fragments, HHResult64* HH_RESTRICT hash) const; - void operator()(const HHKey& key, const char* const* HH_RESTRICT fragments, - const size_t* HH_RESTRICT sizes, const size_t num_fragments, + void operator()(const HHKey& key, const StringView* HH_RESTRICT fragments, + const size_t num_fragments, HHResult128* HH_RESTRICT hash) const; - void operator()(const HHKey& key, const char* const* HH_RESTRICT fragments, - const size_t* HH_RESTRICT sizes, const size_t num_fragments, + void operator()(const HHKey& key, const StringView* HH_RESTRICT fragments, + const size_t num_fragments, HHResult256* HH_RESTRICT hash) const; };
diff --git a/highwayhash/sip_hash_test.cc b/highwayhash/sip_hash_test.cc index a77239e..425dfea 100644 --- a/highwayhash/sip_hash_test.cc +++ b/highwayhash/sip_hash_test.cc
@@ -16,6 +16,8 @@ #include <cassert> #include <numeric> +#include <stdio.h> +#include <stdlib.h> #ifdef HH_GOOGLETEST #include "base/integral_types.h"