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

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


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Zhihui Yang (YGGkk)

<details>
<summary>Changes</summary>

Part of https://github.com/llvm/llvm-project/issues/147386

in preparation for: https://discourse.llvm.org/t/rfc-make-clang-builtin-math-functions-constexpr-with-llvm-libc-to-support-c-23-constexpr-math-functions/86450

---
Full diff: https://github.com/llvm/llvm-project/pull/177167.diff


8 Files Affected:

- (modified) libc/shared/math.h (+1) 
- (added) libc/shared/math/f16sqrt.h (+23) 
- (modified) libc/src/__support/math/CMakeLists.txt (+10) 
- (added) libc/src/__support/math/f16sqrt.h (+24) 
- (modified) libc/src/math/generic/f16sqrt.cpp (+2-6) 
- (modified) libc/test/shared/CMakeLists.txt (+1) 
- (modified) libc/test/shared/shared_math_test.cpp (+1) 
- (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+11-1) 


``````````diff
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",
     ],
 )
 

``````````

</details>


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


More information about the libc-commits mailing list