[libc-commits] [libc] [llvm] [libc][math] Refactor f16sqrtl to Header Only. (PR #176333)

Cheng Lingfei via libc-commits libc-commits at lists.llvm.org
Fri Jan 16 01:49:49 PST 2026


https://github.com/clingfei created https://github.com/llvm/llvm-project/pull/176333

builds correctly with both `clang`, `gcc`, `cmake`, and `Bazel`.

Closes https://github.com/llvm/llvm-project/issues/175331.

>From 7282ad3eff5f6533a51aefc9fb9ddd390b6794ca Mon Sep 17 00:00:00 2001
From: Cheng Lingfei <clf700383 at gmail.com>
Date: Fri, 16 Jan 2026 17:41:55 +0800
Subject: [PATCH] [libc][math] Refactor f16sqrtl to Header Only.

---
 libc/shared/math.h                            |  1 +
 libc/shared/math/f16sqrtl.h                   | 28 ++++++++++++++++
 libc/src/__support/math/CMakeLists.txt        |  9 ++++++
 libc/src/__support/math/f16sqrtl.h            | 32 +++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  4 +--
 libc/src/math/generic/f16sqrtl.cpp            |  8 ++---
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  1 +
 .../llvm-project-overlay/libc/BUILD.bazel     | 19 ++++++++++-
 9 files changed, 95 insertions(+), 8 deletions(-)
 create mode 100644 libc/shared/math/f16sqrtl.h
 create mode 100644 libc/src/__support/math/f16sqrtl.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 287f3aea1565a..a8812f8fcf1f0 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -60,6 +60,7 @@
 #include "math/frexpf.h"
 #include "math/frexpf128.h"
 #include "math/frexpf16.h"
+#include "math/f16sqrtl.h"
 #include "math/ilogbf16.h"
 #include "math/ldexpf.h"
 #include "math/ldexpf128.h"
diff --git a/libc/shared/math/f16sqrtl.h b/libc/shared/math/f16sqrtl.h
new file mode 100644
index 0000000000000..2cda158bde61c
--- /dev/null
+++ b/libc/shared/math/f16sqrtl.h
@@ -0,0 +1,28 @@
+//===-- 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_F16SQRTL_H
+#define LLVM_LIBC_SHARED_MATH_F16SQRTL_H
+
+#include "include/llvm-libc-types/float_t.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/math/f16sqrtl.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::f16sqrtl;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+       //
+#endif // LLVM_LIBC_SHARED_MATH_F16SQRTL_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 4139a1b1d3444..6f9fba935bbd3 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -594,6 +594,15 @@ add_header_library(
     libc.src.__support.math.exp10_float16_constants
 )
 
+add_header_library(
+  f16sqrtl
+  HDRS
+    f16sqrtl.h
+  DEPENDS
+    libc.src.__support.FPUtil.generic.sqrt
+    libc.src.__support.macros.properties.types
+)
+
 add_header_library(
   frexpf128
   HDRS
diff --git a/libc/src/__support/math/f16sqrtl.h b/libc/src/__support/math/f16sqrtl.h
new file mode 100644
index 0000000000000..01a8cc72fec42
--- /dev/null
+++ b/libc/src/__support/math/f16sqrtl.h
@@ -0,0 +1,32 @@
+//===-- Implementation of f16sqrt function --------------------------------===//
+//
+// 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_F16SQRTL_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_F16SQRTL_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 static constexpr float16 f16sqrtl(long double x) {
+  return fputil::sqrt<float16>(x);
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_F16SQRTL_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 6de4cf376bf02..44920e362885a 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5202,8 +5202,8 @@ add_entrypoint_object(
   HDRS
     ../f16sqrtl.h
   DEPENDS
-    libc.src.__support.macros.properties.types
-    libc.src.__support.FPUtil.sqrt
+    libc.src.__support.math.f16sqrtl
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/f16sqrtl.cpp b/libc/src/math/generic/f16sqrtl.cpp
index c6ce73de7fec9..fe9abd41834b7 100644
--- a/libc/src/math/generic/f16sqrtl.cpp
+++ b/libc/src/math/generic/f16sqrtl.cpp
@@ -1,4 +1,4 @@
-//===-- Implementation of f16sqrtl function -------------------------------===//
+//===-- Implementation of f16sqrt function --------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/f16sqrtl.h"
-#include "src/__support/FPUtil/sqrt.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/f16sqrtl.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float16, f16sqrtl, (long double x)) {
-  return fputil::sqrt<float16>(x);
+  return math::f16sqrtl(x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index c5955ecfd54cb..fd7d70fbfcc90 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -53,6 +53,7 @@ add_fp_unittest(
     libc.src.__support.math.exp10f16
     libc.src.__support.math.expf
     libc.src.__support.math.expf16
+    libc.src.__support.math.f16sqrtl
     libc.src.__support.math.frexpf
     libc.src.__support.math.frexpf128
     libc.src.__support.math.frexpf16
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 20a98a1a9138c..72ff412aa6aa9 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -44,6 +44,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::ilogbf16(1.0f16));
 
   EXPECT_FP_EQ(0x1.921fb6p+0f16, LIBC_NAMESPACE::shared::acosf16(0.0f16));
+  EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::f16sqrtl(float128(1.0f)));
 }
 
 #endif // LIBC_TYPES_HAS_FLOAT16
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index c03c21b834895..bcf173cb1f357 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -174,6 +174,11 @@ libc_support_library(
     deps = [":llvm_libc_macros_float_macros"],
 )
 
+libc_support_library(
+    name = "llvm_libc_types_float_t",
+    hdrs = ["include/llvm-libc-types/float_t.h"],
+)
+
 libc_support_library(
     name = "llvm_libc_types_cfloat128",
     hdrs = ["include/llvm-libc-types/cfloat128.h"],
@@ -1139,6 +1144,17 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_f16sqrtl",
+    hdrs = ["src/__support/math/f16sqrtl.h"],
+    deps = [
+        ":__support_common",
+        ":__support_fputil_sqrt",
+        ":__support_macros_config",
+        ":llvm_libc_types_float_t",
+    ],
+)
+
 libc_support_library(
     name = "__support_fixed_point",
     hdrs = [
@@ -3980,7 +3996,8 @@ libc_math_function(
 libc_math_function(
     name = "f16sqrtl",
     additional_deps = [
-        ":__support_fputil_sqrt",
+        ":__support_math_f16sqrtl",
+        ":errno",
     ],
 )
 



More information about the libc-commits mailing list