Skip to content

Commit d92188a

Browse files
committed
Implement 32-bit hash combine function from Boost.
1 parent c5ea386 commit d92188a

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

CesiumUtility/include/CesiumUtility/Hash.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct Hash {
1818
* @param second The second hash value.
1919
* @return A new hash value which is a combination of the two.
2020
*/
21-
static size_t combine(uint64_t first, uint64_t second);
21+
static size_t combine(size_t first, size_t second);
2222
};
2323

2424
} // namespace CesiumUtility

CesiumUtility/src/Hash.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,31 @@ inline uint64_t mix(uint64_t x) {
7272
return x;
7373
}
7474

75+
// This function is adapted from Boost v1.86.0, `hash_mix_impl<32>` function.
76+
//
77+
// hash_mix for 32 bit size_t
78+
//
79+
// We use the "best xmxmx" implementation from
80+
// https://github.com/skeeto/hash-prospector/issues/19
81+
inline uint32_t mix(uint32_t x) {
82+
uint32_t const m1 = 0x21f0aaad;
83+
uint32_t const m2 = 0x735a2d97;
84+
85+
x ^= x >> 16;
86+
x *= m1;
87+
x ^= x >> 15;
88+
x *= m2;
89+
x ^= x >> 15;
90+
91+
return x;
92+
}
93+
7594
} // namespace
7695

7796
// This function is adapted from Boost's `hash_combine`.
78-
std::size_t Hash::combine(uint64_t first, uint64_t second) {
97+
size_t Hash::combine(size_t first, size_t second) {
7998
// This will truncate bits on 32-bit builds.
80-
return static_cast<std::size_t>(mix(first + 0x9e3779b9 + second));
99+
return mix(first + 0x9e3779b9 + second);
81100
}
82101

83102
} // namespace CesiumUtility

0 commit comments

Comments
 (0)