[libc-commits] [libc] [llvm] [libc][math] Refactor fsqrt to Header Only (PR #175444)

Jolynn Wee Zhuo Lin via libc-commits libc-commits at lists.llvm.org
Mon Jan 12 07:49:33 PST 2026


https://github.com/jolwnn updated https://github.com/llvm/llvm-project/pull/175444

>From 7de4af131fc49e97ee1b2e54511f104378f4b2b2 Mon Sep 17 00:00:00 2001
From: jolwnn <zhuolinwee at gmail.com>
Date: Mon, 12 Jan 2026 00:40:46 +0800
Subject: [PATCH 1/5] refactor fsqrt to header only

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

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 287f3aea1565a..0fe55647f20d9 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -68,5 +68,6 @@
 #include "math/rsqrtf.h"
 #include "math/rsqrtf16.h"
 #include "math/sin.h"
+#include "math/fsqrt.h"
 
 #endif // LLVM_LIBC_SHARED_MATH_H
diff --git a/libc/shared/math/fsqrt.h b/libc/shared/math/fsqrt.h
new file mode 100644
index 0000000000000..635b155b58e44
--- /dev/null
+++ b/libc/shared/math/fsqrt.h
@@ -0,0 +1,24 @@
+//===-- Implementation header for fsqrt ------------------------*- 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_FSQRT_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FSQRT_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/fsqrt.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace shared {
+
+using math::fsqrt;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_FSQRT_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..9cda62426220c 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -594,6 +594,14 @@ add_header_library(
     libc.src.__support.math.exp10_float16_constants
 )
 
+add_header_library(
+  fsqrt
+  HDRS
+    fsqrt.h
+  DEPENDS
+    libc.src.__support.FPUtil.generic.sqrt
+)
+
 add_header_library(
   frexpf128
   HDRS
diff --git a/libc/src/__support/math/fsqrt.h b/libc/src/__support/math/fsqrt.h
new file mode 100644
index 0000000000000..8dd6afc7845f0
--- /dev/null
+++ b/libc/src/__support/math/fsqrt.h
@@ -0,0 +1,26 @@
+//===-- Implementation header for fsqrt ------------------------*- 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_FSQRT_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FSQRT_H
+
+#include "src/__support/FPUtil/generic/sqrt.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float fsqrt(double x) {
+  return fputil::sqrt<float>(x);
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FSQRT_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 6de4cf376bf02..fb5fd8ef4a4af 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5224,7 +5224,7 @@ add_entrypoint_object(
   HDRS
     ../fsqrt.h
   DEPENDS
-    libc.src.__support.FPUtil.generic.sqrt
+    libc.src.__support.math.fsqrt
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/fsqrt.cpp b/libc/src/math/generic/fsqrt.cpp
index d54471fd067bf..df1d4a9add9b2 100644
--- a/libc/src/math/generic/fsqrt.cpp
+++ b/libc/src/math/generic/fsqrt.cpp
@@ -7,12 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/fsqrt.h"
-#include "src/__support/FPUtil/generic/sqrt.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/fsqrt.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(float, fsqrt, (double x)) { return fputil::sqrt<float>(x); }
+LLVM_LIBC_FUNCTION(float, fsqrt, (double x)) { return math::fsqrt(x); }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 20a98a1a9138c..aaa706f4f8383 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::fsqrt(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..2f3dc8a52a8b6 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2804,6 +2804,14 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_fsqrt",
+    hdrs = ["src/__support/math/fsqrt.h"],
+    deps = [
+        ":__support_fputil_sqrt",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_frexpf128",
     hdrs = ["src/__support/math/frexpf128.h"],
@@ -4278,7 +4286,7 @@ libc_math_function(name = "fromfpxf16")
 libc_math_function(
     name = "fsqrt",
     additional_deps = [
-        ":__support_fputil_sqrt",
+        ":__support_math_fsqrt",
     ],
 )
 

>From 7cd9ee425515c532b8bc8ca3819cae9783bf135e Mon Sep 17 00:00:00 2001
From: jolwnn <zhuolinwee at gmail.com>
Date: Mon, 12 Jan 2026 00:56:21 +0800
Subject: [PATCH 2/5] fix clang-format issues

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

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 0fe55647f20d9..7b82d8288a845 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/fsqrt.h"
 #include "math/ilogbf16.h"
 #include "math/ldexpf.h"
 #include "math/ldexpf128.h"
@@ -68,6 +69,5 @@
 #include "math/rsqrtf.h"
 #include "math/rsqrtf16.h"
 #include "math/sin.h"
-#include "math/fsqrt.h"
 
 #endif // LLVM_LIBC_SHARED_MATH_H

>From fc35a4c1516336a6017a495fe5f66508d2d928c1 Mon Sep 17 00:00:00 2001
From: jolwnn <zhuolinwee at gmail.com>
Date: Mon, 12 Jan 2026 01:19:16 +0800
Subject: [PATCH 3/5] fix build error

---
 libc/shared/math/fsqrt.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/libc/shared/math/fsqrt.h b/libc/shared/math/fsqrt.h
index 635b155b58e44..12b6249b03b55 100644
--- a/libc/shared/math/fsqrt.h
+++ b/libc/shared/math/fsqrt.h
@@ -6,8 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_FSQRT_H
-#define LLVM_LIBC_SRC___SUPPORT_MATH_FSQRT_H
+#ifndef LLVM_LIBC_SHARED_MATH_FSQRT_H
+#define LLVM_LIBC_SHARED_MATH_FSQRT_H
 
 #include "shared/libc_common.h"
 #include "src/__support/math/fsqrt.h"
@@ -21,4 +21,4 @@ using math::fsqrt;
 } // namespace shared
 } // namespace LIBC_NAMESPACE_DECL
 
-#endif // LLVM_LIBC_SHARED_MATH_FSQRT_H
\ No newline at end of file
+#endif // LLVM_LIBC_SHARED_MATH_FSQRT_H

>From 79a6338680bfdc8c982fc128228174e59a61f845 Mon Sep 17 00:00:00 2001
From: Jolynn Wee Zhuo Lin <85361959+jolwnn at users.noreply.github.com>
Date: Mon, 12 Jan 2026 23:03:51 +0800
Subject: [PATCH 4/5] Update libc/shared/math/fsqrt.h

Co-authored-by: Muhammad Bassiouni <60100307+bassiounix at users.noreply.github.com>
---
 libc/shared/math/fsqrt.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/shared/math/fsqrt.h b/libc/shared/math/fsqrt.h
index 12b6249b03b55..c8d6f262622de 100644
--- a/libc/shared/math/fsqrt.h
+++ b/libc/shared/math/fsqrt.h
@@ -1,4 +1,4 @@
-//===-- Implementation header for fsqrt ------------------------*- C++ -*-===//
+//===-- Shared header for fsqrt ---------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.

>From a3bb5b54ecdb173e2f9a924f9c850f2cdcf3842c Mon Sep 17 00:00:00 2001
From: jolwnn <zhuolinwee at gmail.com>
Date: Mon, 12 Jan 2026 23:38:30 +0800
Subject: [PATCH 5/5] fix order and update cmake with fsqrt test

---
 libc/test/shared/CMakeLists.txt                   |  1 +
 utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 14 +++++++-------
 2 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index c5955ecfd54cb..cf4f5477136ae 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.fsqrt
     libc.src.__support.math.ilogbf16
     libc.src.__support.math.log
     libc.src.__support.math.ldexpf
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 2f3dc8a52a8b6..fe149261cc29f 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2805,19 +2805,19 @@ libc_support_library(
 )
 
 libc_support_library(
-    name = "__support_math_fsqrt",
-    hdrs = ["src/__support/math/fsqrt.h"],
+    name = "__support_math_frexpf128",
+    hdrs = ["src/__support/math/frexpf128.h"],
     deps = [
-        ":__support_fputil_sqrt",
+        ":__support_fputil_manipulation_functions",
+        ":__support_macros_properties_types",
     ],
 )
 
 libc_support_library(
-    name = "__support_math_frexpf128",
-    hdrs = ["src/__support/math/frexpf128.h"],
+    name = "__support_math_fsqrt",
+    hdrs = ["src/__support/math/fsqrt.h"],
     deps = [
-        ":__support_fputil_manipulation_functions",
-        ":__support_macros_properties_types",
+        ":__support_fputil_sqrt",
     ],
 )
 



More information about the libc-commits mailing list