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/mantissa_util.h"
7 #include "common/fp/unpacked.h"
8 #include "common/u128.h"
9 
10 namespace Dynarmic::FP {
11 
12 constexpr size_t product_point_position = normalized_point_position * 2;
13 
ReduceMantissa(bool sign,int exponent,const u128 & mantissa)14 static FPUnpacked ReduceMantissa(bool sign, int exponent, const u128& mantissa) {
15     constexpr int point_position_correction = normalized_point_position - (product_point_position - 64);
16     // We round-to-odd here when reducing the bitwidth of the mantissa so that subsequent roundings are accurate.
17     return {sign, exponent + point_position_correction, mantissa.upper | static_cast<u64>(mantissa.lower != 0)};
18 }
19 
FusedMulAdd(FPUnpacked addend,FPUnpacked op1,FPUnpacked op2)20 FPUnpacked FusedMulAdd(FPUnpacked addend, FPUnpacked op1, FPUnpacked op2) {
21     const bool product_sign = op1.sign != op2.sign;
22     const auto [product_exponent, product_value] = [op1, op2]{
23         int exponent = op1.exponent + op2.exponent;
24         u128 value = Multiply64To128(op1.mantissa, op2.mantissa);
25         if (value.Bit<product_point_position + 1>()) {
26             value = value >> 1;
27             exponent++;
28         }
29         return std::make_tuple(exponent, value);
30     }();
31 
32     if (product_value == 0) {
33         return addend;
34     }
35 
36     if (addend.mantissa == 0) {
37         return ReduceMantissa(product_sign, product_exponent, product_value);
38     }
39 
40     const int exp_diff = product_exponent - addend.exponent;
41 
42     if (product_sign == addend.sign) {
43         // Addition
44 
45         if (exp_diff <= 0) {
46             // addend > product
47             const u64 result = addend.mantissa + StickyLogicalShiftRight(product_value, normalized_point_position - exp_diff).lower;
48             return FPUnpacked{addend.sign, addend.exponent, result};
49         }
50 
51         // addend < product
52         const u128 result = product_value + StickyLogicalShiftRight(addend.mantissa, exp_diff - normalized_point_position);
53         return ReduceMantissa(product_sign, product_exponent, result);
54     }
55 
56     // Subtraction
57 
58     const u128 addend_long = u128(addend.mantissa) << normalized_point_position;
59 
60     bool result_sign;
61     u128 result;
62     int result_exponent;
63 
64     if (exp_diff == 0 && product_value > addend_long) {
65         result_sign = product_sign;
66         result_exponent = product_exponent;
67         result = product_value - addend_long;
68     } else if (exp_diff <= 0) {
69         result_sign = !product_sign;
70         result_exponent = addend.exponent;
71         result = addend_long - StickyLogicalShiftRight(product_value, -exp_diff);
72     } else {
73         result_sign = product_sign;
74         result_exponent = product_exponent;
75         result = product_value - StickyLogicalShiftRight(addend_long, exp_diff);
76     }
77 
78     if (result.upper == 0) {
79         return FPUnpacked{result_sign, result_exponent, result.lower};
80     }
81 
82     const int required_shift = normalized_point_position - Common::HighestSetBit(result.upper);
83     result = result << required_shift;
84     result_exponent -= required_shift;
85     return ReduceMantissa(result_sign, result_exponent, result);
86 }
87 
88 } // namespace Dynarmic::FP
89