[libc-commits] [libc] [libc][math] add smoke tests to shared/math.h (PR #149741)
Muhammad Bassiouni via libc-commits
libc-commits at lists.llvm.org
Sun Jul 20 14:42:53 PDT 2025
https://github.com/bassiounix updated https://github.com/llvm/llvm-project/pull/149741
>From b6764aec1b66dbaa997aad46b8e9c2cef0fd4bca Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Mon, 21 Jul 2025 00:29:02 +0300
Subject: [PATCH 1/2] [libc][math] add smoke tests to shared/math.h
---
libc/src/__support/math/acos.h | 6 +--
libc/src/__support/math/acosf.h | 8 +++
libc/src/__support/math/asin_utils.h | 4 +-
libc/src/__support/math/inv_trigf_utils.h | 4 ++
libc/src/math/generic/asin.cpp | 1 +
libc/src/math/generic/asinf.cpp | 1 +
libc/src/math/generic/atan2f.cpp | 1 +
libc/src/math/generic/atanf.cpp | 1 +
libc/test/CMakeLists.txt | 1 +
libc/test/shared/CMakeLists.txt | 3 ++
libc/test/shared/smoke/CMakeLists.txt | 13 +++++
libc/test/shared/smoke/acos_test.cpp | 62 +++++++++++++++++++++++
12 files changed, 100 insertions(+), 5 deletions(-)
create mode 100644 libc/test/shared/CMakeLists.txt
create mode 100644 libc/test/shared/smoke/CMakeLists.txt
create mode 100644 libc/test/shared/smoke/acos_test.cpp
diff --git a/libc/src/__support/math/acos.h b/libc/src/__support/math/acos.h
index a7287f11aa302..d25d3d32ee644 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_utils_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 941c39f6c3eb6..100f1393a6451 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 3146444afc51f..89ecb94729e85 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_utils_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_utils_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 b8f4914eeb9e5..4a8fbeca93e49 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 c033597334345..a7382fa3be244 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_utils_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 c8d6b38ab1560..77d6de910962c 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 0a768494baa64..32b977f45d7e7 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 d12456c591016..22f962ef4cce4 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 1a0780faff512..87bcb1a214c1c 100644
--- a/libc/test/CMakeLists.txt
+++ b/libc/test/CMakeLists.txt
@@ -35,3 +35,4 @@ endif()
add_subdirectory(IntegrationTest)
add_subdirectory(integration)
+add_subdirectory(shared)
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
new file mode 100644
index 0000000000000..e904bd544afcc
--- /dev/null
+++ b/libc/test/shared/CMakeLists.txt
@@ -0,0 +1,3 @@
+add_custom_target(libc-shared-unittests)
+
+add_subdirectory(smoke)
diff --git a/libc/test/shared/smoke/CMakeLists.txt b/libc/test/shared/smoke/CMakeLists.txt
new file mode 100644
index 0000000000000..b0e5e6b7ec60f
--- /dev/null
+++ b/libc/test/shared/smoke/CMakeLists.txt
@@ -0,0 +1,13 @@
+add_custom_target(libc-shared-math-smoke-tests)
+# add_dependencies(libc-shared-unittests libc-shared-smoke-tests)
+
+add_fp_unittest(
+ acos_test
+ SUITE
+ libc-shared-math-smoke-tests
+ SRCS
+ acos_test.cpp
+ DEPENDS
+ libc.src.errno.errno
+ libc.src.__support.math.acos
+)
diff --git a/libc/test/shared/smoke/acos_test.cpp b/libc/test/shared/smoke/acos_test.cpp
new file mode 100644
index 0000000000000..89f08b675fef0
--- /dev/null
+++ b/libc/test/shared/smoke/acos_test.cpp
@@ -0,0 +1,62 @@
+//===-- Unittests for acos ------------------------------------------------===//
+//
+// 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 "src/__support/libc_errno.h"
+#include "test/UnitTest/FPMatcher.h"
+
+using LlvmLibcAcosTest = LIBC_NAMESPACE::testing::FPTest<double>;
+
+TEST_F(LlvmLibcAcosTest, SpecialNumbers) {
+ EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::math::acos(sNaN),
+ FE_INVALID);
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::math::acos(aNaN));
+ EXPECT_FP_EQ(0x1.921fb54442d18p0, LIBC_NAMESPACE::math::acos(zero));
+ EXPECT_FP_EQ(0x1.921fb54442d18p0, LIBC_NAMESPACE::math::acos(neg_zero));
+
+ libc_errno = 0;
+ EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::math::acos(inf),
+ FE_INVALID);
+ EXPECT_MATH_ERRNO(EDOM);
+ EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::math::acos(neg_inf),
+ FE_INVALID);
+ EXPECT_MATH_ERRNO(EDOM);
+ EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::math::acos(2.0),
+ FE_INVALID);
+ EXPECT_MATH_ERRNO(EDOM);
+ EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::math::acos(-2.0),
+ FE_INVALID);
+ EXPECT_MATH_ERRNO(EDOM);
+ EXPECT_FP_EQ(zero, LIBC_NAMESPACE::math::acos(1.0));
+ EXPECT_FP_EQ(0x1.921fb54442d18p1, LIBC_NAMESPACE::math::acos(-1.0));
+ EXPECT_FP_EQ(0x1.921fb54442d18p0, LIBC_NAMESPACE::math::acos(0x1.0p-54));
+}
+
+#ifdef LIBC_TEST_FTZ_DAZ
+
+using namespace LIBC_NAMESPACE::testing;
+
+TEST_F(LlvmLibcAcosTest, FTZMode) {
+ ModifyMXCSR mxcsr(FTZ);
+
+ EXPECT_FP_EQ(0x1.921fb54442d18p0, LIBC_NAMESPACE::math::acos(min_denormal));
+}
+
+TEST_F(LlvmLibcAcosTest, DAZMode) {
+ ModifyMXCSR mxcsr(DAZ);
+
+ EXPECT_FP_EQ(0x1.921fb54442d18p0, LIBC_NAMESPACE::math::acos(min_denormal));
+}
+
+TEST_F(LlvmLibcAcosTest, FTZDAZMode) {
+ ModifyMXCSR mxcsr(FTZ | DAZ);
+
+ EXPECT_FP_EQ(0x1.921fb54442d18p0, LIBC_NAMESPACE::math::acos(min_denormal));
+}
+
+#endif
>From 3fb746441795447b8a472ee861c03942637f8e4c Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Mon, 21 Jul 2025 00:42:39 +0300
Subject: [PATCH 2/2] fix style
---
libc/src/__support/math/acosf.h | 2 +-
libc/test/shared/smoke/acos_test.cpp | 20 ++++++++++----------
2 files changed, 11 insertions(+), 11 deletions(-)
diff --git a/libc/src/__support/math/acosf.h b/libc/src/__support/math/acosf.h
index 100f1393a6451..153087e998211 100644
--- a/libc/src/__support/math/acosf.h
+++ b/libc/src/__support/math/acosf.h
@@ -43,7 +43,7 @@ static constexpr fputil::ExceptValues<float, N_EXCEPTS> ACOSF_EXCEPTS = {{
#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-} //namespace acosf_internal
+} // namespace acosf_internal
static constexpr float acosf(float x) {
using namespace acosf_internal;
diff --git a/libc/test/shared/smoke/acos_test.cpp b/libc/test/shared/smoke/acos_test.cpp
index 89f08b675fef0..71db8815080db 100644
--- a/libc/test/shared/smoke/acos_test.cpp
+++ b/libc/test/shared/smoke/acos_test.cpp
@@ -13,24 +13,24 @@
using LlvmLibcAcosTest = LIBC_NAMESPACE::testing::FPTest<double>;
TEST_F(LlvmLibcAcosTest, SpecialNumbers) {
- EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::math::acos(sNaN),
- FE_INVALID);
+ EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(
+ aNaN, LIBC_NAMESPACE::math::acos(sNaN), FE_INVALID);
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::math::acos(aNaN));
EXPECT_FP_EQ(0x1.921fb54442d18p0, LIBC_NAMESPACE::math::acos(zero));
EXPECT_FP_EQ(0x1.921fb54442d18p0, LIBC_NAMESPACE::math::acos(neg_zero));
libc_errno = 0;
- EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::math::acos(inf),
- FE_INVALID);
+ EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(
+ aNaN, LIBC_NAMESPACE::math::acos(inf), FE_INVALID);
EXPECT_MATH_ERRNO(EDOM);
- EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::math::acos(neg_inf),
- FE_INVALID);
+ EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(
+ aNaN, LIBC_NAMESPACE::math::acos(neg_inf), FE_INVALID);
EXPECT_MATH_ERRNO(EDOM);
- EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::math::acos(2.0),
- FE_INVALID);
+ EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(
+ aNaN, LIBC_NAMESPACE::math::acos(2.0), FE_INVALID);
EXPECT_MATH_ERRNO(EDOM);
- EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::math::acos(-2.0),
- FE_INVALID);
+ EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(
+ aNaN, LIBC_NAMESPACE::math::acos(-2.0), FE_INVALID);
EXPECT_MATH_ERRNO(EDOM);
EXPECT_FP_EQ(zero, LIBC_NAMESPACE::math::acos(1.0));
EXPECT_FP_EQ(0x1.921fb54442d18p1, LIBC_NAMESPACE::math::acos(-1.0));
More information about the libc-commits
mailing list