[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
Sat Jan 24 21:46:25 PST 2026


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

>From a5a93927d260c7bffa8f09c63c92bae78b3cd86e 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/5] [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 5126e46c9772e..a1232617e843d 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 d18ebc03ba7b8..f52cfa62a934c 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 3addfab6c6603..bd809dae6112f 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5187,7 +5187,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 dabf5f6b168cc..ef32e31839e58 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 cd389c41cb7b2..5da7073d4be80 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -116,7 +116,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 346864d2276c0..bdc2b1101d44a 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"],
@@ -4553,7 +4561,7 @@ libc_math_function(
 libc_math_function(
     name = "fsqrtl",
     additional_deps = [
-        ":__support_fputil_sqrt",
+        ":__support_math_fsqrtl",
     ],
 )
 

>From 86e63fea1e9dfd43e4eb771ead5781d47e6a87d6 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/5] 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 5da7073d4be80..3ab2ba1303186 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -118,8 +118,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 5323aaa8ffdaf9770ddd4e9b4181d2fc7f638607 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/5] 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 3ab2ba1303186..58df5f824e066 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -116,7 +116,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 c4f6b6d4599c4bebae7236af8569187bdd162cfc 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/5] 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);
 }
 

>From d734ae564536f9f924908304c35da2a04e09b3c3 Mon Sep 17 00:00:00 2001
From: Atharva062006 <72563240+Atharva062006 at users.noreply.github.com>
Date: Sun, 25 Jan 2026 11:14:48 +0530
Subject: [PATCH 5/5] fixed formatting

---
 libc/shared/math/fsqrtl.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libc/shared/math/fsqrtl.h b/libc/shared/math/fsqrtl.h
index 0f2271864d1b2..f1af239cf0657 100644
--- a/libc/shared/math/fsqrtl.h
+++ b/libc/shared/math/fsqrtl.h
@@ -1,4 +1,5 @@
-//===-- Shared header for fsqrtl ---------------------------------*- C++ -*-===//
+//===-- 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.



More information about the libc-commits mailing list