[libc-commits] [libc] [llvm] [libc][math] Refactor sqrt to header-only (PR #178335)
via libc-commits
libc-commits at lists.llvm.org
Tue Jan 27 17:46:27 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Saina Daneshmand (SainaDaneshmandjahromi)
<details>
<summary>Changes</summary>
This refactors `sqrt` to be header-only, following the libc math refactoring plan.
Testing:
- ninja -C build-libc/runtimes/runtimes-bins libc.test.shared.shared_math_test.__unit__
- ninja -C build-libc/runtimes/runtimes-bins libc.test.src.math.smoke.sqrt_test.__unit__
- ninja -C build-libc/runtimes/runtimes-bins libc.test.src.math.smoke.generic_sqrt_test.__unit__
Note: `check-libc` fails locally on my macOS setup on main as well due to Darwin futex availability
(`-Werror=unguarded-availability-new`), so I relied on the targeted tests above.
Part of #<!-- -->147386
Closes #<!-- -->177648
---
Full diff: https://github.com/llvm/llvm-project/pull/178335.diff
9 Files Affected:
- (modified) libc/shared/math.h (+1)
- (added) libc/shared/math/sqrt.h (+24)
- (modified) libc/src/__support/math/CMakeLists.txt (+8)
- (added) libc/src/__support/math/sqrt.h (+26)
- (modified) libc/src/math/generic/CMakeLists.txt (+1-1)
- (modified) libc/src/math/generic/sqrt.cpp (+3-4)
- (modified) libc/test/shared/CMakeLists.txt (+1)
- (modified) libc/test/shared/shared_math_test.cpp (+1)
- (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+9-1)
``````````diff
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 5126e46c9772e..c855302e214ed 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -84,6 +84,7 @@
#include "math/rsqrtf.h"
#include "math/rsqrtf16.h"
#include "math/sin.h"
+#include "math/sqrt.h"
#include "math/tan.h"
#include "math/tanf.h"
diff --git a/libc/shared/math/sqrt.h b/libc/shared/math/sqrt.h
new file mode 100644
index 0000000000000..a645245bb8fe3
--- /dev/null
+++ b/libc/shared/math/sqrt.h
@@ -0,0 +1,24 @@
+//===-- Shared header for sqrt ---------------------------------*- 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_SHARED_MATH_SQRT_H
+#define LLVM_LIBC_SHARED_MATH_SQRT_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/sqrt.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace shared {
+
+using math::sqrt;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_SQRT_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index d18ebc03ba7b8..323323a13bc1e 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1115,6 +1115,14 @@ add_header_library(
libc.src.__support.FPUtil.multiply_add
)
+add_header_library(
+ sqrt
+ HDRS
+ sqrt.h
+ DEPENDS
+ libc.src.__support.FPUtil.generic.sqrt
+)
+
add_header_library(
log
HDRS
diff --git a/libc/src/__support/math/sqrt.h b/libc/src/__support/math/sqrt.h
new file mode 100644
index 0000000000000..1fa440e543282
--- /dev/null
+++ b/libc/src/__support/math/sqrt.h
@@ -0,0 +1,26 @@
+//===-- Implementation header for sqrt -------------------------*- 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_SRC___SUPPORT_MATH_SQRT_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_SQRT_H
+
+#include "src/__support/FPUtil/generic/sqrt.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr double sqrt(double x) {
+ return fputil::sqrt<double>(x);
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_SQRT_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 3addfab6c6603..6128e4e37acc6 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3026,7 +3026,7 @@ add_entrypoint_object(
HDRS
../sqrt.h
DEPENDS
- libc.src.__support.FPUtil.sqrt
+ libc.src.__support.math.sqrt
)
diff --git a/libc/src/math/generic/sqrt.cpp b/libc/src/math/generic/sqrt.cpp
index 791975e1be2d5..ff12694011ca7 100644
--- a/libc/src/math/generic/sqrt.cpp
+++ b/libc/src/math/generic/sqrt.cpp
@@ -7,12 +7,11 @@
//===----------------------------------------------------------------------===//
#include "src/math/sqrt.h"
-#include "src/__support/FPUtil/sqrt.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/sqrt.h"
+
namespace LIBC_NAMESPACE_DECL {
-LLVM_LIBC_FUNCTION(double, sqrt, (double x)) { return fputil::sqrt<double>(x); }
+LLVM_LIBC_FUNCTION(double, sqrt, (double x)) { return math::sqrt(x); }
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index dabf5f6b168cc..eb1aea65a7bec 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -80,6 +80,7 @@ add_fp_unittest(
libc.src.__support.math.rsqrtf
libc.src.__support.math.rsqrtf16
libc.src.__support.math.sin
+ libc.src.__support.math.sqrt
libc.src.__support.math.tan
libc.src.__support.math.tanf
)
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index cd389c41cb7b2..924e559b0219f 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -110,6 +110,7 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::log1p(0.0));
EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::log2(1.0));
EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::sin(0.0));
+ EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::sqrt(0.0));
EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::tan(0.0));
}
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 346864d2276c0..dd0d7799938ba 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3464,6 +3464,14 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_sqrt",
+ hdrs = ["src/__support/math/sqrt.h"],
+ deps = [
+ ":__support_fputil_sqrt",
+ ],
+)
+
libc_support_library(
name = "__support_math_hypotf",
hdrs = ["src/__support/math/hypotf.h"],
@@ -5140,7 +5148,7 @@ libc_math_function(
libc_math_function(
name = "sqrt",
additional_deps = [
- ":__support_fputil_sqrt",
+ ":__support_math_sqrt",
],
)
``````````
</details>
https://github.com/llvm/llvm-project/pull/178335
More information about the libc-commits
mailing list