2018-06-29 18:52:48 +01:00
|
|
|
/* This file is part of the dynarmic project.
|
|
|
|
* Copyright (c) 2018 MerryMage
|
2020-04-23 15:25:11 +01:00
|
|
|
* SPDX-License-Identifier: 0BSD
|
2018-06-29 18:52:48 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <tuple>
|
|
|
|
#include <vector>
|
|
|
|
|
2021-08-08 12:51:37 +01:00
|
|
|
#include <catch2/catch.hpp>
|
2022-04-19 15:36:26 +01:00
|
|
|
#include <mcl/stdint.hpp>
|
2018-06-29 18:52:48 +01:00
|
|
|
|
2021-05-19 17:28:35 +01:00
|
|
|
#include "../rand_int.h"
|
|
|
|
#include "dynarmic/common/fp/mantissa_util.h"
|
|
|
|
#include "dynarmic/common/safe_ops.h"
|
2018-06-29 18:52:48 +01:00
|
|
|
|
|
|
|
using namespace Dynarmic;
|
|
|
|
using namespace Dynarmic::FP;
|
|
|
|
|
|
|
|
TEST_CASE("ResidualErrorOnRightShift", "[fp]") {
|
2021-05-22 14:51:20 +01:00
|
|
|
const std::vector<std::tuple<u32, int, ResidualError>> test_cases{
|
2018-06-29 18:52:48 +01:00
|
|
|
{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++) {
|
2022-04-19 15:36:26 +01:00
|
|
|
const u64 mantissa = mcl::bit::sign_extend<32, u64>(RandInt<u32>(0, 0xFFFFFFFF));
|
2018-06-29 18:52:48 +01:00
|
|
|
const int shift = RandInt<int>(-60, 60);
|
|
|
|
|
|
|
|
const ResidualError result = ResidualErrorOnRightShift(mantissa, shift);
|
|
|
|
|
2018-07-23 11:52:37 +01:00
|
|
|
const u64 calculated_error = Safe::ArithmeticShiftRightDouble(mantissa, u64(0), shift);
|
2021-05-22 14:51:20 +01:00
|
|
|
const ResidualError expected_result = [&] {
|
2018-06-29 18:52:48 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|