diff options
author | OverMighty <its.overmighty@gmail.com> | 2024-06-25 14:48:28 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-25 08:48:28 -0400 |
commit | edbe698eadaefca31564e3207074c8ed4b10bc12 (patch) | |
tree | 17174a5451fb4f73859e9a586880d6442437dd6a /libc/test | |
parent | abc8c4be3bb70755328051c99e1b196663140c66 (diff) | |
download | llvm-edbe698eadaefca31564e3207074c8ed4b10bc12.zip llvm-edbe698eadaefca31564e3207074c8ed4b10bc12.tar.gz llvm-edbe698eadaefca31564e3207074c8ed4b10bc12.tar.bz2 |
[libc][math][c23] Add f16divf C23 math function (#96131)
Part of #93566.
Diffstat (limited to 'libc/test')
-rw-r--r-- | libc/test/src/math/CMakeLists.txt | 13 | ||||
-rw-r--r-- | libc/test/src/math/DivTest.h | 74 | ||||
-rw-r--r-- | libc/test/src/math/f16divf_test.cpp | 13 | ||||
-rw-r--r-- | libc/test/src/math/smoke/CMakeLists.txt | 14 | ||||
-rw-r--r-- | libc/test/src/math/smoke/DivTest.h | 171 | ||||
-rw-r--r-- | libc/test/src/math/smoke/f16divf_test.cpp | 13 |
6 files changed, 298 insertions, 0 deletions
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt index bb364c3..ba58866 100644 --- a/libc/test/src/math/CMakeLists.txt +++ b/libc/test/src/math/CMakeLists.txt @@ -1891,6 +1891,19 @@ add_fp_unittest( ) add_fp_unittest( + f16divf_test + NEED_MPFR + SUITE + libc-math-unittests + SRCS + f16divf_test.cpp + HDRS + DivTest.h + DEPENDS + libc.src.math.f16divf +) + +add_fp_unittest( f16fmaf_test NEED_MPFR SUITE diff --git a/libc/test/src/math/DivTest.h b/libc/test/src/math/DivTest.h new file mode 100644 index 0000000..1cdc139 --- /dev/null +++ b/libc/test/src/math/DivTest.h @@ -0,0 +1,74 @@ +//===-- Utility class to test different flavors of float div ----*- 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_DIVTEST_H +#define LLVM_LIBC_TEST_SRC_MATH_DIVTEST_H + +#include "test/UnitTest/FEnvSafeTest.h" +#include "test/UnitTest/FPMatcher.h" +#include "test/UnitTest/Test.h" +#include "utils/MPFRWrapper/MPFRUtils.h" + +namespace mpfr = LIBC_NAMESPACE::testing::mpfr; + +template <typename OutType, typename InType> +class DivTest : public LIBC_NAMESPACE::testing::FEnvSafeTest { + + struct InConstants { + DECLARE_SPECIAL_CONSTANTS(InType) + }; + + using InFPBits = typename InConstants::FPBits; + using InStorageType = typename InConstants::StorageType; + + static constexpr InStorageType IN_MAX_NORMAL_U = + InFPBits::max_normal().uintval(); + static constexpr InStorageType IN_MIN_NORMAL_U = + InFPBits::min_normal().uintval(); + static constexpr InStorageType IN_MAX_SUBNORMAL_U = + InFPBits::max_subnormal().uintval(); + static constexpr InStorageType IN_MIN_SUBNORMAL_U = + InFPBits::min_subnormal().uintval(); + +public: + using DivFunc = OutType (*)(InType, InType); + + void test_subnormal_range(DivFunc func) { + constexpr InStorageType COUNT = 100'001; + constexpr InStorageType STEP = + (IN_MAX_SUBNORMAL_U - IN_MIN_SUBNORMAL_U) / COUNT; + for (InStorageType i = 0, v = 0, w = IN_MAX_SUBNORMAL_U; i <= COUNT; + ++i, v += STEP, w -= STEP) { + InType x = InFPBits(v).get_val(); + InType y = InFPBits(w).get_val(); + mpfr::BinaryInput<InType> input{x, y}; + EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Div, input, func(x, y), + 0.5); + } + } + + void test_normal_range(DivFunc func) { + constexpr InStorageType COUNT = 100'001; + constexpr InStorageType STEP = (IN_MAX_NORMAL_U - IN_MIN_NORMAL_U) / COUNT; + for (InStorageType i = 0, v = 0, w = IN_MAX_NORMAL_U; i <= COUNT; + ++i, v += STEP, w -= STEP) { + InType x = InFPBits(v).get_val(); + InType y = InFPBits(w).get_val(); + mpfr::BinaryInput<InType> input{x, y}; + EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Div, input, func(x, y), + 0.5); + } + } +}; + +#define LIST_DIV_TESTS(OutType, InType, func) \ + using LlvmLibcDivTest = DivTest<OutType, InType>; \ + TEST_F(LlvmLibcDivTest, SubnormalRange) { test_subnormal_range(&func); } \ + TEST_F(LlvmLibcDivTest, NormalRange) { test_normal_range(&func); } + +#endif // LLVM_LIBC_TEST_SRC_MATH_DIVTEST_H diff --git a/libc/test/src/math/f16divf_test.cpp b/libc/test/src/math/f16divf_test.cpp new file mode 100644 index 0000000..85be1eb --- /dev/null +++ b/libc/test/src/math/f16divf_test.cpp @@ -0,0 +1,13 @@ +//===-- Unittests for f16divf ---------------------------------------------===// +// +// 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 "DivTest.h" + +#include "src/math/f16divf.h" + +LIST_DIV_TESTS(float16, float, LIBC_NAMESPACE::f16divf) diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt index ffa8ba1..ee99fb9 100644 --- a/libc/test/src/math/smoke/CMakeLists.txt +++ b/libc/test/src/math/smoke/CMakeLists.txt @@ -3631,6 +3631,20 @@ add_fp_unittest( ) add_fp_unittest( + f16divf_test + SUITE + libc-math-smoke-tests + SRCS + f16divf_test.cpp + HDRS + DivTest.h + DEPENDS + libc.hdr.fenv_macros + libc.src.__support.FPUtil.basic_operations + libc.src.math.f16divf +) + +add_fp_unittest( f16fmaf_test SUITE libc-math-smoke-tests diff --git a/libc/test/src/math/smoke/DivTest.h b/libc/test/src/math/smoke/DivTest.h new file mode 100644 index 0000000..71cfb32 --- /dev/null +++ b/libc/test/src/math/smoke/DivTest.h @@ -0,0 +1,171 @@ +//===-- Utility class to test different flavors of float div --------------===// +// +// 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_SMOKE_DIVTEST_H +#define LLVM_LIBC_TEST_SRC_MATH_SMOKE_DIVTEST_H + +#include "hdr/fenv_macros.h" +#include "src/__support/FPUtil/BasicOperations.h" +#include "test/UnitTest/FEnvSafeTest.h" +#include "test/UnitTest/FPMatcher.h" +#include "test/UnitTest/RoundingModeUtils.h" +#include "test/UnitTest/Test.h" + +template <typename OutType, typename InType> +class DivTest : public LIBC_NAMESPACE::testing::FEnvSafeTest { + + DECLARE_SPECIAL_CONSTANTS(OutType) + + struct InConstants { + DECLARE_SPECIAL_CONSTANTS(InType) + }; + + using InFPBits = typename InConstants::FPBits; + using InStorageType = typename InConstants::StorageType; + +public: + using DivFunc = OutType (*)(InType, InType); + + void test_special_numbers(DivFunc func) { + EXPECT_FP_IS_NAN(func(aNaN, aNaN)); + EXPECT_FP_IS_NAN_WITH_EXCEPTION(func(sNaN, sNaN), FE_INVALID); + + InType qnan_42 = InFPBits::quiet_nan(Sign::POS, 0x42).get_val(); + EXPECT_FP_EQ(InType(0x42.0p+0), + LIBC_NAMESPACE::fputil::getpayload(func(qnan_42, zero))); + EXPECT_FP_EQ(InType(0x42.0p+0), + LIBC_NAMESPACE::fputil::getpayload(func(zero, qnan_42))); + + if constexpr (sizeof(OutType) < sizeof(InType)) { + InStorageType max_payload = InFPBits::FRACTION_MASK >> 1; + InType qnan_max = InFPBits::quiet_nan(Sign::POS, max_payload).get_val(); + EXPECT_FP_EQ(zero, + LIBC_NAMESPACE::fputil::getpayload(func(qnan_max, zero))); + EXPECT_FP_EQ(zero, + LIBC_NAMESPACE::fputil::getpayload(func(zero, qnan_max))); + EXPECT_FP_EQ(InType(0x42.0p+0), + LIBC_NAMESPACE::fputil::getpayload(func(qnan_max, qnan_42))); + EXPECT_FP_EQ(InType(0x42.0p+0), + LIBC_NAMESPACE::fputil::getpayload(func(qnan_42, qnan_max))); + } + + EXPECT_FP_EQ(inf, func(inf, zero)); + EXPECT_FP_EQ(neg_inf, func(neg_inf, zero)); + EXPECT_FP_EQ(neg_inf, func(inf, neg_zero)); + EXPECT_FP_EQ(inf, func(neg_inf, neg_zero)); + } + + void test_division_by_zero(DivFunc func) { + EXPECT_FP_EQ_WITH_EXCEPTION(inf, func(InType(1.0), zero), FE_DIVBYZERO); + EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, func(InType(-1.0), zero), + FE_DIVBYZERO); + EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, func(InType(1.0), neg_zero), + FE_DIVBYZERO); + EXPECT_FP_EQ_WITH_EXCEPTION(inf, func(InType(1.0), zero), FE_DIVBYZERO); + } + + void test_invalid_operations(DivFunc func) { + EXPECT_FP_IS_NAN_WITH_EXCEPTION(func(zero, zero), FE_INVALID); + EXPECT_FP_IS_NAN_WITH_EXCEPTION(func(neg_zero, zero), FE_INVALID); + EXPECT_FP_IS_NAN_WITH_EXCEPTION(func(zero, neg_zero), FE_INVALID); + EXPECT_FP_IS_NAN_WITH_EXCEPTION(func(neg_zero, neg_zero), FE_INVALID); + + EXPECT_FP_IS_NAN_WITH_EXCEPTION(func(inf, inf), FE_INVALID); + EXPECT_MATH_ERRNO(EDOM); + EXPECT_FP_IS_NAN_WITH_EXCEPTION(func(neg_inf, inf), FE_INVALID); + EXPECT_MATH_ERRNO(EDOM); + EXPECT_FP_IS_NAN_WITH_EXCEPTION(func(inf, neg_inf), FE_INVALID); + EXPECT_MATH_ERRNO(EDOM); + EXPECT_FP_IS_NAN_WITH_EXCEPTION(func(neg_inf, neg_inf), FE_INVALID); + EXPECT_MATH_ERRNO(EDOM); + } + + void test_range_errors(DivFunc func) { + using namespace LIBC_NAMESPACE::fputil::testing; + + if (ForceRoundingMode r(RoundingMode::Nearest); r.success) { + EXPECT_FP_EQ_WITH_EXCEPTION(inf, func(max_normal, min_normal), + FE_OVERFLOW | FE_INEXACT); + EXPECT_MATH_ERRNO(ERANGE); + EXPECT_FP_EQ_WITH_EXCEPTION(-inf, func(neg_max_normal, min_denormal), + FE_OVERFLOW | FE_INEXACT); + EXPECT_MATH_ERRNO(ERANGE); + + EXPECT_FP_EQ_WITH_EXCEPTION(zero, func(min_denormal, max_normal), + FE_UNDERFLOW | FE_INEXACT); + EXPECT_MATH_ERRNO(ERANGE); + EXPECT_FP_EQ_WITH_EXCEPTION(neg_zero, func(neg_min_denormal, max_normal), + FE_UNDERFLOW | FE_INEXACT); + EXPECT_MATH_ERRNO(ERANGE); + } + + if (ForceRoundingMode r(RoundingMode::TowardZero); r.success) { + EXPECT_FP_EQ_WITH_EXCEPTION(max_normal, func(max_normal, min_normal), + FE_OVERFLOW | FE_INEXACT); + EXPECT_FP_EQ_WITH_EXCEPTION(neg_max_normal, + func(neg_max_normal, min_denormal), + FE_OVERFLOW | FE_INEXACT); + + EXPECT_FP_EQ_WITH_EXCEPTION(zero, func(min_denormal, max_normal), + FE_UNDERFLOW | FE_INEXACT); + EXPECT_MATH_ERRNO(ERANGE); + EXPECT_FP_EQ_WITH_EXCEPTION(neg_zero, func(neg_min_denormal, max_normal), + FE_UNDERFLOW | FE_INEXACT); + EXPECT_MATH_ERRNO(ERANGE); + } + + if (ForceRoundingMode r(RoundingMode::Downward); r.success) { + EXPECT_FP_EQ_WITH_EXCEPTION(max_normal, func(max_normal, min_normal), + FE_OVERFLOW | FE_INEXACT); + EXPECT_FP_EQ_WITH_EXCEPTION(-inf, func(neg_max_normal, min_denormal), + FE_OVERFLOW | FE_INEXACT); + EXPECT_MATH_ERRNO(ERANGE); + + EXPECT_FP_EQ_WITH_EXCEPTION(zero, func(min_denormal, max_normal), + FE_UNDERFLOW | FE_INEXACT); + EXPECT_MATH_ERRNO(ERANGE); + EXPECT_FP_EQ_WITH_EXCEPTION(neg_min_denormal, + func(neg_min_denormal, max_normal), + FE_UNDERFLOW | FE_INEXACT); + EXPECT_MATH_ERRNO(ERANGE); + } + + if (ForceRoundingMode r(RoundingMode::Upward); r.success) { + EXPECT_FP_EQ_WITH_EXCEPTION(inf, func(max_normal, min_normal), + FE_OVERFLOW | FE_INEXACT); + EXPECT_MATH_ERRNO(ERANGE); + EXPECT_FP_EQ_WITH_EXCEPTION(neg_max_normal, + func(neg_max_normal, min_denormal), + FE_OVERFLOW | FE_INEXACT); + + EXPECT_FP_EQ_WITH_EXCEPTION(min_denormal, func(min_denormal, max_normal), + FE_UNDERFLOW | FE_INEXACT); + EXPECT_MATH_ERRNO(ERANGE); + EXPECT_FP_EQ_WITH_EXCEPTION(neg_zero, func(neg_min_denormal, max_normal), + FE_UNDERFLOW | FE_INEXACT); + EXPECT_MATH_ERRNO(ERANGE); + } + } + + void test_inexact_results(DivFunc func) { + func(InType(1.0), InType(3.0)); + EXPECT_FP_EXCEPTION(FE_INEXACT); + } +}; + +#define LIST_DIV_TESTS(OutType, InType, func) \ + using LlvmLibcDivTest = DivTest<OutType, InType>; \ + TEST_F(LlvmLibcDivTest, SpecialNumbers) { test_special_numbers(&func); } \ + TEST_F(LlvmLibcDivTest, DivisionByZero) { test_division_by_zero(&func); } \ + TEST_F(LlvmLibcDivTest, InvalidOperations) { \ + test_invalid_operations(&func); \ + } \ + TEST_F(LlvmLibcDivTest, RangeErrors) { test_range_errors(&func); } \ + TEST_F(LlvmLibcDivTest, InexactResults) { test_inexact_results(&func); } + +#endif // LLVM_LIBC_TEST_SRC_MATH_SMOKE_DIVTEST_H diff --git a/libc/test/src/math/smoke/f16divf_test.cpp b/libc/test/src/math/smoke/f16divf_test.cpp new file mode 100644 index 0000000..85be1eb --- /dev/null +++ b/libc/test/src/math/smoke/f16divf_test.cpp @@ -0,0 +1,13 @@ +//===-- Unittests for f16divf ---------------------------------------------===// +// +// 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 "DivTest.h" + +#include "src/math/f16divf.h" + +LIST_DIV_TESTS(float16, float, LIBC_NAMESPACE::f16divf) |