[libc-commits] [libc] [llvm] [libc][math] Refactor sqrtf to header-only (PR #178778)

Saina Daneshmand via libc-commits libc-commits at lists.llvm.org
Thu Jan 29 20:56:25 PST 2026


https://github.com/SainaDaneshmandjahromi updated https://github.com/llvm/llvm-project/pull/178778

>From 7d450d34626e549adba00eed3d94d22077f1cd22 Mon Sep 17 00:00:00 2001
From: SainaDaneshmandjahromi <daneshmand.saina at gmail.com>
Date: Thu, 29 Jan 2026 16:29:47 -0700
Subject: [PATCH 1/2] [libc][math] Refactor sqrtf to header-only shared
 implementation

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

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 6b1f94fc6cce5..6d922a8014f45 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -99,6 +99,7 @@
 #include "math/sinhf16.h"
 #include "math/sinpif.h"
 #include "math/sqrt.h"
+#include "math/sqrtf.h"
 #include "math/sqrtf16.h"
 #include "math/tan.h"
 #include "math/tanf.h"
diff --git a/libc/shared/math/sqrtf.h b/libc/shared/math/sqrtf.h
new file mode 100644
index 0000000000000..0f053cd87bba2
--- /dev/null
+++ b/libc/shared/math/sqrtf.h
@@ -0,0 +1,24 @@
+//===-- Shared header for sqrtf ----------------------------------*- 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_SQRTF_H
+#define LLVM_LIBC_SHARED_MATH_SQRTF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/sqrtf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::sqrtf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_SQRTF_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index aad2270340c66..713a026e98268 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1435,6 +1435,14 @@ add_header_library(
     libc.src.__support.FPUtil.sqrt
 )
 
+add_header_library(
+  sqrtf
+  HDRS
+    sqrtf.h
+  DEPENDS
+    libc.src.__support.FPUtil.sqrt
+)
+
 add_header_library(
   dfmal
   HDRS
diff --git a/libc/src/__support/math/sqrtf.h b/libc/src/__support/math/sqrtf.h
new file mode 100644
index 0000000000000..c32282e99b069
--- /dev/null
+++ b/libc/src/__support/math/sqrtf.h
@@ -0,0 +1,25 @@
+//===-- Implementation header for sqrtf --------------------------*- 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_SQRTF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_SQRTF_H
+
+#include "src/__support/FPUtil/sqrt.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static float sqrtf(float x) { return fputil::sqrt<float>(x); }
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_SQRTF_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 21c3a861c87d7..c0d40ce83bccd 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -2985,7 +2985,6 @@ add_entrypoint_object(
     libc.src.__support.math.sqrt
 )
 
-
 add_entrypoint_object(
   sqrtf
   SRCS
@@ -2993,7 +2992,7 @@ add_entrypoint_object(
   HDRS
     ../sqrtf.h
   DEPENDS
-    libc.src.__support.FPUtil.sqrt
+    libc.src.__support.math.sqrtf
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/sqrtf.cpp b/libc/src/math/generic/sqrtf.cpp
index 69e0c34770055..af352b3b8e5ad 100644
--- a/libc/src/math/generic/sqrtf.cpp
+++ b/libc/src/math/generic/sqrtf.cpp
@@ -7,12 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/sqrtf.h"
-#include "src/__support/FPUtil/sqrt.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/sqrtf.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(float, sqrtf, (float x)) { return fputil::sqrt<float>(x); }
+LLVM_LIBC_FUNCTION(float, sqrtf, (float x)) { return math::sqrtf(x); }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index b0a5ad730cbb2..2211d80c9009f 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -97,6 +97,7 @@ add_fp_unittest(
     libc.src.__support.math.sinhf16
     libc.src.__support.math.sinpif
     libc.src.__support.math.sqrt
+    libc.src.__support.math.sqrtf
     libc.src.__support.math.tan
     libc.src.__support.math.tanf
 )
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index a21863e23c3f9..49e7450df1861 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -99,6 +99,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
   EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::rsqrtf(1.0f));
   EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::sinpif(0.0f));
   EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::sinf(0.0f));
+  EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::sqrtf(0.0f));
   EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::tanf(0.0f));
 }
 
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index a313ba48889d8..233a0388775a7 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3639,6 +3639,14 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_sqrtf",
+    hdrs = ["src/__support/math/sqrtf.h"],
+    deps = [
+        ":__support_fputil_sqrt",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_hypotf",
     hdrs = ["src/__support/math/hypotf.h"],
@@ -5354,7 +5362,7 @@ libc_math_function(
 libc_math_function(
     name = "sqrtf",
     additional_deps = [
-        ":__support_fputil_sqrt",
+        ":__support_math_sqrtf",
     ],
 )
 

>From 1387dc2289a7eed446b09238476b13cd526e9740 Mon Sep 17 00:00:00 2001
From: SainaDaneshmandjahromi <daneshmand.saina at gmail.com>
Date: Thu, 29 Jan 2026 21:56:10 -0700
Subject: [PATCH 2/2] requested changes applied

---
 libc/shared/math/sqrtf.h        | 3 +--
 libc/src/__support/math/sqrtf.h | 3 +--
 2 files changed, 2 insertions(+), 4 deletions(-)

diff --git a/libc/shared/math/sqrtf.h b/libc/shared/math/sqrtf.h
index 0f053cd87bba2..51a9020db4bf3 100644
--- a/libc/shared/math/sqrtf.h
+++ b/libc/shared/math/sqrtf.h
@@ -1,5 +1,4 @@
-//===-- Shared header for sqrtf ----------------------------------*- C++
-//-*-===//
+//===-- Shared header for sqrtf ----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/libc/src/__support/math/sqrtf.h b/libc/src/__support/math/sqrtf.h
index c32282e99b069..ba7b2783e9544 100644
--- a/libc/src/__support/math/sqrtf.h
+++ b/libc/src/__support/math/sqrtf.h
@@ -1,5 +1,4 @@
-//===-- Implementation header for sqrtf --------------------------*- C++
-//-*-===//
+//===-- Implementation header for sqrtf --------------------------*- 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