[libc] [llvm] [libc][math] Refactor `f16sqrtf` to Header Only. (PR #180749)

via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 10 07:01:11 PST 2026


https://github.com/jinyouzhi created https://github.com/llvm/llvm-project/pull/180749

fix https://github.com/llvm/llvm-project/issues/175329

>From fc481447d9dd71aad88f3e0b8acaef71dfbfcc99 Mon Sep 17 00:00:00 2001
From: iLeGend <824040212 at qq.com>
Date: Tue, 10 Feb 2026 22:30:43 +0800
Subject: [PATCH] [libc][math] Refactor `f16sqrtf` to Header Only.

---
 libc/shared/math.h                            |  1 +
 libc/shared/math/f16sqrtf.h                   | 29 +++++++++++++++++
 libc/src/__support/math/CMakeLists.txt        | 10 ++++++
 libc/src/__support/math/f16sqrtf.h            | 32 +++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  3 +-
 libc/src/math/generic/f16sqrtf.cpp            |  8 ++---
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  2 ++
 .../llvm-project-overlay/libc/BUILD.bazel     | 13 +++++++-
 9 files changed, 90 insertions(+), 9 deletions(-)
 create mode 100644 libc/shared/math/f16sqrtf.h
 create mode 100644 libc/src/__support/math/f16sqrtf.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 66132326c2257..6248df5662977 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -64,6 +64,7 @@
 #include "math/f16fmaf128.h"
 #include "math/f16fmal.h"
 #include "math/f16sqrt.h"
+#include "math/f16sqrtf.h"
 #include "math/f16sqrtl.h"
 #include "math/ffmal.h"
 #include "math/frexpf.h"
diff --git a/libc/shared/math/f16sqrtf.h b/libc/shared/math/f16sqrtf.h
new file mode 100644
index 0000000000000..65f580cb3fed8
--- /dev/null
+++ b/libc/shared/math/f16sqrtf.h
@@ -0,0 +1,29 @@
+//===-- Shared f16sqrtf function --------------------------------*- 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_F16SQRTF_H
+#define LLVM_LIBC_SHARED_MATH_F16SQRTF_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "shared/libc_common.h"
+#include "src/__support/math/f16sqrtf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::f16sqrtf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_F16SQRTF_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index c0293c0969217..c8555f61ca018 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -613,6 +613,16 @@ add_header_library(
     libc.src.__support.macros.config
 )
 
+add_header_library(
+  f16sqrtf
+  HDRS
+    f16sqrtf.h
+  DEPENDS
+    libc.src.__support.FPUtil.sqrt
+    libc.src.__support.common
+    libc.src.__support.macros.config
+)
+
 add_header_library(
   f16sqrtl
   HDRS
diff --git a/libc/src/__support/math/f16sqrtf.h b/libc/src/__support/math/f16sqrtf.h
new file mode 100644
index 0000000000000..e6b513cc8e617
--- /dev/null
+++ b/libc/src/__support/math/f16sqrtf.h
@@ -0,0 +1,32 @@
+//===-- Implementation header for f16sqrtf ----------------------*- 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_F16SQRTF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_F16SQRTF_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE constexpr float16 f16sqrtf(float x) {
+  return fputil::sqrt<float16>(x);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_F16SQRTF_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 96dc0e7ca4851..01824aab9b12c 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5072,8 +5072,7 @@ add_entrypoint_object(
   HDRS
     ../f16sqrtf.h
   DEPENDS
-    libc.src.__support.macros.properties.types
-    libc.src.__support.FPUtil.sqrt
+    libc.src.__support.math.f16sqrtf
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/f16sqrtf.cpp b/libc/src/math/generic/f16sqrtf.cpp
index be43a77787fae..6ac777d4dca9a 100644
--- a/libc/src/math/generic/f16sqrtf.cpp
+++ b/libc/src/math/generic/f16sqrtf.cpp
@@ -7,14 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/f16sqrtf.h"
-#include "src/__support/FPUtil/sqrt.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/f16sqrtf.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(float16, f16sqrtf, (float x)) {
-  return fputil::sqrt<float16>(x);
-}
+LLVM_LIBC_FUNCTION(float16, f16sqrtf, (float x)) { return math::f16sqrtf(x); }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 7d72ced3e057c..1ba11aa0f7cc0 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -60,6 +60,7 @@ add_fp_unittest(
     libc.src.__support.math.f16fmaf128
     libc.src.__support.math.f16fmal
     libc.src.__support.math.f16sqrt
+    libc.src.__support.math.f16sqrtf
     libc.src.__support.math.f16sqrtl
     libc.src.__support.math.ffmal
     libc.src.__support.math.frexpf
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 9e024387efbfa..2bbed3215b0da 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -50,6 +50,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
 
   EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::f16sqrt(0.0));
 
+  EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::f16sqrtf(0.0f));
+
   EXPECT_FP_EQ(float16(10.0),
                LIBC_NAMESPACE::shared::f16fmal(2.0L, 3.0L, 4.0L));
 
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 3eae5e4a05b51..b2796c29c2764 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -1421,6 +1421,17 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_f16sqrtf",
+    hdrs = ["src/__support/math/f16sqrtf.h"],
+    deps = [
+        ":__support_common",
+        ":__support_fputil_sqrt",
+        ":__support_macros_config",
+        ":llvm_libc_macros_float16_macros",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_f16sqrtl",
     hdrs = ["src/__support/math/f16sqrtl.h"],
@@ -4407,7 +4418,7 @@ libc_math_function(
 
 libc_math_function(
     name = "f16sqrtf",
-    additional_deps = [":__support_fputil_sqrt"],
+    additional_deps = [":__support_math_f16sqrtf"],
 )
 
 libc_math_function(



More information about the llvm-commits mailing list