From 622b60efd6b7d5639d709587b56471709541b11f Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 7 Sep 2018 22:45:00 -0400 Subject: [PATCH] ir: Add opcodes for unsigned reciprocal estimate --- src/CMakeLists.txt | 1 + src/backend/x64/emit_x64_vector.cpp | 17 +++++++++++++++ src/common/fp/op/FPRecipEstimate.cpp | 27 ++---------------------- src/common/math_util.cpp | 31 ++++++++++++++++++++++++++++ src/common/math_util.h | 18 ++++++++++++---- src/frontend/ir/ir_emitter.cpp | 4 ++++ src/frontend/ir/ir_emitter.h | 1 + src/frontend/ir/opcodes.inc | 1 + 8 files changed, 71 insertions(+), 29 deletions(-) create mode 100644 src/common/math_util.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d5347f46..5a62b2f9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -52,6 +52,7 @@ add_library(dynarmic common/llvm_disassemble.cpp common/llvm_disassemble.h common/macro_util.h + common/math_util.cpp common/math_util.h common/memory_pool.cpp common/memory_pool.h diff --git a/src/backend/x64/emit_x64_vector.cpp b/src/backend/x64/emit_x64_vector.cpp index e71bfec4..a800b164 100644 --- a/src/backend/x64/emit_x64_vector.cpp +++ b/src/backend/x64/emit_x64_vector.cpp @@ -16,6 +16,7 @@ #include "common/assert.h" #include "common/bit_util.h" #include "common/common_types.h" +#include "common/math_util.h" #include "common/mp/function_info.h" #include "frontend/ir/basic_block.h" #include "frontend/ir/microinstruction.h" @@ -3320,6 +3321,22 @@ void EmitX64::EmitVectorUnsignedAbsoluteDifference32(EmitContext& ctx, IR::Inst* EmitVectorUnsignedAbsoluteDifference(32, ctx, inst, code); } +void EmitX64::EmitVectorUnsignedRecipEstimate(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] & 0x80000000) == 0) { + result[i] = 0xFFFFFFFF; + continue; + } + + const u32 input = Common::Bits<23, 31>(a[i]); + const u32 estimate = Common::RecipEstimate(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/FPRecipEstimate.cpp b/src/common/fp/op/FPRecipEstimate.cpp index c809a9c6..95e02a07 100644 --- a/src/common/fp/op/FPRecipEstimate.cpp +++ b/src/common/fp/op/FPRecipEstimate.cpp @@ -4,7 +4,6 @@ * General Public License version 2 or any later version. */ -#include #include #include "common/assert.h" @@ -16,32 +15,10 @@ #include "common/fp/process_exception.h" #include "common/fp/process_nan.h" #include "common/fp/unpacked.h" +#include "common/math_util.h" namespace Dynarmic::FP { -constexpr u64 lut_offset = 256; - -/// Input is a u0.9 fixed point number. Only values in [0.5, 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 RecipEstimate(u64 a) { - using LUT = std::array; - - static const LUT lut = [] { - LUT result{}; - for (u64 i = 0; i < result.size(); i++) { - u64 a = i + lut_offset; - - a = a * 2 + 1; - u64 b = (1u << 19) / a; - result[i] = static_cast((b + 1) / 2); - } - return result; - }(); - - return lut[a - lut_offset]; -} - template FPT FPRecipEstimate(FPT op, FPCR fpcr, FPSR& fpsr) { FPType type; @@ -92,7 +69,7 @@ FPT FPRecipEstimate(FPT op, FPCR fpcr, FPSR& fpsr) { } const u64 scaled = value.mantissa >> (normalized_point_position - 8); - u64 estimate = static_cast(RecipEstimate(scaled)) << (FPInfo::explicit_mantissa_width - 8); + u64 estimate = static_cast(Common::RecipEstimate(scaled)) << (FPInfo::explicit_mantissa_width - 8); int result_exponent = -(value.exponent + 1); if (result_exponent < FPInfo::exponent_min) { switch (result_exponent) { diff --git a/src/common/math_util.cpp b/src/common/math_util.cpp new file mode 100644 index 00000000..ecd6bde6 --- /dev/null +++ b/src/common/math_util.cpp @@ -0,0 +1,31 @@ +/* This file is part of the dynarmic project. + * Copyright (c) 2018 MerryMage + * This software may be used and distributed according to the terms of the GNU + * General Public License version 2 or any later version. + */ + +#include +#include "common/math_util.h" + +namespace Dynarmic::Common { + +u8 RecipEstimate(u64 a) { + using LUT = std::array; + static constexpr u64 lut_offset = 256; + + static const LUT lut = [] { + LUT result{}; + for (u64 i = 0; i < result.size(); i++) { + u64 a = i + lut_offset; + + a = a * 2 + 1; + u64 b = (1u << 19) / a; + result[i] = static_cast((b + 1) / 2); + } + return result; + }(); + + return lut[a - lut_offset]; +} + +} // namespace Dynarmic::Common diff --git a/src/common/math_util.h b/src/common/math_util.h index 045e0259..02963440 100644 --- a/src/common/math_util.h +++ b/src/common/math_util.h @@ -8,8 +8,9 @@ #include -namespace Dynarmic { -namespace Common { +#include "common/common_types.h" + +namespace Dynarmic::Common { /** * This function is a workaround for a bug in MSVC 19.12 where fold expressions @@ -24,5 +25,14 @@ constexpr T Sum(T first, Ts&&... rest) { } } -} // namespace Common -} // namespace Dynarmic +/** + * Input is a u0.9 fixed point number. Only values in [0.5, 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 RecipEstimate() within the ARMv8 architecture reference manual + * for a general overview of the requirements of the algorithm. + */ +u8 RecipEstimate(u64 a); + +} // namespace Dynarmic::Common diff --git a/src/frontend/ir/ir_emitter.cpp b/src/frontend/ir/ir_emitter.cpp index a54038bf..1fbc64bc 100644 --- a/src/frontend/ir/ir_emitter.cpp +++ b/src/frontend/ir/ir_emitter.cpp @@ -1619,6 +1619,10 @@ U128 IREmitter::VectorUnsignedAbsoluteDifference(size_t esize, const U128& a, co return {}; } +U128 IREmitter::VectorUnsignedRecipEstimate(const U128& a) { + return Inst(Opcode::VectorUnsignedRecipEstimate, 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 7a732b14..124e6b82 100644 --- a/src/frontend/ir/ir_emitter.h +++ b/src/frontend/ir/ir_emitter.h @@ -273,6 +273,7 @@ public: Table VectorTable(std::vector values); 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 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 3e542c2e..31767dc4 100644 --- a/src/frontend/ir/opcodes.inc +++ b/src/frontend/ir/opcodes.inc @@ -418,6 +418,7 @@ OPCODE(VectorTableLookup, U128, U128, OPCODE(VectorUnsignedAbsoluteDifference8, U128, U128, U128 ) OPCODE(VectorUnsignedAbsoluteDifference16, U128, U128, U128 ) OPCODE(VectorUnsignedAbsoluteDifference32, U128, U128, U128 ) +OPCODE(VectorUnsignedRecipEstimate, U128, U128 ) OPCODE(VectorUnsignedSaturatedNarrow16, U128, U128 ) OPCODE(VectorUnsignedSaturatedNarrow32, U128, U128 ) OPCODE(VectorUnsignedSaturatedNarrow64, U128, U128 )