Skip to content

Conversation

bassiounix
Copy link
Contributor

@bassiounix bassiounix commented Aug 22, 2025

@bassiounix bassiounix added bazel "Peripheral" support tier build system: utils/bazel libc labels Aug 22, 2025 — with Graphite App
Copy link
Contributor Author

bassiounix commented Aug 22, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

@llvmbot
Copy link
Member

llvmbot commented Aug 22, 2025

@llvm/pr-subscribers-libc

Author: Muhammad Bassiouni (bassiounix)

Changes

Part of #147386

in preparation for: https://discourse.llvm.org/t/rfc-make-clang-builtin-math-functions-constexpr-with-llvm-libc-to-support-c-23-constexpr-math-functions/86450


Full diff: https://github.com/llvm/llvm-project/pull/154868.diff

10 Files Affected:

  • (modified) libc/shared/math.h (+1)
  • (added) libc/shared/math/dsqrtl.h (+23)
  • (modified) libc/src/__support/FPUtil/generic/sqrt.h (+4-4)
  • (modified) libc/src/__support/math/CMakeLists.txt (+8)
  • (added) libc/src/__support/math/dsqrtl.h (+26)
  • (modified) libc/src/math/generic/CMakeLists.txt (+1-1)
  • (modified) libc/src/math/generic/dsqrtl.cpp (+2-7)
  • (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 --git a/libc/shared/math.h b/libc/shared/math.h
index 69d785b3e0291..210aa65463a2c 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -39,6 +39,7 @@
 #include "math/coshf16.h"
 #include "math/cospif.h"
 #include "math/cospif16.h"
+#include "math/dsqrtl.h"
 #include "math/erff.h"
 #include "math/exp.h"
 #include "math/exp10.h"
diff --git a/libc/shared/math/dsqrtl.h b/libc/shared/math/dsqrtl.h
new file mode 100644
index 0000000000000..bf1d15f3e6549
--- /dev/null
+++ b/libc/shared/math/dsqrtl.h
@@ -0,0 +1,23 @@
+//===-- Shared dsqrtl function ----------------------------------*- 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_DSQRTL_H
+#define LLVM_LIBC_SHARED_MATH_DSQRTL_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/dsqrtl.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::dsqrtl;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_DSQRTL_H
diff --git a/libc/src/__support/FPUtil/generic/sqrt.h b/libc/src/__support/FPUtil/generic/sqrt.h
index 497ebd145c6b4..2295fdf85a114 100644
--- a/libc/src/__support/FPUtil/generic/sqrt.h
+++ b/libc/src/__support/FPUtil/generic/sqrt.h
@@ -69,10 +69,10 @@ LIBC_INLINE void normalize<long double>(int &exponent, UInt128 &mantissa) {
 // Correctly rounded IEEE 754 SQRT for all rounding modes.
 // Shift-and-add algorithm.
 template <typename OutType, typename InType>
-LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<OutType> &&
-                                 cpp::is_floating_point_v<InType> &&
-                                 sizeof(OutType) <= sizeof(InType),
-                             OutType>
+LIBC_INLINE static constexpr cpp::enable_if_t<
+    cpp::is_floating_point_v<OutType> && cpp::is_floating_point_v<InType> &&
+        sizeof(OutType) <= sizeof(InType),
+    OutType>
 sqrt(InType x) {
   if constexpr (internal::SpecialLongDouble<OutType>::VALUE &&
                 internal::SpecialLongDouble<InType>::VALUE) {
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 39dc0e57f4472..648f33d347ce1 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -457,6 +457,14 @@ add_header_library(
     libc.src.__support.macros.optimization
 )
 
+add_header_library(
+  dsqrtl
+  HDRS
+    dsqrtl.h
+  DEPENDS
+    libc.src.__support.FPUtil.generic.sqrt
+)
+
 add_header_library(
   erff
   HDRS
diff --git a/libc/src/__support/math/dsqrtl.h b/libc/src/__support/math/dsqrtl.h
new file mode 100644
index 0000000000000..e66b6502d27c4
--- /dev/null
+++ b/libc/src/__support/math/dsqrtl.h
@@ -0,0 +1,26 @@
+//===-- Implementation header for dsqrtl ------------------------*- 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_DSQRTL_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_DSQRTL_H
+
+#include "src/__support/FPUtil/generic/sqrt.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr double dsqrtl(long double x) {
+  return fputil::sqrt<double>(x);
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_DSQRTL_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 5eba5e3616472..abb109cdc9ac8 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -242,7 +242,7 @@ add_entrypoint_object(
   HDRS
     ../dsqrtl.h
   DEPENDS
-    libc.src.__support.FPUtil.generic.sqrt
+    libc.src.__support.math.dsqrtl
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/dsqrtl.cpp b/libc/src/math/generic/dsqrtl.cpp
index bf1dae9161460..3c7b3b34a5c93 100644
--- a/libc/src/math/generic/dsqrtl.cpp
+++ b/libc/src/math/generic/dsqrtl.cpp
@@ -7,14 +7,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/dsqrtl.h"
-#include "src/__support/FPUtil/generic/sqrt.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
-
+#include "src/__support/math/dsqrtl.h"
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(double, dsqrtl, (long double x)) {
-  return fputil::sqrt<double>(x);
-}
+LLVM_LIBC_FUNCTION(double, dsqrtl, (long double x)) { return math::dsqrtl(x); }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 48241d3f55287..88afa91959108 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -35,6 +35,7 @@ add_fp_unittest(
     libc.src.__support.math.coshf16
     libc.src.__support.math.cospif
     libc.src.__support.math.cospif16
+    libc.src.__support.math.dsqrtl
     libc.src.__support.math.erff
     libc.src.__support.math.exp
     libc.src.__support.math.exp10
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 2e5a2d51146d4..1fd88335a5d0c 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -55,6 +55,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
   EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::cosf(0.0f));
   EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::coshf(0.0f));
   EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::cospif(0.0f));
+  EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::dsqrtl(0.0f));
   EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::erff(0.0f));
   EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::exp10f(0.0f));
   EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::expf(0.0f));
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index f8f7713655adb..39140ca0a3e18 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2419,6 +2419,14 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_dsqrtl",
+    hdrs = ["src/__support/math/dsqrtl.h"],
+    deps = [
+        ":__support_fputil_sqrt",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_erff",
     hdrs = ["src/__support/math/erff.h"],
@@ -3287,7 +3295,7 @@ libc_math_function(name = "dmulf128")
 libc_math_function(
     name = "dsqrtl",
     additional_deps = [
-        ":__support_fputil_sqrt",
+        ":__support_math_dsqrtl",
     ],
 )
 

@bassiounix bassiounix force-pushed the users/bassiounix/spr/08-19-_libc_math_refactor_cospif16_implementation_to_header-only_in_src___support_math_folder branch 2 times, most recently from 0d287b7 to 51d48be Compare August 22, 2025 01:56
Base automatically changed from users/bassiounix/spr/08-19-_libc_math_refactor_cospif16_implementation_to_header-only_in_src___support_math_folder to main August 22, 2025 02:04
@bassiounix bassiounix force-pushed the users/bassiounix/spr/08-22-_libc_math_refactor_dsqrtl_implementation_to_header-only_in_src___support_math_folder branch from 7225675 to f81c15c Compare August 22, 2025 02:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bazel "Peripheral" support tier build system: utils/bazel libc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants