aboutsummaryrefslogtreecommitdiff
path: root/libc/utils
diff options
context:
space:
mode:
authorTue Ly <lntue@google.com>2020-07-23 02:04:33 -0400
committerTue Ly <lntue@google.com>2020-07-23 15:23:08 -0400
commit4096088e19416d8515de0c3d114fb06052efaa2c (patch)
treec57f1af3b56a423e38aec8f46b4e079888b3c122 /libc/utils
parent9b2164063f770ef8085669b570dfd636cfa342b7 (diff)
downloadllvm-4096088e19416d8515de0c3d114fb06052efaa2c.zip
llvm-4096088e19416d8515de0c3d114fb06052efaa2c.tar.gz
llvm-4096088e19416d8515de0c3d114fb06052efaa2c.tar.bz2
[libc] Add implementations of fmax, fmaxf, and fmaxl.
Summary: Add implementations of fmax, fmaxf, and fmaxl. Reviewers: sivachandra Subscribers: mgorny, tschuett, libc-commits, ecnelises Tags: #libc-project Differential Revision: https://reviews.llvm.org/D84385
Diffstat (limited to 'libc/utils')
-rw-r--r--libc/utils/FPUtil/BasicOperations.h19
1 files changed, 19 insertions, 0 deletions
diff --git a/libc/utils/FPUtil/BasicOperations.h b/libc/utils/FPUtil/BasicOperations.h
index 2f86ddf..7885692 100644
--- a/libc/utils/FPUtil/BasicOperations.h
+++ b/libc/utils/FPUtil/BasicOperations.h
@@ -43,6 +43,25 @@ static inline T fmin(T x, T y) {
}
}
+template <typename T,
+ cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
+static inline T fmax(T x, T y) {
+ FPBits<T> bitx(x), bity(y);
+
+ if (bitx.isNaN()) {
+ return y;
+ } else if (bity.isNaN()) {
+ return x;
+ } else if (bitx.sign != bity.sign) {
+ // To make sure that fmax(+0, -0) == +0 == fmax(-0, +0), whenever x and
+ // y has different signs and both are not NaNs, we return the number
+ // with positive sign.
+ return (bitx.sign ? y : x);
+ } else {
+ return (x > y ? x : y);
+ }
+}
+
} // namespace fputil
} // namespace __llvm_libc