[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:15:31 PDT 2026
https://github.com/Sukumarsawant updated https://github.com/llvm/llvm-project/pull/187849
>From d43df2491c05388be0ca5ad9ff5d5bbb00161d8f Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sat, 21 Mar 2026 14:32:50 +0530
Subject: [PATCH 1/2] refactor: sqrtbf16 to header-only
---
libc/shared/math.h | 1 +
libc/shared/math/sqrtbf16.h | 23 ++++++++++++
libc/src/__support/math/CMakeLists.txt | 11 ++++++
libc/src/__support/math/sqrtbf16.h | 27 ++++++++++++++
libc/src/math/generic/CMakeLists.txt | 6 +--
libc/src/math/generic/sqrtbf16.cpp | 37 ++++++++-----------
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 16 ++++++++
9 files changed, 97 insertions(+), 26 deletions(-)
create mode 100644 libc/shared/math/sqrtbf16.h
create mode 100644 libc/src/__support/math/sqrtbf16.h
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"],
>From 64ec6d141379484478a7cc507efa5978a5d0cdec Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sat, 21 Mar 2026 14:45:07 +0530
Subject: [PATCH 2/2] nits
---
libc/src/__support/math/sqrtbf16.h | 10 +++---
libc/src/math/generic/sqrtbf16.cpp | 34 ++++++++++---------
.../llvm-project-overlay/libc/BUILD.bazel | 2 +-
3 files changed, 23 insertions(+), 23 deletions(-)
diff --git a/libc/src/__support/math/sqrtbf16.h b/libc/src/__support/math/sqrtbf16.h
index 14168438c3f9b..365618fde892b 100644
--- a/libc/src/__support/math/sqrtbf16.h
+++ b/libc/src/__support/math/sqrtbf16.h
@@ -1,4 +1,4 @@
-//===-- Implementation header for sqrtf16 -----------------------*- C++ -*-===//
+//===-- Implementation header for sqrtbf16 ----------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
@@ -6,8 +6,8 @@
//
//===----------------------------------------------------------------------===//
-#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_SQRTF16_H
-#define LLVM_LIBC_SRC___SUPPORT_MATH_SQRTF16_H
+#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_SQRTBF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_SQRTBF16_H
#include "src/__support/FPUtil/bfloat16.h"
#include "src/__support/FPUtil/sqrt.h"
@@ -17,9 +17,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 sqrtbf16(bfloat16 x) {
- return fputil::sqrt<bfloat16>(x);
-}
+LIBC_INLINE bfloat16 sqrtbf16(bfloat16 x) { return fputil::sqrt<bfloat16>(x); }
} // namespace math
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/sqrtbf16.cpp b/libc/src/math/generic/sqrtbf16.cpp
index f994fb3bb73f3..762378dedb0f0 100644
--- a/libc/src/math/generic/sqrtbf16.cpp
+++ b/libc/src/math/generic/sqrtbf16.cpp
@@ -1,16 +1,18 @@
-//===-- 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
+//===-- 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/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 8ff2ebec677f5..7f287a45c3711 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -5749,9 +5749,9 @@ libc_support_library(
name = "__support_math_sqrtbf16",
hdrs = ["src/__support/math/sqrtbf16.h"],
deps = [
+ ":__support_common",
":__support_fputil_bfloat16",
":__support_fputil_sqrt",
- ":__support_common",
":__support_macros_config",
],
)
More information about the libc-commits
mailing list