[libc-commits] [libc] 2865f1b - [libc][math] add smoke tests to shared/math.h (#149741)

via libc-commits libc-commits at lists.llvm.org
Mon Jul 21 10:49:01 PDT 2025


Author: Muhammad Bassiouni
Date: 2025-07-21T20:48:57+03:00
New Revision: 2865f1ba966c21d4ebff610875394ce9c7a5ff38

URL: https://github.com/llvm/llvm-project/commit/2865f1ba966c21d4ebff610875394ce9c7a5ff38
DIFF: https://github.com/llvm/llvm-project/commit/2865f1ba966c21d4ebff610875394ce9c7a5ff38.diff

LOG: [libc][math] add smoke tests to shared/math.h (#149741)

Adding smoke tests for shared math header.

part of #147386

Added: 
    libc/test/shared/CMakeLists.txt
    libc/test/shared/shared_math_test.cpp

Modified: 
    libc/shared/math/exp10f16.h
    libc/src/__support/math/acos.h
    libc/src/__support/math/acosf.h
    libc/src/__support/math/asin_utils.h
    libc/src/__support/math/inv_trigf_utils.h
    libc/src/math/generic/asin.cpp
    libc/src/math/generic/asinf.cpp
    libc/src/math/generic/atan2f.cpp
    libc/src/math/generic/atanf.cpp
    libc/test/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/shared/math/exp10f16.h b/libc/shared/math/exp10f16.h
index 8acdbdb7c70a1..af00787b058bc 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 a7287f11aa302..7c9fc7671b248 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 941c39f6c3eb6..153087e998211 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..e0c9096e2bb78 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 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..d286fceaab6ac 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 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..011ad6aeb34b7 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 0000000000000..07a4e735362b2
--- /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 0000000000000..40fea3bb9b952
--- /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));
+}


        


More information about the libc-commits mailing list