Skip to content

Commit

Permalink
Merge pull request #6870 from lioncash/hash
Browse files Browse the repository at this point in the history
Common/Hash: Namespace code under the Common namespace
  • Loading branch information
degasus committed May 16, 2018
2 parents 8ca1bf5 + 011ee11 commit 8fa8aa3
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 23 deletions.
3 changes: 3 additions & 0 deletions Source/Core/Common/Hash.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include <arm_acle.h> #include <arm_acle.h>
#endif #endif


namespace Common
{
static u64 (*ptrHashFunction)(const u8* src, u32 len, u32 samples) = nullptr; static u64 (*ptrHashFunction)(const u8* src, u32 len, u32 samples) = nullptr;


// uint32_t // uint32_t
Expand Down Expand Up @@ -524,3 +526,4 @@ void SetHash64Function()
ptrHashFunction = &GetMurmurHash3; ptrHashFunction = &GetMurmurHash3;
} }
} }
} // namespace Common
3 changes: 3 additions & 0 deletions Source/Core/Common/Hash.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@


#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"


namespace Common
{
u32 HashFletcher(const u8* data_u8, size_t length); // FAST. Length & 1 == 0. u32 HashFletcher(const u8* data_u8, size_t length); // FAST. Length & 1 == 0.
u32 HashAdler32(const u8* data, size_t len); // Fairly accurate, slightly slower u32 HashAdler32(const u8* data, size_t len); // Fairly accurate, slightly slower
u32 HashEctor(const u8* ptr, int length); // JUNK. DO NOT USE FOR NEW THINGS u32 HashEctor(const u8* ptr, int length); // JUNK. DO NOT USE FOR NEW THINGS
u64 GetHash64(const u8* src, u32 len, u32 samples); u64 GetHash64(const u8* src, u32 len, u32 samples);
void SetHash64Function(); void SetHash64Function();
} // namespace Common
4 changes: 2 additions & 2 deletions Source/Core/Core/DSP/DSPCore.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ static bool VerifyRoms()
{0x128ea7a2, 0xa4a575f5}, {0x128ea7a2, 0xa4a575f5},
}}; }};


u32 hash_irom = HashAdler32((u8*)g_dsp.irom, DSP_IROM_BYTE_SIZE); const u32 hash_irom = Common::HashAdler32(reinterpret_cast<u8*>(g_dsp.irom), DSP_IROM_BYTE_SIZE);
u32 hash_drom = HashAdler32((u8*)g_dsp.coef, DSP_COEF_BYTE_SIZE); const u32 hash_drom = Common::HashAdler32(reinterpret_cast<u8*>(g_dsp.coef), DSP_COEF_BYTE_SIZE);
int rom_idx = -1; int rom_idx = -1;


for (size_t i = 0; i < known_roms.size(); ++i) for (size_t i = 0; i < known_roms.size(); ++i)
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/DSP/DSPHWInterface.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ static const u8* gdsp_idma_in(u16 dsp_addr, u32 addr, u32 size)
u16* dst = g_dsp.iram + (dsp_addr / 2); u16* dst = g_dsp.iram + (dsp_addr / 2);


const u8* code = &g_dsp.cpu_ram[addr & 0x0fffffff]; const u8* code = &g_dsp.cpu_ram[addr & 0x0fffffff];
g_dsp.iram_crc = HashEctor(code, size); g_dsp.iram_crc = Common::HashEctor(code, size);


Common::UnWriteProtectMemory(g_dsp.iram, DSP_IRAM_BYTE_SIZE, false); Common::UnWriteProtectMemory(g_dsp.iram, DSP_IRAM_BYTE_SIZE, false);
memcpy(dst, code, size); memcpy(dst, code, size);
Expand Down
5 changes: 3 additions & 2 deletions Source/Core/Core/HW/DSPHLE/UCodes/ROM.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ void ROMUCode::HandleMail(u32 mail)


void ROMUCode::BootUCode() void ROMUCode::BootUCode()
{ {
u32 ector_crc = HashEctor((u8*)HLEMemory_Get_Pointer(m_current_ucode.m_ram_address), const u32 ector_crc =
m_current_ucode.m_length); Common::HashEctor(static_cast<u8*>(HLEMemory_Get_Pointer(m_current_ucode.m_ram_address)),
m_current_ucode.m_length);


if (SConfig::GetInstance().m_DumpUCode) if (SConfig::GetInstance().m_DumpUCode)
{ {
Expand Down
5 changes: 3 additions & 2 deletions Source/Core/Core/HW/DSPHLE/UCodes/UCodes.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -182,8 +182,9 @@ void UCodeInterface::PrepareBootUCode(u32 mail)
m_needs_resume_mail = true; m_needs_resume_mail = true;
m_upload_setup_in_progress = false; m_upload_setup_in_progress = false;


u32 ector_crc = const u32 ector_crc =
HashEctor((u8*)HLEMemory_Get_Pointer(m_next_ucode.iram_mram_addr), m_next_ucode.iram_size); Common::HashEctor(static_cast<u8*>(HLEMemory_Get_Pointer(m_next_ucode.iram_mram_addr)),
m_next_ucode.iram_size);


if (SConfig::GetInstance().m_DumpUCode) if (SConfig::GetInstance().m_DumpUCode)
{ {
Expand Down
6 changes: 4 additions & 2 deletions Source/Core/Core/Movie.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1426,8 +1426,10 @@ void GetSettings()
file_coef.Close(); file_coef.Close();
for (u16& entry : coef) for (u16& entry : coef)
entry = Common::swap16(entry); entry = Common::swap16(entry);
s_DSPiromHash = HashAdler32(reinterpret_cast<u8*>(irom.data()), DSP::DSP_IROM_BYTE_SIZE); s_DSPiromHash =
s_DSPcoefHash = HashAdler32(reinterpret_cast<u8*>(coef.data()), DSP::DSP_COEF_BYTE_SIZE); Common::HashAdler32(reinterpret_cast<u8*>(irom.data()), DSP::DSP_IROM_BYTE_SIZE);
s_DSPcoefHash =
Common::HashAdler32(reinterpret_cast<u8*>(coef.data()), DSP::DSP_COEF_BYTE_SIZE);
} }
else else
{ {
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/DiscIO/CompressedBlob.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ bool CompressedBlobReader::GetBlock(u64 block_num, u8* out_ptr)
} }


// First, check hash. // First, check hash.
u32 block_hash = HashAdler32(m_zlib_buffer.data(), comp_block_size); u32 block_hash = Common::HashAdler32(m_zlib_buffer.data(), comp_block_size);
if (block_hash != m_hashes[block_num]) if (block_hash != m_hashes[block_num])
PanicAlertT("The disc image \"%s\" is corrupt.\n" PanicAlertT("The disc image \"%s\" is corrupt.\n"
"Hash of block %" PRIu64 " is %08x instead of %08x.", "Hash of block %" PRIu64 " is %08x instead of %08x.",
Expand Down Expand Up @@ -304,7 +304,7 @@ bool CompressFileToBlob(const std::string& infile_path, const std::string& outfi


position += write_size; position += write_size;


hashes[i] = HashAdler32(write_buf, write_size); hashes[i] = Common::HashAdler32(write_buf, write_size);
} }


header.compressed_data_size = position; header.compressed_data_size = position;
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/VideoCommon/NativeVertexFormat.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ struct hash<PortableVertexDeclaration>
{ {
size_t operator()(const PortableVertexDeclaration& decl) const size_t operator()(const PortableVertexDeclaration& decl) const
{ {
return HashFletcher((u8*)&decl, sizeof(decl)); return Common::HashFletcher(reinterpret_cast<const u8*>(&decl), sizeof(decl));
} }
}; };
} }
Expand Down
22 changes: 11 additions & 11 deletions Source/Core/VideoCommon/TextureCacheBase.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ TextureCacheBase::TextureCacheBase()


HiresTexture::Init(); HiresTexture::Init();


SetHash64Function(); Common::SetHash64Function();


InvalidateAllBindPoints(); InvalidateAllBindPoints();
} }
Expand Down Expand Up @@ -733,13 +733,13 @@ TextureCacheBase::GetTexture(u32 address, u32 width, u32 height, const TextureFo


// TODO: This doesn't hash GB tiles for preloaded RGBA8 textures (instead, it's hashing more data // TODO: This doesn't hash GB tiles for preloaded RGBA8 textures (instead, it's hashing more data
// from the low tmem bank than it should) // from the low tmem bank than it should)
base_hash = GetHash64(src_data, texture_size, textureCacheSafetyColorSampleSize); base_hash = Common::GetHash64(src_data, texture_size, textureCacheSafetyColorSampleSize);
u32 palette_size = 0; u32 palette_size = 0;
if (isPaletteTexture) if (isPaletteTexture)
{ {
palette_size = TexDecoder_GetPaletteSize(texformat); palette_size = TexDecoder_GetPaletteSize(texformat);
full_hash = full_hash = base_hash ^ Common::GetHash64(&texMem[tlutaddr], palette_size,
base_hash ^ GetHash64(&texMem[tlutaddr], palette_size, textureCacheSafetyColorSampleSize); textureCacheSafetyColorSampleSize);
} }
else else
{ {
Expand Down Expand Up @@ -1225,17 +1225,17 @@ std::optional<TextureLookupInformation> TextureCacheBase::ComputeTextureInformat


// TODO: This doesn't hash GB tiles for preloaded RGBA8 textures (instead, it's hashing more data // TODO: This doesn't hash GB tiles for preloaded RGBA8 textures (instead, it's hashing more data
// from the low tmem bank than it should) // from the low tmem bank than it should)
tex_info.base_hash = GetHash64(tex_info.src_data, tex_info.total_bytes, tex_info.base_hash = Common::GetHash64(tex_info.src_data, tex_info.total_bytes,
tex_info.texture_cache_safety_color_sample_size); tex_info.texture_cache_safety_color_sample_size);


tex_info.is_palette_texture = IsColorIndexed(tex_format); tex_info.is_palette_texture = IsColorIndexed(tex_format);


if (tex_info.is_palette_texture) if (tex_info.is_palette_texture)
{ {
tex_info.palette_size = TexDecoder_GetPaletteSize(tex_format); tex_info.palette_size = TexDecoder_GetPaletteSize(tex_format);
tex_info.full_hash = tex_info.full_hash = tex_info.base_hash ^
tex_info.base_hash ^ GetHash64(&texMem[tex_info.tlut_address], tex_info.palette_size, Common::GetHash64(&texMem[tex_info.tlut_address], tex_info.palette_size,
tex_info.texture_cache_safety_color_sample_size); tex_info.texture_cache_safety_color_sample_size);
} }
else else
{ {
Expand Down Expand Up @@ -2043,7 +2043,7 @@ u64 TextureCacheBase::TCacheEntry::CalculateHash() const
u8* ptr = Memory::GetPointer(addr); u8* ptr = Memory::GetPointer(addr);
if (memory_stride == BytesPerRow()) if (memory_stride == BytesPerRow())
{ {
return GetHash64(ptr, size_in_bytes, HashSampleSize()); return Common::GetHash64(ptr, size_in_bytes, HashSampleSize());
} }
else else
{ {
Expand All @@ -2062,7 +2062,7 @@ u64 TextureCacheBase::TCacheEntry::CalculateHash() const
{ {
// Multiply by a prime number to mix the hash up a bit. This prevents identical blocks from // Multiply by a prime number to mix the hash up a bit. This prevents identical blocks from
// canceling each other out // canceling each other out
temp_hash = (temp_hash * 397) ^ GetHash64(ptr, BytesPerRow(), samples_per_row); temp_hash = (temp_hash * 397) ^ Common::GetHash64(ptr, BytesPerRow(), samples_per_row);
ptr += memory_stride; ptr += memory_stride;
} }
return temp_hash; return temp_hash;
Expand Down

0 comments on commit 8fa8aa3

Please sign in to comment.