aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/fp/mantissa_util_tests.cpp
blob: cff942c73f6d07e10a1c2c9735ea7926a7ad4657 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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);
    }
}