diff options
author | Tue Ly <lntue@google.com> | 2020-09-17 23:22:18 -0400 |
---|---|---|
committer | Tue Ly <lntue@google.com> | 2020-12-03 11:08:20 -0500 |
commit | 3b487d51e2ec699c27387fc30374f0d035b2a482 (patch) | |
tree | 34ee4daf3342133c5e52d31bf79ec2a38f1c8d22 /libc/test | |
parent | c00516d52054e31179ce921a35a3815032feed0f (diff) | |
download | llvm-project-3b487d51e2ec699c27387fc30374f0d035b2a482.tar.gz |
[libc] Add implementation of hypot.
Refactor src/math/hypotf.cpp and test/src/math/hypotf_test.cpp and reuse them for hypot and hypot_test
Differential Revision: https://reviews.llvm.org/D91831
Diffstat (limited to 'libc/test')
-rw-r--r-- | libc/test/src/math/CMakeLists.txt | 13 | ||||
-rw-r--r-- | libc/test/src/math/HypotTest.h | 75 | ||||
-rw-r--r-- | libc/test/src/math/hypot_test.cpp | 20 | ||||
-rw-r--r-- | libc/test/src/math/hypotf_test.cpp | 55 |
4 files changed, 115 insertions, 48 deletions
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt index cdffe737d8df..8635e7aba427 100644 --- a/libc/test/src/math/CMakeLists.txt +++ b/libc/test/src/math/CMakeLists.txt @@ -736,3 +736,16 @@ add_fp_unittest( libc.src.math.hypotf libc.utils.FPUtil.fputil ) + +add_fp_unittest( + hypot_test + NEED_MPFR + SUITE + libc_math_unittests + SRCS + hypot_test.cpp + DEPENDS + libc.include.math + libc.src.math.hypot + libc.utils.FPUtil.fputil +) diff --git a/libc/test/src/math/HypotTest.h b/libc/test/src/math/HypotTest.h new file mode 100644 index 000000000000..f90807b62c5f --- /dev/null +++ b/libc/test/src/math/HypotTest.h @@ -0,0 +1,75 @@ +//===-- Utility class to test different flavors of hypot ------------------===// +// +// 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_TEST_SRC_MATH_HYPOTTEST_H +#define LLVM_LIBC_TEST_SRC_MATH_HYPOTTEST_H + +#include "include/math.h" +#include "utils/FPUtil/FPBits.h" +#include "utils/FPUtil/Hypot.h" +#include "utils/FPUtil/TestHelpers.h" +#include "utils/MPFRWrapper/MPFRUtils.h" +#include "utils/UnitTest/Test.h" + +namespace mpfr = __llvm_libc::testing::mpfr; + +template <typename T> +class HypotTestTemplate : public __llvm_libc::testing::Test { +private: + using Func = T (*)(T, T); + using FPBits = __llvm_libc::fputil::FPBits<T>; + using UIntType = typename FPBits::UIntType; + const T nan = __llvm_libc::fputil::FPBits<T>::buildNaN(1); + const T inf = __llvm_libc::fputil::FPBits<T>::inf(); + const T negInf = __llvm_libc::fputil::FPBits<T>::negInf(); + const T zero = __llvm_libc::fputil::FPBits<T>::zero(); + const T negZero = __llvm_libc::fputil::FPBits<T>::negZero(); + +public: + void testSpecialNumbers(Func func) { + EXPECT_FP_EQ(func(inf, nan), inf); + EXPECT_FP_EQ(func(nan, negInf), inf); + EXPECT_FP_EQ(func(zero, inf), inf); + EXPECT_FP_EQ(func(negInf, negZero), inf); + + EXPECT_FP_EQ(func(nan, nan), nan); + EXPECT_FP_EQ(func(nan, zero), nan); + EXPECT_FP_EQ(func(negZero, nan), nan); + + EXPECT_FP_EQ(func(negZero, zero), zero); + } + + void testSubnormalRange(Func func) { + constexpr UIntType count = 1000001; + constexpr UIntType step = + (FPBits::maxSubnormal - FPBits::minSubnormal) / count; + for (UIntType v = FPBits::minSubnormal, w = FPBits::maxSubnormal; + v <= FPBits::maxSubnormal && w >= FPBits::minSubnormal; + v += step, w -= step) { + T x = FPBits(v), y = FPBits(w); + T result = func(x, y); + mpfr::BinaryInput<T> input{x, y}; + ASSERT_MPFR_MATCH(mpfr::Operation::Hypot, input, result, 0.5); + } + } + + void testNormalRange(Func func) { + constexpr UIntType count = 1000001; + constexpr UIntType step = (FPBits::maxNormal - FPBits::minNormal) / count; + for (UIntType v = FPBits::minNormal, w = FPBits::maxNormal; + v <= FPBits::maxNormal && w >= FPBits::minNormal; + v += step, w -= step) { + T x = FPBits(v), y = FPBits(w); + T result = func(x, y); + mpfr::BinaryInput<T> input{x, y}; + ASSERT_MPFR_MATCH(mpfr::Operation::Hypot, input, result, 0.5); + } + } +}; + +#endif // LLVM_LIBC_TEST_SRC_MATH_HYPOTTEST_H diff --git a/libc/test/src/math/hypot_test.cpp b/libc/test/src/math/hypot_test.cpp new file mode 100644 index 000000000000..d723f5264afc --- /dev/null +++ b/libc/test/src/math/hypot_test.cpp @@ -0,0 +1,20 @@ +//===-- Unittests for hypot -----------------------------------------------===// +// +// 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 "HypotTest.h" + +#include "include/math.h" +#include "src/math/hypot.h" + +using HypotTest = HypotTestTemplate<double>; + +TEST_F(HypotTest, SpecialNumbers) { testSpecialNumbers(&__llvm_libc::hypot); } + +TEST_F(HypotTest, SubnormalRange) { testSubnormalRange(&__llvm_libc::hypot); } + +TEST_F(HypotTest, NormalRange) { testNormalRange(&__llvm_libc::hypot); } diff --git a/libc/test/src/math/hypotf_test.cpp b/libc/test/src/math/hypotf_test.cpp index 1769307099a9..21d1bea03291 100644 --- a/libc/test/src/math/hypotf_test.cpp +++ b/libc/test/src/math/hypotf_test.cpp @@ -6,56 +6,15 @@ // //===----------------------------------------------------------------------===// -#include "src/math/hypotf.h" -#include "utils/FPUtil/FPBits.h" -#include "utils/FPUtil/TestHelpers.h" -#include "utils/MPFRWrapper/MPFRUtils.h" -#include "utils/UnitTest/Test.h" -#include <math.h> - -using FPBits = __llvm_libc::fputil::FPBits<float>; -using UIntType = FPBits::UIntType; - -namespace mpfr = __llvm_libc::testing::mpfr; +#include "HypotTest.h" -DECLARE_SPECIAL_CONSTANTS(float) - -TEST(HypotfTest, SpecialNumbers) { - EXPECT_FP_EQ(__llvm_libc::hypotf(inf, nan), inf); - EXPECT_FP_EQ(__llvm_libc::hypotf(nan, negInf), inf); - EXPECT_FP_EQ(__llvm_libc::hypotf(zero, inf), inf); - EXPECT_FP_EQ(__llvm_libc::hypotf(negInf, negZero), inf); +#include "include/math.h" +#include "src/math/hypotf.h" - EXPECT_FP_EQ(__llvm_libc::hypotf(nan, nan), nan); - EXPECT_FP_EQ(__llvm_libc::hypotf(nan, zero), nan); - EXPECT_FP_EQ(__llvm_libc::hypotf(negZero, nan), nan); +using HypotfTest = HypotTestTemplate<float>; - EXPECT_FP_EQ(__llvm_libc::hypotf(negZero, zero), zero); -} +TEST_F(HypotfTest, SpecialNumbers) { testSpecialNumbers(&__llvm_libc::hypotf); } -TEST(HypotfTest, SubnormalRange) { - constexpr UIntType count = 1000001; - constexpr UIntType step = - (FPBits::maxSubnormal - FPBits::minSubnormal) / count; - for (UIntType v = FPBits::minSubnormal, w = FPBits::maxSubnormal; - v <= FPBits::maxSubnormal && w >= FPBits::minSubnormal; - v += step, w -= step) { - float x = FPBits(v), y = FPBits(w); - float result = __llvm_libc::hypotf(x, y); - mpfr::BinaryInput<float> input{x, y}; - ASSERT_MPFR_MATCH(mpfr::Operation::Hypot, input, result, 0.5); - } -} +TEST_F(HypotfTest, SubnormalRange) { testSubnormalRange(&__llvm_libc::hypotf); } -TEST(HypotfTest, NormalRange) { - constexpr UIntType count = 1000001; - constexpr UIntType step = (FPBits::maxNormal - FPBits::minNormal) / count; - for (UIntType v = FPBits::minNormal, w = FPBits::maxNormal; - v <= FPBits::maxNormal && w >= FPBits::minNormal; v += step, w -= step) { - float x = FPBits(v), y = FPBits(w); - float result = __llvm_libc::hypotf(x, y); - ; - mpfr::BinaryInput<float> input{x, y}; - ASSERT_MPFR_MATCH(mpfr::Operation::Hypot, input, result, 0.5); - } -} +TEST_F(HypotfTest, NormalRange) { testNormalRange(&__llvm_libc::hypotf); } |