diff options
author | Muhammad Bassiouni <60100307+bassiounix@users.noreply.github.com> | 2025-07-21 20:48:57 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-07-21 20:48:57 +0300 |
commit | 2865f1ba966c21d4ebff610875394ce9c7a5ff38 (patch) | |
tree | c92b6f4e6c71872a831dbbc2e2e2300394e28e57 /libc | |
parent | f7347e9f784860d9482ad8fe757761514cceff31 (diff) | |
download | llvm-2865f1ba966c21d4ebff610875394ce9c7a5ff38.zip llvm-2865f1ba966c21d4ebff610875394ce9c7a5ff38.tar.gz llvm-2865f1ba966c21d4ebff610875394ce9c7a5ff38.tar.bz2 |
[libc][math] add smoke tests to shared/math.h (#149741)
Adding smoke tests for shared math header.
part of #147386
Diffstat (limited to 'libc')
-rw-r--r-- | libc/shared/math/exp10f16.h | 2 | ||||
-rw-r--r-- | libc/src/__support/math/acos.h | 6 | ||||
-rw-r--r-- | libc/src/__support/math/acosf.h | 8 | ||||
-rw-r--r-- | libc/src/__support/math/asin_utils.h | 4 | ||||
-rw-r--r-- | libc/src/__support/math/inv_trigf_utils.h | 4 | ||||
-rw-r--r-- | libc/src/math/generic/asin.cpp | 1 | ||||
-rw-r--r-- | libc/src/math/generic/asinf.cpp | 1 | ||||
-rw-r--r-- | libc/src/math/generic/atan2f.cpp | 1 | ||||
-rw-r--r-- | libc/src/math/generic/atanf.cpp | 1 | ||||
-rw-r--r-- | libc/test/CMakeLists.txt | 1 | ||||
-rw-r--r-- | libc/test/shared/CMakeLists.txt | 24 | ||||
-rw-r--r-- | libc/test/shared/shared_math_test.cpp | 65 |
12 files changed, 112 insertions, 6 deletions
diff --git a/libc/shared/math/exp10f16.h b/libc/shared/math/exp10f16.h index 8acdbdb..af00787 100644 --- a/libc/shared/math/exp10f16.h +++ b/libc/shared/math/exp10f16.h @@ -10,10 +10,10 @@ #define LLVM_LIBC_SHARED_MATH_EXP10F_H #include "include/llvm-libc-macros/float16-macros.h" +#include "shared/libc_common.h" #ifdef LIBC_TYPES_HAS_FLOAT16 -#include "shared/libc_common.h" #include "src/__support/math/exp10f16.h" namespace LIBC_NAMESPACE_DECL { diff --git a/libc/src/__support/math/acos.h b/libc/src/__support/math/acos.h index a7287f1..7c9fc76 100644 --- a/libc/src/__support/math/acos.h +++ b/libc/src/__support/math/acos.h @@ -24,10 +24,10 @@ namespace LIBC_NAMESPACE_DECL { namespace math { -using DoubleDouble = fputil::DoubleDouble; -using Float128 = fputil::DyadicFloat<128>; - static constexpr double acos(double x) { + using DoubleDouble = fputil::DoubleDouble; + using Float128 = fputil::DyadicFloat<128>; + using namespace asin_internal; using FPBits = fputil::FPBits<double>; FPBits xbits(x); diff --git a/libc/src/__support/math/acosf.h b/libc/src/__support/math/acosf.h index 941c39f..153087e 100644 --- a/libc/src/__support/math/acosf.h +++ b/libc/src/__support/math/acosf.h @@ -22,7 +22,10 @@ namespace LIBC_NAMESPACE_DECL { namespace math { +namespace acosf_internal { + #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS + static constexpr size_t N_EXCEPTS = 4; // Exceptional values when |x| <= 0.5 @@ -37,9 +40,14 @@ static constexpr fputil::ExceptValues<float, N_EXCEPTS> ACOSF_EXCEPTS = {{ // x = -0x1.04c444p-12, acosf(x) = 0x1.923p0 (RZ) {0xb9826222, 0x3fc91800, 1, 0, 1}, }}; + #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS +} // namespace acosf_internal + static constexpr float acosf(float x) { + using namespace acosf_internal; + using namespace inv_trigf_utils_internal; using FPBits = typename fputil::FPBits<float>; FPBits xbits(x); diff --git a/libc/src/__support/math/asin_utils.h b/libc/src/__support/math/asin_utils.h index 3146444..e0c9096 100644 --- a/libc/src/__support/math/asin_utils.h +++ b/libc/src/__support/math/asin_utils.h @@ -19,7 +19,7 @@ namespace LIBC_NAMESPACE_DECL { -namespace { +namespace asin_internal { using DoubleDouble = fputil::DoubleDouble; using Float128 = fputil::DyadicFloat<128>; @@ -567,7 +567,7 @@ LIBC_INLINE static constexpr Float128 asin_eval(const Float128 &u, #endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS -} // anonymous namespace +} // namespace asin_internal } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/__support/math/inv_trigf_utils.h b/libc/src/__support/math/inv_trigf_utils.h index b8f4914..4a8fbec 100644 --- a/libc/src/__support/math/inv_trigf_utils.h +++ b/libc/src/__support/math/inv_trigf_utils.h @@ -16,6 +16,8 @@ namespace LIBC_NAMESPACE_DECL { +namespace inv_trigf_utils_internal { + // PI and PI / 2 static constexpr double M_MATH_PI = 0x1.921fb54442d18p+1; static constexpr double M_MATH_PI_2 = 0x1.921fb54442d18p+0; @@ -175,6 +177,8 @@ LIBC_INLINE static double asin_eval(double xsq) { return fputil::multiply_add(xsq, r2, r1); } +} // namespace inv_trigf_utils_internal + } // namespace LIBC_NAMESPACE_DECL #endif // LLVM_LIBC_SRC___SUPPORT_MATH_INV_TRIGF_UTILS_H diff --git a/libc/src/math/generic/asin.cpp b/libc/src/math/generic/asin.cpp index c033597..d286fce 100644 --- a/libc/src/math/generic/asin.cpp +++ b/libc/src/math/generic/asin.cpp @@ -25,6 +25,7 @@ using DoubleDouble = fputil::DoubleDouble; using Float128 = fputil::DyadicFloat<128>; LLVM_LIBC_FUNCTION(double, asin, (double x)) { + using namespace asin_internal; using FPBits = fputil::FPBits<double>; FPBits xbits(x); diff --git a/libc/src/math/generic/asinf.cpp b/libc/src/math/generic/asinf.cpp index c8d6b38..77d6de9 100644 --- a/libc/src/math/generic/asinf.cpp +++ b/libc/src/math/generic/asinf.cpp @@ -44,6 +44,7 @@ static constexpr fputil::ExceptValues<float, N_EXCEPTS> ASINF_EXCEPTS_HI = {{ #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS LLVM_LIBC_FUNCTION(float, asinf, (float x)) { + using namespace inv_trigf_utils_internal; using FPBits = typename fputil::FPBits<float>; FPBits xbits(x); diff --git a/libc/src/math/generic/atan2f.cpp b/libc/src/math/generic/atan2f.cpp index 0a768494..32b977f 100644 --- a/libc/src/math/generic/atan2f.cpp +++ b/libc/src/math/generic/atan2f.cpp @@ -236,6 +236,7 @@ float atan2f_double_double(double num_d, double den_d, double q_d, int idx, // which is about rounding errors of double-double (2^-104). LLVM_LIBC_FUNCTION(float, atan2f, (float y, float x)) { + using namespace inv_trigf_utils_internal; using FPBits = typename fputil::FPBits<float>; constexpr double IS_NEG[2] = {1.0, -1.0}; constexpr double PI = 0x1.921fb54442d18p1; diff --git a/libc/src/math/generic/atanf.cpp b/libc/src/math/generic/atanf.cpp index d12456c..22f962e 100644 --- a/libc/src/math/generic/atanf.cpp +++ b/libc/src/math/generic/atanf.cpp @@ -20,6 +20,7 @@ namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(float, atanf, (float x)) { + using namespace inv_trigf_utils_internal; using FPBits = typename fputil::FPBits<float>; constexpr double FINAL_SIGN[2] = {1.0, -1.0}; diff --git a/libc/test/CMakeLists.txt b/libc/test/CMakeLists.txt index 1a0780f..011ad6ae 100644 --- a/libc/test/CMakeLists.txt +++ b/libc/test/CMakeLists.txt @@ -20,6 +20,7 @@ endif() add_subdirectory(src) add_subdirectory(utils) +add_subdirectory(shared) if(NOT LLVM_LIBC_FULL_BUILD) return() diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt new file mode 100644 index 0000000..07a4e73 --- /dev/null +++ b/libc/test/shared/CMakeLists.txt @@ -0,0 +1,24 @@ +add_custom_target(libc-shared-tests) + +add_fp_unittest( + shared_math_test + SUITE + libc-shared-tests + SRCS + shared_math_test.cpp + DEPENDS + libc.src.__support.math.acos + libc.src.__support.math.acosf + libc.src.__support.math.exp + libc.src.__support.math.exp10 + libc.src.__support.math.exp10f + libc.src.__support.math.exp10f16 + libc.src.__support.math.expf + libc.src.__support.math.expf16 + libc.src.__support.math.frexpf + libc.src.__support.math.frexpf128 + libc.src.__support.math.frexpf16 + libc.src.__support.math.ldexpf + libc.src.__support.math.ldexpf128 + libc.src.__support.math.ldexpf16 +) diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp new file mode 100644 index 0000000..40fea3b --- /dev/null +++ b/libc/test/shared/shared_math_test.cpp @@ -0,0 +1,65 @@ +//===-- Unittests for shared math functions -------------------------------===// +// +// 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 "shared/math.h" +#include "test/UnitTest/FPMatcher.h" + +#ifdef LIBC_TYPES_HAS_FLOAT16 + +TEST(LlvmLibcSharedMathTest, AllFloat16) { + int exponent; + + EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::exp10f16(0.0f16)); + + EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::expf16(0.0f16)); + + ASSERT_FP_EQ(float16(8 << 5), LIBC_NAMESPACE::shared::ldexpf16(float(8), 5)); + ASSERT_FP_EQ(float16(-1 * (8 << 5)), + LIBC_NAMESPACE::shared::ldexpf16(float(-8), 5)); + + EXPECT_FP_EQ_ALL_ROUNDING(0.75f16, + LIBC_NAMESPACE::shared::frexpf16(24.0f, &exponent)); + EXPECT_EQ(exponent, 5); +} + +#endif + +TEST(LlvmLibcSharedMathTest, AllFloat) { + int exponent; + + EXPECT_FP_EQ(0x1.921fb6p+0, LIBC_NAMESPACE::shared::acosf(0.0f)); + EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::exp10f(0.0f)); + EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::expf(0.0f)); + + EXPECT_FP_EQ_ALL_ROUNDING(0.75f, + LIBC_NAMESPACE::shared::frexpf(24.0f, &exponent)); + EXPECT_EQ(exponent, 5); + + ASSERT_FP_EQ(float(8 << 5), LIBC_NAMESPACE::shared::ldexpf(float(8), 5)); + ASSERT_FP_EQ(float(-1 * (8 << 5)), + LIBC_NAMESPACE::shared::ldexpf(float(-8), 5)); +} + +TEST(LlvmLibcSharedMathTest, AllDouble) { + EXPECT_FP_EQ(0x1.921fb54442d18p+0, LIBC_NAMESPACE::shared::acos(0.0)); + EXPECT_FP_EQ(0x1p+0, LIBC_NAMESPACE::shared::exp(0.0)); + EXPECT_FP_EQ(0x1p+0, LIBC_NAMESPACE::shared::exp10(0.0)); +} + +TEST(LlvmLibcSharedMathTest, AllFloat128) { + int exponent; + + EXPECT_FP_EQ_ALL_ROUNDING( + float128(0.75), LIBC_NAMESPACE::shared::frexpf128(24.0f, &exponent)); + EXPECT_EQ(exponent, 5); + + ASSERT_FP_EQ(float128(8 << 5), + LIBC_NAMESPACE::shared::ldexpf128(float(8), 5)); + ASSERT_FP_EQ(float128(-1 * (8 << 5)), + LIBC_NAMESPACE::shared::ldexpf128(float(-8), 5)); +} |