Skip to content

[libc] [math] Refactor fsqrtl to be header-only#176169

Merged
bassiounix merged 7 commits intollvm:mainfrom
Atharva062006:refactor-fsqrtl-header-only
Jan 28, 2026
Merged

[libc] [math] Refactor fsqrtl to be header-only#176169
bassiounix merged 7 commits intollvm:mainfrom
Atharva062006:refactor-fsqrtl-header-only

Conversation

@Atharva062006
Copy link
Copy Markdown
Contributor

@Atharva062006 Atharva062006 commented Jan 15, 2026

This PR refactors fsqrtl to be header only as discussed. No functional change intended. Test and build files were updated as required by the refactor
Fixes #175335

@github-actions
Copy link
Copy Markdown

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

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

llvmbot commented Jan 15, 2026

@llvm/pr-subscribers-libc

Author: Atharv Mane (Atharva062006)

Changes

This PR refactors fsqrtl to be header only as discussed. No functional change intended. Test and build files were updated as required by the refactor


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

9 Files Affected:

  • (modified) libc/shared/math.h (+1)
  • (added) libc/shared/math/fsqrtl.h (+24)
  • (modified) libc/src/__support/math/CMakeLists.txt (+8)
  • (added) libc/src/__support/math/fsqrtl.h (+26)
  • (modified) libc/src/math/generic/CMakeLists.txt (+1-1)
  • (modified) libc/src/math/generic/fsqrtl.cpp (+2-3)
  • (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 287f3aea1565a..8fe5f82da696b 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -60,6 +60,7 @@
 #include "math/frexpf.h"
 #include "math/frexpf128.h"
 #include "math/frexpf16.h"
+#include "math/fsqrtl.h"
 #include "math/ilogbf16.h"
 #include "math/ldexpf.h"
 #include "math/ldexpf128.h"
diff --git a/libc/shared/math/fsqrtl.h b/libc/shared/math/fsqrtl.h
new file mode 100644
index 0000000000000..9e99c229431ae
--- /dev/null
+++ b/libc/shared/math/fsqrtl.h
@@ -0,0 +1,24 @@
+//===-- Shared header for fsqrtl ---------------------------------*- 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_FSQRTL_H
+#define LLVM_LIBC_SHARED_MATH_FSQRTL_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/fsqrtl.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace shared {
+
+using math::fsqrtl;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_FSQRTL_H
\ No newline at end of file
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 4139a1b1d3444..005ad69b487d5 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1078,3 +1078,11 @@ add_header_library(
     libc.src.__support.math.sincos_eval
     libc.src.__support.macros.optimization
 )
+
+add_header_library(
+  fsqrtl
+  HDRS
+    fsqrtl.h
+  DEPENDS
+    libc.src.__support.FPUtil.generic.sqrt
+)
diff --git a/libc/src/__support/math/fsqrtl.h b/libc/src/__support/math/fsqrtl.h
new file mode 100644
index 0000000000000..5a21ea6c9d25c
--- /dev/null
+++ b/libc/src/__support/math/fsqrtl.h
@@ -0,0 +1,26 @@
+//===-- Implementation header for fsqrtl ------------------------*- 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_FSQRTL_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FSQRTL_H
+
+#include "src/__support/FPUtil/generic/sqrt.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float fsqrt(long double x) {
+  return fputil::sqrt<float>(x);
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FSQRTL_H
\ No newline at end of file
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 6de4cf376bf02..895bf77b12ee5 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5234,7 +5234,7 @@ add_entrypoint_object(
   HDRS
     ../fsqrtl.h
   DEPENDS
-    libc.src.__support.FPUtil.generic.sqrt
+    libc.src.__support.math.fsqrtl
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/fsqrtl.cpp b/libc/src/math/generic/fsqrtl.cpp
index b896a84aeded8..9deb86b558ea7 100644
--- a/libc/src/math/generic/fsqrtl.cpp
+++ b/libc/src/math/generic/fsqrtl.cpp
@@ -10,11 +10,10 @@
 #include "src/__support/FPUtil/generic/sqrt.h"
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
+#include "src/__support/math/fsqrtl.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(float, fsqrtl, (long double x)) {
-  return fputil::sqrt<float>(x);
-}
+LLVM_LIBC_FUNCTION(float, fsqrtl, (long double x)) { return math::fsqrtl(x); }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index c5955ecfd54cb..ab74a1fff22a6 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -56,6 +56,7 @@ add_fp_unittest(
     libc.src.__support.math.frexpf
     libc.src.__support.math.frexpf128
     libc.src.__support.math.frexpf16
+    libc.src.__support.math.fsqrtl
     libc.src.__support.math.ilogbf16
     libc.src.__support.math.log
     libc.src.__support.math.ldexpf
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 20a98a1a9138c..d1480977dda19 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -92,6 +92,7 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
   EXPECT_FP_EQ(0x1p+0, LIBC_NAMESPACE::shared::exp2(0.0));
   EXPECT_FP_EQ(0x1p+0, LIBC_NAMESPACE::shared::exp10(0.0));
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::expm1(0.0));
+  EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::fsqrtl(0.0));
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::log(1.0));
   EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::sin(0.0));
 }
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index c03c21b834895..cf2ad3bdcb039 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2813,6 +2813,14 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_fsqrtl",
+    hdrs = ["src/__support/math/fsqrtl.h"],
+    deps = [
+        ":__support_fputil_sqrt",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_inv_trigf_utils",
     hdrs = ["src/__support/math/inv_trigf_utils.h"],
@@ -4285,7 +4293,7 @@ libc_math_function(
 libc_math_function(
     name = "fsqrtl",
     additional_deps = [
-        ":__support_fputil_sqrt",
+        ":__support_math_fsqrtl",
     ],
 )
 

Copy link
Copy Markdown
Member

@bassiounix bassiounix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for you PR, you are on the right track with some changes.


} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FSQRTL_H No newline at end of file
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FSQRTL_H
#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FSQRTL_H

Comment on lines 10 to 12
#include "src/__support/FPUtil/generic/sqrt.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleanup

Suggested change
#include "src/__support/FPUtil/generic/sqrt.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"

EXPECT_FP_EQ(0x1p+0, LIBC_NAMESPACE::shared::exp2(0.0));
EXPECT_FP_EQ(0x1p+0, LIBC_NAMESPACE::shared::exp10(0.0));
EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::expm1(0.0));
EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::fsqrtl(0.0));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a new section for long double functions, please fetch the latest changes and move your function there

@github-actions
Copy link
Copy Markdown

⚠️ We detected that you are using a GitHub private e-mail address to contribute to the repo.
Please turn off Keep my email addresses private setting in your account.
See LLVM Developer Policy and LLVM Discourse for more information.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Jan 16, 2026

✅ With the latest revision this PR passed the C/C++ code formatter.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Jan 16, 2026

🐧 Linux x64 Test Results

✅ The build succeeded and no tests ran. This is expected in some build configurations.

@Atharva062006 Atharva062006 force-pushed the refactor-fsqrtl-header-only branch from 1cb8bb9 to 058bbb3 Compare January 16, 2026 17:49
@Atharva062006
Copy link
Copy Markdown
Contributor Author

@bassiounix i did all the recommended changes, does this look okay?

@Atharva062006 Atharva062006 force-pushed the refactor-fsqrtl-header-only branch from 058bbb3 to ec8ded2 Compare January 16, 2026 18:18
TEST(LlvmLibcSharedMathTest, AllLongDouble) {
EXPECT_FP_EQ(0x0p+0L,
LIBC_NAMESPACE::shared::dfmal(0x0.p+0L, 0x0.p+0L, 0x0.p+0L));
EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::fsqrtl(0.0));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the long literal type suffix

@Atharva062006 Atharva062006 force-pushed the refactor-fsqrtl-header-only branch from ec8ded2 to ce66633 Compare January 19, 2026 18:28
@Atharva062006
Copy link
Copy Markdown
Contributor Author

@bassiounix Done


namespace math {

LIBC_INLINE static constexpr float fsqrt(long double x) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
LIBC_INLINE static constexpr float fsqrt(long double x) {
LIBC_INLINE static constexpr float fsqrtl(long double x) {

@Atharva062006 Atharva062006 force-pushed the refactor-fsqrtl-header-only branch from b08e00b to 3da7e10 Compare January 24, 2026 05:47
@Atharva062006
Copy link
Copy Markdown
Contributor Author

@bassiounix

@bassiounix
Copy link
Copy Markdown
Member

please check code formatting

@Atharva062006 Atharva062006 force-pushed the refactor-fsqrtl-header-only branch from 3da7e10 to d734ae5 Compare January 25, 2026 05:46
@Atharva062006
Copy link
Copy Markdown
Contributor Author

@bassiounix used clang-format, should be fixed

@bassiounix
Copy link
Copy Markdown
Member

⚠️ We detected that you are using a GitHub private e-mail address to contribute to the repo. Please turn off Keep my email addresses private setting in your account. See LLVM Developer Policy and LLVM Discourse for more information.

I can't accept this PR unless you resolve this.

Copy link
Copy Markdown
Member

@bassiounix bassiounix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
Thank you for the refactor!

@bassiounix bassiounix enabled auto-merge (squash) January 28, 2026 20:07
@bassiounix bassiounix merged commit 18925d1 into llvm:main Jan 28, 2026
26 of 27 checks passed
@github-actions
Copy link
Copy Markdown

@Atharva062006 Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

honeygoyal pushed a commit to honeygoyal/llvm-project that referenced this pull request Jan 30, 2026
This PR refactors fsqrtl to be header only as discussed. No functional
change intended. Test and build files were updated as required by the
refactor
Fixes llvm#175335
sshrestha-aa pushed a commit to sshrestha-aa/llvm-project that referenced this pull request Feb 4, 2026
This PR refactors fsqrtl to be header only as discussed. No functional
change intended. Test and build files were updated as required by the
refactor
Fixes llvm#175335
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.

[libc][math] Refactor fsqrtl to Header Only.

3 participants