blob: 5bf3effc1c01d86d93b12a800ec844778bdb28c4 [file] [log] [blame]
Siva Chandra Reddy6fb7f3a2020-06-15 21:06:24 -07001//===-- 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
Siva Chandra Reddy13901822020-06-08 11:11:49 -07009#ifndef LLVM_LIBC_UTILS_FPUTIL_BASIC_OPERATIONS_H
10#define LLVM_LIBC_UTILS_FPUTIL_BASIC_OPERATIONS_H
11
12#include "FPBits.h"
13
14#include "utils/CPP/TypeTraits.h"
Siva Chandra Reddy6fb7f3a2020-06-15 21:06:24 -070015
16namespace __llvm_libc {
17namespace fputil {
18
19template <typename T,
20 cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
21static inline T abs(T x) {
Siva Chandra Reddy13901822020-06-08 11:11:49 -070022 FPBits<T> bits(x);
23 bits.sign = 0;
24 return T(bits);
Siva Chandra Reddy6fb7f3a2020-06-15 21:06:24 -070025}
26
Tue Ly7ce32f82020-07-09 15:30:29 -040027template <typename T,
28 cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
29static inline T fmin(T x, T y) {
30 FPBits<T> bitx(x), bity(y);
31
32 if (bitx.isNaN()) {
33 return y;
34 } else if (bity.isNaN()) {
35 return x;
36 } else if (bitx.sign != bity.sign) {
37 // To make sure that fmin(+0, -0) == -0 == fmin(-0, +0), whenever x and
38 // y has different signs and both are not NaNs, we return the number
39 // with negative sign.
40 return (bitx.sign ? x : y);
41 } else {
42 return (x < y ? x : y);
43 }
44}
45
Tue Ly40960882020-07-23 02:04:33 -040046template <typename T,
47 cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
48static inline T fmax(T x, T y) {
49 FPBits<T> bitx(x), bity(y);
50
51 if (bitx.isNaN()) {
52 return y;
53 } else if (bity.isNaN()) {
54 return x;
55 } else if (bitx.sign != bity.sign) {
56 // To make sure that fmax(+0, -0) == +0 == fmax(-0, +0), whenever x and
57 // y has different signs and both are not NaNs, we return the number
58 // with positive sign.
59 return (bitx.sign ? y : x);
60 } else {
61 return (x > y ? x : y);
62 }
63}
64
Tue Lyd4128042020-11-05 14:55:46 -050065template <typename T,
66 cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
67static inline T fdim(T x, T y) {
68 FPBits<T> bitx(x), bity(y);
69
70 if (bitx.isNaN()) {
71 return x;
72 }
73
74 if (bity.isNaN()) {
75 return y;
76 }
77
78 return (x > y ? x - y : 0);
79}
80
Siva Chandra Reddy6fb7f3a2020-06-15 21:06:24 -070081} // namespace fputil
82} // namespace __llvm_libc
Siva Chandra Reddy13901822020-06-08 11:11:49 -070083
84#endif // LLVM_LIBC_UTILS_FPUTIL_BASIC_OPERATIONS_H