[libc-commits] [libc] [llvm] [libc] [math] Refactor fsqrtl to be header-only (PR #176169)

Atharv Mane via libc-commits libc-commits at lists.llvm.org
Fri Jan 23 21:47:52 PST 2026


https://github.com/Atharva062006 updated https://github.com/llvm/llvm-project/pull/176169

>From d6be6779b1a0ca734a859bac6ebae112421cdca0 Mon Sep 17 00:00:00 2001
From: Atharva062006 <72563240+Atharva062006 at users.noreply.github.com>
Date: Thu, 15 Jan 2026 19:22:49 +0530
Subject: [PATCH 1/4] [libc] Refactor fsqrtl to be header-only

---
 libc/shared/math.h                            |  1 +
 libc/shared/math/fsqrtl.h                     | 24 +++++++++++++++++
 libc/src/__support/math/CMakeLists.txt        |  8 ++++++
 libc/src/__support/math/fsqrtl.h              | 26 +++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  2 +-
 libc/src/math/generic/fsqrtl.cpp              |  8 ++----
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  3 +++
 .../llvm-project-overlay/libc/BUILD.bazel     | 10 ++++++-
 9 files changed, 75 insertions(+), 8 deletions(-)
 create mode 100644 libc/shared/math/fsqrtl.h
 create mode 100644 libc/src/__support/math/fsqrtl.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index a331abcc033c7..0d1cea7d6018f 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -66,6 +66,7 @@
 #include "math/frexpf16.h"
 #include "math/fsqrt.h"
 #include "math/fsqrtf128.h"
+#include "math/fsqrtl.h"
 #include "math/hypotf.h"
 #include "math/ilogbf.h"
 #include "math/ilogbf16.h"
diff --git a/libc/shared/math/fsqrtl.h b/libc/shared/math/fsqrtl.h
new file mode 100644
index 0000000000000..0f2271864d1b2
--- /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
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index a03952d5c5ed0..90d0e285e1893 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -610,6 +610,14 @@ add_header_library(
     libc.src.__support.FPUtil.generic.sqrt
 )
 
+add_header_library(
+  fsqrtl
+  HDRS
+    fsqrtl.h
+  DEPENDS
+    libc.src.__support.FPUtil.generic.sqrt
+)
+
 add_header_library(
   inv_trigf_utils
   HDRS
diff --git a/libc/src/__support/math/fsqrtl.h b/libc/src/__support/math/fsqrtl.h
new file mode 100644
index 0000000000000..b34e468c62221
--- /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
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 04ad1aa896613..ee1396661fddb 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5196,7 +5196,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..d6f27b166b299 100644
--- a/libc/src/math/generic/fsqrtl.cpp
+++ b/libc/src/math/generic/fsqrtl.cpp
@@ -7,14 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/fsqrtl.h"
-#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 4f82d00cc6c84..f0b5b4ca85224 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -62,6 +62,7 @@ add_fp_unittest(
     libc.src.__support.math.frexpf16
     libc.src.__support.math.fsqrt
     libc.src.__support.math.fsqrtf128
+    libc.src.__support.math.fsqrtl
     libc.src.__support.math.hypotf
     libc.src.__support.math.ilogbf
     libc.src.__support.math.ilogbf16
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 5ec8a7b23081e..06317667a9b55 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -115,7 +115,10 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
 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+0L, LIBC_NAMESPACE::shared::fsqrtl(0.0L));
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::ilogbl(0x1.p+0L));
+  
+
 }
 
 #ifdef LIBC_TYPES_HAS_FLOAT128
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 853182ce18b57..0ac5253e9639f 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2915,6 +2915,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"],
@@ -4537,7 +4545,7 @@ libc_math_function(
 libc_math_function(
     name = "fsqrtl",
     additional_deps = [
-        ":__support_fputil_sqrt",
+        ":__support_math_fsqrtl",
     ],
 )
 

>From 4f65b61f733c22236ccf8d1f69b87d51989076b7 Mon Sep 17 00:00:00 2001
From: Atharva062006 <72563240+Atharva062006 at users.noreply.github.com>
Date: Tue, 20 Jan 2026 00:03:18 +0530
Subject: [PATCH 2/4] added long literal type suffix

---
 libc/test/shared/shared_math_test.cpp | 2 --
 1 file changed, 2 deletions(-)

diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 06317667a9b55..433caac4fbb6f 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -117,8 +117,6 @@ TEST(LlvmLibcSharedMathTest, AllLongDouble) {
                LIBC_NAMESPACE::shared::dfmal(0x0.p+0L, 0x0.p+0L, 0x0.p+0L));
   EXPECT_FP_EQ(0x0p+0L, LIBC_NAMESPACE::shared::fsqrtl(0.0L));
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::ilogbl(0x1.p+0L));
-  
-
 }
 
 #ifdef LIBC_TYPES_HAS_FLOAT128

>From 9ff41859495cd3abc3ec3e0b41b756230bf69d70 Mon Sep 17 00:00:00 2001
From: Atharva062006 <72563240+Atharva062006 at users.noreply.github.com>
Date: Tue, 20 Jan 2026 01:15:30 +0530
Subject: [PATCH 3/4] return value suffix to float

---
 libc/test/shared/shared_math_test.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 433caac4fbb6f..fca7215e36140 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -115,7 +115,7 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
 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+0L, LIBC_NAMESPACE::shared::fsqrtl(0.0L));
+  EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::fsqrtl(0.0L));
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::ilogbl(0x1.p+0L));
 }
 

>From 3da7e10825219d4c0c293150578b2be9ae05d4be Mon Sep 17 00:00:00 2001
From: Atharva062006 <72563240+Atharva062006 at users.noreply.github.com>
Date: Sat, 24 Jan 2026 11:03:23 +0530
Subject: [PATCH 4/4] fixed fsqrtl naming

---
 libc/src/__support/math/fsqrtl.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/__support/math/fsqrtl.h b/libc/src/__support/math/fsqrtl.h
index b34e468c62221..3033f0a5242f3 100644
--- a/libc/src/__support/math/fsqrtl.h
+++ b/libc/src/__support/math/fsqrtl.h
@@ -15,7 +15,7 @@ namespace LIBC_NAMESPACE_DECL {
 
 namespace math {
 
-LIBC_INLINE static constexpr float fsqrt(long double x) {
+LIBC_INLINE static constexpr float fsqrtl(long double x) {
   return fputil::sqrt<float>(x);
 }
 



More information about the libc-commits mailing list