[libc-commits] [libc] [llvm] [libc][math] Refactor sqrtbf16 function header-only (PR #187849)
via libc-commits
libc-commits at lists.llvm.org
Sat Mar 21 02:05:36 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Zorojuro (Sukumarsawant)
<details>
<summary>Changes</summary>
This PR intends to make the sqrtbf16 function header-only.
---
Full diff: https://github.com/llvm/llvm-project/pull/187849.diff
9 Files Affected:
- (modified) libc/shared/math.h (+1)
- (added) libc/shared/math/sqrtbf16.h (+23)
- (modified) libc/src/__support/math/CMakeLists.txt (+11)
- (added) libc/src/__support/math/sqrtbf16.h (+27)
- (modified) libc/src/math/generic/CMakeLists.txt (+1-5)
- (modified) libc/src/math/generic/sqrtbf16.cpp (+16-21)
- (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 (+16)
``````````diff
diff --git a/libc/shared/math.h b/libc/shared/math.h
index ef006c668f3ab..3f20b096fc5be 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -245,6 +245,7 @@
#include "math/sinpif.h"
#include "math/sinpif16.h"
#include "math/sqrt.h"
+#include "math/sqrtbf16.h"
#include "math/sqrtf.h"
#include "math/sqrtf128.h"
#include "math/sqrtf16.h"
diff --git a/libc/shared/math/sqrtbf16.h b/libc/shared/math/sqrtbf16.h
new file mode 100644
index 0000000000000..0b10869704f27
--- /dev/null
+++ b/libc/shared/math/sqrtbf16.h
@@ -0,0 +1,23 @@
+//===-- Shared sqrtbf16 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_SQRTBF16_H
+#define LLVM_LIBC_SHARED_MATH_SQRTBF16_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/sqrtbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::sqrtbf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_SQRTBF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index abac483e40fa9..61f2d297b5624 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -3071,6 +3071,17 @@ add_header_library(
libc.src.__support.FPUtil.sqrt
)
+add_header_library(
+ sqrtbf16
+ HDRS
+ sqrtbf16.h
+ DEPENDS
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.sqrt
+ libc.src.__support.common
+ libc.src.__support.macros.config
+)
+
add_header_library(
sqrtf
HDRS
diff --git a/libc/src/__support/math/sqrtbf16.h b/libc/src/__support/math/sqrtbf16.h
new file mode 100644
index 0000000000000..14168438c3f9b
--- /dev/null
+++ b/libc/src/__support/math/sqrtbf16.h
@@ -0,0 +1,27 @@
+//===-- Implementation header for sqrtf16 -----------------------*- 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_SQRTF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_SQRTF16_H
+
+#include "src/__support/FPUtil/bfloat16.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 bfloat16 sqrtbf16(bfloat16 x) {
+ return fputil::sqrt<bfloat16>(x);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_SQRTBF16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 8aaeea0565eea..767fba6d8b4fb 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -2897,11 +2897,7 @@ add_entrypoint_object(
HDRS
../sqrtbf16.h
DEPENDS
- libc.src.__support.common
- libc.src.__support.FPUtil.bfloat16
- libc.src.__support.FPUtil.sqrt
- libc.src.__support.macros.config
- libc.src.__support.macros.properties.types
+ libc.src.__support.math.sqrtbf16
)
add_entrypoint_object(
diff --git a/libc/src/math/generic/sqrtbf16.cpp b/libc/src/math/generic/sqrtbf16.cpp
index 061e5522a4d8f..f994fb3bb73f3 100644
--- a/libc/src/math/generic/sqrtbf16.cpp
+++ b/libc/src/math/generic/sqrtbf16.cpp
@@ -1,21 +1,16 @@
-//===-- Implementation of sqrtbf16 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
-//
-//===----------------------------------------------------------------------===//
-
-#include "src/math/sqrtbf16.h"
-#include "src/__support/FPUtil/bfloat16.h"
-#include "src/__support/FPUtil/sqrt.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
-
-namespace LIBC_NAMESPACE_DECL {
-
-LLVM_LIBC_FUNCTION(bfloat16, sqrtbf16, (bfloat16 x)) {
- return fputil::sqrt<bfloat16>(x);
-}
-
-} // namespace LIBC_NAMESPACE_DECL
+//===-- Implementation of sqrtbf16 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/sqrtbf16.h"
+#include "src/__support/math/sqrtbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, sqrtbf16, (bfloat16 x)) { return math::sqrtbf16(x); }
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index bfacee9cecac4..2c432cbf64adc 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -245,6 +245,7 @@ add_fp_unittest(
libc.src.__support.math.sinpif
libc.src.__support.math.sinpif16
libc.src.__support.math.sqrt
+ libc.src.__support.math.sqrtbf16
libc.src.__support.math.sqrtf
libc.src.__support.math.tan
libc.src.__support.math.tanf
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index d958529d04fba..9fd8ee60ee6d1 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -478,4 +478,5 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) {
LIBC_NAMESPACE::shared::nexttowardbf16(bfloat16(0.0), 0.0L));
EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::nextafterbf16(
bfloat16(0.0), bfloat16(0.0)));
+ EXPECT_FP_EQ(bfloat16(1.0), LIBC_NAMESPACE::shared::sqrtbf16(bfloat16(1.0)));
}
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index a73d64efae515..8ff2ebec677f5 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -5745,6 +5745,17 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_sqrtbf16",
+ hdrs = ["src/__support/math/sqrtbf16.h"],
+ deps = [
+ ":__support_fputil_bfloat16",
+ ":__support_fputil_sqrt",
+ ":__support_common",
+ ":__support_macros_config",
+ ],
+)
+
libc_support_library(
name = "__support_math_sqrtf",
hdrs = ["src/__support/math/sqrtf.h"],
@@ -8331,6 +8342,11 @@ libc_math_function(
additional_deps = [":__support_math_sqrt"],
)
+libc_math_function(
+ name = "sqrtbf16",
+ additional_deps = [":__support_math_sqrtbf16"],
+)
+
libc_math_function(
name = "sqrtf",
additional_deps = [":__support_math_sqrtf"],
``````````
</details>
https://github.com/llvm/llvm-project/pull/187849
More information about the libc-commits
mailing list