FloatUtils.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright 2018 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "Common/FloatUtils.h"
  4. #include <bit>
  5. #include <cmath>
  6. namespace Common
  7. {
  8. u32 ClassifyDouble(double dvalue)
  9. {
  10. const u64 ivalue = std::bit_cast<u64>(dvalue);
  11. const u64 sign = ivalue & DOUBLE_SIGN;
  12. const u64 exp = ivalue & DOUBLE_EXP;
  13. if (exp > DOUBLE_ZERO && exp < DOUBLE_EXP)
  14. {
  15. // Nice normalized number.
  16. return sign ? PPC_FPCLASS_NN : PPC_FPCLASS_PN;
  17. }
  18. const u64 mantissa = ivalue & DOUBLE_FRAC;
  19. if (mantissa)
  20. {
  21. if (exp)
  22. return PPC_FPCLASS_QNAN;
  23. // Denormalized number.
  24. return sign ? PPC_FPCLASS_ND : PPC_FPCLASS_PD;
  25. }
  26. if (exp)
  27. {
  28. // Infinite
  29. return sign ? PPC_FPCLASS_NINF : PPC_FPCLASS_PINF;
  30. }
  31. // Zero
  32. return sign ? PPC_FPCLASS_NZ : PPC_FPCLASS_PZ;
  33. }
  34. u32 ClassifyFloat(float fvalue)
  35. {
  36. const u32 ivalue = std::bit_cast<u32>(fvalue);
  37. const u32 sign = ivalue & FLOAT_SIGN;
  38. const u32 exp = ivalue & FLOAT_EXP;
  39. if (exp > FLOAT_ZERO && exp < FLOAT_EXP)
  40. {
  41. // Nice normalized number.
  42. return sign ? PPC_FPCLASS_NN : PPC_FPCLASS_PN;
  43. }
  44. const u32 mantissa = ivalue & FLOAT_FRAC;
  45. if (mantissa)
  46. {
  47. if (exp)
  48. return PPC_FPCLASS_QNAN; // Quiet NAN
  49. // Denormalized number.
  50. return sign ? PPC_FPCLASS_ND : PPC_FPCLASS_PD;
  51. }
  52. if (exp)
  53. {
  54. // Infinite
  55. return sign ? PPC_FPCLASS_NINF : PPC_FPCLASS_PINF;
  56. }
  57. // Zero
  58. return sign ? PPC_FPCLASS_NZ : PPC_FPCLASS_PZ;
  59. }
  60. const std::array<BaseAndDec, 32> frsqrte_expected = {{
  61. {0x1a7e800, -0x568}, {0x17cb800, -0x4f3}, {0x1552800, -0x48d}, {0x130c000, -0x435},
  62. {0x10f2000, -0x3e7}, {0x0eff000, -0x3a2}, {0x0d2e000, -0x365}, {0x0b7c000, -0x32e},
  63. {0x09e5000, -0x2fc}, {0x0867000, -0x2d0}, {0x06ff000, -0x2a8}, {0x05ab800, -0x283},
  64. {0x046a000, -0x261}, {0x0339800, -0x243}, {0x0218800, -0x226}, {0x0105800, -0x20b},
  65. {0x3ffa000, -0x7a4}, {0x3c29000, -0x700}, {0x38aa000, -0x670}, {0x3572000, -0x5f2},
  66. {0x3279000, -0x584}, {0x2fb7000, -0x524}, {0x2d26000, -0x4cc}, {0x2ac0000, -0x47e},
  67. {0x2881000, -0x43a}, {0x2665000, -0x3fa}, {0x2468000, -0x3c2}, {0x2287000, -0x38e},
  68. {0x20c1000, -0x35e}, {0x1f12000, -0x332}, {0x1d79000, -0x30a}, {0x1bf4000, -0x2e6},
  69. }};
  70. double ApproximateReciprocalSquareRoot(double val)
  71. {
  72. s64 integral = std::bit_cast<s64>(val);
  73. s64 mantissa = integral & ((1LL << 52) - 1);
  74. const s64 sign = integral & (1ULL << 63);
  75. s64 exponent = integral & (0x7FFLL << 52);
  76. // Special case 0
  77. if (mantissa == 0 && exponent == 0)
  78. {
  79. return sign ? -std::numeric_limits<double>::infinity() :
  80. std::numeric_limits<double>::infinity();
  81. }
  82. // Special case NaN-ish numbers
  83. if (exponent == (0x7FFLL << 52))
  84. {
  85. if (mantissa == 0)
  86. {
  87. if (sign)
  88. return std::numeric_limits<double>::quiet_NaN();
  89. return 0.0;
  90. }
  91. return 0.0 + val;
  92. }
  93. // Negative numbers return NaN
  94. if (sign)
  95. return std::numeric_limits<double>::quiet_NaN();
  96. if (!exponent)
  97. {
  98. // "Normalize" denormal values
  99. do
  100. {
  101. exponent -= 1LL << 52;
  102. mantissa <<= 1;
  103. } while (!(mantissa & (1LL << 52)));
  104. mantissa &= (1LL << 52) - 1;
  105. exponent += 1LL << 52;
  106. }
  107. const s64 exponent_lsb = exponent & (1LL << 52);
  108. exponent = ((0x3FFLL << 52) - ((exponent - (0x3FELL << 52)) / 2)) & (0x7FFLL << 52);
  109. integral = sign | exponent;
  110. const int i = static_cast<int>((exponent_lsb | mantissa) >> 37);
  111. const auto& entry = frsqrte_expected[i / 2048];
  112. integral |= static_cast<s64>(entry.m_base + entry.m_dec * (i % 2048)) << 26;
  113. return std::bit_cast<double>(integral);
  114. }
  115. const std::array<BaseAndDec, 32> fres_expected = {{
  116. {0x7ff800, 0x3e1}, {0x783800, 0x3a7}, {0x70ea00, 0x371}, {0x6a0800, 0x340}, {0x638800, 0x313},
  117. {0x5d6200, 0x2ea}, {0x579000, 0x2c4}, {0x520800, 0x2a0}, {0x4cc800, 0x27f}, {0x47ca00, 0x261},
  118. {0x430800, 0x245}, {0x3e8000, 0x22a}, {0x3a2c00, 0x212}, {0x360800, 0x1fb}, {0x321400, 0x1e5},
  119. {0x2e4a00, 0x1d1}, {0x2aa800, 0x1be}, {0x272c00, 0x1ac}, {0x23d600, 0x19b}, {0x209e00, 0x18b},
  120. {0x1d8800, 0x17c}, {0x1a9000, 0x16e}, {0x17ae00, 0x15b}, {0x14f800, 0x15b}, {0x124400, 0x143},
  121. {0x0fbe00, 0x143}, {0x0d3800, 0x12d}, {0x0ade00, 0x12d}, {0x088400, 0x11a}, {0x065000, 0x11a},
  122. {0x041c00, 0x108}, {0x020c00, 0x106},
  123. }};
  124. // Used by fres and ps_res.
  125. double ApproximateReciprocal(double val)
  126. {
  127. s64 integral = std::bit_cast<s64>(val);
  128. const s64 mantissa = integral & ((1LL << 52) - 1);
  129. const s64 sign = integral & (1ULL << 63);
  130. s64 exponent = integral & (0x7FFLL << 52);
  131. // Special case 0
  132. if (mantissa == 0 && exponent == 0)
  133. return std::copysign(std::numeric_limits<double>::infinity(), val);
  134. // Special case NaN-ish numbers
  135. if (exponent == (0x7FFLL << 52))
  136. {
  137. if (mantissa == 0)
  138. return std::copysign(0.0, val);
  139. return 0.0 + val;
  140. }
  141. // Special case small inputs
  142. if (exponent < (895LL << 52))
  143. return std::copysign(std::numeric_limits<float>::max(), val);
  144. // Special case large inputs
  145. if (exponent >= (1149LL << 52))
  146. return std::copysign(0.0, val);
  147. exponent = (0x7FDLL << 52) - exponent;
  148. const int i = static_cast<int>(mantissa >> 37);
  149. const auto& entry = fres_expected[i / 1024];
  150. integral = sign | exponent;
  151. integral |= static_cast<s64>(entry.m_base - (entry.m_dec * (i % 1024) + 1) / 2) << 29;
  152. return std::bit_cast<double>(integral);
  153. }
  154. } // namespace Common