diff options
author | OverMighty <its.overmighty@gmail.com> | 2024-06-13 18:57:24 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-13 12:57:24 -0400 |
commit | a2393435217a0c8832b6b34e13977bb29d722d39 (patch) | |
tree | 2808e6c790d9157b28c4e391ce2d4ff9a9991190 /libc/test | |
parent | 389142efa0976436aa9210af014a5c92bcb53cdd (diff) | |
download | llvm-a2393435217a0c8832b6b34e13977bb29d722d39.zip llvm-a2393435217a0c8832b6b34e13977bb29d722d39.tar.gz llvm-a2393435217a0c8832b6b34e13977bb29d722d39.tar.bz2 |
[libc][math][c23] Add f16sqrtf C23 math function (#95251)
Part of #95250.
Diffstat (limited to 'libc/test')
-rw-r--r-- | libc/test/src/math/exhaustive/CMakeLists.txt | 15 | ||||
-rw-r--r-- | libc/test/src/math/exhaustive/exhaustive_test.h | 21 | ||||
-rw-r--r-- | libc/test/src/math/exhaustive/f16sqrtf_test.cpp | 25 | ||||
-rw-r--r-- | libc/test/src/math/smoke/CMakeLists.txt | 40 | ||||
-rw-r--r-- | libc/test/src/math/smoke/SqrtTest.h | 30 | ||||
-rw-r--r-- | libc/test/src/math/smoke/f16sqrtf_test.cpp | 13 |
6 files changed, 108 insertions, 36 deletions
diff --git a/libc/test/src/math/exhaustive/CMakeLists.txt b/libc/test/src/math/exhaustive/CMakeLists.txt index 938e519..34df872 100644 --- a/libc/test/src/math/exhaustive/CMakeLists.txt +++ b/libc/test/src/math/exhaustive/CMakeLists.txt @@ -420,3 +420,18 @@ add_fp_unittest( LINK_LIBRARIES -lpthread ) + +add_fp_unittest( + f16sqrtf_test + NO_RUN_POSTBUILD + NEED_MPFR + SUITE + libc_math_exhaustive_tests + SRCS + f16sqrtf_test.cpp + DEPENDS + .exhaustive_test + libc.src.math.f16sqrtf + LINK_LIBRARIES + -lpthread +) diff --git a/libc/test/src/math/exhaustive/exhaustive_test.h b/libc/test/src/math/exhaustive/exhaustive_test.h index c4ae382..13e2727 100644 --- a/libc/test/src/math/exhaustive/exhaustive_test.h +++ b/libc/test/src/math/exhaustive/exhaustive_test.h @@ -35,16 +35,16 @@ // LlvmLibcUnaryOpExhaustiveMathTest<FloatType, Op, Func>. namespace mpfr = LIBC_NAMESPACE::testing::mpfr; -template <typename T> using UnaryOp = T(T); +template <typename OutType, typename InType = OutType> +using UnaryOp = OutType(InType); -template <typename T, mpfr::Operation Op, UnaryOp<T> Func> +template <typename OutType, typename InType, mpfr::Operation Op, + UnaryOp<OutType, InType> Func> struct UnaryOpChecker : public virtual LIBC_NAMESPACE::testing::Test { - using FloatType = T; + using FloatType = InType; using FPBits = LIBC_NAMESPACE::fputil::FPBits<FloatType>; using StorageType = typename FPBits::StorageType; - static constexpr UnaryOp<FloatType> *FUNC = Func; - // Check in a range, return the number of failures. uint64_t check(StorageType start, StorageType stop, mpfr::RoundingMode rounding) { @@ -57,11 +57,11 @@ struct UnaryOpChecker : public virtual LIBC_NAMESPACE::testing::Test { FPBits xbits(bits); FloatType x = xbits.get_val(); bool correct = - TEST_MPFR_MATCH_ROUNDING_SILENTLY(Op, x, FUNC(x), 0.5, rounding); + TEST_MPFR_MATCH_ROUNDING_SILENTLY(Op, x, Func(x), 0.5, rounding); failed += (!correct); // Uncomment to print out failed values. // if (!correct) { - // TEST_MPFR_MATCH(Op::Operation, x, Op::func(x), 0.5, rounding); + // EXPECT_MPFR_MATCH_ROUNDING(Op, x, Func(x), 0.5, rounding); // } } while (bits++ < stop); return failed; @@ -169,4 +169,9 @@ struct LlvmLibcExhaustiveMathTest template <typename FloatType, mpfr::Operation Op, UnaryOp<FloatType> Func> using LlvmLibcUnaryOpExhaustiveMathTest = - LlvmLibcExhaustiveMathTest<UnaryOpChecker<FloatType, Op, Func>>; + LlvmLibcExhaustiveMathTest<UnaryOpChecker<FloatType, FloatType, Op, Func>>; + +template <typename OutType, typename InType, mpfr::Operation Op, + UnaryOp<OutType, InType> Func> +using LlvmLibcUnaryNarrowingOpExhaustiveMathTest = + LlvmLibcExhaustiveMathTest<UnaryOpChecker<OutType, InType, Op, Func>>; diff --git a/libc/test/src/math/exhaustive/f16sqrtf_test.cpp b/libc/test/src/math/exhaustive/f16sqrtf_test.cpp new file mode 100644 index 0000000..3a42ff8 --- /dev/null +++ b/libc/test/src/math/exhaustive/f16sqrtf_test.cpp @@ -0,0 +1,25 @@ +//===-- Exhaustive test for f16sqrtf --------------------------------------===// +// +// 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 "exhaustive_test.h" +#include "src/math/f16sqrtf.h" +#include "utils/MPFRWrapper/MPFRUtils.h" + +namespace mpfr = LIBC_NAMESPACE::testing::mpfr; + +using LlvmLibcF16sqrtfExhaustiveTest = + LlvmLibcUnaryNarrowingOpExhaustiveMathTest< + float16, float, mpfr::Operation::Sqrt, LIBC_NAMESPACE::f16sqrtf>; + +// Range: [0, Inf]; +static constexpr uint32_t POS_START = 0x0000'0000U; +static constexpr uint32_t POS_STOP = 0x7f80'0000U; + +TEST_F(LlvmLibcF16sqrtfExhaustiveTest, PostiveRange) { + test_full_range_all_roundings(POS_START, POS_STOP); +} diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt index 68cd412..3bb87d2 100644 --- a/libc/test/src/math/smoke/CMakeLists.txt +++ b/libc/test/src/math/smoke/CMakeLists.txt @@ -2504,9 +2504,10 @@ add_fp_unittest( libc-math-smoke-tests SRCS sqrtf_test.cpp + HDRS + SqrtTest.h DEPENDS libc.src.math.sqrtf - libc.src.__support.FPUtil.fp_bits ) add_fp_unittest( @@ -2515,9 +2516,10 @@ add_fp_unittest( libc-math-smoke-tests SRCS sqrt_test.cpp + HDRS + SqrtTest.h DEPENDS libc.src.math.sqrt - libc.src.__support.FPUtil.fp_bits ) add_fp_unittest( @@ -2526,9 +2528,10 @@ add_fp_unittest( libc-math-smoke-tests SRCS sqrtl_test.cpp + HDRS + SqrtTest.h DEPENDS libc.src.math.sqrtl - libc.src.__support.FPUtil.fp_bits ) add_fp_unittest( @@ -2537,9 +2540,10 @@ add_fp_unittest( libc-math-smoke-tests SRCS sqrtf128_test.cpp + HDRS + SqrtTest.h DEPENDS libc.src.math.sqrtf128 - libc.src.__support.FPUtil.fp_bits ) add_fp_unittest( @@ -2548,9 +2552,9 @@ add_fp_unittest( libc-math-smoke-tests SRCS generic_sqrtf_test.cpp + HDRS + SqrtTest.h DEPENDS - libc.src.math.sqrtf - libc.src.__support.FPUtil.fp_bits libc.src.__support.FPUtil.generic.sqrt COMPILE_OPTIONS -O3 @@ -2562,9 +2566,9 @@ add_fp_unittest( libc-math-smoke-tests SRCS generic_sqrt_test.cpp + HDRS + SqrtTest.h DEPENDS - libc.src.math.sqrt - libc.src.__support.FPUtil.fp_bits libc.src.__support.FPUtil.generic.sqrt COMPILE_OPTIONS -O3 @@ -2576,9 +2580,9 @@ add_fp_unittest( libc-math-smoke-tests SRCS generic_sqrtl_test.cpp + HDRS + SqrtTest.h DEPENDS - libc.src.math.sqrtl - libc.src.__support.FPUtil.fp_bits libc.src.__support.FPUtil.generic.sqrt COMPILE_OPTIONS -O3 @@ -2590,9 +2594,9 @@ add_fp_unittest( libc-math-smoke-tests SRCS generic_sqrtf128_test.cpp + HDRS + SqrtTest.h DEPENDS - libc.src.math.sqrtf128 - libc.src.__support.FPUtil.fp_bits libc.src.__support.FPUtil.generic.sqrt COMPILE_OPTIONS -O3 @@ -3543,3 +3547,15 @@ add_fp_unittest( DEPENDS libc.src.math.totalordermagf16 ) + +add_fp_unittest( + f16sqrtf_test + SUITE + libc-math-smoke-tests + SRCS + f16sqrtf_test.cpp + HDRS + SqrtTest.h + DEPENDS + libc.src.math.f16sqrtf +) diff --git a/libc/test/src/math/smoke/SqrtTest.h b/libc/test/src/math/smoke/SqrtTest.h index 8afacaf..ce9f2f8 100644 --- a/libc/test/src/math/smoke/SqrtTest.h +++ b/libc/test/src/math/smoke/SqrtTest.h @@ -6,37 +6,35 @@ // //===----------------------------------------------------------------------===// -#include "src/__support/CPP/bit.h" #include "test/UnitTest/FEnvSafeTest.h" #include "test/UnitTest/FPMatcher.h" #include "test/UnitTest/Test.h" -#include "hdr/math_macros.h" - -template <typename T> +template <typename OutType, typename InType> class SqrtTest : public LIBC_NAMESPACE::testing::FEnvSafeTest { - DECLARE_SPECIAL_CONSTANTS(T) - - static constexpr StorageType HIDDEN_BIT = - StorageType(1) << LIBC_NAMESPACE::fputil::FPBits<T>::FRACTION_LEN; + DECLARE_SPECIAL_CONSTANTS(OutType) public: - typedef T (*SqrtFunc)(T); + typedef OutType (*SqrtFunc)(InType); void test_special_numbers(SqrtFunc func) { ASSERT_FP_EQ(aNaN, func(aNaN)); ASSERT_FP_EQ(inf, func(inf)); ASSERT_FP_EQ(aNaN, func(neg_inf)); - ASSERT_FP_EQ(0.0, func(0.0)); - ASSERT_FP_EQ(-0.0, func(-0.0)); - ASSERT_FP_EQ(aNaN, func(T(-1.0))); - ASSERT_FP_EQ(T(1.0), func(T(1.0))); - ASSERT_FP_EQ(T(2.0), func(T(4.0))); - ASSERT_FP_EQ(T(3.0), func(T(9.0))); + ASSERT_FP_EQ(zero, func(zero)); + ASSERT_FP_EQ(neg_zero, func(neg_zero)); + ASSERT_FP_EQ(aNaN, func(InType(-1.0))); + ASSERT_FP_EQ(OutType(1.0), func(InType(1.0))); + ASSERT_FP_EQ(OutType(2.0), func(InType(4.0))); + ASSERT_FP_EQ(OutType(3.0), func(InType(9.0))); } }; #define LIST_SQRT_TESTS(T, func) \ - using LlvmLibcSqrtTest = SqrtTest<T>; \ + using LlvmLibcSqrtTest = SqrtTest<T, T>; \ + TEST_F(LlvmLibcSqrtTest, SpecialNumbers) { test_special_numbers(&func); } + +#define LIST_NARROWING_SQRT_TESTS(OutType, InType, func) \ + using LlvmLibcSqrtTest = SqrtTest<OutType, InType>; \ TEST_F(LlvmLibcSqrtTest, SpecialNumbers) { test_special_numbers(&func); } diff --git a/libc/test/src/math/smoke/f16sqrtf_test.cpp b/libc/test/src/math/smoke/f16sqrtf_test.cpp new file mode 100644 index 0000000..36231ae --- /dev/null +++ b/libc/test/src/math/smoke/f16sqrtf_test.cpp @@ -0,0 +1,13 @@ +//===-- Unittests for f16sqrtf --------------------------------------------===// +// +// 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 "SqrtTest.h" + +#include "src/math/f16sqrtf.h" + +LIST_NARROWING_SQRT_TESTS(float16, float, LIBC_NAMESPACE::f16sqrtf) |