diff --git a/src/backend/x64/emit_x64_vector.cpp b/src/backend/x64/emit_x64_vector.cpp index a800b164..230b69ae 100644 --- a/src/backend/x64/emit_x64_vector.cpp +++ b/src/backend/x64/emit_x64_vector.cpp @@ -3337,6 +3337,22 @@ void EmitX64::EmitVectorUnsignedRecipEstimate(EmitContext& ctx, IR::Inst* inst) }); } +void EmitX64::EmitVectorUnsignedRecipSqrtEstimate(EmitContext& ctx, IR::Inst* inst) { + EmitOneArgumentFallback(code, ctx, inst, [](VectorArray& result, const VectorArray& a) { + for (size_t i = 0; i < result.size(); i++) { + if ((a[i] & 0xC0000000) == 0) { + result[i] = 0xFFFFFFFF; + continue; + } + + const u32 input = Common::Bits<23, 31>(a[i]); + const u32 estimate = Common::RecipSqrtEstimate(input); + + result[i] = (0b100000000 | estimate) << 23; + } + }); +} + void EmitX64::EmitVectorUnsignedSaturatedNarrow16(EmitContext& ctx, IR::Inst* inst) { EmitOneArgumentFallbackWithSaturation(code, ctx, inst, [](VectorArray& result, const VectorArray& a) { bool qc_flag = false; diff --git a/src/common/fp/op/FPRSqrtEstimate.cpp b/src/common/fp/op/FPRSqrtEstimate.cpp index 28e6d650..96dc3176 100644 --- a/src/common/fp/op/FPRSqrtEstimate.cpp +++ b/src/common/fp/op/FPRSqrtEstimate.cpp @@ -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; - - 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((b + 1) / 2); - } - return result; - }(); - - return lut[a & 0x1FF]; -} - template FPT FPRSqrtEstimate(FPT op, FPCR fpcr, FPSR& fpsr) { auto [type, sign, value] = FPUnpack(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(result_exponent + FPInfo::exponent_bias); const FPT bits_mantissa = static_cast(estimate << (FPInfo::explicit_mantissa_width - 8)); diff --git a/src/common/math_util.cpp b/src/common/math_util.cpp index ecd6bde6..702ab84d 100644 --- a/src/common/math_util.cpp +++ b/src/common/math_util.cpp @@ -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; + + 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((b + 1) / 2); + } + return result; + }(); + + return lut[a & 0x1FF]; +} + } // namespace Dynarmic::Common diff --git a/src/common/math_util.h b/src/common/math_util.h index 02963440..427b1325 100644 --- a/src/common/math_util.h +++ b/src/common/math_util.h @@ -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 diff --git a/src/frontend/ir/ir_emitter.cpp b/src/frontend/ir/ir_emitter.cpp index 1fbc64bc..9e83be06 100644 --- a/src/frontend/ir/ir_emitter.cpp +++ b/src/frontend/ir/ir_emitter.cpp @@ -1623,6 +1623,10 @@ U128 IREmitter::VectorUnsignedRecipEstimate(const U128& a) { return Inst(Opcode::VectorUnsignedRecipEstimate, a); } +U128 IREmitter::VectorUnsignedRecipSqrtEstimate(const U128& a) { + return Inst(Opcode::VectorUnsignedRecipSqrtEstimate, a); +} + U128 IREmitter::VectorUnsignedSaturatedNarrow(size_t esize, const U128& a) { switch (esize) { case 16: diff --git a/src/frontend/ir/ir_emitter.h b/src/frontend/ir/ir_emitter.h index 124e6b82..2d947391 100644 --- a/src/frontend/ir/ir_emitter.h +++ b/src/frontend/ir/ir_emitter.h @@ -274,6 +274,7 @@ public: U128 VectorTableLookup(const U128& defaults, const Table& table, const U128& indices); U128 VectorUnsignedAbsoluteDifference(size_t esize, const U128& a, const U128& b); U128 VectorUnsignedRecipEstimate(const U128& a); + U128 VectorUnsignedRecipSqrtEstimate(const U128& a); U128 VectorUnsignedSaturatedNarrow(size_t esize, const U128& a); U128 VectorZeroExtend(size_t original_esize, const U128& a); U128 VectorZeroUpper(const U128& a); diff --git a/src/frontend/ir/opcodes.inc b/src/frontend/ir/opcodes.inc index 31767dc4..e3a76411 100644 --- a/src/frontend/ir/opcodes.inc +++ b/src/frontend/ir/opcodes.inc @@ -419,6 +419,7 @@ OPCODE(VectorUnsignedAbsoluteDifference8, U128, U128, OPCODE(VectorUnsignedAbsoluteDifference16, U128, U128, U128 ) OPCODE(VectorUnsignedAbsoluteDifference32, U128, U128, U128 ) OPCODE(VectorUnsignedRecipEstimate, U128, U128 ) +OPCODE(VectorUnsignedRecipSqrtEstimate, U128, U128 ) OPCODE(VectorUnsignedSaturatedNarrow16, U128, U128 ) OPCODE(VectorUnsignedSaturatedNarrow32, U128, U128 ) OPCODE(VectorUnsignedSaturatedNarrow64, U128, U128 )