1 //===-- Basic operations on floating point numbers --------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC_SUPPORT_FPUTIL_BASIC_OPERATIONS_H
10 #define LLVM_LIBC_SRC_SUPPORT_FPUTIL_BASIC_OPERATIONS_H
14 #include "src/__support/CPP/type_traits.h"
15 #include "src/__support/common.h"
17 namespace __llvm_libc
{
20 template <typename T
, cpp::enable_if_t
<cpp::is_floating_point_v
<T
>, int> = 0>
21 LIBC_INLINE T
abs(T x
) {
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
);
33 } else if (bity
.is_nan()) {
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
);
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
);
51 } else if (bity
.is_nan()) {
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
);
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
);
75 return (x
> y
? x
- y
: 0);
79 } // namespace __llvm_libc
81 #endif // LLVM_LIBC_SRC_SUPPORT_FPUTIL_BASIC_OPERATIONS_H