mantissa_util_tests.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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/mantissa_util.h"
  11. #include "dynarmic/common/safe_ops.h"
  12. using namespace Dynarmic;
  13. using namespace Dynarmic::FP;
  14. TEST_CASE("ResidualErrorOnRightShift", "[fp]") {
  15. const std::vector<std::tuple<u32, int, ResidualError>> test_cases{
  16. {0x00000001, 1, ResidualError::Half},
  17. {0x00000002, 1, ResidualError::Zero},
  18. {0x00000001, 2, ResidualError::LessThanHalf},
  19. {0x00000002, 2, ResidualError::Half},
  20. {0x00000003, 2, ResidualError::GreaterThanHalf},
  21. {0x00000004, 2, ResidualError::Zero},
  22. {0x00000005, 2, ResidualError::LessThanHalf},
  23. {0x00000006, 2, ResidualError::Half},
  24. {0x00000007, 2, ResidualError::GreaterThanHalf},
  25. };
  26. for (auto [mantissa, shift, expected_result] : test_cases) {
  27. const ResidualError result = ResidualErrorOnRightShift(mantissa, shift);
  28. REQUIRE(result == expected_result);
  29. }
  30. }
  31. TEST_CASE("ResidualErrorOnRightShift Randomized", "[fp]") {
  32. for (size_t test = 0; test < 100000; test++) {
  33. const u64 mantissa = mcl::bit::sign_extend<32, u64>(RandInt<u32>(0, 0xFFFFFFFF));
  34. const int shift = RandInt<int>(-60, 60);
  35. const ResidualError result = ResidualErrorOnRightShift(mantissa, shift);
  36. const u64 calculated_error = Safe::ArithmeticShiftRightDouble(mantissa, u64(0), shift);
  37. const ResidualError expected_result = [&] {
  38. constexpr u64 half_error = 0x8000'0000'0000'0000ull;
  39. if (calculated_error == 0) {
  40. return ResidualError::Zero;
  41. }
  42. if (calculated_error < half_error) {
  43. return ResidualError::LessThanHalf;
  44. }
  45. if (calculated_error == half_error) {
  46. return ResidualError::Half;
  47. }
  48. return ResidualError::GreaterThanHalf;
  49. }();
  50. INFO(std::hex << "mantissa " << mantissa << " shift " << shift << " calculated_error " << calculated_error);
  51. REQUIRE(result == expected_result);
  52. }
  53. }