ir: Add opcodes for unsigned reciprocal estimate

This commit is contained in:
Lioncash 2018-09-07 22:45:00 -04:00 committed by MerryMage
parent d46dea136f
commit af83360f89
8 changed files with 71 additions and 29 deletions

View file

@ -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

View file

@ -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<u32>& result, const VectorArray<u32>& 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<u8>& result, const VectorArray<u16>& a) {
bool qc_flag = false;

View file

@ -4,7 +4,6 @@
* General Public License version 2 or any later version.
*/
#include <array>
#include <tuple>
#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<u8, 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<u8>((b + 1) / 2);
}
return result;
}();
return lut[a - lut_offset];
}
template<typename FPT>
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<u64>(RecipEstimate(scaled)) << (FPInfo<FPT>::explicit_mantissa_width - 8);
u64 estimate = static_cast<u64>(Common::RecipEstimate(scaled)) << (FPInfo<FPT>::explicit_mantissa_width - 8);
int result_exponent = -(value.exponent + 1);
if (result_exponent < FPInfo<FPT>::exponent_min) {
switch (result_exponent) {

31
src/common/math_util.cpp Normal file
View file

@ -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 <array>
#include "common/math_util.h"
namespace Dynarmic::Common {
u8 RecipEstimate(u64 a) {
using LUT = std::array<u8, 256>;
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<u8>((b + 1) / 2);
}
return result;
}();
return lut[a - lut_offset];
}
} // namespace Dynarmic::Common

View file

@ -8,8 +8,9 @@
#include <utility>
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

View file

@ -1619,6 +1619,10 @@ U128 IREmitter::VectorUnsignedAbsoluteDifference(size_t esize, const U128& a, co
return {};
}
U128 IREmitter::VectorUnsignedRecipEstimate(const U128& a) {
return Inst<U128>(Opcode::VectorUnsignedRecipEstimate, a);
}
U128 IREmitter::VectorUnsignedSaturatedNarrow(size_t esize, const U128& a) {
switch (esize) {
case 16:

View file

@ -273,6 +273,7 @@ public:
Table VectorTable(std::vector<U128> 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);

View file

@ -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 )