[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
Sun Jan 11 08:56:35 PST 2026
https://github.com/jolwnn updated https://github.com/llvm/llvm-project/pull/175444
>From ac7fefc8062525bc580b3c6f46fe7dda15fdced9 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/2] 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 7fb4c43f509c4..8fcd8ef98d9b6 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -66,5 +66,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 741da7432c94f..86811c223e497 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 9c0da076b6cf0..01783691a1c1c 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5244,7 +5244,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 f823d414e2afd..1ab47b3a96bfd 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -90,6 +90,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(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 210c25dddd0b9..e4a36fa52a7fb 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"],
@@ -4250,7 +4258,7 @@ libc_math_function(name = "fromfpxf16")
libc_math_function(
name = "fsqrt",
additional_deps = [
- ":__support_fputil_sqrt",
+ ":__support_math_fsqrt",
],
)
>From 226731a59464f04196ff967fb96cdc8c52fe750d 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/2] 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 8fcd8ef98d9b6..05c901e3984cf 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -60,12 +60,12 @@
#include "math/frexpf.h"
#include "math/frexpf128.h"
#include "math/frexpf16.h"
+#include "math/fsqrt.h"
#include "math/ldexpf.h"
#include "math/ldexpf128.h"
#include "math/ldexpf16.h"
#include "math/rsqrtf.h"
#include "math/rsqrtf16.h"
#include "math/sin.h"
-#include "math/fsqrt.h"
#endif // LLVM_LIBC_SHARED_MATH_H
More information about the libc-commits
mailing list