aboutsummaryrefslogtreecommitdiff
path: root/libc/test
diff options
context:
space:
mode:
authorTue Ly <lntue@google.com>2020-07-28 01:41:36 -0400
committerTue Ly <lntue@google.com>2020-08-26 09:46:18 -0400
commit5078825aa982905088502f14b5387fc5c96017fe (patch)
tree3fc4f656503ac5c860b2c30e13eea6e105a4b75b /libc/test
parent75d159f924868ec93e3008b04b637412b64de29e (diff)
downloadllvm-project-5078825aa982905088502f14b5387fc5c96017fe.tar.gz
[libc] Add implementations for sqrt, sqrtf, and sqrtl.
Differential Revision: https://reviews.llvm.org/D84726
Diffstat (limited to 'libc/test')
-rw-r--r--libc/test/src/math/CMakeLists.txt39
-rw-r--r--libc/test/src/math/sqrt_test.cpp67
-rw-r--r--libc/test/src/math/sqrtf_test.cpp67
-rw-r--r--libc/test/src/math/sqrtl_test.cpp67
4 files changed, 240 insertions, 0 deletions
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 2fe766a2ffc6..07b505207452 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -513,3 +513,42 @@ add_fp_unittest(
libc.src.math.fmaxl
libc.utils.FPUtil.fputil
)
+
+add_fp_unittest(
+ sqrtf_test
+ NEED_MPFR
+ SUITE
+ libc_math_unittests
+ SRCS
+ sqrtf_test.cpp
+ DEPENDS
+ libc.include.math
+ libc.src.math.sqrtf
+ libc.utils.FPUtil.fputil
+)
+
+add_fp_unittest(
+ sqrt_test
+ NEED_MPFR
+ SUITE
+ libc_math_unittests
+ SRCS
+ sqrt_test.cpp
+ DEPENDS
+ libc.include.math
+ libc.src.math.sqrt
+ libc.utils.FPUtil.fputil
+)
+
+add_fp_unittest(
+ sqrtl_test
+ NEED_MPFR
+ SUITE
+ libc_math_unittests
+ SRCS
+ sqrtl_test.cpp
+ DEPENDS
+ libc.include.math
+ libc.src.math.sqrtl
+ libc.utils.FPUtil.fputil
+)
diff --git a/libc/test/src/math/sqrt_test.cpp b/libc/test/src/math/sqrt_test.cpp
new file mode 100644
index 000000000000..7ff4978ec9e3
--- /dev/null
+++ b/libc/test/src/math/sqrt_test.cpp
@@ -0,0 +1,67 @@
+//===-- Unittests for sqrt -----------------------------------------------===//
+//
+// 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/sqrt.h"
+#include "utils/FPUtil/FPBits.h"
+#include "utils/FPUtil/TestHelpers.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using FPBits = __llvm_libc::fputil::FPBits<double>;
+using UIntType = typename FPBits::UIntType;
+
+namespace mpfr = __llvm_libc::testing::mpfr;
+
+constexpr UIntType HiddenBit =
+ UIntType(1) << __llvm_libc::fputil::MantissaWidth<double>::value;
+
+double nan = FPBits::buildNaN(1);
+double inf = FPBits::inf();
+double negInf = FPBits::negInf();
+
+TEST(SqrtTest, SpecialValues) {
+ ASSERT_FP_EQ(nan, __llvm_libc::sqrt(nan));
+ ASSERT_FP_EQ(inf, __llvm_libc::sqrt(inf));
+ ASSERT_FP_EQ(nan, __llvm_libc::sqrt(negInf));
+ ASSERT_FP_EQ(0.0, __llvm_libc::sqrt(0.0));
+ ASSERT_FP_EQ(-0.0, __llvm_libc::sqrt(-0.0));
+ ASSERT_FP_EQ(nan, __llvm_libc::sqrt(-1.0));
+ ASSERT_FP_EQ(1.0, __llvm_libc::sqrt(1.0));
+ ASSERT_FP_EQ(2.0, __llvm_libc::sqrt(4.0));
+ ASSERT_FP_EQ(3.0, __llvm_libc::sqrt(9.0));
+}
+
+TEST(SqrtTest, DenormalValues) {
+ for (UIntType mant = 1; mant < HiddenBit; mant <<= 1) {
+ FPBits denormal(0.0);
+ denormal.mantissa = mant;
+
+ ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, double(denormal),
+ __llvm_libc::sqrt(denormal), 0.5);
+ }
+
+ constexpr UIntType count = 1'000'001;
+ constexpr UIntType step = HiddenBit / count;
+ for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
+ double x = *reinterpret_cast<double *>(&v);
+ ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, x, __llvm_libc::sqrt(x), 0.5);
+ }
+}
+
+TEST(SqrtTest, InDoubleRange) {
+ constexpr UIntType count = 10'000'001;
+ constexpr UIntType step = UIntType(-1) / count;
+ for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
+ double x = *reinterpret_cast<double *>(&v);
+ if (isnan(x) || (x < 0)) {
+ continue;
+ }
+
+ ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, x, __llvm_libc::sqrt(x), 0.5);
+ }
+}
diff --git a/libc/test/src/math/sqrtf_test.cpp b/libc/test/src/math/sqrtf_test.cpp
new file mode 100644
index 000000000000..8c429065bb45
--- /dev/null
+++ b/libc/test/src/math/sqrtf_test.cpp
@@ -0,0 +1,67 @@
+//===-- Unittests for sqrtf -----------------------------------------------===//
+//
+// 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/sqrtf.h"
+#include "utils/FPUtil/FPBits.h"
+#include "utils/FPUtil/TestHelpers.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using FPBits = __llvm_libc::fputil::FPBits<float>;
+using UIntType = typename FPBits::UIntType;
+
+namespace mpfr = __llvm_libc::testing::mpfr;
+
+constexpr UIntType HiddenBit =
+ UIntType(1) << __llvm_libc::fputil::MantissaWidth<float>::value;
+
+float nan = FPBits::buildNaN(1);
+float inf = FPBits::inf();
+float negInf = FPBits::negInf();
+
+TEST(SqrtfTest, SpecialValues) {
+ ASSERT_FP_EQ(nan, __llvm_libc::sqrtf(nan));
+ ASSERT_FP_EQ(inf, __llvm_libc::sqrtf(inf));
+ ASSERT_FP_EQ(nan, __llvm_libc::sqrtf(negInf));
+ ASSERT_FP_EQ(0.0f, __llvm_libc::sqrtf(0.0f));
+ ASSERT_FP_EQ(-0.0f, __llvm_libc::sqrtf(-0.0f));
+ ASSERT_FP_EQ(nan, __llvm_libc::sqrtf(-1.0f));
+ ASSERT_FP_EQ(1.0f, __llvm_libc::sqrtf(1.0f));
+ ASSERT_FP_EQ(2.0f, __llvm_libc::sqrtf(4.0f));
+ ASSERT_FP_EQ(3.0f, __llvm_libc::sqrtf(9.0f));
+}
+
+TEST(SqrtfTest, DenormalValues) {
+ for (UIntType mant = 1; mant < HiddenBit; mant <<= 1) {
+ FPBits denormal(0.0f);
+ denormal.mantissa = mant;
+
+ ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, float(denormal),
+ __llvm_libc::sqrtf(denormal), 0.5);
+ }
+
+ constexpr UIntType count = 1'000'001;
+ constexpr UIntType step = HiddenBit / count;
+ for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
+ float x = *reinterpret_cast<float *>(&v);
+ ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, x, __llvm_libc::sqrtf(x), 0.5);
+ }
+}
+
+TEST(SqrtfTest, InFloatRange) {
+ constexpr UIntType count = 10'000'001;
+ constexpr UIntType step = UIntType(-1) / count;
+ for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
+ float x = *reinterpret_cast<float *>(&v);
+ if (isnan(x) || (x < 0)) {
+ continue;
+ }
+
+ ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, x, __llvm_libc::sqrtf(x), 0.5);
+ }
+}
diff --git a/libc/test/src/math/sqrtl_test.cpp b/libc/test/src/math/sqrtl_test.cpp
new file mode 100644
index 000000000000..1fab3b2567e5
--- /dev/null
+++ b/libc/test/src/math/sqrtl_test.cpp
@@ -0,0 +1,67 @@
+//===-- Unittests for sqrtl ----------------------------------------------===//
+//
+// 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/sqrtl.h"
+#include "utils/FPUtil/FPBits.h"
+#include "utils/FPUtil/TestHelpers.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using FPBits = __llvm_libc::fputil::FPBits<long double>;
+using UIntType = typename FPBits::UIntType;
+
+namespace mpfr = __llvm_libc::testing::mpfr;
+
+constexpr UIntType HiddenBit =
+ UIntType(1) << __llvm_libc::fputil::MantissaWidth<long double>::value;
+
+long double nan = FPBits::buildNaN(1);
+long double inf = FPBits::inf();
+long double negInf = FPBits::negInf();
+
+TEST(SqrtlTest, SpecialValues) {
+ ASSERT_FP_EQ(nan, __llvm_libc::sqrtl(nan));
+ ASSERT_FP_EQ(inf, __llvm_libc::sqrtl(inf));
+ ASSERT_FP_EQ(nan, __llvm_libc::sqrtl(negInf));
+ ASSERT_FP_EQ(0.0L, __llvm_libc::sqrtl(0.0L));
+ ASSERT_FP_EQ(-0.0L, __llvm_libc::sqrtl(-0.0L));
+ ASSERT_FP_EQ(nan, __llvm_libc::sqrtl(-1.0L));
+ ASSERT_FP_EQ(1.0L, __llvm_libc::sqrtl(1.0L));
+ ASSERT_FP_EQ(2.0L, __llvm_libc::sqrtl(4.0L));
+ ASSERT_FP_EQ(3.0L, __llvm_libc::sqrtl(9.0L));
+}
+
+TEST(SqrtlTest, DenormalValues) {
+ for (UIntType mant = 1; mant < HiddenBit; mant <<= 1) {
+ FPBits denormal(0.0L);
+ denormal.mantissa = mant;
+
+ ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, static_cast<long double>(denormal),
+ __llvm_libc::sqrtl(denormal), 0.5);
+ }
+
+ constexpr UIntType count = 1'000'001;
+ constexpr UIntType step = HiddenBit / count;
+ for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
+ long double x = *reinterpret_cast<long double *>(&v);
+ ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, x, __llvm_libc::sqrtl(x), 0.5);
+ }
+}
+
+TEST(SqrtlTest, InLongDoubleRange) {
+ constexpr UIntType count = 10'000'001;
+ constexpr UIntType step = UIntType(-1) / count;
+ for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
+ long double x = *reinterpret_cast<long double *>(&v);
+ if (isnan(x) || (x < 0)) {
+ continue;
+ }
+
+ ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, x, __llvm_libc::sqrtl(x), 0.5);
+ }
+}