Skip to content

[libc][math] Refactor sqrtl family to header-only#194510

Merged
bassiounix merged 4 commits into
llvm:mainfrom
AnonMiraj:refactor_sqrtl_family
Apr 28, 2026
Merged

[libc][math] Refactor sqrtl family to header-only#194510
bassiounix merged 4 commits into
llvm:mainfrom
AnonMiraj:refactor_sqrtl_family

Conversation

@AnonMiraj
Copy link
Copy Markdown
Contributor

Refactors the sqrtl math family to be header-only.

part of: #147386

Target Functions:

  • sqrtl

@llvmbot llvmbot added libc bazel "Peripheral" support tier build system: utils/bazel labels Apr 28, 2026
@llvmbot
Copy link
Copy Markdown
Member

llvmbot commented Apr 28, 2026

@llvm/pr-subscribers-libc

Author: Anonmiraj (AnonMiraj)

Changes

Refactors the sqrtl math family to be header-only.

part of: #147386

Target Functions:

  • sqrtl

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

10 Files Affected:

  • (modified) libc/shared/math.h (+1)
  • (added) libc/shared/math/sqrtl.h (+23)
  • (modified) libc/src/__support/math/CMakeLists.txt (+8)
  • (added) libc/src/__support/math/sqrtl.h (+25)
  • (modified) libc/src/math/generic/CMakeLists.txt (+1-1)
  • (modified) libc/src/math/generic/sqrtl.cpp (+2-4)
  • (modified) libc/test/shared/CMakeLists.txt (+2)
  • (modified) libc/test/shared/shared_math_constexpr_test.cpp (+1)
  • (modified) libc/test/shared/shared_math_test.cpp (+2)
  • (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+12-1)
diff --git a/libc/shared/math.h b/libc/shared/math.h
index f1243f7925527..9892da63043e6 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -284,6 +284,7 @@
 #include "math/sqrtf.h"
 #include "math/sqrtf128.h"
 #include "math/sqrtf16.h"
+#include "math/sqrtl.h"
 #include "math/tan.h"
 #include "math/tanf.h"
 #include "math/tanf16.h"
diff --git a/libc/shared/math/sqrtl.h b/libc/shared/math/sqrtl.h
new file mode 100644
index 0000000000000..889998fea4ba1
--- /dev/null
+++ b/libc/shared/math/sqrtl.h
@@ -0,0 +1,23 @@
+//===-- Shared sqrtl 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_SQRTL_H
+#define LLVM_LIBC_SHARED_MATH_SQRTL_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/sqrtl.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::sqrtl;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_SQRTL_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 04172ac516514..f62dc5998053f 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -3500,6 +3500,14 @@ add_header_library(
     libc.src.__support.uint128
     libc.include.llvm-libc-types.float128
 )
+add_header_library(
+  sqrtl
+  HDRS
+    sqrtl.h
+  DEPENDS
+    libc.src.__support.FPUtil.sqrt
+    libc.src.__support.macros.config
+)
 
 add_header_library(
   tan
diff --git a/libc/src/__support/math/sqrtl.h b/libc/src/__support/math/sqrtl.h
new file mode 100644
index 0000000000000..97643d70212a1
--- /dev/null
+++ b/libc/src/__support/math/sqrtl.h
@@ -0,0 +1,25 @@
+//===-- Implementation header for sqrtl -------------------------*- 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_SQRTL_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_SQRTL_H
+
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE constexpr long double sqrtl(long double x) {
+  return fputil::sqrt<long double>(x);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_SQRTL_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 42b2046a1eb94..d72131a608ddb 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -2841,7 +2841,7 @@ add_entrypoint_object(
   HDRS
     ../sqrtl.h
   DEPENDS
-    libc.src.__support.FPUtil.sqrt
+    libc.src.__support.math.sqrtl
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/sqrtl.cpp b/libc/src/math/generic/sqrtl.cpp
index 2368182740ca8..b7a0b8bdce70a 100644
--- a/libc/src/math/generic/sqrtl.cpp
+++ b/libc/src/math/generic/sqrtl.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/sqrtl.h"
-#include "src/__support/FPUtil/sqrt.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/sqrtl.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(long double, sqrtl, (long double x)) {
-  return fputil::sqrt<long double>(x);
+  return math::sqrtl(x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 8f054499b202f..e9664b18a9e98 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -282,6 +282,7 @@ add_fp_unittest(
     libc.src.__support.math.sqrt
     libc.src.__support.math.sqrtbf16
     libc.src.__support.math.sqrtf
+    libc.src.__support.math.sqrtl
     libc.src.__support.math.tan
     libc.src.__support.math.tanf
     libc.src.__support.math.tanf16
@@ -344,6 +345,7 @@ add_fp_unittest(
     libc.src.__support.math.log
     libc.src.__support.math.logbbf16
     libc.src.__support.math.ilogbbf16
+    libc.src.__support.math.sqrtl
 )
 
 add_fp_unittest(
diff --git a/libc/test/shared/shared_math_constexpr_test.cpp b/libc/test/shared/shared_math_constexpr_test.cpp
index 6df46ecc48154..2bb9a15cb4684 100644
--- a/libc/test/shared/shared_math_constexpr_test.cpp
+++ b/libc/test/shared/shared_math_constexpr_test.cpp
@@ -65,6 +65,7 @@ static_assert(1.0L == LIBC_NAMESPACE::shared::fdiml(1.0L, 0.0L));
 static_assert(0.0f == LIBC_NAMESPACE::shared::fdivl(0.0L, 1.0L));
 static_assert(0.0L == LIBC_NAMESPACE::shared::floorl(0.0L));
 static_assert(bfloat16(0.0) == LIBC_NAMESPACE::shared::bf16subl(0.0L, 0.0L));
+static_assert(0.0L == LIBC_NAMESPACE::shared::sqrtl(0.0L));
 
 #endif
 
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 74e6a0484d1cb..78135ad8cc30f 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -364,6 +364,8 @@ TEST(LlvmLibcSharedMathTest, AllLongDouble) {
   EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::shared::f16sqrtl(1.0L));
   EXPECT_FP_EQ(10.0f16, LIBC_NAMESPACE::shared::f16fmal(2.0L, 3.0L, 4.0L));
 #endif // LIBC_TYPES_HAS_FLOAT16
+
+  EXPECT_FP_EQ(0x0p+0L, LIBC_NAMESPACE::shared::sqrtl(0.0L));
 }
 
 #endif // LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 3e9bd3b6f6cf6..34aecfc02a6ce 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -6149,6 +6149,15 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_sqrtl",
+    hdrs = ["src/__support/math/sqrtl.h"],
+    deps = [
+        ":__support_fputil_normal_float",
+        ":__support_macros_config",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_logf16",
     hdrs = ["src/__support/math/logf16.h"],
@@ -9182,7 +9191,9 @@ libc_math_function(
 
 libc_math_function(
     name = "sqrtl",
-    additional_deps = [":__support_fputil_sqrt"],
+    additional_deps = [
+        ":__support_math_sqrtl"
+    ],
 )
 
 libc_math_function(

@AnonMiraj AnonMiraj force-pushed the refactor_sqrtl_family branch from f3977fa to 02641b4 Compare April 28, 2026 01:56
@AnonMiraj AnonMiraj force-pushed the refactor_sqrtl_family branch from 02641b4 to 149cb53 Compare April 28, 2026 02:00
@bassiounix bassiounix self-requested a review April 28, 2026 03:51
@bassiounix bassiounix merged commit 6a0a18b into llvm:main Apr 28, 2026
34 checks passed
yingopq pushed a commit to yingopq/llvm-project that referenced this pull request Apr 29, 2026
part of: llvm#147386

---------

Co-authored-by: bassiounix <muhammad.m.bassiouni@gmail.com>
KHicketts pushed a commit to KHicketts/llvm-project that referenced this pull request Apr 30, 2026
part of: llvm#147386

---------

Co-authored-by: bassiounix <muhammad.m.bassiouni@gmail.com>
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