ir: Add opcodes for performing unsigned reciprocal square root estimates

This commit is contained in:
Lioncash 2018-09-07 23:41:05 -04:00 committed by MerryMage
parent bd3582e811
commit b6e74fd17d
7 changed files with 71 additions and 38 deletions

View file

@ -16,47 +16,11 @@
#include "common/fp/process_exception.h"
#include "common/fp/process_nan.h"
#include "common/fp/unpacked.h"
#include "common/math_util.h"
#include "common/safe_ops.h"
namespace Dynarmic::FP {
/// Input is a u0.9 fixed point number. Only values in [0.25, 1.0) are valid.
/// Output is a u0.8 fixed point number, with an implied 1 prefixed.
/// i.e.: The output is a value in [1.0, 2.0).
static u8 RecipSqrtEstimate(u64 a) {
using LUT = std::array<u8, 512>;
static const LUT lut = [] {
LUT result{};
for (u64 i = 128; i < result.size(); i++) {
u64 a = i;
// Convert to u.10 (with 8 significant bits), force to odd
if (a < 256) {
// [0.25, 0.5)
a = a * 2 + 1;
} else {
// [0.5, 1.0)
a = (a | 1) * 2;
}
// Calculate largest b which for which b < 1.0 / sqrt(a).
// Start from b = 1.0 (in u.9) since b cannot be smaller.
u64 b = 512;
// u.10 * u.9 * u.9 -> u.28
while (a * (b + 1) * (b + 1) < (1u << 28)) {
b++;
}
// Round to nearest u0.8 (with implied set integer bit).
result[i] = static_cast<u8>((b + 1) / 2);
}
return result;
}();
return lut[a & 0x1FF];
}
template<typename FPT>
FPT FPRSqrtEstimate(FPT op, FPCR fpcr, FPSR& fpsr) {
auto [type, sign, value] = FPUnpack<FPT>(op, fpcr, fpsr);
@ -83,7 +47,7 @@ FPT FPRSqrtEstimate(FPT op, FPCR fpcr, FPSR& fpsr) {
const bool was_exponent_odd = (value.exponent) % 2 == 0;
const u64 scaled = Safe::LogicalShiftRight(value.mantissa, normalized_point_position - (was_exponent_odd ? 7 : 8));
const u64 estimate = RecipSqrtEstimate(scaled);
const u64 estimate = Common::RecipSqrtEstimate(scaled);
const FPT bits_exponent = static_cast<FPT>(result_exponent + FPInfo<FPT>::exponent_bias);
const FPT bits_mantissa = static_cast<FPT>(estimate << (FPInfo<FPT>::explicit_mantissa_width - 8));

View file

@ -28,4 +28,41 @@ u8 RecipEstimate(u64 a) {
return lut[a - lut_offset];
}
/// Input is a u0.9 fixed point number. Only values in [0.25, 1.0) are valid.
/// Output is a u0.8 fixed point number, with an implied 1 prefixed.
/// i.e.: The output is a value in [1.0, 2.0).
u8 RecipSqrtEstimate(u64 a) {
using LUT = std::array<u8, 512>;
static const LUT lut = [] {
LUT result{};
for (u64 i = 128; i < result.size(); i++) {
u64 a = i;
// Convert to u.10 (with 8 significant bits), force to odd
if (a < 256) {
// [0.25, 0.5)
a = a * 2 + 1;
} else {
// [0.5, 1.0)
a = (a | 1) * 2;
}
// Calculate largest b which for which b < 1.0 / sqrt(a).
// Start from b = 1.0 (in u.9) since b cannot be smaller.
u64 b = 512;
// u.10 * u.9 * u.9 -> u.28
while (a * (b + 1) * (b + 1) < (1u << 28)) {
b++;
}
// Round to nearest u0.8 (with implied set integer bit).
result[i] = static_cast<u8>((b + 1) / 2);
}
return result;
}();
return lut[a & 0x1FF];
}
} // namespace Dynarmic::Common

View file

@ -35,4 +35,14 @@ constexpr T Sum(T first, Ts&&... rest) {
*/
u8 RecipEstimate(u64 a);
/**
* Input is a u0.9 fixed point number. Only values in [0.25, 1.0) are valid.
* Output is a u0.8 fixed point number, with an implied 1 prefixed.
* i.e.: The output is a value in [1.0, 2.0).
*
* @see RecipSqrtEstimate() within the ARMv8 architecture reference manual
* for a general overview of the requirements of the algorithm.
*/
u8 RecipSqrtEstimate(u64 a);
} // namespace Dynarmic::Common