[libc-commits] [libc] [llvm] [libc][math] Refactor f16sqrtl to Header Only. (PR #176333)
via libc-commits
libc-commits at lists.llvm.org
Fri Jan 16 01:50:24 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Cheng Lingfei (clingfei)
<details>
<summary>Changes</summary>
builds correctly with both `clang`, `gcc`, `cmake`, and `Bazel`.
Closes https://github.com/llvm/llvm-project/issues/175331.
---
Full diff: https://github.com/llvm/llvm-project/pull/176333.diff
9 Files Affected:
- (modified) libc/shared/math.h (+1)
- (added) libc/shared/math/f16sqrtl.h (+28)
- (modified) libc/src/__support/math/CMakeLists.txt (+9)
- (added) libc/src/__support/math/f16sqrtl.h (+32)
- (modified) libc/src/math/generic/CMakeLists.txt (+2-2)
- (modified) libc/src/math/generic/f16sqrtl.cpp (+3-5)
- (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 (+18-1)
``````````diff
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",
],
)
``````````
</details>
https://github.com/llvm/llvm-project/pull/176333
More information about the libc-commits
mailing list