Skip to content

Commit

Permalink
Merge pull request #76250 from akrieger/backport_76153
Browse files Browse the repository at this point in the history
Fix point hash to be well distributed.
  • Loading branch information
Maleclypse authored Sep 8, 2024
2 parents c513797 + 337e961 commit c6ba9a2
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions src/point.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,20 @@ namespace std
template <>
struct hash<point> {
std::size_t operator()( const point &k ) const noexcept {
constexpr uint64_t a = 2862933555777941757;
size_t result = k.y;
result *= a;
result += k.x;
return result;
// We cast k.y to uint32_t because otherwise when promoting to uint64_t for binary `or` it
// will sign extend and turn the upper 32 bits into all 1s.
uint64_t x = static_cast<uint64_t>( k.x ) << 32 | static_cast<uint32_t>( k.y );

// Found through https://nullprogram.com/blog/2018/07/31/
// Public domain source from https://xoshiro.di.unimi.it/splitmix64.c
x ^= x >> 30;
x *= 0xbf58476d1ce4e5b9U;
x ^= x >> 27;
x *= 0x94d049bb133111ebU;
x ^= x >> 31;
return x;

return x;
}
};
} // namespace std
Expand All @@ -319,13 +328,21 @@ namespace std
template <>
struct hash<tripoint> {
std::size_t operator()( const tripoint &k ) const noexcept {
constexpr uint64_t a = 2862933555777941757;
size_t result = k.z;
result *= a;
result += k.y;
result *= a;
result += k.x;
return result;
// We cast k.y to uint32_t because otherwise when promoting to uint64_t for binary `or` it
// will sign extend and turn the upper 32 bits into all 1s.
uint64_t x = static_cast<uint64_t>( k.x ) << 32 | static_cast<uint32_t>( k.y );

// Found through https://nullprogram.com/blog/2018/07/31/
// Public domain source from https://xoshiro.di.unimi.it/splitmix64.c
x ^= x >> 30;
x *= 0xbf58476d1ce4e5b9U;
x ^= x >> 27;

// Sprinkle in z now.
x ^= static_cast<uint64_t>( k.z );
x *= 0x94d049bb133111ebU;
x ^= x >> 31;
return x;
}
};
} // namespace std
Expand Down

0 comments on commit c6ba9a2

Please sign in to comment.