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