A64/decoder: Use a lookup table instead of doing a linear scan

This commit is contained in:
MerryMage 2020-04-19 11:35:13 +01:00
parent 0c51313479
commit 8db4d65587

View file

@ -24,14 +24,23 @@ template <typename Visitor>
using Matcher = Decoder::Matcher<Visitor, u32>; using Matcher = Decoder::Matcher<Visitor, u32>;
template <typename Visitor> template <typename Visitor>
std::vector<Matcher<Visitor>> GetDecodeTable() { using DecodeTable = std::array<std::vector<Matcher<Visitor>>, 0x1000>;
std::vector<Matcher<Visitor>> table = {
namespace detail {
inline size_t ToFastLookupIndex(u32 instruction) {
return ((instruction >> 10) & 0x00F) | ((instruction >> 18) & 0xFF0);
}
} // namespace detail
template <typename Visitor>
DecodeTable<Visitor> GetDecodeTable() {
std::vector<Matcher<Visitor>> list = {
#define INST(fn, name, bitstring) Decoder::detail::detail<Matcher<Visitor>>::GetMatcher(&Visitor::fn, name, bitstring), #define INST(fn, name, bitstring) Decoder::detail::detail<Matcher<Visitor>>::GetMatcher(&Visitor::fn, name, bitstring),
#include "a64.inc" #include "a64.inc"
#undef INST #undef INST
}; };
std::stable_sort(table.begin(), table.end(), [](const auto& matcher1, const auto& matcher2) { std::stable_sort(list.begin(), list.end(), [](const auto& matcher1, const auto& matcher2) {
// If a matcher has more bits in its mask it is more specific, so it should come first. // If a matcher has more bits in its mask it is more specific, so it should come first.
return Common::BitCount(matcher1.GetMask()) > Common::BitCount(matcher2.GetMask()); return Common::BitCount(matcher1.GetMask()) > Common::BitCount(matcher2.GetMask());
}); });
@ -43,10 +52,20 @@ std::vector<Matcher<Visitor>> GetDecodeTable() {
"Unallocated SIMD modified immediate", "Unallocated SIMD modified immediate",
}; };
std::stable_partition(table.begin(), table.end(), [&](const auto& matcher) { std::stable_partition(list.begin(), list.end(), [&](const auto& matcher) {
return comes_first.count(matcher.GetName()) > 0; return comes_first.count(matcher.GetName()) > 0;
}); });
DecodeTable<Visitor> table{};
for (size_t i = 0; i < table.size(); ++i) {
for (auto matcher : list) {
const auto expect = detail::ToFastLookupIndex(matcher.GetExpected());
const auto mask = detail::ToFastLookupIndex(matcher.GetMask());
if ((i & mask) == expect) {
table[i].push_back(matcher);
}
}
}
return table; return table;
} }
@ -56,8 +75,9 @@ std::optional<std::reference_wrapper<const Matcher<Visitor>>> Decode(u32 instruc
const auto matches_instruction = [instruction](const auto& matcher) { return matcher.Matches(instruction); }; const auto matches_instruction = [instruction](const auto& matcher) { return matcher.Matches(instruction); };
auto iter = std::find_if(table.begin(), table.end(), matches_instruction); const auto& subtable = table[detail::ToFastLookupIndex(instruction)];
return iter != table.end() ? std::optional<std::reference_wrapper<const Matcher<Visitor>>>(*iter) : std::nullopt; auto iter = std::find_if(subtable.begin(), subtable.end(), matches_instruction);
return iter != subtable.end() ? std::optional<std::reference_wrapper<const Matcher<Visitor>>>(*iter) : std::nullopt;
} }
} // namespace Dynarmic::A64 } // namespace Dynarmic::A64