1//===-- Add and subtract IEEE 754 floating-point numbers --------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_ADD_SUB_H
10#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_ADD_SUB_H
11
12#include "hdr/fenv_macros.h"
13#include "src/__support/CPP/algorithm.h"
14#include "src/__support/CPP/bit.h"
15#include "src/__support/CPP/type_traits.h"
16#include "src/__support/FPUtil/BasicOperations.h"
17#include "src/__support/FPUtil/FEnvImpl.h"
18#include "src/__support/FPUtil/FPBits.h"
19#include "src/__support/FPUtil/cast.h"
20#include "src/__support/FPUtil/dyadic_float.h"
21#include "src/__support/FPUtil/rounding_mode.h"
22#include "src/__support/macros/attributes.h"
23#include "src/__support/macros/config.h"
24#include "src/__support/macros/optimization.h"
25
26namespace LIBC_NAMESPACE_DECL {
27namespace fputil::generic {
28
29template <bool IsSub, typename OutType, typename InType>
30LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_floating_point_v<OutType> &&
31 cpp::is_floating_point_v<InType> &&
32 sizeof(OutType) <= sizeof(InType),
33 OutType>
34add_or_sub(InType x, InType y) {
35 using OutFPBits = FPBits<OutType>;
36 using OutStorageType = typename OutFPBits::StorageType;
37 using InFPBits = FPBits<InType>;
38 using InStorageType = typename InFPBits::StorageType;
39
40 constexpr int GUARD_BITS_LEN = 3;
41 constexpr int RESULT_FRACTION_LEN = InFPBits::FRACTION_LEN + GUARD_BITS_LEN;
42 constexpr int RESULT_MANTISSA_LEN = RESULT_FRACTION_LEN + 1;
43
44 using DyadicFloat =
45 DyadicFloat<cpp::bit_ceil(value: static_cast<size_t>(RESULT_MANTISSA_LEN))>;
46
47 InFPBits x_bits(x);
48 InFPBits y_bits(y);
49
50 bool is_effectively_add = (x_bits.sign() == y_bits.sign()) != IsSub;
51
52 if (LIBC_UNLIKELY(x_bits.is_inf_or_nan() || y_bits.is_inf_or_nan() ||
53 x_bits.is_zero() || y_bits.is_zero())) {
54 if (x_bits.is_nan() || y_bits.is_nan()) {
55 if (x_bits.is_signaling_nan() || y_bits.is_signaling_nan())
56 raise_except_if_required(FE_INVALID);
57
58 if (x_bits.is_quiet_nan()) {
59 InStorageType x_payload = x_bits.get_mantissa();
60 x_payload >>= InFPBits::FRACTION_LEN - OutFPBits::FRACTION_LEN;
61 return OutFPBits::quiet_nan(x_bits.sign(),
62 static_cast<OutStorageType>(x_payload))
63 .get_val();
64 }
65
66 if (y_bits.is_quiet_nan()) {
67 InStorageType y_payload = y_bits.get_mantissa();
68 y_payload >>= InFPBits::FRACTION_LEN - OutFPBits::FRACTION_LEN;
69 return OutFPBits::quiet_nan(y_bits.sign(),
70 static_cast<OutStorageType>(y_payload))
71 .get_val();
72 }
73
74 return OutFPBits::quiet_nan().get_val();
75 }
76
77 if (x_bits.is_inf()) {
78 if (y_bits.is_inf()) {
79 if (!is_effectively_add) {
80 raise_except_if_required(FE_INVALID);
81 return OutFPBits::quiet_nan().get_val();
82 }
83
84 return OutFPBits::inf(x_bits.sign()).get_val();
85 }
86
87 return OutFPBits::inf(x_bits.sign()).get_val();
88 }
89
90 if (y_bits.is_inf()) {
91 if constexpr (IsSub)
92 return OutFPBits::inf(y_bits.sign().negate()).get_val();
93 else
94 return OutFPBits::inf(y_bits.sign()).get_val();
95 }
96
97 if (x_bits.is_zero()) {
98 if (y_bits.is_zero()) {
99 if (is_effectively_add)
100 return OutFPBits::zero(x_bits.sign()).get_val();
101#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
102 return OutFPBits::zero(Sign::POS).get_val();
103#else
104 switch (fputil::quick_get_round()) {
105 case FE_DOWNWARD:
106 return OutFPBits::zero(Sign::NEG).get_val();
107 default:
108 return OutFPBits::zero(Sign::POS).get_val();
109 }
110#endif // LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
111 }
112
113 if constexpr (cpp::is_same_v<InType, OutType>) {
114 OutFPBits out_y_bits(y);
115 if constexpr (IsSub)
116 out_y_bits.set_sign(out_y_bits.sign().negate());
117 return out_y_bits.get_val();
118 } else {
119
120#ifdef LIBC_USE_CONSTEXPR
121 InType tmp = y;
122#else
123 // volatile prevents Clang from converting tmp to OutType and then
124 // immediately back to InType before negating it, resulting in double
125 // rounding.
126 volatile InType tmp = y;
127#endif // LIBC_USE_CONSTEXPR
128 if constexpr (IsSub)
129 tmp = -tmp;
130 return cast<OutType>(tmp);
131 }
132 }
133
134 if (y_bits.is_zero())
135 return cast<OutType>(x);
136 }
137
138 InType x_abs = x_bits.abs().get_val();
139 InType y_abs = y_bits.abs().get_val();
140
141 if (x_abs == y_abs && !is_effectively_add) {
142#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
143 return OutFPBits::zero(Sign::POS).get_val();
144#else
145 switch (fputil::quick_get_round()) {
146 case FE_DOWNWARD:
147 return OutFPBits::zero(Sign::NEG).get_val();
148 default:
149 return OutFPBits::zero(Sign::POS).get_val();
150 }
151#endif // LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
152 }
153
154 Sign result_sign = Sign::POS;
155
156 if (x_abs > y_abs) {
157 result_sign = x_bits.sign();
158 } else if (x_abs < y_abs) {
159 result_sign = y_bits.sign();
160 if constexpr (IsSub)
161 result_sign = result_sign.negate();
162 } else if (is_effectively_add) {
163 result_sign = x_bits.sign();
164 }
165
166 InFPBits max_bits(cpp::max(x_abs, y_abs));
167 InFPBits min_bits(cpp::min(x_abs, y_abs));
168
169 InStorageType result_mant{};
170
171 if (max_bits.is_subnormal()) {
172 // min_bits must be subnormal too.
173
174 if (is_effectively_add)
175 result_mant = max_bits.get_mantissa() + min_bits.get_mantissa();
176 else
177 result_mant = max_bits.get_mantissa() - min_bits.get_mantissa();
178
179 result_mant <<= GUARD_BITS_LEN;
180 } else {
181 InStorageType max_mant = static_cast<InStorageType>(
182 max_bits.get_explicit_mantissa() << GUARD_BITS_LEN);
183 InStorageType min_mant = static_cast<InStorageType>(
184 min_bits.get_explicit_mantissa() << GUARD_BITS_LEN);
185
186 int alignment = (max_bits.get_biased_exponent() - max_bits.is_normal()) -
187 (min_bits.get_biased_exponent() - min_bits.is_normal());
188
189 InStorageType aligned_min_mant = static_cast<InStorageType>(
190 min_mant >> cpp::min(a: alignment, b: RESULT_MANTISSA_LEN));
191 bool aligned_min_mant_sticky{};
192
193 if (alignment <= GUARD_BITS_LEN)
194 aligned_min_mant_sticky = false;
195 else if (alignment > InFPBits::FRACTION_LEN + GUARD_BITS_LEN)
196 aligned_min_mant_sticky = true;
197 else
198 aligned_min_mant_sticky =
199 (static_cast<InStorageType>(
200 min_mant << (InFPBits::STORAGE_LEN - alignment))) != 0;
201
202 InStorageType min_mant_sticky =
203 static_cast<InStorageType>(static_cast<int>(aligned_min_mant_sticky));
204
205 if (is_effectively_add)
206 result_mant = max_mant + (aligned_min_mant | min_mant_sticky);
207 else
208 result_mant = max_mant - (aligned_min_mant | min_mant_sticky);
209 }
210
211 int result_exp = max_bits.get_explicit_exponent() - RESULT_FRACTION_LEN;
212 DyadicFloat result(result_sign, result_exp, result_mant);
213 return result.template as<OutType, /*ShouldSignalExceptions=*/true>();
214}
215
216template <typename OutType, typename InType>
217LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_floating_point_v<OutType> &&
218 cpp::is_floating_point_v<InType> &&
219 sizeof(OutType) <= sizeof(InType),
220 OutType>
221add(InType x, InType y) {
222 return add_or_sub</*IsSub=*/false, OutType>(x, y);
223}
224
225template <typename OutType, typename InType>
226LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_floating_point_v<OutType> &&
227 cpp::is_floating_point_v<InType> &&
228 sizeof(OutType) <= sizeof(InType),
229 OutType>
230sub(InType x, InType y) {
231 return add_or_sub</*IsSub=*/true, OutType>(x, y);
232}
233
234} // namespace fputil::generic
235} // namespace LIBC_NAMESPACE_DECL
236
237#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_ADD_SUB_H
238