mirror of
https://git.suyu.dev/suyu/dynarmic.git
synced 2026-03-10 14:06:27 +00:00
IR: Implement FPRSqrtEstimate
This commit is contained in:
@@ -7,4 +7,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "common/fp/op/FPRoundInt.h"
|
||||
#include "common/fp/op/FPRSqrtEstimate.h"
|
||||
#include "common/fp/op/FPToFixed.h"
|
||||
|
||||
83
src/common/fp/op/FPRSqrtEstimate.cpp
Normal file
83
src/common/fp/op/FPRSqrtEstimate.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
/* 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/assert.h"
|
||||
#include "common/bit_util.h"
|
||||
#include "common/common_types.h"
|
||||
#include "common/fp/fpcr.h"
|
||||
#include "common/fp/fpsr.h"
|
||||
#include "common/fp/info.h"
|
||||
#include "common/fp/op/FPRSqrtEstimate.h"
|
||||
#include "common/fp/process_exception.h"
|
||||
#include "common/fp/process_nan.h"
|
||||
#include "common/fp/unpacked.h"
|
||||
#include "common/safe_ops.h"
|
||||
|
||||
namespace Dynarmic::FP {
|
||||
|
||||
/// Input is a u1.8 fixed point number.
|
||||
static u8 RecipSqrtEstimate(u64 a) {
|
||||
static const std::array<u8, 512> lut = []{
|
||||
std::array<u8, 512> result{};
|
||||
for (u64 i = 128; i < 512; i++) {
|
||||
u64 a = i;
|
||||
if (a < 256) {
|
||||
a = a * 2 + 1;
|
||||
} else {
|
||||
a = (a | 1) * 2;
|
||||
}
|
||||
u64 b = 512;
|
||||
while (a * (b + 1) * (b + 1) < (1u << 28)) {
|
||||
b++;
|
||||
}
|
||||
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);
|
||||
|
||||
if (type == FPType::SNaN || type == FPType::QNaN) {
|
||||
return FPProcessNaN(type, op, fpcr, fpsr);
|
||||
}
|
||||
|
||||
if (type == FPType::Zero) {
|
||||
FPProcessException(FPExc::DivideByZero, fpcr, fpsr);
|
||||
return FPInfo<FPT>::Infinity(sign);
|
||||
}
|
||||
|
||||
if (sign) {
|
||||
FPProcessException(FPExc::InvalidOp, fpcr, fpsr);
|
||||
return FPInfo<FPT>::DefaultNaN();
|
||||
}
|
||||
|
||||
if (type == FPType::Infinity) {
|
||||
return FPInfo<FPT>::Zero(false);
|
||||
}
|
||||
|
||||
const int highest_bit = Common::HighestSetBit(value.mantissa);
|
||||
const int result_exponent = (-(value.exponent + highest_bit + 1)) >> 1;
|
||||
const bool was_exponent_odd = (value.exponent + highest_bit) % 2 == 0;
|
||||
|
||||
const u64 scaled = Safe::LogicalShiftRight(value.mantissa, highest_bit - (was_exponent_odd ? 7 : 8));
|
||||
const u64 estimate = 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));
|
||||
return (bits_exponent << FPInfo<FPT>::explicit_mantissa_width) | (bits_mantissa & FPInfo<FPT>::mantissa_mask);
|
||||
}
|
||||
|
||||
template u32 FPRSqrtEstimate<u32>(u32 op, FPCR fpcr, FPSR& fpsr);
|
||||
template u64 FPRSqrtEstimate<u64>(u64 op, FPCR fpcr, FPSR& fpsr);
|
||||
|
||||
} // namespace Dynarmic::FP
|
||||
19
src/common/fp/op/FPRSqrtEstimate.h
Normal file
19
src/common/fp/op/FPRSqrtEstimate.h
Normal file
@@ -0,0 +1,19 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "common/common_types.h"
|
||||
#include "common/fp/fpcr.h"
|
||||
#include "common/fp/fpsr.h"
|
||||
#include "common/fp/rounding_mode.h"
|
||||
|
||||
namespace Dynarmic::FP {
|
||||
|
||||
template<typename FPT>
|
||||
FPT FPRSqrtEstimate(FPT op, FPCR fpcr, FPSR& fpsr);
|
||||
|
||||
} // namespace Dynarmic::FP
|
||||
Reference in New Issue
Block a user