rpcs3/Utilities/hash.h

52 lines
963 B
C
Raw Normal View History

#pragma once
2020-12-12 12:01:29 +00:00
#include "util/types.hpp"
namespace rpcs3
{
template<typename T>
2020-12-18 07:39:54 +00:00
static usz hash_base(T value)
{
2020-12-18 07:39:54 +00:00
return static_cast<usz>(value);
}
2018-07-24 20:41:45 +00:00
template<typename T, typename U>
2020-12-18 07:39:54 +00:00
static usz hash_struct_base(const T& value)
{
// FNV 64-bit
2020-12-18 07:39:54 +00:00
usz result = 14695981039346656037ull;
2018-07-24 20:41:45 +00:00
const U *bits = reinterpret_cast<const U*>(&value);
2020-12-18 07:39:54 +00:00
for (usz n = 0; n < (sizeof(T) / sizeof(U)); ++n)
{
2018-07-24 20:41:45 +00:00
result ^= bits[n];
result *= 1099511628211ull;
}
return result;
}
2018-07-24 20:41:45 +00:00
template<typename T>
2020-12-18 07:39:54 +00:00
static usz hash_struct(const T& value)
2018-07-24 20:41:45 +00:00
{
static constexpr auto block_sz = sizeof(T);
if constexpr ((block_sz & 0x7) == 0)
2018-07-24 20:41:45 +00:00
{
return hash_struct_base<T, u64>(value);
}
if constexpr ((block_sz & 0x3) == 0)
2018-07-24 20:41:45 +00:00
{
return hash_struct_base<T, u32>(value);
}
if constexpr ((block_sz & 0x1) == 0)
2018-07-24 20:41:45 +00:00
{
return hash_struct_base<T, u16>(value);
}
return hash_struct_base<T, u8>(value);
}
}