aboutsummaryrefslogtreecommitdiff
path: root/libc/test
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/test
parent9b2164063f770ef8085669b570dfd636cfa342b7 (diff)
downloadllvm-project-4096088e19416d8515de0c3d114fb06052efaa2c.tar.gz
[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/test')
-rw-r--r--libc/test/src/math/CMakeLists.txt38
-rw-r--r--libc/test/src/math/fmax_test.cpp73
-rw-r--r--libc/test/src/math/fmaxf_test.cpp73
-rw-r--r--libc/test/src/math/fmaxl_test.cpp73
4 files changed, 256 insertions, 1 deletions
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index a6898b370621..3bd40f6b32f1 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -472,4 +472,40 @@ add_math_unittest(
libc.include.math
libc.src.math.fminl
libc.utils.FPUtil.fputil
-) \ No newline at end of file
+)
+
+add_math_unittest(
+ fmaxf_test
+ SUITE
+ libc_math_unittests
+ SRCS
+ fmaxf_test.cpp
+ DEPENDS
+ libc.include.math
+ libc.src.math.fmaxf
+ libc.utils.FPUtil.fputil
+)
+
+add_math_unittest(
+ fmax_test
+ SUITE
+ libc_math_unittests
+ SRCS
+ fmax_test.cpp
+ DEPENDS
+ libc.include.math
+ libc.src.math.fmax
+ libc.utils.FPUtil.fputil
+)
+
+add_math_unittest(
+ fmaxl_test
+ SUITE
+ libc_math_unittests
+ SRCS
+ fmaxl_test.cpp
+ DEPENDS
+ libc.include.math
+ libc.src.math.fmaxl
+ libc.utils.FPUtil.fputil
+)
diff --git a/libc/test/src/math/fmax_test.cpp b/libc/test/src/math/fmax_test.cpp
new file mode 100644
index 000000000000..8d1c6c276382
--- /dev/null
+++ b/libc/test/src/math/fmax_test.cpp
@@ -0,0 +1,73 @@
+//===-- Unittests for fmax -----------------------------------------------===//
+//
+// 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 "include/math.h"
+#include "src/math/fmax.h"
+#include "utils/FPUtil/FPBits.h"
+#include "utils/UnitTest/Test.h"
+
+using FPBits = __llvm_libc::fputil::FPBits<double>;
+
+double nan = FPBits::buildNaN(1);
+double inf = FPBits::inf();
+double negInf = FPBits::negInf();
+
+TEST(FmaxTest, NaNArg) {
+ EXPECT_EQ(inf, __llvm_libc::fmax(nan, inf));
+ EXPECT_EQ(negInf, __llvm_libc::fmax(negInf, nan));
+ EXPECT_EQ(0.0, __llvm_libc::fmax(nan, 0.0));
+ EXPECT_EQ(-0.0, __llvm_libc::fmax(-0.0, nan));
+ EXPECT_EQ(-1.2345, __llvm_libc::fmax(nan, -1.2345));
+ EXPECT_EQ(1.2345, __llvm_libc::fmax(1.2345, nan));
+ EXPECT_NE(isnan(__llvm_libc::fmax(nan, nan)), 0);
+}
+
+TEST(FmaxTest, InfArg) {
+ EXPECT_EQ(inf, __llvm_libc::fmax(negInf, inf));
+ EXPECT_EQ(inf, __llvm_libc::fmax(inf, 0.0));
+ EXPECT_EQ(inf, __llvm_libc::fmax(-0.0, inf));
+ EXPECT_EQ(inf, __llvm_libc::fmax(inf, 1.2345));
+ EXPECT_EQ(inf, __llvm_libc::fmax(-1.2345, inf));
+}
+
+TEST(FmaxTest, NegInfArg) {
+ EXPECT_EQ(inf, __llvm_libc::fmax(inf, negInf));
+ EXPECT_EQ(0.0, __llvm_libc::fmax(negInf, 0.0));
+ EXPECT_EQ(-0.0, __llvm_libc::fmax(-0.0, negInf));
+ EXPECT_EQ(-1.2345, __llvm_libc::fmax(negInf, -1.2345));
+ EXPECT_EQ(1.2345, __llvm_libc::fmax(1.2345, negInf));
+}
+
+TEST(FmaxTest, BothZero) {
+ EXPECT_EQ(0.0, __llvm_libc::fmax(0.0, 0.0));
+ EXPECT_EQ(0.0, __llvm_libc::fmax(-0.0, 0.0));
+ EXPECT_EQ(0.0, __llvm_libc::fmax(0.0, -0.0));
+ EXPECT_EQ(-0.0, __llvm_libc::fmax(-0.0, -0.0));
+}
+
+TEST(FmaxTest, InDoubleRange) {
+ using UIntType = FPBits::UIntType;
+ constexpr UIntType count = 10000001;
+ 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::fmax(x, y));
+ } else {
+ ASSERT_EQ(y, __llvm_libc::fmax(x, y));
+ }
+ }
+}
diff --git a/libc/test/src/math/fmaxf_test.cpp b/libc/test/src/math/fmaxf_test.cpp
new file mode 100644
index 000000000000..fe9eaad171bd
--- /dev/null
+++ b/libc/test/src/math/fmaxf_test.cpp
@@ -0,0 +1,73 @@
+//===-- Unittests for fmaxf -----------------------------------------------===//
+//
+// 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 "include/math.h"
+#include "src/math/fmaxf.h"
+#include "utils/FPUtil/FPBits.h"
+#include "utils/UnitTest/Test.h"
+
+using FPBits = __llvm_libc::fputil::FPBits<float>;
+
+float nan = FPBits::buildNaN(1);
+float inf = FPBits::inf();
+float negInf = FPBits::negInf();
+
+TEST(FmaxfTest, NaNArg) {
+ EXPECT_EQ(inf, __llvm_libc::fmaxf(nan, inf));
+ EXPECT_EQ(negInf, __llvm_libc::fmaxf(negInf, nan));
+ EXPECT_EQ(0.0f, __llvm_libc::fmaxf(nan, 0.0f));
+ EXPECT_EQ(-0.0f, __llvm_libc::fmaxf(-0.0f, nan));
+ EXPECT_EQ(-1.2345f, __llvm_libc::fmaxf(nan, -1.2345f));
+ EXPECT_EQ(1.2345f, __llvm_libc::fmaxf(1.2345f, nan));
+ EXPECT_NE(isnan(__llvm_libc::fmaxf(nan, nan)), 0);
+}
+
+TEST(FmaxfTest, InfArg) {
+ EXPECT_EQ(inf, __llvm_libc::fmaxf(negInf, inf));
+ EXPECT_EQ(inf, __llvm_libc::fmaxf(inf, 0.0f));
+ EXPECT_EQ(inf, __llvm_libc::fmaxf(-0.0f, inf));
+ EXPECT_EQ(inf, __llvm_libc::fmaxf(inf, 1.2345f));
+ EXPECT_EQ(inf, __llvm_libc::fmaxf(-1.2345f, inf));
+}
+
+TEST(FmaxfTest, NegInfArg) {
+ EXPECT_EQ(inf, __llvm_libc::fmaxf(inf, negInf));
+ EXPECT_EQ(0.0f, __llvm_libc::fmaxf(negInf, 0.0f));
+ EXPECT_EQ(-0.0f, __llvm_libc::fmaxf(-0.0f, negInf));
+ EXPECT_EQ(-1.2345f, __llvm_libc::fmaxf(negInf, -1.2345f));
+ EXPECT_EQ(1.2345f, __llvm_libc::fmaxf(1.2345f, negInf));
+}
+
+TEST(FmaxfTest, BothZero) {
+ EXPECT_EQ(0.0f, __llvm_libc::fmaxf(0.0f, 0.0f));
+ EXPECT_EQ(0.0f, __llvm_libc::fmaxf(-0.0f, 0.0f));
+ EXPECT_EQ(0.0f, __llvm_libc::fmaxf(0.0f, -0.0f));
+ EXPECT_EQ(-0.0f, __llvm_libc::fmaxf(-0.0f, -0.0f));
+}
+
+TEST(FmaxfTest, InFloatRange) {
+ using UIntType = FPBits::UIntType;
+ constexpr UIntType count = 10000001;
+ 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::fmaxf(x, y));
+ } else {
+ ASSERT_EQ(y, __llvm_libc::fmaxf(x, y));
+ }
+ }
+}
diff --git a/libc/test/src/math/fmaxl_test.cpp b/libc/test/src/math/fmaxl_test.cpp
new file mode 100644
index 000000000000..9c7aa21582eb
--- /dev/null
+++ b/libc/test/src/math/fmaxl_test.cpp
@@ -0,0 +1,73 @@
+//===-- Unittests for fmaxl -----------------------------------------------===//
+//
+// 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 "include/math.h"
+#include "src/math/fmaxl.h"
+#include "utils/FPUtil/FPBits.h"
+#include "utils/UnitTest/Test.h"
+
+using FPBits = __llvm_libc::fputil::FPBits<long double>;
+
+long double nan = FPBits::buildNaN(1);
+long double inf = FPBits::inf();
+long double negInf = FPBits::negInf();
+
+TEST(FmaxlTest, NaNArg) {
+ EXPECT_EQ(inf, __llvm_libc::fmaxl(nan, inf));
+ EXPECT_EQ(negInf, __llvm_libc::fmaxl(negInf, nan));
+ EXPECT_EQ(0.0L, __llvm_libc::fmaxl(nan, 0.0L));
+ EXPECT_EQ(-0.0L, __llvm_libc::fmaxl(-0.0L, nan));
+ EXPECT_EQ(-1.2345L, __llvm_libc::fmaxl(nan, -1.2345L));
+ EXPECT_EQ(1.2345L, __llvm_libc::fmaxl(1.2345L, nan));
+ EXPECT_NE(isnan(__llvm_libc::fmaxl(nan, nan)), 0);
+}
+
+TEST(FmaxlTest, InfArg) {
+ EXPECT_EQ(inf, __llvm_libc::fmaxl(negInf, inf));
+ EXPECT_EQ(inf, __llvm_libc::fmaxl(inf, 0.0L));
+ EXPECT_EQ(inf, __llvm_libc::fmaxl(-0.0L, inf));
+ EXPECT_EQ(inf, __llvm_libc::fmaxl(inf, 1.2345L));
+ EXPECT_EQ(inf, __llvm_libc::fmaxl(-1.2345L, inf));
+}
+
+TEST(FmaxlTest, NegInfArg) {
+ EXPECT_EQ(inf, __llvm_libc::fmaxl(inf, negInf));
+ EXPECT_EQ(0.0L, __llvm_libc::fmaxl(negInf, 0.0L));
+ EXPECT_EQ(-0.0L, __llvm_libc::fmaxl(-0.0L, negInf));
+ EXPECT_EQ(-1.2345L, __llvm_libc::fmaxl(negInf, -1.2345L));
+ EXPECT_EQ(1.2345L, __llvm_libc::fmaxl(1.2345L, negInf));
+}
+
+TEST(FmaxlTest, BothZero) {
+ EXPECT_EQ(0.0L, __llvm_libc::fmaxl(0.0L, 0.0L));
+ EXPECT_EQ(0.0L, __llvm_libc::fmaxl(-0.0L, 0.0L));
+ EXPECT_EQ(0.0L, __llvm_libc::fmaxl(0.0L, -0.0L));
+ EXPECT_EQ(-0.0L, __llvm_libc::fmaxl(-0.0L, -0.0L));
+}
+
+TEST(FmaxlTest, InLongDoubleRange) {
+ using UIntType = FPBits::UIntType;
+ constexpr UIntType count = 10000001;
+ 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::fmaxl(x, y));
+ } else {
+ ASSERT_EQ(y, __llvm_libc::fmaxl(x, y));
+ }
+ }
+}