rpcsx/rpcs3/util/fnv_hash.hpp

65 lines
1.3 KiB
C++
Raw Normal View History

#pragma once
2020-12-12 13:01:29 +01:00
#include "util/types.hpp"
#include <cstring>
namespace rpcs3
{
constexpr usz fnv_seed = 14695981039346656037ull;
constexpr usz fnv_prime = 1099511628211ull;
template <typename T>
2020-12-18 08:39:54 +01:00
static usz hash_base(T value)
{
2020-12-18 08:39:54 +01:00
return static_cast<usz>(value);
}
template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
static inline usz hash64(usz hash_value, T data)
{
hash_value ^= data;
hash_value *= fnv_prime;
return hash_value;
}
template <typename T, typename U>
2020-12-18 08:39:54 +01:00
static usz hash_struct_base(const T& value)
{
// FNV 64-bit
usz result = fnv_seed;
const uchar* bits = reinterpret_cast<const uchar*>(&value);
2020-12-18 08:39:54 +01:00
for (usz n = 0; n < (sizeof(T) / sizeof(U)); ++n)
{
U val{};
std::memcpy(&val, bits + (n * sizeof(U)), sizeof(U));
result = hash64(result, val);
}
return result;
}
2018-07-24 22:41:45 +02:00
template <typename T>
2020-12-18 08:39:54 +01:00
static usz hash_struct(const T& value)
2018-07-24 22:41:45 +02:00
{
static constexpr auto block_sz = sizeof(T);
if constexpr ((block_sz & 0x7) == 0)
2018-07-24 22:41:45 +02:00
{
return hash_struct_base<T, u64>(value);
}
if constexpr ((block_sz & 0x3) == 0)
2018-07-24 22:41:45 +02:00
{
return hash_struct_base<T, u32>(value);
}
if constexpr ((block_sz & 0x1) == 0)
2018-07-24 22:41:45 +02:00
{
return hash_struct_base<T, u16>(value);
}
return hash_struct_base<T, u8>(value);
}
}