Debian Codegraph @

Source/UnitTests/Core/PowerPC/Jit64Common/Frsqrte.cpp

raw symbols SCIP
1 // Copyright 2018 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include <bit>
5
6 #include "Common/CommonTypes.h"
7 #include "Common/FloatUtils.h"
8 #include "Common/ScopeGuard.h"
9 #include "Common/x64ABI.h"
10 #include "Core/Core.h"
11 #include "Core/PowerPC/Gekko.h"
12 #include "Core/PowerPC/Jit64/Jit.h"
13 #include "Core/PowerPC/Jit64Common/Jit64AsmCommon.h"
14 #include "Core/PowerPC/Jit64Common/Jit64PowerPCState.h"
15 #include "Core/System.h"
16
17 #include "../TestValues.h"
18
19 #include <gtest/gtest.h>
20
21 namespace
22 {
23 class TestCommonAsmRoutines : public CommonAsmRoutines
24 {
25 public:
26 explicit TestCommonAsmRoutines(Core::System& system) : CommonAsmRoutines(jit), jit(system)
27 {
28 using namespace Gen;
29
30 AllocCodeSpace(4096);
31 m_const_pool.Init(AllocChildCodeSpace(1024), 1024);
32
33 const auto raw_frsqrte = reinterpret_cast<double (*)(double)>(AlignCode4());
34 GenFrsqrte();
35
36 wrapped_frsqrte = reinterpret_cast<u64 (*)(u64, UReg_FPSCR&)>(AlignCode4());
37 ABI_PushRegistersAndAdjustStack(ABI_ALL_CALLEE_SAVED, 8, 16);
38
39 // We know that the only part of PowerPCState that the frsqrte routine accesses is the fpscr.
40 // We manufacture a PPCSTATE pointer so it reads/writes to our provided fpscr instead.
41 LEA(64, RPPCSTATE, MDisp(ABI_PARAM2, -PPCSTATE_OFF(fpscr)));
42
43 // Call
44 MOVQ_xmm(XMM0, R(ABI_PARAM1));
45 ABI_CallFunction(raw_frsqrte);
46 MOVQ_xmm(R(ABI_RETURN), XMM0);
47
48 ABI_PopRegistersAndAdjustStack(ABI_ALL_CALLEE_SAVED, 8, 16);
49 RET();
50 }
51
52 u64 (*wrapped_frsqrte)(u64, UReg_FPSCR&);
53 Jit64 jit;
54 };
55 } // namespace
56
57 TEST(Jit64, Frsqrte)
58 {
59 Core::DeclareAsCPUThread();
60 Common::ScopeGuard cpu_thread_guard([] { Core::UndeclareAsCPUThread(); });
61
62 const TestCommonAsmRoutines routines(Core::System::GetInstance());
63
64 UReg_FPSCR fpscr;
65
66 for (const u64 ivalue : double_test_values)
67 {
68 const double dvalue = std::bit_cast<double>(ivalue);
69
70 u64 expected = std::bit_cast<u64>(Common::ApproximateReciprocalSquareRoot(dvalue));
71
72 u64 actual = routines.wrapped_frsqrte(ivalue, fpscr);
73
74 if (expected != actual)
75 fmt::print("{:016x} -> {:016x} == {:016x}\n", ivalue, actual, expected);
76
77 EXPECT_EQ(expected, actual);
78 }
79 }