FPToFixed.cpp 2.0 KB

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