[libc-commits] [libc] 7ce32f8 - Add implementations for fmin, fminf, and fminl. Testing infrastructure update is splitted to https://reviews.llvm.org/D83931.
Tue Ly via libc-commits
libc-commits at lists.llvm.org
Tue Jul 21 14:27:49 PDT 2020
Author: Tue Ly
Date: 2020-07-21T17:24:15-04:00
New Revision: 7ce32f87f96224445a1a54acbfab924b6251d485
URL: https://github.com/llvm/llvm-project/commit/7ce32f87f96224445a1a54acbfab924b6251d485
DIFF: https://github.com/llvm/llvm-project/commit/7ce32f87f96224445a1a54acbfab924b6251d485.diff
LOG: Add implementations for fmin, fminf, and fminl. Testing infrastructure update is splitted to https://reviews.llvm.org/D83931.
Added:
libc/src/math/fmin.cpp
libc/src/math/fmin.h
libc/src/math/fminf.cpp
libc/src/math/fminf.h
libc/src/math/fminl.cpp
libc/src/math/fminl.h
libc/test/src/math/fmin_test.cpp
libc/test/src/math/fminf_test.cpp
libc/test/src/math/fminl_test.cpp
Modified:
libc/config/linux/aarch64/entrypoints.txt
libc/config/linux/api.td
libc/config/linux/x86_64/entrypoints.txt
libc/spec/stdc.td
libc/src/math/CMakeLists.txt
libc/test/src/math/CMakeLists.txt
libc/utils/FPUtil/BasicOperations.h
libc/utils/FPUtil/CMakeLists.txt
Removed:
################################################################################
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 592d142ca6c4..cd805d7b8653 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -31,6 +31,9 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.floor
libc.src.math.floorf
libc.src.math.floorl
+ libc.src.math.fmin
+ libc.src.math.fminf
+ libc.src.math.fminl
libc.src.math.frexp
libc.src.math.frexpf
libc.src.math.frexpl
diff --git a/libc/config/linux/api.td b/libc/config/linux/api.td
index 9b9ed1b49298..22e97b891a75 100644
--- a/libc/config/linux/api.td
+++ b/libc/config/linux/api.td
@@ -163,6 +163,9 @@ def MathAPI : PublicAPI<"math.h"> {
"floor",
"floorf",
"floorl",
+ "fmin",
+ "fminf",
+ "fminl",
"frexp",
"frexpf",
"frexpl",
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index f08f775cd892..e8a1adbb278e 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -64,6 +64,9 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.floor
libc.src.math.floorf
libc.src.math.floorl
+ libc.src.math.fmin
+ libc.src.math.fminf
+ libc.src.math.fminl
libc.src.math.frexp
libc.src.math.frexpf
libc.src.math.frexpl
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 2af79373c30c..cdeee89be507 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -205,6 +205,10 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"floorf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
FunctionSpec<"floorl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
+ FunctionSpec<"fmin", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
+ FunctionSpec<"fminf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
+ FunctionSpec<"fminl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>]>,
+
FunctionSpec<"frexp", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<IntPtr>]>,
FunctionSpec<"frexpf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<IntPtr>]>,
FunctionSpec<"frexpl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<IntPtr>]>,
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index aa8680383b7a..d2d694807168 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -413,3 +413,39 @@ add_entrypoint_object(
COMPILE_OPTIONS
-O2
)
+
+add_entrypoint_object(
+ fmin
+ SRCS
+ fmin.cpp
+ HDRS
+ fmin.h
+ DEPENDS
+ libc.utils.FPUtil.fputil
+ COMPILE_OPTIONS
+ -O2
+)
+
+add_entrypoint_object(
+ fminf
+ SRCS
+ fminf.cpp
+ HDRS
+ fminf.h
+ DEPENDS
+ libc.utils.FPUtil.fputil
+ COMPILE_OPTIONS
+ -O2
+)
+
+add_entrypoint_object(
+ fminl
+ SRCS
+ fminl.cpp
+ HDRS
+ fminl.h
+ DEPENDS
+ libc.utils.FPUtil.fputil
+ COMPILE_OPTIONS
+ -O2
+)
diff --git a/libc/src/math/fmin.cpp b/libc/src/math/fmin.cpp
new file mode 100644
index 000000000000..ef5efad59811
--- /dev/null
+++ b/libc/src/math/fmin.cpp
@@ -0,0 +1,18 @@
+//===-- Implementation of fmin function -----------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/common.h"
+#include "utils/FPUtil/BasicOperations.h"
+
+namespace __llvm_libc {
+
+double LLVM_LIBC_ENTRYPOINT(fmin)(double x, double y) {
+ return fputil::fmin(x, y);
+}
+
+} // namespace __llvm_libc
diff --git a/libc/src/math/fmin.h b/libc/src/math/fmin.h
new file mode 100644
index 000000000000..52334ee8e456
--- /dev/null
+++ b/libc/src/math/fmin.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for fmin --------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_MATH_FMIN_H
+#define LLVM_LIBC_SRC_MATH_FMIN_H
+
+namespace __llvm_libc {
+
+double fmin(double x, double y);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_MATH_FMIN_H
diff --git a/libc/src/math/fminf.cpp b/libc/src/math/fminf.cpp
new file mode 100644
index 000000000000..8f5b54873f8f
--- /dev/null
+++ b/libc/src/math/fminf.cpp
@@ -0,0 +1,18 @@
+//===-- Implementation of fminf function ----------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/common.h"
+#include "utils/FPUtil/BasicOperations.h"
+
+namespace __llvm_libc {
+
+float LLVM_LIBC_ENTRYPOINT(fminf)(float x, float y) {
+ return fputil::fmin(x, y);
+}
+
+} // namespace __llvm_libc
\ No newline at end of file
diff --git a/libc/src/math/fminf.h b/libc/src/math/fminf.h
new file mode 100644
index 000000000000..62dad57d852a
--- /dev/null
+++ b/libc/src/math/fminf.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for fminf -------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_MATH_FMINF_H
+#define LLVM_LIBC_SRC_MATH_FMINF_H
+
+namespace __llvm_libc {
+
+float fminf(float x, float y);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_MATH_FMINF_H
diff --git a/libc/src/math/fminl.cpp b/libc/src/math/fminl.cpp
new file mode 100644
index 000000000000..e66f0d72bfc3
--- /dev/null
+++ b/libc/src/math/fminl.cpp
@@ -0,0 +1,18 @@
+//===-- Implementation of fminl function ----------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/common.h"
+#include "utils/FPUtil/BasicOperations.h"
+
+namespace __llvm_libc {
+
+long double LLVM_LIBC_ENTRYPOINT(fminl)(long double x, long double y) {
+ return fputil::fmin(x, y);
+}
+
+} // namespace __llvm_libc
diff --git a/libc/src/math/fminl.h b/libc/src/math/fminl.h
new file mode 100644
index 000000000000..c19505c01828
--- /dev/null
+++ b/libc/src/math/fminl.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for fminl -------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_MATH_FMINL_H
+#define LLVM_LIBC_SRC_MATH_FMINL_H
+
+namespace __llvm_libc {
+
+long double fminl(long double x, long double y);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_MATH_FMINL_H
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 6c6cb9036e53..a6898b370621 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -437,3 +437,39 @@ add_math_unittest(
libc.src.math.modfl
libc.utils.FPUtil.fputil
)
+
+add_math_unittest(
+ fminf_test
+ SUITE
+ libc_math_unittests
+ SRCS
+ fminf_test.cpp
+ DEPENDS
+ libc.include.math
+ libc.src.math.fminf
+ libc.utils.FPUtil.fputil
+)
+
+add_math_unittest(
+ fmin_test
+ SUITE
+ libc_math_unittests
+ SRCS
+ fmin_test.cpp
+ DEPENDS
+ libc.include.math
+ libc.src.math.fmin
+ libc.utils.FPUtil.fputil
+)
+
+add_math_unittest(
+ fminl_test
+ SUITE
+ libc_math_unittests
+ SRCS
+ fminl_test.cpp
+ DEPENDS
+ libc.include.math
+ libc.src.math.fminl
+ libc.utils.FPUtil.fputil
+)
\ No newline at end of file
diff --git a/libc/test/src/math/fmin_test.cpp b/libc/test/src/math/fmin_test.cpp
new file mode 100644
index 000000000000..c1467e4cbb90
--- /dev/null
+++ b/libc/test/src/math/fmin_test.cpp
@@ -0,0 +1,75 @@
+//===-- Unittests for fmin -----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===---------------------------------------------------------------------===//
+
+#include <vector>
+
+#include "include/math.h"
+#include "src/math/fmin.h"
+#include "utils/FPUtil/FPBits.h"
+#include "utils/UnitTest/Test.h"
+
+using FPBits = __llvm_libc::fputil::FPBits<double>;
+
+double nan = static_cast<double>(FPBits::buildNaN(1));
+double inf = static_cast<double>(FPBits::inf());
+double negInf = static_cast<double>(FPBits::negInf());
+
+TEST(FminTest, NaNArg) {
+ EXPECT_EQ(inf, __llvm_libc::fmin(nan, inf));
+ EXPECT_EQ(negInf, __llvm_libc::fmin(negInf, nan));
+ EXPECT_EQ(0.0, __llvm_libc::fmin(nan, 0.0));
+ EXPECT_EQ(-0.0, __llvm_libc::fmin(-0.0, nan));
+ EXPECT_EQ(-1.2345, __llvm_libc::fmin(nan, -1.2345));
+ EXPECT_EQ(1.2345, __llvm_libc::fmin(1.2345, nan));
+ EXPECT_NE(isnan(__llvm_libc::fmin(nan, nan)), 0);
+}
+
+TEST(FminTest, InfArg) {
+ EXPECT_EQ(negInf, __llvm_libc::fmin(negInf, inf));
+ EXPECT_EQ(0.0, __llvm_libc::fmin(inf, 0.0));
+ EXPECT_EQ(-0.0, __llvm_libc::fmin(-0.0, inf));
+ EXPECT_EQ(1.2345, __llvm_libc::fmin(inf, 1.2345));
+ EXPECT_EQ(-1.2345, __llvm_libc::fmin(-1.2345, inf));
+}
+
+TEST(FminTest, NegInfArg) {
+ EXPECT_EQ(negInf, __llvm_libc::fmin(inf, negInf));
+ EXPECT_EQ(negInf, __llvm_libc::fmin(negInf, 0.0));
+ EXPECT_EQ(negInf, __llvm_libc::fmin(-0.0, negInf));
+ EXPECT_EQ(negInf, __llvm_libc::fmin(negInf, -1.2345));
+ EXPECT_EQ(negInf, __llvm_libc::fmin(1.2345, negInf));
+}
+
+TEST(FminTest, BothZero) {
+ EXPECT_EQ(0.0, __llvm_libc::fmin(0.0, 0.0));
+ EXPECT_EQ(-0.0, __llvm_libc::fmin(-0.0, 0.0));
+ EXPECT_EQ(-0.0, __llvm_libc::fmin(0.0, -0.0));
+ EXPECT_EQ(-0.0, __llvm_libc::fmin(-0.0, -0.0));
+}
+
+TEST(FminTest, InFloatRange) {
+ using UIntType = FPBits::UIntType;
+ constexpr UIntType count = 10000000;
+ constexpr UIntType step = UIntType(-1) / count;
+ for (UIntType i = 0, v = 0, w = UIntType(-1); i <= count;
+ ++i, v += step, w -= step) {
+ double x = FPBits(v), y = FPBits(w);
+ if (isnan(x) || isinf(x))
+ continue;
+ if (isnan(y) || isinf(y))
+ continue;
+ if ((x == 0) && (y == 0))
+ continue;
+
+ if (x < y) {
+ ASSERT_EQ(x, __llvm_libc::fmin(x, y));
+ } else {
+ ASSERT_EQ(y, __llvm_libc::fmin(x, y));
+ }
+ }
+}
diff --git a/libc/test/src/math/fminf_test.cpp b/libc/test/src/math/fminf_test.cpp
new file mode 100644
index 000000000000..8d03d92f7237
--- /dev/null
+++ b/libc/test/src/math/fminf_test.cpp
@@ -0,0 +1,75 @@
+//===-- Unittests for fminf ----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===---------------------------------------------------------------------===//
+
+#include <vector>
+
+#include "include/math.h"
+#include "src/math/fminf.h"
+#include "utils/FPUtil/FPBits.h"
+#include "utils/UnitTest/Test.h"
+
+using FPBits = __llvm_libc::fputil::FPBits<float>;
+
+float nan = static_cast<float>(FPBits::buildNaN(1));
+float inf = static_cast<float>(FPBits::inf());
+float negInf = static_cast<float>(FPBits::negInf());
+
+TEST(FminfTest, NaNArg) {
+ EXPECT_EQ(inf, __llvm_libc::fminf(nan, inf));
+ EXPECT_EQ(negInf, __llvm_libc::fminf(negInf, nan));
+ EXPECT_EQ(0.0f, __llvm_libc::fminf(nan, 0.0f));
+ EXPECT_EQ(-0.0f, __llvm_libc::fminf(-0.0f, nan));
+ EXPECT_EQ(-1.2345f, __llvm_libc::fminf(nan, -1.2345f));
+ EXPECT_EQ(1.2345f, __llvm_libc::fminf(1.2345f, nan));
+ EXPECT_NE(isnan(__llvm_libc::fminf(nan, nan)), 0);
+}
+
+TEST(FminfTest, InfArg) {
+ EXPECT_EQ(negInf, __llvm_libc::fminf(negInf, inf));
+ EXPECT_EQ(0.0f, __llvm_libc::fminf(inf, 0.0f));
+ EXPECT_EQ(-0.0f, __llvm_libc::fminf(-0.0f, inf));
+ EXPECT_EQ(1.2345f, __llvm_libc::fminf(inf, 1.2345f));
+ EXPECT_EQ(-1.2345f, __llvm_libc::fminf(-1.2345f, inf));
+}
+
+TEST(FminfTest, NegInfArg) {
+ EXPECT_EQ(negInf, __llvm_libc::fminf(inf, negInf));
+ EXPECT_EQ(negInf, __llvm_libc::fminf(negInf, 0.0f));
+ EXPECT_EQ(negInf, __llvm_libc::fminf(-0.0f, negInf));
+ EXPECT_EQ(negInf, __llvm_libc::fminf(negInf, -1.2345f));
+ EXPECT_EQ(negInf, __llvm_libc::fminf(1.2345f, negInf));
+}
+
+TEST(FminfTest, BothZero) {
+ EXPECT_EQ(0.0f, __llvm_libc::fminf(0.0f, 0.0f));
+ EXPECT_EQ(-0.0f, __llvm_libc::fminf(-0.0f, 0.0f));
+ EXPECT_EQ(-0.0f, __llvm_libc::fminf(0.0f, -0.0f));
+ EXPECT_EQ(-0.0f, __llvm_libc::fminf(-0.0f, -0.0f));
+}
+
+TEST(FminfTest, InFloatRange) {
+ using UIntType = FPBits::UIntType;
+ constexpr UIntType count = 10000000;
+ constexpr UIntType step = UIntType(-1) / count;
+ for (UIntType i = 0, v = 0, w = UIntType(-1); i <= count;
+ ++i, v += step, w -= step) {
+ float x = FPBits(v), y = FPBits(w);
+ if (isnan(x) || isinf(x))
+ continue;
+ if (isnan(y) || isinf(y))
+ continue;
+ if ((x == 0) && (y == 0))
+ continue;
+
+ if (x < y) {
+ ASSERT_EQ(x, __llvm_libc::fminf(x, y));
+ } else {
+ ASSERT_EQ(y, __llvm_libc::fminf(x, y));
+ }
+ }
+}
diff --git a/libc/test/src/math/fminl_test.cpp b/libc/test/src/math/fminl_test.cpp
new file mode 100644
index 000000000000..519c1f33fba6
--- /dev/null
+++ b/libc/test/src/math/fminl_test.cpp
@@ -0,0 +1,75 @@
+//===-- Unittests for fmin -----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===---------------------------------------------------------------------===//
+
+#include <vector>
+
+#include "include/math.h"
+#include "src/math/fminl.h"
+#include "utils/FPUtil/FPBits.h"
+#include "utils/UnitTest/Test.h"
+
+using FPBits = __llvm_libc::fputil::FPBits<long double>;
+
+long double nan = static_cast<long double>(FPBits::buildNaN(1));
+long double inf = static_cast<long double>(FPBits::inf());
+long double negInf = static_cast<long double>(FPBits::negInf());
+
+TEST(FminlTest, NaNArg) {
+ EXPECT_EQ(inf, __llvm_libc::fminl(nan, inf));
+ EXPECT_EQ(negInf, __llvm_libc::fminl(negInf, nan));
+ EXPECT_EQ(0.0L, __llvm_libc::fminl(nan, 0.0L));
+ EXPECT_EQ(-0.0L, __llvm_libc::fminl(-0.0L, nan));
+ EXPECT_EQ(-1.2345L, __llvm_libc::fminl(nan, -1.2345L));
+ EXPECT_EQ(1.2345L, __llvm_libc::fminl(1.2345L, nan));
+ EXPECT_NE(isnan(__llvm_libc::fminl(nan, nan)), 0);
+}
+
+TEST(FminlTest, InfArg) {
+ EXPECT_EQ(negInf, __llvm_libc::fminl(negInf, inf));
+ EXPECT_EQ(0.0L, __llvm_libc::fminl(inf, 0.0L));
+ EXPECT_EQ(-0.0L, __llvm_libc::fminl(-0.0L, inf));
+ EXPECT_EQ(1.2345L, __llvm_libc::fminl(inf, 1.2345L));
+ EXPECT_EQ(-1.2345L, __llvm_libc::fminl(-1.2345L, inf));
+}
+
+TEST(FminlTest, NegInfArg) {
+ EXPECT_EQ(negInf, __llvm_libc::fminl(inf, negInf));
+ EXPECT_EQ(negInf, __llvm_libc::fminl(negInf, 0.0L));
+ EXPECT_EQ(negInf, __llvm_libc::fminl(-0.0L, negInf));
+ EXPECT_EQ(negInf, __llvm_libc::fminl(negInf, -1.2345L));
+ EXPECT_EQ(negInf, __llvm_libc::fminl(1.2345L, negInf));
+}
+
+TEST(FminlTest, BothZero) {
+ EXPECT_EQ(0.0L, __llvm_libc::fminl(0.0L, 0.0L));
+ EXPECT_EQ(-0.0L, __llvm_libc::fminl(-0.0L, 0.0L));
+ EXPECT_EQ(-0.0L, __llvm_libc::fminl(0.0L, -0.0L));
+ EXPECT_EQ(-0.0L, __llvm_libc::fminl(-0.0L, -0.0L));
+}
+
+TEST(FminlTest, InLongDoubleRange) {
+ using UIntType = FPBits::UIntType;
+ constexpr UIntType count = 10000000;
+ constexpr UIntType step = UIntType(-1) / count;
+ for (UIntType i = 0, v = 0, w = UIntType(-1); i <= count;
+ ++i, v += step, w -= step) {
+ long double x = FPBits(v), y = FPBits(w);
+ if (isnan(x) || isinf(x))
+ continue;
+ if (isnan(y) || isinf(y))
+ continue;
+ if ((x == 0) && (y == 0))
+ continue;
+
+ if (x < y) {
+ ASSERT_EQ(x, __llvm_libc::fminl(x, y));
+ } else {
+ ASSERT_EQ(y, __llvm_libc::fminl(x, y));
+ }
+ }
+}
diff --git a/libc/utils/FPUtil/BasicOperations.h b/libc/utils/FPUtil/BasicOperations.h
index 70c0bb557cce..2f86ddf5678c 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
diff erent 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 682db93859a9..e6113723592d 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
More information about the libc-commits
mailing list