[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
Mon Jan 19 11:47:32 PST 2026


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

>From ce6663380bce22280c7a81ac9886984e2e939ac7 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/3] [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 d58238703701d..016e98aec66ca 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -64,6 +64,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 bf1c4463b8066..bc24f7fbfaba1 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 b578d1805f2a8..7701ca53c80bd 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5232,7 +5232,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 cfc2eda1aaec9..bddc94b1f8410 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -60,6 +60,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 88e6ecc98a0a7..f2b2611b2d1f9 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -108,7 +108,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 90b1da0e3376e..dc98eda392317 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2841,6 +2841,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"],
@@ -4389,7 +4397,7 @@ libc_math_function(
 libc_math_function(
     name = "fsqrtl",
     additional_deps = [
-        ":__support_fputil_sqrt",
+        ":__support_math_fsqrtl",
     ],
 )
 

>From 0fbbaa2a7458e3f78140bc56f8968ede13d19710 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/3] 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 f2b2611b2d1f9..574c7e2002f92 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -110,8 +110,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 b08e00be3b2db37c9c05aee0fca2d6fd220ec54f 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/3] 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 574c7e2002f92..6d7ed50d7eebd 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -108,7 +108,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));
 }
 



More information about the libc-commits mailing list