uint128.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2019 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <cstring>
  6. #include <utility>
  7. #ifdef _MSC_VER
  8. #include <intrin.h>
  9. #pragma intrinsic(__umulh)
  10. #pragma intrinsic(_umul128)
  11. #pragma intrinsic(_udiv128)
  12. #else
  13. #include <x86intrin.h>
  14. #endif
  15. #include "common/common_types.h"
  16. namespace Common {
  17. // This function multiplies 2 u64 values and divides it by a u64 value.
  18. [[nodiscard]] static inline u64 MultiplyAndDivide64(u64 a, u64 b, u64 d) {
  19. #ifdef _MSC_VER
  20. u128 r{};
  21. r[0] = _umul128(a, b, &r[1]);
  22. u64 remainder;
  23. #if _MSC_VER < 1923
  24. return udiv128(r[1], r[0], d, &remainder);
  25. #else
  26. return _udiv128(r[1], r[0], d, &remainder);
  27. #endif
  28. #else
  29. const u64 diva = a / d;
  30. const u64 moda = a % d;
  31. const u64 divb = b / d;
  32. const u64 modb = b % d;
  33. return diva * b + moda * divb + moda * modb / d;
  34. #endif
  35. }
  36. // This function multiplies 2 u64 values and produces a u128 value;
  37. [[nodiscard]] static inline u128 Multiply64Into128(u64 a, u64 b) {
  38. u128 result;
  39. #ifdef _MSC_VER
  40. result[0] = _umul128(a, b, &result[1]);
  41. #else
  42. unsigned __int128 tmp = a;
  43. tmp *= b;
  44. std::memcpy(&result, &tmp, sizeof(u128));
  45. #endif
  46. return result;
  47. }
  48. [[nodiscard]] static inline u64 GetFixedPoint64Factor(u64 numerator, u64 divisor) {
  49. #ifdef __SIZEOF_INT128__
  50. const auto base = static_cast<unsigned __int128>(numerator) << 64ULL;
  51. return static_cast<u64>(base / divisor);
  52. #elif defined(_M_X64) || defined(_M_ARM64)
  53. std::array<u64, 2> r = {0, numerator};
  54. u64 remainder;
  55. #if _MSC_VER < 1923
  56. return udiv128(r[1], r[0], divisor, &remainder);
  57. #else
  58. return _udiv128(r[1], r[0], divisor, &remainder);
  59. #endif
  60. #else
  61. // This one is bit more inaccurate.
  62. return MultiplyAndDivide64(std::numeric_limits<u64>::max(), numerator, divisor);
  63. #endif
  64. }
  65. [[nodiscard]] static inline u64 MultiplyHigh(u64 a, u64 b) {
  66. #ifdef __SIZEOF_INT128__
  67. return (static_cast<unsigned __int128>(a) * static_cast<unsigned __int128>(b)) >> 64;
  68. #elif defined(_M_X64) || defined(_M_ARM64)
  69. return __umulh(a, b); // MSVC
  70. #else
  71. // Generic fallback
  72. const u64 a_lo = u32(a);
  73. const u64 a_hi = a >> 32;
  74. const u64 b_lo = u32(b);
  75. const u64 b_hi = b >> 32;
  76. const u64 a_x_b_hi = a_hi * b_hi;
  77. const u64 a_x_b_mid = a_hi * b_lo;
  78. const u64 b_x_a_mid = b_hi * a_lo;
  79. const u64 a_x_b_lo = a_lo * b_lo;
  80. const u64 carry_bit = (static_cast<u64>(static_cast<u32>(a_x_b_mid)) +
  81. static_cast<u64>(static_cast<u32>(b_x_a_mid)) + (a_x_b_lo >> 32)) >>
  82. 32;
  83. const u64 multhi = a_x_b_hi + (a_x_b_mid >> 32) + (b_x_a_mid >> 32) + carry_bit;
  84. return multhi;
  85. #endif
  86. }
  87. // This function divides a u128 by a u32 value and produces two u64 values:
  88. // the result of division and the remainder
  89. [[nodiscard]] static inline std::pair<u64, u64> Divide128On32(u128 dividend, u32 divisor) {
  90. u64 remainder = dividend[0] % divisor;
  91. u64 accum = dividend[0] / divisor;
  92. if (dividend[1] == 0)
  93. return {accum, remainder};
  94. // We ignore dividend[1] / divisor as that overflows
  95. const u64 first_segment = (dividend[1] % divisor) << 32;
  96. accum += (first_segment / divisor) << 32;
  97. const u64 second_segment = (first_segment % divisor) << 32;
  98. accum += (second_segment / divisor);
  99. remainder += second_segment % divisor;
  100. if (remainder >= divisor) {
  101. accum++;
  102. remainder -= divisor;
  103. }
  104. return {accum, remainder};
  105. }
  106. } // namespace Common