diff options
author | Siva Chandra Reddy <sivachandra@google.com> | 2020-11-03 16:30:16 -0800 |
---|---|---|
committer | Siva Chandra Reddy <sivachandra@google.com> | 2020-11-05 16:27:44 -0800 |
commit | 930cf1cb9fdb77138dedf5ac4afc2ebfc46d63a3 (patch) | |
tree | 9af658c77bc5ea252b8e7db11a3fdae8035fbe89 /libc/test | |
parent | e9e2e3107d6b4fb1bfdd877a83f1e214fcefea76 (diff) | |
download | llvm-project-930cf1cb9fdb77138dedf5ac4afc2ebfc46d63a3.tar.gz |
[libc] Add implementations of ilogb[f|l].
Depends on D90805.
Reviewed By: lntue
Differential Revision: https://reviews.llvm.org/D90806
Diffstat (limited to 'libc/test')
-rw-r--r-- | libc/test/src/math/CMakeLists.txt | 42 | ||||
-rw-r--r-- | libc/test/src/math/ILogbTest.h | 108 | ||||
-rw-r--r-- | libc/test/src/math/ilogb_test.cpp | 41 | ||||
-rw-r--r-- | libc/test/src/math/ilogbf_test.cpp | 41 | ||||
-rw-r--r-- | libc/test/src/math/ilogbl_test.cpp | 41 |
5 files changed, 273 insertions, 0 deletions
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt index a90736992f1f..4f8f77e4cf03 100644 --- a/libc/test/src/math/CMakeLists.txt +++ b/libc/test/src/math/CMakeLists.txt @@ -371,6 +371,48 @@ add_fp_unittest( ) add_fp_unittest( + ilogb_test + SUITE + libc_math_unittests + SRCS + ilogb_test.cpp + HDRS + ILogbTest.h + DEPENDS + libc.include.math + libc.src.math.ilogb + libc.utils.FPUtil.fputil +) + +add_fp_unittest( + ilogbf_test + SUITE + libc_math_unittests + SRCS + ilogbf_test.cpp + HDRS + ILogbTest.h + DEPENDS + libc.include.math + libc.src.math.ilogbf + libc.utils.FPUtil.fputil +) + +add_fp_unittest( + ilogbl_test + SUITE + libc_math_unittests + SRCS + ilogbl_test.cpp + HDRS + ILogbTest.h + DEPENDS + libc.include.math + libc.src.math.ilogbl + libc.utils.FPUtil.fputil +) + +add_fp_unittest( logb_test SUITE libc_math_unittests diff --git a/libc/test/src/math/ILogbTest.h b/libc/test/src/math/ILogbTest.h new file mode 100644 index 000000000000..001b419de6cd --- /dev/null +++ b/libc/test/src/math/ILogbTest.h @@ -0,0 +1,108 @@ +//===-- Utility class to test different flavors of ilogb --------*- 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_TEST_SRC_MATH_ILOGBTEST_H +#define LLVM_LIBC_TEST_SRC_MATH_ILOGBTEST_H + +#include "include/math.h" +#include "utils/FPUtil/FPBits.h" +#include "utils/FPUtil/ManipulationFunctions.h" +#include "utils/UnitTest/Test.h" + +#include <limits.h> + +class ILogbTest : public __llvm_libc::testing::Test { +public: + template <typename T> struct ILogbFunc { typedef int (*Func)(T); }; + + template <typename T> + void testSpecialNumbers(typename ILogbFunc<T>::Func func) { + EXPECT_EQ(FP_ILOGB0, func(__llvm_libc::fputil::FPBits<T>::zero())); + EXPECT_EQ(FP_ILOGB0, func(__llvm_libc::fputil::FPBits<T>::negZero())); + + EXPECT_EQ(FP_ILOGBNAN, func(__llvm_libc::fputil::FPBits<T>::buildNaN(1))); + + EXPECT_EQ(INT_MAX, func(__llvm_libc::fputil::FPBits<T>::inf())); + EXPECT_EQ(INT_MAX, func(__llvm_libc::fputil::FPBits<T>::negInf())); + } + + template <typename T> void testPowersOfTwo(typename ILogbFunc<T>::Func func) { + EXPECT_EQ(0, func(T(1.0))); + EXPECT_EQ(0, func(T(-1.0))); + + EXPECT_EQ(1, func(T(2.0))); + EXPECT_EQ(1, func(T(-2.0))); + + EXPECT_EQ(2, func(T(4.0))); + EXPECT_EQ(2, func(T(-4.0))); + + EXPECT_EQ(3, func(T(8.0))); + EXPECT_EQ(3, func(-8.0)); + + EXPECT_EQ(4, func(16.0)); + EXPECT_EQ(4, func(-16.0)); + + EXPECT_EQ(5, func(32.0)); + EXPECT_EQ(5, func(-32.0)); + } + + template <typename T> + void testSomeIntegers(typename ILogbFunc<T>::Func func) { + EXPECT_EQ(1, func(T(3.0))); + EXPECT_EQ(1, func(T(-3.0))); + + EXPECT_EQ(2, func(T(7.0))); + EXPECT_EQ(2, func(T(-7.0))); + + EXPECT_EQ(3, func(T(10.0))); + EXPECT_EQ(3, func(T(-10.0))); + + EXPECT_EQ(4, func(T(31.0))); + EXPECT_EQ(4, func(-31.0)); + + EXPECT_EQ(5, func(55.0)); + EXPECT_EQ(5, func(-55.0)); + } + + template <typename T> + void testSubnormalRange(typename ILogbFunc<T>::Func func) { + using FPBits = __llvm_libc::fputil::FPBits<T>; + using UIntType = typename FPBits::UIntType; + constexpr UIntType count = 1000001; + constexpr UIntType step = + (FPBits::maxSubnormal - FPBits::minSubnormal) / count; + for (UIntType v = FPBits::minSubnormal; v <= FPBits::maxSubnormal; + v += step) { + T x = FPBits(v); + if (isnan(x) || isinf(x) || x == 0.0) + continue; + + int exponent; + __llvm_libc::fputil::frexp(x, exponent); + ASSERT_EQ(exponent, func(x) + 1); + } + } + + template <typename T> void testNormalRange(typename ILogbFunc<T>::Func func) { + using FPBits = __llvm_libc::fputil::FPBits<T>; + using UIntType = typename FPBits::UIntType; + constexpr UIntType count = 1000001; + constexpr UIntType step = (FPBits::maxNormal - FPBits::minNormal) / count; + for (UIntType v = FPBits::minNormal; v <= FPBits::maxNormal; v += step) { + T x = FPBits(v); + if (isnan(x) || isinf(x) || x == 0.0) + continue; + + int exponent; + __llvm_libc::fputil::frexp(x, exponent); + ASSERT_EQ(exponent, func(x) + 1); + } + } +}; + +#endif // LLVM_LIBC_TEST_SRC_MATH_ILOGBTEST_H diff --git a/libc/test/src/math/ilogb_test.cpp b/libc/test/src/math/ilogb_test.cpp new file mode 100644 index 000000000000..cf71ac07be18 --- /dev/null +++ b/libc/test/src/math/ilogb_test.cpp @@ -0,0 +1,41 @@ +//===-- Unittests for ilogb -----------------------------------------------===// +// +// 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 "ILogbTest.h" + +#include "include/math.h" +#include "src/math/ilogb.h" +#include "utils/CPP/Functional.h" +#include "utils/FPUtil/FPBits.h" +#include "utils/FPUtil/ManipulationFunctions.h" +#include "utils/FPUtil/TestHelpers.h" +#include "utils/UnitTest/Test.h" + +#include <limits.h> + +using RunContext = __llvm_libc::testing::RunContext; + +TEST_F(ILogbTest, SpecialNumbers_ilogb) { + testSpecialNumbers<double>(&__llvm_libc::ilogb); +} + +TEST_F(ILogbTest, PowersOfTwo_ilogb) { + testPowersOfTwo<double>(&__llvm_libc::ilogb); +} + +TEST_F(ILogbTest, SomeIntegers_ilogb) { + testSomeIntegers<double>(&__llvm_libc::ilogb); +} + +TEST_F(ILogbTest, SubnormalRange_ilogb) { + testSubnormalRange<double>(&__llvm_libc::ilogb); +} + +TEST_F(ILogbTest, NormalRange_ilogb) { + testNormalRange<double>(&__llvm_libc::ilogb); +} diff --git a/libc/test/src/math/ilogbf_test.cpp b/libc/test/src/math/ilogbf_test.cpp new file mode 100644 index 000000000000..109dddeca7b1 --- /dev/null +++ b/libc/test/src/math/ilogbf_test.cpp @@ -0,0 +1,41 @@ +//===-- Unittests for ilogbf ----------------------------------------------===// +// +// 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 "ILogbTest.h" + +#include "include/math.h" +#include "src/math/ilogbf.h" +#include "utils/CPP/Functional.h" +#include "utils/FPUtil/FPBits.h" +#include "utils/FPUtil/ManipulationFunctions.h" +#include "utils/FPUtil/TestHelpers.h" +#include "utils/UnitTest/Test.h" + +#include <limits.h> + +using RunContext = __llvm_libc::testing::RunContext; + +TEST_F(ILogbTest, SpecialNumbers_ilogbf) { + testSpecialNumbers<float>(&__llvm_libc::ilogbf); +} + +TEST_F(ILogbTest, PowersOfTwo_ilogbf) { + testPowersOfTwo<float>(&__llvm_libc::ilogbf); +} + +TEST_F(ILogbTest, SomeIntegers_ilogbf) { + testSomeIntegers<float>(&__llvm_libc::ilogbf); +} + +TEST_F(ILogbTest, SubnormalRange_ilogbf) { + testSubnormalRange<float>(&__llvm_libc::ilogbf); +} + +TEST_F(ILogbTest, NormalRange_ilogbf) { + testNormalRange<float>(&__llvm_libc::ilogbf); +} diff --git a/libc/test/src/math/ilogbl_test.cpp b/libc/test/src/math/ilogbl_test.cpp new file mode 100644 index 000000000000..6404c6b00fd1 --- /dev/null +++ b/libc/test/src/math/ilogbl_test.cpp @@ -0,0 +1,41 @@ +//===-- Unittests for ilogbl ----------------------------------------------===// +// +// 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 "ILogbTest.h" + +#include "include/math.h" +#include "src/math/ilogbl.h" +#include "utils/CPP/Functional.h" +#include "utils/FPUtil/FPBits.h" +#include "utils/FPUtil/ManipulationFunctions.h" +#include "utils/FPUtil/TestHelpers.h" +#include "utils/UnitTest/Test.h" + +#include <limits.h> + +using RunContext = __llvm_libc::testing::RunContext; + +TEST_F(ILogbTest, SpecialNumbers_ilogbl) { + testSpecialNumbers<long double>(&__llvm_libc::ilogbl); +} + +TEST_F(ILogbTest, PowersOfTwo_ilogbl) { + testPowersOfTwo<long double>(&__llvm_libc::ilogbl); +} + +TEST_F(ILogbTest, SomeIntegers_ilogbl) { + testSomeIntegers<long double>(&__llvm_libc::ilogbl); +} + +TEST_F(ILogbTest, SubnormalRange_ilogbl) { + testSubnormalRange<long double>(&__llvm_libc::ilogbl); +} + +TEST_F(ILogbTest, NormalRange_ilogbl) { + testNormalRange<long double>(&__llvm_libc::ilogbl); +} |