| 1 | // Copyright 2019 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "Common/CommonTypes.h" |
| 5 | #include "Common/ScopeGuard.h" |
| 6 | #include "Common/x64ABI.h" |
| 7 | #include "Core/Core.h" |
| 8 | #include "Core/PowerPC/Interpreter/Interpreter_FPUtils.h" |
| 9 | #include "Core/PowerPC/Jit64/Jit.h" |
| 10 | #include "Core/PowerPC/Jit64Common/Jit64AsmCommon.h" |
| 11 | #include "Core/PowerPC/Jit64Common/Jit64Constants.h" |
| 12 | #include "Core/System.h" |
| 13 | |
| 14 | #include "../TestValues.h" |
| 15 | |
| 16 | #include <gtest/gtest.h> |
| 17 | |
| 18 | namespace |
| 19 | { |
| 20 | class TestCommonAsmRoutines : public CommonAsmRoutines |
| 21 | { |
| 22 | public: |
| 23 | explicit TestCommonAsmRoutines(Core::System& system) : CommonAsmRoutines(jit), jit(system) |
| 24 | { |
| 25 | using namespace Gen; |
| 26 | |
| 27 | AllocCodeSpace(4096); |
| 28 | m_const_pool.Init(AllocChildCodeSpace(1024), 1024); |
| 29 | |
| 30 | const auto raw_cdts = reinterpret_cast<double (*)(double)>(AlignCode4()); |
| 31 | GenConvertDoubleToSingle(); |
| 32 | |
| 33 | wrapped_cdts = reinterpret_cast<u32 (*)(u64)>(AlignCode4()); |
| 34 | ABI_PushRegistersAndAdjustStack(ABI_ALL_CALLEE_SAVED, 8, 16); |
| 35 | |
| 36 | // Call |
| 37 | MOVQ_xmm(XMM0, R(ABI_PARAM1)); |
| 38 | ABI_CallFunction(raw_cdts); |
| 39 | MOV(32, R(ABI_RETURN), R(RSCRATCH)); |
| 40 | |
| 41 | ABI_PopRegistersAndAdjustStack(ABI_ALL_CALLEE_SAVED, 8, 16); |
| 42 | RET(); |
| 43 | } |
| 44 | |
| 45 | u32 (*wrapped_cdts)(u64); |
| 46 | Jit64 jit; |
| 47 | }; |
| 48 | } // namespace |
| 49 | |
| 50 | TEST(Jit64, ConvertDoubleToSingle) |
| 51 | { |
| 52 | Core::DeclareAsCPUThread(); |
| 53 | Common::ScopeGuard cpu_thread_guard([] { Core::UndeclareAsCPUThread(); }); |
| 54 | |
| 55 | const TestCommonAsmRoutines routines(Core::System::GetInstance()); |
| 56 | |
| 57 | for (const u64 input : double_test_values) |
| 58 | { |
| 59 | const u32 expected = ConvertToSingle(input); |
| 60 | const u32 actual = routines.wrapped_cdts(input); |
| 61 | |
| 62 | if (expected != actual) |
| 63 | fmt::print("{:016x} -> {:08x} == {:08x}\n", input, actual, expected); |
| 64 | |
| 65 | EXPECT_EQ(expected, actual); |
| 66 | } |
| 67 | } |