[libc-commits] [libc] [llvm] [libc][math] Refactor f16sqrt to Header Only (PR #177167)

Zhihui Yang via libc-commits libc-commits at lists.llvm.org
Wed Jan 21 05:41:23 PST 2026


https://github.com/YGGkk updated https://github.com/llvm/llvm-project/pull/177167

>From a09a7289c68966da1066aa688d2ab09adb4c1100 Mon Sep 17 00:00:00 2001
From: Zhihui Yang <youngwisdm at gmail.com>
Date: Wed, 21 Jan 2026 05:37:18 -0800
Subject: [PATCH 1/2] [libc][math] Refactor f16sqrt to Header Only

---
 libc/shared/math.h                            |  1 +
 libc/shared/math/f16sqrt.h                    | 23 ++++++++++++++++++
 libc/src/__support/math/CMakeLists.txt        | 10 ++++++++
 libc/src/__support/math/f16sqrt.h             | 24 +++++++++++++++++++
 libc/src/math/generic/f16sqrt.cpp             |  8 ++-----
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  1 +
 .../llvm-project-overlay/libc/BUILD.bazel     | 12 +++++++++-
 8 files changed, 73 insertions(+), 7 deletions(-)
 create mode 100644 libc/shared/math/f16sqrt.h
 create mode 100644 libc/src/__support/math/f16sqrt.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 2e7bbe1ef13be..59b3c1ccfc27a 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -69,6 +69,7 @@
 #include "math/log.h"
 #include "math/rsqrtf.h"
 #include "math/rsqrtf16.h"
+#include "math/f16sqrt.h"
 #include "math/sin.h"
 
 #endif // LLVM_LIBC_SHARED_MATH_H
diff --git a/libc/shared/math/f16sqrt.h b/libc/shared/math/f16sqrt.h
new file mode 100644
index 0000000000000..e41e7865c8448
--- /dev/null
+++ b/libc/shared/math/f16sqrt.h
@@ -0,0 +1,23 @@
+//===-- Shared f16sqrt 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_F16SQRT_H
+#define LLVM_LIBC_SHARED_MATH_F16SQRT_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/f16sqrt.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::f16sqrt;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_F16SQRT_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index b45ccfc0eb3cb..9003e0980ccd6 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1096,3 +1096,13 @@ add_header_library(
     libc.src.__support.common
     libc.src.__support.macros.config
 )
+
+add_header_library(
+  f16sqrt
+  HDRS
+    f16sqrt.h
+  DEPENDS
+    libc.src.__support.FPUtil.generic.sqrt
+    libc.src.__support.common
+    libc.src.__support.macros.config
+)
\ No newline at end of file
diff --git a/libc/src/__support/math/f16sqrt.h b/libc/src/__support/math/f16sqrt.h
new file mode 100644
index 0000000000000..6294265a730ce
--- /dev/null
+++ b/libc/src/__support/math/f16sqrt.h
@@ -0,0 +1,24 @@
+//===-- Implementation header for f16sqrt -----------------------*- 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_SINPIF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_SINPIF_H
+
+#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 static constexpr float16 f16sqrt(double x) {
+  return fputil::sqrt<float16>(x);
+}
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_SINPIF_H
diff --git a/libc/src/math/generic/f16sqrt.cpp b/libc/src/math/generic/f16sqrt.cpp
index 4addb553a5851..d33d51355bd39 100644
--- a/libc/src/math/generic/f16sqrt.cpp
+++ b/libc/src/math/generic/f16sqrt.cpp
@@ -7,14 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/f16sqrt.h"
-#include "src/__support/FPUtil/sqrt.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/f16sqrt.h"
 
 namespace LIBC_NAMESPACE_DECL {
-
 LLVM_LIBC_FUNCTION(float16, f16sqrt, (double x)) {
-  return fputil::sqrt<float16>(x);
+  return math::f16sqrt(x);
 }
-
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 58214bf6f88eb..ae3869a52c7e7 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -66,4 +66,5 @@ add_fp_unittest(
     libc.src.__support.math.rsqrtf
     libc.src.__support.math.rsqrtf16
     libc.src.__support.math.sin
+    libc.src.__support.math.f16sqrt
 )
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 6b78de0281347..8e91acc7704a3 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -32,6 +32,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
   EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::exp2m1f16(0.0f16));
   EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::expf16(0.0f16));
   EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::expm1f16(0.0f16));
+  EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::f16sqrt(0.0f16));
 
   ASSERT_FP_EQ(float16(8 << 5), LIBC_NAMESPACE::shared::ldexpf16(8.0f16, 5));
   ASSERT_FP_EQ(float16(-1 * (8 << 5)),
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 4251f20a5dfcf..86621b33a999d 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3259,6 +3259,16 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_f16sqrt",
+    hdrs = ["src/__support/math/f16sqrt.h"],
+    deps = [
+        ":__support_common",
+        ":__support_fputil_generic_sqrt",
+        ":__support_macros_config",
+    ]
+)
+
 ############################### complex targets ################################
 
 libc_function(
@@ -3977,7 +3987,7 @@ libc_math_function(name = "f16mull")
 libc_math_function(
     name = "f16sqrt",
     additional_deps = [
-        ":__support_fputil_sqrt",
+        ":__support_math_f16sqrt",
     ],
 )
 

>From 0ac4d7f3a6b5a0431abe8cebd950eecd3e9ac7f9 Mon Sep 17 00:00:00 2001
From: Zhihui Yang <youngwisdm at gmail.com>
Date: Wed, 21 Jan 2026 05:41:09 -0800
Subject: [PATCH 2/2] format code

---
 libc/shared/math.h                | 2 +-
 libc/src/math/generic/f16sqrt.cpp | 4 +---
 2 files changed, 2 insertions(+), 4 deletions(-)

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 59b3c1ccfc27a..4d782c5d39a56 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -58,6 +58,7 @@
 #include "math/expm1.h"
 #include "math/expm1f.h"
 #include "math/expm1f16.h"
+#include "math/f16sqrt.h"
 #include "math/frexpf.h"
 #include "math/frexpf128.h"
 #include "math/frexpf16.h"
@@ -69,7 +70,6 @@
 #include "math/log.h"
 #include "math/rsqrtf.h"
 #include "math/rsqrtf16.h"
-#include "math/f16sqrt.h"
 #include "math/sin.h"
 
 #endif // LLVM_LIBC_SHARED_MATH_H
diff --git a/libc/src/math/generic/f16sqrt.cpp b/libc/src/math/generic/f16sqrt.cpp
index d33d51355bd39..17fac910af1af 100644
--- a/libc/src/math/generic/f16sqrt.cpp
+++ b/libc/src/math/generic/f16sqrt.cpp
@@ -10,7 +10,5 @@
 #include "src/__support/math/f16sqrt.h"
 
 namespace LIBC_NAMESPACE_DECL {
-LLVM_LIBC_FUNCTION(float16, f16sqrt, (double x)) {
-  return math::f16sqrt(x);
-}
+LLVM_LIBC_FUNCTION(float16, f16sqrt, (double x)) { return math::f16sqrt(x); }
 } // namespace LIBC_NAMESPACE_DECL



More information about the libc-commits mailing list