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