| 1 | // Copyright 2021 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include <bit> |
| 5 | #include <functional> |
| 6 | #include <vector> |
| 7 | |
| 8 | #include "Common/Arm64Emitter.h" |
| 9 | #include "Common/CommonTypes.h" |
| 10 | #include "Common/ScopeGuard.h" |
| 11 | #include "Core/Core.h" |
| 12 | #include "Core/PowerPC/Interpreter/Interpreter_FPUtils.h" |
| 13 | #include "Core/PowerPC/JitArm64/Jit.h" |
| 14 | #include "Core/PowerPC/PowerPC.h" |
| 15 | #include "Core/System.h" |
| 16 | |
| 17 | #include "../TestValues.h" |
| 18 | |
| 19 | #include <gtest/gtest.h> |
| 20 | |
| 21 | namespace |
| 22 | { |
| 23 | using namespace Arm64Gen; |
| 24 | |
| 25 | class TestFPRF : public JitArm64 |
| 26 | { |
| 27 | public: |
| 28 | explicit TestFPRF(Core::System& system) : JitArm64(system) |
| 29 | { |
| 30 | const Common::ScopedJITPageWriteAndNoExecute enable_jit_page_writes; |
| 31 | |
| 32 | AllocCodeSpace(4096); |
| 33 | |
| 34 | const u8* raw_fprf_single = GetCodePtr(); |
| 35 | GenerateFPRF(true); |
| 36 | const u8* raw_fprf_double = GetCodePtr(); |
| 37 | GenerateFPRF(false); |
| 38 | |
| 39 | auto& ppc_state = system.GetPPCState(); |
| 40 | |
| 41 | fprf_single = std::bit_cast<void (*)(u32)>(GetCodePtr()); |
| 42 | MOV(ARM64Reg::X15, ARM64Reg::X30); |
| 43 | MOV(ARM64Reg::X14, PPC_REG); |
| 44 | MOVP2R(PPC_REG, &ppc_state); |
| 45 | BL(raw_fprf_single); |
| 46 | MOV(ARM64Reg::X30, ARM64Reg::X15); |
| 47 | MOV(PPC_REG, ARM64Reg::X14); |
| 48 | RET(); |
| 49 | |
| 50 | fprf_double = std::bit_cast<void (*)(u64)>(GetCodePtr()); |
| 51 | MOV(ARM64Reg::X15, ARM64Reg::X30); |
| 52 | MOV(ARM64Reg::X14, PPC_REG); |
| 53 | MOVP2R(PPC_REG, &ppc_state); |
| 54 | BL(raw_fprf_double); |
| 55 | MOV(ARM64Reg::X30, ARM64Reg::X15); |
| 56 | MOV(PPC_REG, ARM64Reg::X14); |
| 57 | RET(); |
| 58 | } |
| 59 | |
| 60 | std::function<void(u32)> fprf_single; |
| 61 | std::function<void(u64)> fprf_double; |
| 62 | }; |
| 63 | |
| 64 | } // namespace |
| 65 | |
| 66 | static u32 RunUpdateFPRF(PowerPC::PowerPCState& ppc_state, const std::function<void()>& f) |
| 67 | { |
| 68 | ppc_state.fpscr.Hex = 0x12345678; |
| 69 | f(); |
| 70 | return ppc_state.fpscr.Hex; |
| 71 | } |
| 72 | |
| 73 | TEST(JitArm64, FPRF) |
| 74 | { |
| 75 | Core::DeclareAsCPUThread(); |
| 76 | Common::ScopeGuard cpu_thread_guard([] { Core::UndeclareAsCPUThread(); }); |
| 77 | |
| 78 | auto& system = Core::System::GetInstance(); |
| 79 | auto& ppc_state = system.GetPPCState(); |
| 80 | TestFPRF test(system); |
| 81 | |
| 82 | for (const u64 double_input : double_test_values) |
| 83 | { |
| 84 | const u32 expected_double = RunUpdateFPRF( |
| 85 | ppc_state, [&] { ppc_state.UpdateFPRFDouble(std::bit_cast<double>(double_input)); }); |
| 86 | const u32 actual_double = RunUpdateFPRF(ppc_state, [&] { test.fprf_double(double_input); }); |
| 87 | if (expected_double != actual_double) |
| 88 | fmt::print("{:016x} -> {:08x} == {:08x}\n", double_input, actual_double, expected_double); |
| 89 | EXPECT_EQ(expected_double, actual_double); |
| 90 | |
| 91 | const u32 single_input = ConvertToSingle(double_input); |
| 92 | |
| 93 | const u32 expected_single = RunUpdateFPRF( |
| 94 | ppc_state, [&] { ppc_state.UpdateFPRFSingle(std::bit_cast<float>(single_input)); }); |
| 95 | const u32 actual_single = RunUpdateFPRF(ppc_state, [&] { test.fprf_single(single_input); }); |
| 96 | if (expected_single != actual_single) |
| 97 | fmt::print("{:08x} -> {:08x} == {:08x}\n", single_input, actual_single, expected_single); |
| 98 | EXPECT_EQ(expected_single, actual_single); |
| 99 | } |
| 100 | } |