diff options
author | Dawid Potocki <dawid@dawidpotocki.com> | 2024-03-05 14:09:27 +1300 |
---|---|---|
committer | Dawid Potocki <dawid@dawidpotocki.com> | 2024-03-05 20:34:15 +1300 |
commit | 063e15900bda8453fb0fc6751e78d064501ccbae (patch) | |
tree | a4cd5f01dbca33a262333aff10e1e035217a30c8 /externals/dynarmic/tests/fp | |
parent | 537296095ab24eddcb196b5ef98004f91de9c8c2 (diff) |
Diffstat (limited to 'externals/dynarmic/tests/fp')
-rw-r--r-- | externals/dynarmic/tests/fp/FPToFixed.cpp | 57 | ||||
-rw-r--r-- | externals/dynarmic/tests/fp/FPValue.cpp | 15 | ||||
-rw-r--r-- | externals/dynarmic/tests/fp/mantissa_util_tests.cpp | 63 | ||||
-rw-r--r-- | externals/dynarmic/tests/fp/unpacked_tests.cpp | 95 |
4 files changed, 230 insertions, 0 deletions
diff --git a/externals/dynarmic/tests/fp/FPToFixed.cpp b/externals/dynarmic/tests/fp/FPToFixed.cpp new file mode 100644 index 0000000000..31a684a687 --- /dev/null +++ b/externals/dynarmic/tests/fp/FPToFixed.cpp @@ -0,0 +1,57 @@ +/* This file is part of the dynarmic project. + * Copyright (c) 2018 MerryMage + * SPDX-License-Identifier: 0BSD + */ + +#include <tuple> +#include <vector> + +#include <catch2/catch_test_macros.hpp> +#include <mcl/stdint.hpp> + +#include "../rand_int.h" +#include "dynarmic/common/fp/fpcr.h" +#include "dynarmic/common/fp/fpsr.h" +#include "dynarmic/common/fp/op.h" +#include "dynarmic/common/fp/rounding_mode.h" + +using namespace Dynarmic; +using namespace Dynarmic::FP; + +TEST_CASE("FPToFixed", "[fp]") { + const std::vector<std::tuple<u32, size_t, u64, u32>> test_cases{ + {0x447A0000, 64, 0x000003E8, 0x00}, + {0xC47A0000, 32, 0xFFFFFC18, 0x00}, + {0x4479E000, 64, 0x000003E8, 0x10}, + {0x50800000, 32, 0x7FFFFFFF, 0x01}, + {0xD0800000, 32, 0x80000000, 0x01}, + {0xCF000000, 32, 0x80000000, 0x00}, + {0x80002B94, 64, 0x00000000, 0x10}, + {0x80636D24, 64, 0x00000000, 0x10}, + }; + + const FPCR fpcr; + for (auto [input, ibits, expected_output, expected_fpsr] : test_cases) { + FPSR fpsr; + const u64 output = FPToFixed<u32>(ibits, input, 0, false, fpcr, RoundingMode::ToNearest_TieEven, fpsr); + REQUIRE(output == expected_output); + REQUIRE(fpsr.Value() == expected_fpsr); + } +} + +TEST_CASE("FPToFixed edge cases", "[fp]") { + const std::vector<std::tuple<u64, u64, bool, FP::RoundingMode>> test_cases{ + {0x41dffffffffffffe, 0x7fffffff, false, FP::RoundingMode::ToNearest_TieEven}, + {0x41dffffffffffffe, 0x7fffffff, false, FP::RoundingMode::TowardsPlusInfinity}, + {0x41dffffffffffffe, 0x7fffffff, false, FP::RoundingMode::TowardsMinusInfinity}, + {0x41dffffffffffffe, 0x7fffffff, false, FP::RoundingMode::TowardsZero}, + {0x41dffffffffffffe, 0x7fffffff, false, FP::RoundingMode::ToNearest_TieAwayFromZero}, + }; + + const FPCR fpcr; + FPSR fpsr; + for (auto [input, expected_output, unsigned_, rounding_mode] : test_cases) { + const u64 output = FPToFixed<u64>(32, input, 0, unsigned_, fpcr, rounding_mode, fpsr); + REQUIRE(output == expected_output); + } +} diff --git a/externals/dynarmic/tests/fp/FPValue.cpp b/externals/dynarmic/tests/fp/FPValue.cpp new file mode 100644 index 0000000000..b90f5f70f4 --- /dev/null +++ b/externals/dynarmic/tests/fp/FPValue.cpp @@ -0,0 +1,15 @@ +/* This file is part of the dynarmic project. + * Copyright (c) 2018 MerryMage + * SPDX-License-Identifier: 0BSD + */ + +#include "dynarmic/common/fp/info.h" + +using namespace Dynarmic::FP; + +static_assert(FPValue<u32, false, 0, 1>() == 0x3f800000); +static_assert(FPValue<u32, false, -1, 3>() == 0x3fc00000); +static_assert(FPValue<u32, false, 0, 12739812>() == 0x4b4264e4); +static_assert(FPValue<u32, false, -8, 100>() == 0x3ec80000); +static_assert(FPValue<u32, true, 0, 1>() == 0xbf800000); +static_assert(FPValue<u32, false, -1, 1>() == 0x3f000000); diff --git a/externals/dynarmic/tests/fp/mantissa_util_tests.cpp b/externals/dynarmic/tests/fp/mantissa_util_tests.cpp new file mode 100644 index 0000000000..cff942c73f --- /dev/null +++ b/externals/dynarmic/tests/fp/mantissa_util_tests.cpp @@ -0,0 +1,63 @@ +/* This file is part of the dynarmic project. + * Copyright (c) 2018 MerryMage + * SPDX-License-Identifier: 0BSD + */ + +#include <tuple> +#include <vector> + +#include <catch2/catch_test_macros.hpp> +#include <mcl/stdint.hpp> + +#include "../rand_int.h" +#include "dynarmic/common/fp/mantissa_util.h" +#include "dynarmic/common/safe_ops.h" + +using namespace Dynarmic; +using namespace Dynarmic::FP; + +TEST_CASE("ResidualErrorOnRightShift", "[fp]") { + const std::vector<std::tuple<u32, int, ResidualError>> test_cases{ + {0x00000001, 1, ResidualError::Half}, + {0x00000002, 1, ResidualError::Zero}, + {0x00000001, 2, ResidualError::LessThanHalf}, + {0x00000002, 2, ResidualError::Half}, + {0x00000003, 2, ResidualError::GreaterThanHalf}, + {0x00000004, 2, ResidualError::Zero}, + {0x00000005, 2, ResidualError::LessThanHalf}, + {0x00000006, 2, ResidualError::Half}, + {0x00000007, 2, ResidualError::GreaterThanHalf}, + }; + + for (auto [mantissa, shift, expected_result] : test_cases) { + const ResidualError result = ResidualErrorOnRightShift(mantissa, shift); + REQUIRE(result == expected_result); + } +} + +TEST_CASE("ResidualErrorOnRightShift Randomized", "[fp]") { + for (size_t test = 0; test < 100000; test++) { + const u64 mantissa = mcl::bit::sign_extend<32, u64>(RandInt<u32>(0, 0xFFFFFFFF)); + const int shift = RandInt<int>(-60, 60); + + const ResidualError result = ResidualErrorOnRightShift(mantissa, shift); + + const u64 calculated_error = Safe::ArithmeticShiftRightDouble(mantissa, u64(0), shift); + const ResidualError expected_result = [&] { + constexpr u64 half_error = 0x8000'0000'0000'0000ull; + if (calculated_error == 0) { + return ResidualError::Zero; + } + if (calculated_error < half_error) { + return ResidualError::LessThanHalf; + } + if (calculated_error == half_error) { + return ResidualError::Half; + } + return ResidualError::GreaterThanHalf; + }(); + + INFO(std::hex << "mantissa " << mantissa << " shift " << shift << " calculated_error " << calculated_error); + REQUIRE(result == expected_result); + } +} diff --git a/externals/dynarmic/tests/fp/unpacked_tests.cpp b/externals/dynarmic/tests/fp/unpacked_tests.cpp new file mode 100644 index 0000000000..827b85ac9e --- /dev/null +++ b/externals/dynarmic/tests/fp/unpacked_tests.cpp @@ -0,0 +1,95 @@ +/* This file is part of the dynarmic project. + * Copyright (c) 2018 MerryMage + * SPDX-License-Identifier: 0BSD + */ + +#include <tuple> +#include <vector> + +#include <catch2/catch_test_macros.hpp> +#include <mcl/stdint.hpp> + +#include "../rand_int.h" +#include "dynarmic/common/fp/fpcr.h" +#include "dynarmic/common/fp/fpsr.h" +#include "dynarmic/common/fp/unpacked.h" + +using namespace Dynarmic; +using namespace Dynarmic::FP; + +TEST_CASE("FPUnpack Tests", "[fp]") { + const static std::vector<std::tuple<u32, std::tuple<FPType, bool, FPUnpacked>, u32>> test_cases{ + {0x00000000, {FPType::Zero, false, ToNormalized(false, 0, 0)}, 0}, + {0x7F800000, {FPType::Infinity, false, ToNormalized(false, 1000000, 1)}, 0}, + {0xFF800000, {FPType::Infinity, true, ToNormalized(true, 1000000, 1)}, 0}, + {0x7F800001, {FPType::SNaN, false, ToNormalized(false, 0, 0)}, 0}, + {0xFF800001, {FPType::SNaN, true, ToNormalized(true, 0, 0)}, 0}, + {0x7FC00001, {FPType::QNaN, false, ToNormalized(false, 0, 0)}, 0}, + {0xFFC00001, {FPType::QNaN, true, ToNormalized(true, 0, 0)}, 0}, + {0x00000001, {FPType::Nonzero, false, ToNormalized(false, -149, 1)}, 0}, // Smallest single precision denormal is 2^-149. + {0x3F7FFFFF, {FPType::Nonzero, false, ToNormalized(false, -24, 0xFFFFFF)}, 0}, // 1.0 - epsilon + }; + + const FPCR fpcr; + for (const auto& [input, expected_output, expected_fpsr] : test_cases) { + FPSR fpsr; + const auto output = FPUnpack<u32>(input, fpcr, fpsr); + + INFO("Input: " << std::hex << input); + INFO("Output Sign: " << std::get<2>(output).sign); + INFO("Output Exponent: " << std::get<2>(output).exponent); + INFO("Output Mantissa: " << std::hex << std::get<2>(output).mantissa); + INFO("Expected Sign: " << std::get<2>(expected_output).sign); + INFO("Expected Exponent: " << std::get<2>(expected_output).exponent); + INFO("Expected Mantissa: " << std::hex << std::get<2>(expected_output).mantissa); + + REQUIRE(output == expected_output); + REQUIRE(fpsr.Value() == expected_fpsr); + } +} + +TEST_CASE("FPRound Tests", "[fp]") { + const static std::vector<std::tuple<u32, std::tuple<FPType, bool, FPUnpacked>, u32>> test_cases{ + {0x7F800000, {FPType::Infinity, false, ToNormalized(false, 1000000, 1)}, 0x14}, + {0xFF800000, {FPType::Infinity, true, ToNormalized(true, 1000000, 1)}, 0x14}, + {0x00000001, {FPType::Nonzero, false, ToNormalized(false, -149, 1)}, 0}, // Smallest single precision denormal is 2^-149. + {0x3F7FFFFF, {FPType::Nonzero, false, ToNormalized(false, -24, 0xFFFFFF)}, 0}, // 1.0 - epsilon + {0x3F800000, {FPType::Nonzero, false, ToNormalized(false, -28, 0xFFFFFFF)}, 0x10}, // rounds to 1.0 + }; + + const FPCR fpcr; + for (const auto& [expected_output, input, expected_fpsr] : test_cases) { + FPSR fpsr; + const auto output = FPRound<u32>(std::get<2>(input), fpcr, fpsr); + + INFO("Expected Output: " << std::hex << expected_output); + REQUIRE(output == expected_output); + REQUIRE(fpsr.Value() == expected_fpsr); + } +} + +TEST_CASE("FPUnpack<->FPRound Round-trip Tests", "[fp]") { + const FPCR fpcr; + for (size_t count = 0; count < 100000; count++) { + FPSR fpsr; + const u32 input = RandInt(0, 1) == 0 ? RandInt<u32>(0x00000001, 0x7F800000) : RandInt<u32>(0x80000001, 0xFF800000); + const auto intermediate = std::get<2>(FPUnpack<u32>(input, fpcr, fpsr)); + const u32 output = FPRound<u32>(intermediate, fpcr, fpsr); + + INFO("Count: " << count); + INFO("Intermediate Values: " << std::hex << intermediate.sign << ';' << intermediate.exponent << ';' << intermediate.mantissa); + REQUIRE(input == output); + } +} + +TEST_CASE("FPRound (near zero, round to posinf)", "[fp]") { + const FPUnpacked input = {false, -353, 0x0a98d25ace5b2000}; + + FPSR fpsr; + FPCR fpcr; + fpcr.RMode(RoundingMode::TowardsPlusInfinity); + + const u32 output = FPRound<u32>(input, fpcr, fpsr); + + REQUIRE(output == 0x00000001); +} |