1//===-- Basic operations on 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_BASICOPERATIONS_H
10#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_BASICOPERATIONS_H
11
12#include "FEnvImpl.h"
13#include "FPBits.h"
14#include "dyadic_float.h"
15
16#include "src/__support/CPP/type_traits.h"
17#include "src/__support/big_int.h"
18#include "src/__support/common.h"
19#include "src/__support/macros/config.h"
20#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
21#include "src/__support/macros/properties/architectures.h"
22#include "src/__support/macros/properties/types.h"
23#include "src/__support/uint128.h"
24
25namespace LIBC_NAMESPACE_DECL {
26namespace fputil {
27
28template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
29LIBC_INLINE constexpr T abs(T x) {
30 return FPBits<T>(x).abs().get_val();
31}
32
33namespace internal {
34
35template <typename T>
36LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_floating_point_v<T>, T>
37constexpr_max(T x, T y) {
38 FPBits<T> x_bits(x);
39 FPBits<T> y_bits(y);
40
41 // To make sure that fmax(+0, -0) == +0 == fmax(-0, +0), whenever x and y
42 // have different signs and both are not NaNs, we return the number with
43 // positive sign.
44 if (x_bits.sign() != y_bits.sign())
45 return x_bits.is_pos() ? x : y;
46 return x > y ? x : y;
47}
48
49template <typename T>
50LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_floating_point_v<T>, T>
51max(T x, T y) {
52 return constexpr_max(x, y);
53}
54
55#ifdef LIBC_TYPES_HAS_FLOAT16
56#if defined(__LIBC_USE_BUILTIN_FMAXF16_FMINF16)
57template <> LIBC_INLINE constexpr float16 max(float16 x, float16 y) {
58 if (cpp::is_constant_evaluated())
59 return constexpr_max(x, y);
60 return __builtin_fmaxf16(x, y);
61}
62#elif !defined(LIBC_TARGET_ARCH_IS_AARCH64)
63template <> LIBC_INLINE constexpr float16 max(float16 x, float16 y) {
64 FPBits<float16> x_bits(x);
65 FPBits<float16> y_bits(y);
66
67 int16_t xi = static_cast<int16_t>(x_bits.uintval());
68 int16_t yi = static_cast<int16_t>(y_bits.uintval());
69 return ((xi > yi) != (xi < 0 && yi < 0)) ? x : y;
70}
71#endif
72#endif // LIBC_TYPES_HAS_FLOAT16
73
74#if defined(__LIBC_USE_BUILTIN_FMAX_FMIN) && !defined(LIBC_TARGET_ARCH_IS_X86)
75template <> LIBC_INLINE constexpr float max(float x, float y) {
76 if (cpp::is_constant_evaluated())
77 return constexpr_max(x, y);
78 return __builtin_fmaxf(x, y);
79}
80
81template <> LIBC_INLINE constexpr double max(double x, double y) {
82 if (cpp::is_constant_evaluated())
83 return constexpr_max(x, y);
84 return __builtin_fmax(x, y);
85}
86#endif
87
88template <typename T>
89LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_floating_point_v<T>, T>
90constexpr_min(T x, T y) {
91 FPBits<T> x_bits(x);
92 FPBits<T> y_bits(y);
93
94 // To make sure that fmin(+0, -0) == -0 == fmin(-0, +0), whenever x and y have
95 // different signs and both are not NaNs, we return the number with negative
96 // sign.
97 if (x_bits.sign() != y_bits.sign())
98 return x_bits.is_neg() ? x : y;
99 return x < y ? x : y;
100}
101
102template <typename T>
103LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_floating_point_v<T>, T>
104min(T x, T y) {
105 return constexpr_min(x, y);
106}
107
108#ifdef LIBC_TYPES_HAS_FLOAT16
109#if defined(__LIBC_USE_BUILTIN_FMAXF16_FMINF16)
110template <> LIBC_INLINE constexpr float16 min(float16 x, float16 y) {
111 if (cpp::is_constant_evaluated())
112 return constexpr_min(x, y);
113 return __builtin_fminf16(x, y);
114}
115#elif !defined(LIBC_TARGET_ARCH_IS_AARCH64)
116template <> LIBC_INLINE constexpr float16 min(float16 x, float16 y) {
117 FPBits<float16> x_bits(x);
118 FPBits<float16> y_bits(y);
119
120 int16_t xi = static_cast<int16_t>(x_bits.uintval());
121 int16_t yi = static_cast<int16_t>(y_bits.uintval());
122 return ((xi < yi) != (xi < 0 && yi < 0)) ? x : y;
123}
124#endif
125#endif // LIBC_TYPES_HAS_FLOAT16
126
127#if defined(__LIBC_USE_BUILTIN_FMAX_FMIN) && !defined(LIBC_TARGET_ARCH_IS_X86)
128template <> LIBC_INLINE constexpr float min(float x, float y) {
129 if (cpp::is_constant_evaluated())
130 return constexpr_min(x, y);
131 return __builtin_fminf(x, y);
132}
133
134template <> LIBC_INLINE constexpr double min(double x, double y) {
135 if (cpp::is_constant_evaluated())
136 return constexpr_min(x, y);
137 return __builtin_fmin(x, y);
138}
139#endif
140
141} // namespace internal
142
143template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
144LIBC_INLINE constexpr T fmin(T x, T y) {
145 const FPBits<T> bitx(x), bity(y);
146
147 if (bitx.is_nan())
148 return y;
149 if (bity.is_nan())
150 return x;
151 return internal::min(x, y);
152}
153
154template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
155LIBC_INLINE constexpr T fmax(T x, T y) {
156 FPBits<T> bitx(x), bity(y);
157
158 if (bitx.is_nan())
159 return y;
160 if (bity.is_nan())
161 return x;
162 return internal::max(x, y);
163}
164
165template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
166LIBC_INLINE constexpr T fmaximum(T x, T y) {
167 FPBits<T> bitx(x), bity(y);
168
169 if (bitx.is_nan())
170 return x;
171 if (bity.is_nan())
172 return y;
173 return internal::max(x, y);
174}
175
176template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
177LIBC_INLINE constexpr T fminimum(T x, T y) {
178 const FPBits<T> bitx(x), bity(y);
179
180 if (bitx.is_nan())
181 return x;
182 if (bity.is_nan())
183 return y;
184 return internal::min(x, y);
185}
186
187template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
188LIBC_INLINE constexpr T fmaximum_num(T x, T y) {
189 FPBits<T> bitx(x), bity(y);
190 if (bitx.is_signaling_nan() || bity.is_signaling_nan()) {
191 fputil::raise_except_if_required(FE_INVALID);
192 if (bitx.is_nan() && bity.is_nan())
193 return FPBits<T>::quiet_nan().get_val();
194 }
195 if (bitx.is_nan())
196 return y;
197 if (bity.is_nan())
198 return x;
199 return internal::max(x, y);
200}
201
202template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
203LIBC_INLINE constexpr T fminimum_num(T x, T y) {
204 FPBits<T> bitx(x), bity(y);
205 if (bitx.is_signaling_nan() || bity.is_signaling_nan()) {
206 fputil::raise_except_if_required(FE_INVALID);
207 if (bitx.is_nan() && bity.is_nan())
208 return FPBits<T>::quiet_nan().get_val();
209 }
210 if (bitx.is_nan())
211 return y;
212 if (bity.is_nan())
213 return x;
214 return internal::min(x, y);
215}
216
217template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
218LIBC_INLINE constexpr T fmaximum_mag(T x, T y) {
219 FPBits<T> bitx(x), bity(y);
220
221 if (abs(x) > abs(y))
222 return x;
223 if (abs(y) > abs(x))
224 return y;
225 return fmaximum(x, y);
226}
227
228template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
229LIBC_INLINE constexpr T fminimum_mag(T x, T y) {
230 FPBits<T> bitx(x), bity(y);
231
232 if (abs(x) < abs(y))
233 return x;
234 if (abs(y) < abs(x))
235 return y;
236 return fminimum(x, y);
237}
238
239template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
240LIBC_INLINE constexpr T fmaximum_mag_num(T x, T y) {
241 FPBits<T> bitx(x), bity(y);
242
243 if (abs(x) > abs(y))
244 return x;
245 if (abs(y) > abs(x))
246 return y;
247 return fmaximum_num(x, y);
248}
249
250template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
251LIBC_INLINE constexpr T fminimum_mag_num(T x, T y) {
252 FPBits<T> bitx(x), bity(y);
253
254 if (abs(x) < abs(y))
255 return x;
256 if (abs(y) < abs(x))
257 return y;
258 return fminimum_num(x, y);
259}
260
261template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
262LIBC_INLINE T constexpr fdim(T x, T y) {
263 FPBits<T> bitx(x), bity(y);
264
265 if (bitx.is_nan()) {
266 return x;
267 }
268
269 if (bity.is_nan()) {
270 return y;
271 }
272
273 return (x > y ? x - y : T(0));
274}
275
276// Avoid reusing `issignaling` macro.
277template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
278LIBC_INLINE constexpr int issignaling_impl(const T &x) {
279 FPBits<T> sx(x);
280 return sx.is_signaling_nan();
281}
282
283template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
284LIBC_INLINE constexpr int iscanonical_impl(const T &x) {
285 if constexpr (get_fp_type<T>() == FPType::X86_Binary80) {
286 FPBits<T> sx(x);
287 // The explicit integer bit is set exactly when the exponent is nonzero.
288 return sx.get_implicit_bit() == (sx.get_biased_exponent() != 0);
289 }
290 return 1;
291}
292
293template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
294LIBC_INLINE constexpr int canonicalize(T &cx, const T &x) {
295 FPBits<T> sx(x);
296 if constexpr (get_fp_type<T>() == FPType::X86_Binary80) {
297 // All the pseudo and unnormal numbers are not canonical.
298 // More precisely :
299 // Exponent | Significand | Meaning
300 // | Bits 63-62 | Bits 61-0 |
301 // All Ones | 00 | Zero | Pseudo Infinity, Value = SNaN
302 // All Ones | 00 | Non-Zero | Pseudo NaN, Value = SNaN
303 // All Ones | 01 | Anything | Pseudo NaN, Value = SNaN
304 // | Bit 63 | Bits 62-0 |
305 // All zeroes | One | Anything | Pseudo Denormal, Value =
306 // | | | (−1)**s × m × 2**−16382
307 // All Other | Zero | Anything | Unnormal, Value = SNaN
308 // Values | | |
309 bool bit63 = sx.get_implicit_bit();
310 UInt128 mantissa = sx.get_explicit_mantissa();
311 bool bit62 = static_cast<bool>((mantissa & (1ULL << 62)) >> 62);
312 int exponent = sx.get_biased_exponent();
313 if (exponent == 0x7FFF) {
314 if (!bit63 && !bit62) {
315 if (mantissa == 0) {
316 cx = FPBits<T>::quiet_nan(sx.sign(), mantissa).get_val();
317 raise_except_if_required(FE_INVALID);
318 return 1;
319 }
320 cx = FPBits<T>::quiet_nan(sx.sign(), mantissa).get_val();
321 raise_except_if_required(FE_INVALID);
322 return 1;
323 } else if (!bit63 && bit62) {
324 cx = FPBits<T>::quiet_nan(sx.sign(), mantissa).get_val();
325 raise_except_if_required(FE_INVALID);
326 return 1;
327 } else if (LIBC_UNLIKELY(sx.is_signaling_nan())) {
328 cx = FPBits<T>::quiet_nan(sx.sign(), sx.get_explicit_mantissa())
329 .get_val();
330 raise_except_if_required(FE_INVALID);
331 return 0;
332 } else
333 cx = x;
334 } else if (exponent == 0 && bit63)
335 cx = FPBits<T>::make_value(mantissa, 0).get_val();
336 else if (exponent != 0 && !bit63) {
337 cx = FPBits<T>::quiet_nan(sx.sign(), mantissa).get_val();
338 raise_except_if_required(FE_INVALID);
339 return 1;
340 } else if (LIBC_UNLIKELY(sx.is_signaling_nan())) {
341 cx =
342 FPBits<T>::quiet_nan(sx.sign(), sx.get_explicit_mantissa()).get_val();
343 raise_except_if_required(FE_INVALID);
344 return 0;
345 } else
346 cx = x;
347 } else if (LIBC_UNLIKELY(sx.is_signaling_nan())) {
348 cx = FPBits<T>::quiet_nan(sx.sign(), sx.get_explicit_mantissa()).get_val();
349 raise_except_if_required(FE_INVALID);
350 return 0;
351 } else
352 cx = x;
353 return 0;
354}
355
356template <typename T>
357LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_floating_point_v<T>, bool>
358totalorder(T x, T y) {
359 using FPBits = FPBits<T>;
360 FPBits x_bits(x);
361 FPBits y_bits(y);
362
363 using StorageType = typename FPBits::StorageType;
364 StorageType x_u = x_bits.uintval();
365 StorageType y_u = y_bits.uintval();
366
367 bool has_neg = ((x_u | y_u) & FPBits::SIGN_MASK) != 0;
368 return x_u == y_u || ((x_u < y_u) != has_neg);
369}
370
371template <typename T>
372LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_floating_point_v<T>, bool>
373totalordermag(T x, T y) {
374 return FPBits<T>(x).abs().uintval() <= FPBits<T>(y).abs().uintval();
375}
376
377template <typename T>
378LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_floating_point_v<T>, T>
379getpayload(T x) {
380 using FPBits = FPBits<T>;
381 using StorageType = typename FPBits::StorageType;
382 FPBits x_bits(x);
383
384 if (!x_bits.is_nan())
385 return T(-1.0);
386
387 StorageType payload = x_bits.uintval() & (FPBits::FRACTION_MASK >> 1);
388
389 if constexpr (is_big_int_v<StorageType>) {
390 DyadicFloat<FPBits::STORAGE_LEN> payload_dfloat(Sign::POS, 0, payload);
391
392 return static_cast<T>(payload_dfloat);
393 } else {
394 return static_cast<T>(payload);
395 }
396}
397
398template <bool IsSignaling, typename T>
399LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_floating_point_v<T>, bool>
400setpayload(T &res, T pl) {
401 using FPBits = FPBits<T>;
402 FPBits pl_bits(pl);
403
404 // Signaling NaNs don't have the mantissa's MSB set to 1, so they need a
405 // non-zero payload to distinguish them from infinities.
406 if (!IsSignaling && pl_bits.is_zero()) {
407 res = FPBits::quiet_nan(Sign::POS).get_val();
408 return false;
409 }
410
411 int pl_exp = pl_bits.get_exponent();
412
413 if (pl_bits.is_neg() || pl_exp < 0 || pl_exp >= FPBits::FRACTION_LEN - 1 ||
414 ((pl_bits.get_mantissa() << pl_exp) & FPBits::FRACTION_MASK) != 0) {
415 res = T(0.0);
416 return true;
417 }
418
419 using StorageType = typename FPBits::StorageType;
420 StorageType v(pl_bits.get_explicit_mantissa() >>
421 (FPBits::FRACTION_LEN - pl_exp));
422
423 if constexpr (IsSignaling)
424 res = FPBits::signaling_nan(Sign::POS, v).get_val();
425 else
426 res = FPBits::quiet_nan(Sign::POS, v).get_val();
427 return false;
428}
429
430} // namespace fputil
431} // namespace LIBC_NAMESPACE_DECL
432
433#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_BASICOPERATIONS_H
434