| 1 | // Copyright 2021 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include <functional> |
| 5 | |
| 6 | #include <bit> |
| 7 | |
| 8 | #include "Common/Arm64Emitter.h" |
| 9 | #include "Common/CommonTypes.h" |
| 10 | #include "Common/FPURoundMode.h" |
| 11 | #include "Common/ScopeGuard.h" |
| 12 | #include "Core/Core.h" |
| 13 | #include "Core/PowerPC/Interpreter/Interpreter_FPUtils.h" |
| 14 | #include "Core/PowerPC/JitArm64/Jit.h" |
| 15 | #include "Core/System.h" |
| 16 | |
| 17 | #include "../TestValues.h" |
| 18 | |
| 19 | #include <fmt/format.h> |
| 20 | #include <gtest/gtest.h> |
| 21 | |
| 22 | namespace |
| 23 | { |
| 24 | using namespace Arm64Gen; |
| 25 | |
| 26 | // The ABI situation for returning an std::tuple seems annoying. Let's use this struct instead |
| 27 | template <typename T> |
| 28 | struct Pair |
| 29 | { |
| 30 | T value1; |
| 31 | T value2; |
| 32 | }; |
| 33 | |
| 34 | class TestConversion : private JitArm64 |
| 35 | { |
| 36 | public: |
| 37 | explicit TestConversion(Core::System& system) : JitArm64(system) |
| 38 | { |
| 39 | const Common::ScopedJITPageWriteAndNoExecute enable_jit_page_writes; |
| 40 | |
| 41 | AllocCodeSpace(4096); |
| 42 | AddChildCodeSpace(&m_far_code, 2048); |
| 43 | |
| 44 | gpr.Init(this); |
| 45 | fpr.Init(this); |
| 46 | |
| 47 | js.fpr_is_store_safe = BitSet32(0); |
| 48 | |
| 49 | GetAsmRoutines()->cdts = GetCodePtr(); |
| 50 | GenerateConvertDoubleToSingle(); |
| 51 | GetAsmRoutines()->cstd = GetCodePtr(); |
| 52 | GenerateConvertSingleToDouble(); |
| 53 | |
| 54 | gpr.Lock(ARM64Reg::W30); |
| 55 | fpr.Lock(ARM64Reg::Q0, ARM64Reg::Q1); |
| 56 | |
| 57 | convert_single_to_double_lower = std::bit_cast<u64 (*)(u32)>(GetCodePtr()); |
| 58 | m_float_emit.INS(32, ARM64Reg::S0, 0, ARM64Reg::W0); |
| 59 | ConvertSingleToDoubleLower(0, ARM64Reg::D0, ARM64Reg::S0, ARM64Reg::Q1); |
| 60 | m_float_emit.UMOV(64, ARM64Reg::X0, ARM64Reg::D0, 0); |
| 61 | RET(); |
| 62 | |
| 63 | convert_single_to_double_pair = std::bit_cast<Pair<u64> (*)(u32, u32)>(GetCodePtr()); |
| 64 | m_float_emit.INS(32, ARM64Reg::D0, 0, ARM64Reg::W0); |
| 65 | m_float_emit.INS(32, ARM64Reg::D0, 1, ARM64Reg::W1); |
| 66 | ConvertSingleToDoublePair(0, ARM64Reg::Q0, ARM64Reg::D0, ARM64Reg::Q1); |
| 67 | m_float_emit.UMOV(64, ARM64Reg::X0, ARM64Reg::Q0, 0); |
| 68 | m_float_emit.UMOV(64, ARM64Reg::X1, ARM64Reg::Q0, 1); |
| 69 | RET(); |
| 70 | |
| 71 | convert_double_to_single_lower = std::bit_cast<u32 (*)(u64)>(GetCodePtr()); |
| 72 | m_float_emit.INS(64, ARM64Reg::D0, 0, ARM64Reg::X0); |
| 73 | ConvertDoubleToSingleLower(0, ARM64Reg::S0, ARM64Reg::D0); |
| 74 | m_float_emit.UMOV(32, ARM64Reg::W0, ARM64Reg::S0, 0); |
| 75 | RET(); |
| 76 | |
| 77 | convert_double_to_single_pair = std::bit_cast<Pair<u32> (*)(u64, u64)>(GetCodePtr()); |
| 78 | m_float_emit.INS(64, ARM64Reg::Q0, 0, ARM64Reg::X0); |
| 79 | m_float_emit.INS(64, ARM64Reg::Q0, 1, ARM64Reg::X1); |
| 80 | ConvertDoubleToSinglePair(0, ARM64Reg::D0, ARM64Reg::Q0); |
| 81 | m_float_emit.UMOV(64, ARM64Reg::X0, ARM64Reg::D0, 0); |
| 82 | RET(); |
| 83 | |
| 84 | gpr.Unlock(ARM64Reg::W30); |
| 85 | fpr.Unlock(ARM64Reg::Q0, ARM64Reg::Q1); |
| 86 | |
| 87 | FlushIcache(); |
| 88 | |
| 89 | // Set the rounding mode to something that's as annoying as possible to handle |
| 90 | // (flush-to-zero enabled, and rounding not symmetric about the origin) |
| 91 | Common::FPU::SetSIMDMode(Common::FPU::RoundMode::ROUND_UP, true); |
| 92 | } |
| 93 | |
| 94 | ~TestConversion() override |
| 95 | { |
| 96 | Common::FPU::LoadDefaultSIMDState(); |
| 97 | |
| 98 | FreeCodeSpace(); |
| 99 | } |
| 100 | |
| 101 | u64 ConvertSingleToDouble(const u32 value) const { return convert_single_to_double_lower(value); } |
| 102 | |
| 103 | Pair<u64> ConvertSingleToDouble(const u32 value1, const u32 value2) const |
| 104 | { |
| 105 | return convert_single_to_double_pair(value1, value2); |
| 106 | } |
| 107 | |
| 108 | u32 ConvertDoubleToSingle(const u64 value) const { return convert_double_to_single_lower(value); } |
| 109 | |
| 110 | Pair<u32> ConvertDoubleToSingle(const u64 value1, const u64 value2) const |
| 111 | { |
| 112 | return convert_double_to_single_pair(value1, value2); |
| 113 | } |
| 114 | |
| 115 | private: |
| 116 | std::function<u64(u32)> convert_single_to_double_lower; |
| 117 | std::function<Pair<u64>(u32, u32)> convert_single_to_double_pair; |
| 118 | std::function<u32(u64)> convert_double_to_single_lower; |
| 119 | std::function<Pair<u32>(u64, u64)> convert_double_to_single_pair; |
| 120 | }; |
| 121 | |
| 122 | } // namespace |
| 123 | |
| 124 | TEST(JitArm64, ConvertDoubleToSingle) |
| 125 | { |
| 126 | Core::DeclareAsCPUThread(); |
| 127 | Common::ScopeGuard cpu_thread_guard([] { Core::UndeclareAsCPUThread(); }); |
| 128 | |
| 129 | TestConversion test(Core::System::GetInstance()); |
| 130 | |
| 131 | for (const u64 input : double_test_values) |
| 132 | { |
| 133 | const u32 expected = ConvertToSingle(input); |
| 134 | const u32 actual = test.ConvertDoubleToSingle(input); |
| 135 | |
| 136 | if (expected != actual) |
| 137 | fmt::print("{:016x} -> {:08x} == {:08x}\n", input, actual, expected); |
| 138 | |
| 139 | EXPECT_EQ(expected, actual); |
| 140 | } |
| 141 | |
| 142 | for (const u64 input1 : double_test_values) |
| 143 | { |
| 144 | for (const u64 input2 : double_test_values) |
| 145 | { |
| 146 | const u32 expected1 = ConvertToSingle(input1); |
| 147 | const u32 expected2 = ConvertToSingle(input2); |
| 148 | const auto [actual1, actual2] = test.ConvertDoubleToSingle(input1, input2); |
| 149 | |
| 150 | if (expected1 != actual1 || expected2 != actual2) |
| 151 | { |
| 152 | fmt::print("{:016x} -> {:08x} == {:08x},\n", input1, actual1, expected1); |
| 153 | fmt::print("{:016x} -> {:08x} == {:08x}\n", input2, actual2, expected2); |
| 154 | } |
| 155 | |
| 156 | EXPECT_EQ(expected1, actual1); |
| 157 | EXPECT_EQ(expected2, actual2); |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | TEST(JitArm64, ConvertSingleToDouble) |
| 163 | { |
| 164 | Core::DeclareAsCPUThread(); |
| 165 | Common::ScopeGuard cpu_thread_guard([] { Core::UndeclareAsCPUThread(); }); |
| 166 | |
| 167 | TestConversion test(Core::System::GetInstance()); |
| 168 | |
| 169 | for (const u32 input : single_test_values) |
| 170 | { |
| 171 | const u64 expected = ConvertToDouble(input); |
| 172 | const u64 actual = test.ConvertSingleToDouble(input); |
| 173 | |
| 174 | if (expected != actual) |
| 175 | fmt::print("{:08x} -> {:016x} == {:016x}\n", input, actual, expected); |
| 176 | |
| 177 | EXPECT_EQ(expected, actual); |
| 178 | } |
| 179 | |
| 180 | for (const u32 input1 : single_test_values) |
| 181 | { |
| 182 | for (const u32 input2 : single_test_values) |
| 183 | { |
| 184 | const u64 expected1 = ConvertToDouble(input1); |
| 185 | const u64 expected2 = ConvertToDouble(input2); |
| 186 | const auto [actual1, actual2] = test.ConvertSingleToDouble(input1, input2); |
| 187 | |
| 188 | if (expected1 != actual1 || expected2 != actual2) |
| 189 | { |
| 190 | fmt::print("{:08x} -> {:016x} == {:016x},\n", input1, actual1, expected1); |
| 191 | fmt::print("{:08x} -> {:016x} == {:016x}\n", input2, actual2, expected2); |
| 192 | } |
| 193 | |
| 194 | EXPECT_EQ(expected1, actual1); |
| 195 | EXPECT_EQ(expected2, actual2); |
| 196 | } |
| 197 | } |
| 198 | } |