1 /* This file is part of the dynarmic project.
2  * Copyright (c) 2018 MerryMage
3  * SPDX-License-Identifier: 0BSD
4  */
5 
6 #include "common/fp/fpcr.h"
7 #include "common/fp/fpsr.h"
8 #include "common/fp/fused.h"
9 #include "common/fp/info.h"
10 #include "common/fp/op/FPNeg.h"
11 #include "common/fp/op/FPRecipStepFused.h"
12 #include "common/fp/process_nan.h"
13 #include "common/fp/unpacked.h"
14 
15 namespace Dynarmic::FP {
16 
17 template<typename FPT>
FPRecipStepFused(FPT op1,FPT op2,FPCR fpcr,FPSR & fpsr)18 FPT FPRecipStepFused(FPT op1, FPT op2, FPCR fpcr, FPSR& fpsr) {
19     op1 = FPNeg(op1);
20 
21     const auto [type1, sign1, value1] = FPUnpack<FPT>(op1, fpcr, fpsr);
22     const auto [type2, sign2, value2] = FPUnpack<FPT>(op2, fpcr, fpsr);
23 
24     if (const auto maybe_nan = FPProcessNaNs(type1, type2, op1, op2, fpcr, fpsr)) {
25         return *maybe_nan;
26     }
27 
28     const bool inf1 = type1 == FPType::Infinity;
29     const bool inf2 = type2 == FPType::Infinity;
30     const bool zero1 = type1 == FPType::Zero;
31     const bool zero2 = type2 == FPType::Zero;
32 
33     if ((inf1 && zero2) || (zero1 && inf2)) {
34         // return +2.0
35         return FPValue<FPT, false, 0, 2>();
36     }
37 
38     if (inf1 || inf2) {
39         return FPInfo<FPT>::Infinity(sign1 != sign2);
40     }
41 
42     // result_value = 2.0 + (value1 * value2)
43     const FPUnpacked result_value = FusedMulAdd(ToNormalized(false, 0, 2), value1, value2);
44 
45     if (result_value.mantissa == 0) {
46         return FPInfo<FPT>::Zero(fpcr.RMode() == RoundingMode::TowardsMinusInfinity);
47     }
48     return FPRound<FPT>(result_value, fpcr, fpsr);
49 }
50 
51 template u16 FPRecipStepFused<u16>(u16 op1, u16 op2, FPCR fpcr, FPSR& fpsr);
52 template u32 FPRecipStepFused<u32>(u32 op1, u32 op2, FPCR fpcr, FPSR& fpsr);
53 template u64 FPRecipStepFused<u64>(u64 op1, u64 op2, FPCR fpcr, FPSR& fpsr);
54 
55 } // namespace Dynarmic::FP
56