Debian Codegraph @

Source/UnitTests/Core/PowerPC/JitArm64/MovI2R.cpp

raw symbols SCIP
1 // Copyright 2021 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include <bit>
5 #include <cstddef>
6 #include <random>
7 #include <type_traits>
8
9 #include "Common/Arm64Emitter.h"
10 #include "Common/Assert.h"
11
12 #include <gtest/gtest.h>
13
14 namespace
15 {
16 using namespace Arm64Gen;
17
18 class TestMovI2R : public ARM64CodeBlock
19 {
20 public:
21 TestMovI2R() { AllocCodeSpace(4096); }
22
23 void Check32(u32 value)
24 {
25 ResetCodePtr();
26
27 const u8* fn = GetCodePtr();
28 {
29 const Common::ScopedJITPageWriteAndNoExecute enable_jit_page_writes;
30 MOVI2R(ARM64Reg::W0, value);
31 RET();
32 }
33
34 FlushIcacheSection(const_cast<u8*>(fn), const_cast<u8*>(GetCodePtr()));
35
36 const u64 result = std::bit_cast<u64 (*)()>(fn)();
37 EXPECT_EQ(value, result);
38 }
39
40 void Check64(u64 value)
41 {
42 ResetCodePtr();
43
44 const u8* fn = GetCodePtr();
45 {
46 const Common::ScopedJITPageWriteAndNoExecute enable_jit_page_writes;
47 MOVI2R(ARM64Reg::X0, value);
48 RET();
49 }
50
51 FlushIcacheSection(const_cast<u8*>(fn), const_cast<u8*>(GetCodePtr()));
52
53 const u64 result = std::bit_cast<u64 (*)()>(fn)();
54 EXPECT_EQ(value, result);
55 }
56 };
57
58 } // namespace
59
60 TEST(JitArm64, MovI2R_32BitValues)
61 {
62 std::default_random_engine engine(0);
63 std::uniform_int_distribution<u32> dist;
64 TestMovI2R test;
65 for (u64 i = 0; i < 0x100000; i++)
66 {
67 const u32 value = dist(engine);
68 test.Check32(value);
69 test.Check64(value);
70 }
71 }
72
73 TEST(JitArm64, MovI2R_Rand)
74 {
75 std::default_random_engine engine(0);
76 std::uniform_int_distribution<u64> dist;
77 TestMovI2R test;
78 for (u64 i = 0; i < 0x100000; i++)
79 {
80 test.Check64(dist(engine));
81 }
82 }
83
84 // Construct and test every 64-bit logical immediate
85 TEST(JitArm64, MovI2R_LogImm)
86 {
87 TestMovI2R test;
88
89 for (unsigned size = 2; size <= 64; size *= 2)
90 {
91 for (unsigned length = 1; length < size; ++length)
92 {
93 u64 imm = ~u64{0} >> (64 - length);
94 for (unsigned e = size; e < 64; e *= 2)
95 {
96 imm |= imm << e;
97 }
98 for (unsigned rotation = 0; rotation < size; ++rotation)
99 {
100 test.Check64(imm);
101 EXPECT_EQ(static_cast<bool>(LogicalImm(imm, GPRSize::B64)), true);
102
103 if (size < 64)
104 {
105 test.Check32(imm);
106 EXPECT_EQ(static_cast<bool>(LogicalImm(static_cast<u32>(imm), GPRSize::B32)), true);
107 }
108
109 imm = (imm >> 63) | (imm << 1);
110 }
111 }
112 }
113 }
114
115 TEST(JitArm64, MovI2R_ADP)
116 {
117 TestMovI2R test;
118 const u64 base = std::bit_cast<u64>(test.GetCodePtr());
119
120 // Test offsets around 0
121 for (s64 i = -0x20000; i < 0x20000; i++)
122 {
123 const u64 offset = static_cast<u64>(i);
124 test.Check64(base + offset);
125 }
126
127 // Test offsets around the maximum
128 for (const s64 i : {-0x200000ll, 0x200000ll})
129 {
130 for (s64 j = -4; j < 4; j++)
131 {
132 const u64 offset = static_cast<u64>(i + j);
133 test.Check64(base + offset);
134 }
135 }
136 }
137
138 TEST(JitArm64, MovI2R_ADRP)
139 {
140 TestMovI2R test;
141 const u64 base = std::bit_cast<u64>(test.GetCodePtr()) & ~0xFFF;
142
143 // Test offsets around 0
144 for (s64 i = -0x20000; i < 0x20000; i++)
145 {
146 const u64 offset = static_cast<u64>(i) << 12;
147 test.Check64(base + offset);
148 }
149
150 // Test offsets around the maximum
151 for (const s64 i : {-0x100000000ll, -0x80000000ll, 0x80000000ll, 0x100000000ll})
152 {
153 for (s64 j = -4; j < 4; j++)
154 {
155 const u64 offset = static_cast<u64>(i + (j << 12));
156 test.Check64(base + offset);
157 }
158 }
159 }