Skip to content

Commit 8915931

Browse files
ezbrcopybara-github
authored andcommitted
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
1 parent 4d6cb98 commit 8915931

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

absl/hash/internal/hash.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,7 @@ class ABSL_DLL MixingHashState : public HashStateBase<MixingHashState> {
10991099
template <typename T, absl::enable_if_t<IntegralFastPath<T>::value, int> = 0>
11001100
static size_t hash(T value) {
11011101
return static_cast<size_t>(
1102-
WeakMix(Seed() ^ static_cast<std::make_unsigned_t<T>>(value)));
1102+
WeakMix(Seed(), static_cast<std::make_unsigned_t<T>>(value)));
11031103
}
11041104

11051105
// Overload of MixingHashState::hash()
@@ -1152,7 +1152,7 @@ class ABSL_DLL MixingHashState : public HashStateBase<MixingHashState> {
11521152
// optimize Read1To3 and Read4To8 differently for the string case.
11531153
static MixingHashState combine_raw(MixingHashState hash_state,
11541154
uint64_t value) {
1155-
return MixingHashState(WeakMix(hash_state.state_ ^ value));
1155+
return MixingHashState(WeakMix(hash_state.state_, value));
11561156
}
11571157

11581158
// Implementation of the base case for combine_contiguous where we actually
@@ -1180,7 +1180,7 @@ class ABSL_DLL MixingHashState : public HashStateBase<MixingHashState> {
11801180
// Empty ranges have no effect.
11811181
return state;
11821182
}
1183-
return WeakMix(state ^ v);
1183+
return WeakMix(state, v);
11841184
}
11851185

11861186
ABSL_ATTRIBUTE_ALWAYS_INLINE static uint64_t CombineContiguousImpl9to16(
@@ -1297,7 +1297,9 @@ class ABSL_DLL MixingHashState : public HashStateBase<MixingHashState> {
12971297

12981298
// Slightly lower latency than Mix, but with lower quality. The byte swap
12991299
// helps ensure that low bits still have high quality.
1300-
ABSL_ATTRIBUTE_ALWAYS_INLINE static uint64_t WeakMix(uint64_t n) {
1300+
ABSL_ATTRIBUTE_ALWAYS_INLINE static uint64_t WeakMix(uint64_t lhs,
1301+
uint64_t rhs) {
1302+
const uint64_t n = lhs ^ rhs;
13011303
// WeakMix doesn't work well on 32-bit platforms so just use Mix.
13021304
if constexpr (sizeof(size_t) < 8) return Mix(n, kMul);
13031305
#ifdef __ARM_ACLE

0 commit comments

Comments
 (0)