From 5688a3cfb2af97ae950e90a4142545a5179ef2f5 Mon Sep 17 00:00:00 2001 From: Scott Mansell Date: Sat, 1 Nov 2014 23:15:58 +1300 Subject: [PATCH] IsWiiElf: Optimise inner loop. Instead of swaping each word of the elf code section(s) looking for a match to our pattern, we swap the pattern just once (at compile time) and test against our swapped pattern. --- Source/Core/Core/Boot/Boot_ELF.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/Source/Core/Core/Boot/Boot_ELF.cpp b/Source/Core/Core/Boot/Boot_ELF.cpp index 2e6952e93e38..0f8b2c9960ca 100644 --- a/Source/Core/Core/Boot/Boot_ELF.cpp +++ b/Source/Core/Core/Boot/Boot_ELF.cpp @@ -20,7 +20,7 @@ bool CBoot::IsElfWii(const std::string& filename) { File::IOFile f(filename, "rb"); - f.ReadBytes(elf.get(), (size_t)filesize); + f.ReadBytes(elf.get(), filesize); } // Use the same method as the DOL loader uses: search for mfspr from HID4, @@ -29,21 +29,20 @@ bool CBoot::IsElfWii(const std::string& filename) // Likely to have some false positives/negatives, patches implementing a // better heuristic are welcome. - u32 HID4_pattern = 0x7c13fba6; - u32 HID4_mask = 0xfc1fffff; + // Swap these once, instead of swapping every word in the file. + u32 HID4_pattern = Common::swap32(0x7c13fba6); + u32 HID4_mask = Common::swap32(0xfc1fffff); ElfReader reader(elf.get()); for (int i = 0; i < reader.GetNumSections(); ++i) { if (reader.IsCodeSection(i)) { - for (unsigned int j = 0; j < reader.GetSectionSize(i) / sizeof (u32); ++j) + u32* code = (u32*)reader.GetSectionDataPtr(i); + for (u32 j = 0; j < reader.GetSectionSize(i) / sizeof(u32); ++j) { - u32 word = Common::swap32(((u32*)reader.GetSectionDataPtr(i))[j]); - if ((word & HID4_mask) == HID4_pattern) - { + if ((code[j] & HID4_mask) == HID4_pattern) return true; - } } } }