[libc-commits] [libc] [llvm] [libc][math] Refactor sqrt to header-only (PR #178335)
Saina Daneshmand via libc-commits
libc-commits at lists.llvm.org
Tue Jan 27 19:16:52 PST 2026
https://github.com/SainaDaneshmandjahromi updated https://github.com/llvm/llvm-project/pull/178335
>From 2d1fe74546f99c1723972d6d63ace8cc69513e50 Mon Sep 17 00:00:00 2001
From: SainaDaneshmandjahromi <daneshmand.saina at gmail.com>
Date: Tue, 27 Jan 2026 20:10:44 -0700
Subject: [PATCH 1/2] [libc][math] Refactor sqrt to header-only
---
libc/shared/math.h | 1 +
libc/shared/math/sqrt.h | 24 +++++++++++++++++
libc/src/__support/math/CMakeLists.txt | 9 +++++++
libc/src/__support/math/sqrt.h | 26 +++++++++++++++++++
libc/src/math/generic/CMakeLists.txt | 2 +-
libc/src/math/generic/sqrt.cpp | 7 +++--
.../llvm-project-overlay/libc/BUILD.bazel | 10 ++++++-
7 files changed, 73 insertions(+), 6 deletions(-)
create mode 100644 libc/shared/math/sqrt.h
create mode 100644 libc/src/__support/math/sqrt.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index b284f1b21111c..b6feee9705272 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -92,6 +92,7 @@
#include "math/sinf16.h"
#include "math/sinhf.h"
#include "math/sinhf16.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..0a7671dd9fd2b
--- /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
\ No newline at end of file
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 7e57a648a3592..93be144818294 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1144,6 +1144,7 @@ add_header_library(
libc.src.__support.macros.config
)
+
add_header_library(
log
HDRS
@@ -1344,6 +1345,14 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ sqrt
+ HDRS
+ sqrt.h
+ DEPENDS
+ libc.src.__support.FPUtil.generic.sqrt
+)
+
add_header_library(
dfmal
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 d8f9abe9ed246..1feb44296df54 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3001,7 +3001,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/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 0dd91404c70fb..28e4197ee3e6f 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3562,6 +3562,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"],
@@ -5243,7 +5251,7 @@ libc_math_function(
libc_math_function(
name = "sqrt",
additional_deps = [
- ":__support_fputil_sqrt",
+ ":__support_math_sqrt",
],
)
>From 1fdeabcf5f1ba554ac58b3fffb879fbdb78dc512 Mon Sep 17 00:00:00 2001
From: SainaDaneshmandjahromi <daneshmand.saina at gmail.com>
Date: Tue, 27 Jan 2026 18:43:17 -0700
Subject: [PATCH 2/2] [libc][math] Refactor sqrt to header-only
---
libc/shared/math/sqrt.h | 2 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
3 files changed, 3 insertions(+), 1 deletion(-)
diff --git a/libc/shared/math/sqrt.h b/libc/shared/math/sqrt.h
index 0a7671dd9fd2b..a645245bb8fe3 100644
--- a/libc/shared/math/sqrt.h
+++ b/libc/shared/math/sqrt.h
@@ -21,4 +21,4 @@ using math::sqrt;
} // namespace shared
} // namespace LIBC_NAMESPACE_DECL
-#endif // LLVM_LIBC_SHARED_MATH_SQRT_H
\ No newline at end of file
+#endif // LLVM_LIBC_SHARED_MATH_SQRT_H
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index f8079c6085224..78f47b5f59385 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -89,6 +89,7 @@ add_fp_unittest(
libc.src.__support.math.sinf16
libc.src.__support.math.sinhf
libc.src.__support.math.sinhf16
+ 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 efe6f874f899e..f4cf3ad6e5ef3 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -116,6 +116,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_EQ(0L, LIBC_NAMESPACE::shared::llogb(1.0));
EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::tan(0.0));
}
More information about the libc-commits
mailing list