aboutsummaryrefslogtreecommitdiff
path: root/libc/utils
diff options
context:
space:
mode:
authorTue Ly <lntue@google.com>2020-07-09 15:30:29 -0400
committerTue Ly <lntue@google.com>2020-07-21 17:24:15 -0400
commit7ce32f87f96224445a1a54acbfab924b6251d485 (patch)
treef87566bf4cb74958fc0670c493016a28581de3eb /libc/utils
parentef868a848e6def288d2df7a1b3ebe09463afc8d0 (diff)
downloadllvm-7ce32f87f96224445a1a54acbfab924b6251d485.zip
llvm-7ce32f87f96224445a1a54acbfab924b6251d485.tar.gz
llvm-7ce32f87f96224445a1a54acbfab924b6251d485.tar.bz2
Add implementations for fmin, fminf, and fminl. Testing infrastructure update is splitted to https://reviews.llvm.org/D83931.
Diffstat (limited to 'libc/utils')
-rw-r--r--libc/utils/FPUtil/BasicOperations.h19
-rw-r--r--libc/utils/FPUtil/CMakeLists.txt1
2 files changed, 20 insertions, 0 deletions
diff --git a/libc/utils/FPUtil/BasicOperations.h b/libc/utils/FPUtil/BasicOperations.h
index 70c0bb5..2f86ddf 100644
--- a/libc/utils/FPUtil/BasicOperations.h
+++ b/libc/utils/FPUtil/BasicOperations.h
@@ -24,6 +24,25 @@ static inline T abs(T x) {
return T(bits);
}
+template <typename T,
+ cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
+static inline T fmin(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 fmin(+0, -0) == -0 == fmin(-0, +0), whenever x and
+ // y has different signs and both are not NaNs, we return the number
+ // with negative sign.
+ return (bitx.sign ? x : y);
+ } else {
+ return (x < y ? x : y);
+ }
+}
+
} // namespace fputil
} // namespace __llvm_libc
diff --git a/libc/utils/FPUtil/CMakeLists.txt b/libc/utils/FPUtil/CMakeLists.txt
index 682db93..e611372 100644
--- a/libc/utils/FPUtil/CMakeLists.txt
+++ b/libc/utils/FPUtil/CMakeLists.txt
@@ -14,6 +14,7 @@ add_header_library(
FloatOperations.h
FloatProperties.h
FPBits.h
+ BasicOperations.h
ManipulationFunctions.h
NearestIntegerOperations.h
DEPENDS