Commit c464335f authored by Philip Pronin's avatar Philip Pronin Committed by Facebook GitHub Bot

expose commutative accumulator as commutative_hash_128_to_64

Summary:
So it can be used in cases where data is already hashed, and
order-independent way to reduce the set of hashes is required.

Reviewed By: ot

Differential Revision: D28382187

fbshipit-source-id: 4327ddd67b49a61d0172c4676090b2f96e84c065
parent 83b07aec
......@@ -71,6 +71,15 @@ constexpr uint64_t hash_128_to_64(
return b;
}
// Order-independent way to reduce multiple 64 bit hashes into a single hash.
FOLLY_DISABLE_UNDEFINED_BEHAVIOR_SANITIZER("unsigned-integer-overflow")
constexpr uint64_t commutative_hash_128_to_64(
const uint64_t upper, const uint64_t lower) noexcept {
// Commutative accumulator taken from this paper:
// https://www.preprints.org/manuscript/201710.0192/v1/download
return 3860031 + (upper + lower) * 2779 + (upper * lower * 2);
}
// twang_mix64
//
// Thomas Wang 64 bit mix hash function.
......@@ -643,9 +652,7 @@ uint64_t commutative_hash_combine_value_generic(
uint64_t seed, Hash const& hasher, Value const& value) {
auto const x = hasher(value);
auto const y = IsAvalanchingHasher<Hash, Value>::value ? x : twang_mix64(x);
// Commutative accumulator taken from this paper:
// https://www.preprints.org/manuscript/201710.0192/v1/download
return 3860031 + (seed + y) * 2779 + (seed * y * 2);
return commutative_hash_128_to_64(seed, y);
}
// hash_range combines hashes of items in the range [first, last) in an
......
......@@ -31,6 +31,15 @@
using namespace folly::hash;
TEST(Hash, Test128To64) {
constexpr uint64_t upper = 12345678910111213UL;
constexpr uint64_t lower = 141516171819202122UL;
EXPECT_NE(hash_128_to_64(upper, lower), hash_128_to_64(lower, upper));
EXPECT_EQ(
commutative_hash_128_to_64(upper, lower),
commutative_hash_128_to_64(lower, upper));
}
TEST(Hash, Fnv32) {
const char* s1 = "hello, world!";
const uint32_t s1_res = 3605494790UL;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment