FPToFixed.cpp
1 /* This file is part of the dynarmic project. 2 * Copyright (c) 2018 MerryMage 3 * SPDX-License-Identifier: 0BSD 4 */ 5 6 #include <tuple> 7 #include <vector> 8 9 #include <catch2/catch_test_macros.hpp> 10 #include <mcl/stdint.hpp> 11 12 #include "../rand_int.h" 13 #include "dynarmic/common/fp/fpcr.h" 14 #include "dynarmic/common/fp/fpsr.h" 15 #include "dynarmic/common/fp/op.h" 16 #include "dynarmic/common/fp/rounding_mode.h" 17 18 using namespace Dynarmic; 19 using namespace Dynarmic::FP; 20 21 TEST_CASE("FPToFixed", "[fp]") { 22 const std::vector<std::tuple<u32, size_t, u64, u32>> test_cases{ 23 {0x447A0000, 64, 0x000003E8, 0x00}, 24 {0xC47A0000, 32, 0xFFFFFC18, 0x00}, 25 {0x4479E000, 64, 0x000003E8, 0x10}, 26 {0x50800000, 32, 0x7FFFFFFF, 0x01}, 27 {0xD0800000, 32, 0x80000000, 0x01}, 28 {0xCF000000, 32, 0x80000000, 0x00}, 29 {0x80002B94, 64, 0x00000000, 0x10}, 30 {0x80636D24, 64, 0x00000000, 0x10}, 31 }; 32 33 const FPCR fpcr; 34 for (auto [input, ibits, expected_output, expected_fpsr] : test_cases) { 35 FPSR fpsr; 36 const u64 output = FPToFixed<u32>(ibits, input, 0, false, fpcr, RoundingMode::ToNearest_TieEven, fpsr); 37 REQUIRE(output == expected_output); 38 REQUIRE(fpsr.Value() == expected_fpsr); 39 } 40 } 41 42 TEST_CASE("FPToFixed edge cases", "[fp]") { 43 const std::vector<std::tuple<u64, u64, bool, FP::RoundingMode>> test_cases{ 44 {0x41dffffffffffffe, 0x7fffffff, false, FP::RoundingMode::ToNearest_TieEven}, 45 {0x41dffffffffffffe, 0x7fffffff, false, FP::RoundingMode::TowardsPlusInfinity}, 46 {0x41dffffffffffffe, 0x7fffffff, false, FP::RoundingMode::TowardsMinusInfinity}, 47 {0x41dffffffffffffe, 0x7fffffff, false, FP::RoundingMode::TowardsZero}, 48 {0x41dffffffffffffe, 0x7fffffff, false, FP::RoundingMode::ToNearest_TieAwayFromZero}, 49 }; 50 51 const FPCR fpcr; 52 FPSR fpsr; 53 for (auto [input, expected_output, unsigned_, rounding_mode] : test_cases) { 54 const u64 output = FPToFixed<u64>(32, input, 0, unsigned_, fpcr, rounding_mode, fpsr); 55 REQUIRE(output == expected_output); 56 } 57 }