2017-08-10 19:40:20 +00:00
|
|
|
#pragma once
|
2018-09-25 22:14:10 +00:00
|
|
|
|
2020-12-12 12:01:29 +00:00
|
|
|
#include "util/types.hpp"
|
2017-08-10 19:40:20 +00:00
|
|
|
|
|
|
|
namespace rpcs3
|
|
|
|
{
|
2021-01-25 16:31:00 +00:00
|
|
|
constexpr usz fnv_seed = 14695981039346656037ull;
|
|
|
|
constexpr usz fnv_prime = 1099511628211ull;
|
|
|
|
|
2017-08-10 19:40:20 +00:00
|
|
|
template<typename T>
|
2020-12-18 07:39:54 +00:00
|
|
|
static usz hash_base(T value)
|
2017-08-10 19:40:20 +00:00
|
|
|
{
|
2020-12-18 07:39:54 +00:00
|
|
|
return static_cast<usz>(value);
|
2017-08-10 19:40:20 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 16:31:00 +00:00
|
|
|
template<typename T, typename = std::enable_if_t<std::is_integral<T>::value, bool>>
|
|
|
|
static inline usz hash64(usz hash_value, const T data)
|
|
|
|
{
|
|
|
|
hash_value ^= data;
|
|
|
|
hash_value *= fnv_prime;
|
|
|
|
return hash_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)
|
2017-08-10 19:40:20 +00:00
|
|
|
{
|
|
|
|
// FNV 64-bit
|
2021-01-25 16:31:00 +00:00
|
|
|
usz result = fnv_seed;
|
2018-07-24 20:41:45 +00:00
|
|
|
const U *bits = reinterpret_cast<const U*>(&value);
|
2017-08-10 19:40:20 +00:00
|
|
|
|
2020-12-18 07:39:54 +00:00
|
|
|
for (usz n = 0; n < (sizeof(T) / sizeof(U)); ++n)
|
2017-08-10 19:40:20 +00:00
|
|
|
{
|
2021-01-25 16:31:00 +00:00
|
|
|
result = hash64(result, bits[n]);
|
2017-08-10 19:40:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2019-03-16 18:30:04 +00:00
|
|
|
if constexpr ((block_sz & 0x7) == 0)
|
2018-07-24 20:41:45 +00:00
|
|
|
{
|
|
|
|
return hash_struct_base<T, u64>(value);
|
|
|
|
}
|
|
|
|
|
2019-03-16 18:30:04 +00:00
|
|
|
if constexpr ((block_sz & 0x3) == 0)
|
2018-07-24 20:41:45 +00:00
|
|
|
{
|
|
|
|
return hash_struct_base<T, u32>(value);
|
|
|
|
}
|
|
|
|
|
2019-03-16 18:30:04 +00:00
|
|
|
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);
|
|
|
|
}
|
2018-09-25 22:14:10 +00:00
|
|
|
}
|