Refactor WeakMix to include the XOR of the state with the input value. Motivation: we are considering alternative implementations of WeakMix that don't XOR state with the input value. This refactor makes it clear that the XOR is part of the current implementation of WeakMix. PiperOrigin-RevId: 736929535 Change-Id: I5bca5710ab2a1081c6ab810949aced0dae7ab02d
diff --git a/absl/hash/internal/hash.h b/absl/hash/internal/hash.h index 8ca7d84..4db7f0f 100644 --- a/absl/hash/internal/hash.h +++ b/absl/hash/internal/hash.h
@@ -1099,7 +1099,7 @@ template <typename T, absl::enable_if_t<IntegralFastPath<T>::value, int> = 0> static size_t hash(T value) { return static_cast<size_t>( - WeakMix(Seed() ^ static_cast<std::make_unsigned_t<T>>(value))); + WeakMix(Seed(), static_cast<std::make_unsigned_t<T>>(value))); } // Overload of MixingHashState::hash() @@ -1152,7 +1152,7 @@ // optimize Read1To3 and Read4To8 differently for the string case. static MixingHashState combine_raw(MixingHashState hash_state, uint64_t value) { - return MixingHashState(WeakMix(hash_state.state_ ^ value)); + return MixingHashState(WeakMix(hash_state.state_, value)); } // Implementation of the base case for combine_contiguous where we actually @@ -1180,7 +1180,7 @@ // Empty ranges have no effect. return state; } - return WeakMix(state ^ v); + return WeakMix(state, v); } ABSL_ATTRIBUTE_ALWAYS_INLINE static uint64_t CombineContiguousImpl9to16( @@ -1297,7 +1297,9 @@ // Slightly lower latency than Mix, but with lower quality. The byte swap // helps ensure that low bits still have high quality. - ABSL_ATTRIBUTE_ALWAYS_INLINE static uint64_t WeakMix(uint64_t n) { + ABSL_ATTRIBUTE_ALWAYS_INLINE static uint64_t WeakMix(uint64_t lhs, + uint64_t rhs) { + const uint64_t n = lhs ^ rhs; // WeakMix doesn't work well on 32-bit platforms so just use Mix. if constexpr (sizeof(size_t) < 8) return Mix(n, kMul); #ifdef __ARM_ACLE