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 // This prevents it from declaring InType as volatile for emulated types
124 using InTypeTemp = cpp::conditional_t<cpp::is_class_v<InType>, InType,
125 volatile InType>;
126 InTypeTemp tmp = y;
127 // volatile prevents Clang from converting tmp to OutType and then
128 // immediately back to InType before negating it, resulting in double
129 // rounding.
130#endif // LIBC_USE_CONSTEXPR
131 if constexpr (IsSub)
132 tmp = -tmp;
133 return cast<OutType>(tmp);
134 }
135 }
136
137 if (y_bits.is_zero())
138 return cast<OutType>(x);
139 }
140
141 InType x_abs = x_bits.abs().get_val();
142 InType y_abs = y_bits.abs().get_val();
143
144 if (x_abs == y_abs && !is_effectively_add) {
145#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
146 return OutFPBits::zero(Sign::POS).get_val();
147#else
148 switch (fputil::quick_get_round()) {
149 case FE_DOWNWARD:
150 return OutFPBits::zero(Sign::NEG).get_val();
151 default:
152 return OutFPBits::zero(Sign::POS).get_val();
153 }
154#endif // LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
155 }
156
157 Sign result_sign = Sign::POS;
158
159 if (x_abs > y_abs) {
160 result_sign = x_bits.sign();
161 } else if (x_abs < y_abs) {
162 result_sign = y_bits.sign();
163 if constexpr (IsSub)
164 result_sign = result_sign.negate();
165 } else if (is_effectively_add) {
166 result_sign = x_bits.sign();
167 }
168
169 InFPBits max_bits(cpp::max(x_abs, y_abs));
170 InFPBits min_bits(cpp::min(x_abs, y_abs));
171
172 InStorageType result_mant{};
173
174 if (max_bits.is_subnormal()) {
175 // min_bits must be subnormal too.
176
177 if (is_effectively_add)
178 result_mant = max_bits.get_mantissa() + min_bits.get_mantissa();
179 else
180 result_mant = max_bits.get_mantissa() - min_bits.get_mantissa();
181
182 result_mant <<= GUARD_BITS_LEN;
183 } else {
184 InStorageType max_mant = static_cast<InStorageType>(
185 max_bits.get_explicit_mantissa() << GUARD_BITS_LEN);
186 InStorageType min_mant = static_cast<InStorageType>(
187 min_bits.get_explicit_mantissa() << GUARD_BITS_LEN);
188
189 int alignment = (max_bits.get_biased_exponent() - max_bits.is_normal()) -
190 (min_bits.get_biased_exponent() - min_bits.is_normal());
191
192 InStorageType aligned_min_mant = static_cast<InStorageType>(
193 min_mant >> cpp::min(a: alignment, b: RESULT_MANTISSA_LEN));
194 bool aligned_min_mant_sticky{};
195
196 if (alignment <= GUARD_BITS_LEN)
197 aligned_min_mant_sticky = false;
198 else if (alignment > InFPBits::FRACTION_LEN + GUARD_BITS_LEN)
199 aligned_min_mant_sticky = true;
200 else
201 aligned_min_mant_sticky =
202 (static_cast<InStorageType>(
203 min_mant << (InFPBits::STORAGE_LEN - alignment))) != 0;
204
205 InStorageType min_mant_sticky =
206 static_cast<InStorageType>(static_cast<int>(aligned_min_mant_sticky));
207
208 if (is_effectively_add)
209 result_mant = max_mant + (aligned_min_mant | min_mant_sticky);
210 else
211 result_mant = max_mant - (aligned_min_mant | min_mant_sticky);
212 }
213
214 int result_exp = max_bits.get_explicit_exponent() - RESULT_FRACTION_LEN;
215 DyadicFloat result(result_sign, result_exp, result_mant);
216 return result.template as<OutType, /*ShouldSignalExceptions=*/true>();
217}
218
219template <typename OutType, typename InType>
220LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_floating_point_v<OutType> &&
221 cpp::is_floating_point_v<InType> &&
222 sizeof(OutType) <= sizeof(InType),
223 OutType>
224add(InType x, InType y) {
225 return add_or_sub</*IsSub=*/false, OutType>(x, y);
226}
227
228template <typename OutType, typename InType>
229LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_floating_point_v<OutType> &&
230 cpp::is_floating_point_v<InType> &&
231 sizeof(OutType) <= sizeof(InType),
232 OutType>
233sub(InType x, InType y) {
234 return add_or_sub</*IsSub=*/true, OutType>(x, y);
235}
236
237} // namespace fputil::generic
238} // namespace LIBC_NAMESPACE_DECL
239
240#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_ADD_SUB_H
241