1 /* This file is part of the dynarmic project.
2  * Copyright (c) 2020 MerryMage
3  * SPDX-License-Identifier: 0BSD
4  */
5 
6 #pragma once
7 
8 #include <algorithm>
9 #include <functional>
10 #include <optional>
11 #include <vector>
12 
13 #include "common/bit_util.h"
14 #include "common/common_types.h"
15 #include "frontend/decoder/decoder_detail.h"
16 #include "frontend/decoder/matcher.h"
17 
18 namespace Dynarmic::A32 {
19 
20 template <typename Visitor>
21 using ASIMDMatcher = Decoder::Matcher<Visitor, u32>;
22 
23 template <typename V>
GetASIMDDecodeTable()24 std::vector<ASIMDMatcher<V>> GetASIMDDecodeTable() {
25     std::vector<ASIMDMatcher<V>> table = {
26 
27 #define INST(fn, name, bitstring) Decoder::detail::detail<ASIMDMatcher<V>>::GetMatcher(&V::fn, name, bitstring),
28 #include "asimd.inc"
29 #undef INST
30 
31     };
32 
33     // If a matcher has more bits in its mask it is more specific, so it should come first.
34     std::stable_sort(table.begin(), table.end(), [](const auto& matcher1, const auto& matcher2) {
35         return Common::BitCount(matcher1.GetMask()) > Common::BitCount(matcher2.GetMask());
36     });
37 
38     return table;
39 }
40 
41 template<typename V>
DecodeASIMD(u32 instruction)42 std::optional<std::reference_wrapper<const ASIMDMatcher<V>>> DecodeASIMD(u32 instruction) {
43     static const auto table = GetASIMDDecodeTable<V>();
44 
45     const auto matches_instruction = [instruction](const auto& matcher) { return matcher.Matches(instruction); };
46 
47     auto iter = std::find_if(table.begin(), table.end(), matches_instruction);
48     return iter != table.end() ? std::optional<std::reference_wrapper<const ASIMDMatcher<V>>>(*iter) : std::nullopt;
49 }
50 
51 } // namespace Dynarmic::A32
52