[NFC][Py Reformat] Added more commits to .git-blame-ignore-revs
[llvm-project.git] / libc / src / __support / FPUtil / BasicOperations.h
blob7f735656736ac5ee5b9a300c8cbfa451ec42c027
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 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC_SUPPORT_FPUTIL_BASIC_OPERATIONS_H
10 #define LLVM_LIBC_SRC_SUPPORT_FPUTIL_BASIC_OPERATIONS_H
12 #include "FPBits.h"
14 #include "src/__support/CPP/type_traits.h"
15 #include "src/__support/common.h"
17 namespace __llvm_libc {
18 namespace fputil {
20 template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
21 LIBC_INLINE T abs(T x) {
22 FPBits<T> bits(x);
23 bits.set_sign(0);
24 return T(bits);
27 template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
28 LIBC_INLINE T fmin(T x, T y) {
29 FPBits<T> bitx(x), bity(y);
31 if (bitx.is_nan()) {
32 return y;
33 } else if (bity.is_nan()) {
34 return x;
35 } else if (bitx.get_sign() != bity.get_sign()) {
36 // To make sure that fmin(+0, -0) == -0 == fmin(-0, +0), whenever x and
37 // y has different signs and both are not NaNs, we return the number
38 // with negative sign.
39 return (bitx.get_sign() ? x : y);
40 } else {
41 return (x < y ? x : y);
45 template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
46 LIBC_INLINE T fmax(T x, T y) {
47 FPBits<T> bitx(x), bity(y);
49 if (bitx.is_nan()) {
50 return y;
51 } else if (bity.is_nan()) {
52 return x;
53 } else if (bitx.get_sign() != bity.get_sign()) {
54 // To make sure that fmax(+0, -0) == +0 == fmax(-0, +0), whenever x and
55 // y has different signs and both are not NaNs, we return the number
56 // with positive sign.
57 return (bitx.get_sign() ? y : x);
58 } else {
59 return (x > y ? x : y);
63 template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
64 LIBC_INLINE T fdim(T x, T y) {
65 FPBits<T> bitx(x), bity(y);
67 if (bitx.is_nan()) {
68 return x;
71 if (bity.is_nan()) {
72 return y;
75 return (x > y ? x - y : 0);
78 } // namespace fputil
79 } // namespace __llvm_libc
81 #endif // LLVM_LIBC_SRC_SUPPORT_FPUTIL_BASIC_OPERATIONS_H