[libc-commits] [libc] [llvm] [libc][math] Refactor cbrtbf16 function header-only (PR #188204)
via libc-commits
libc-commits at lists.llvm.org
Tue Mar 24 02:50:28 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 cbrtbf16 function header-only.
---
Full diff: https://github.com/llvm/llvm-project/pull/188204.diff
9 Files Affected:
- (modified) libc/shared/math.h (+1)
- (added) libc/shared/math/cbrtbf16.h (+23)
- (modified) libc/src/__support/math/CMakeLists.txt (+10)
- (added) libc/src/__support/math/cbrtbf16.h (+26)
- (modified) libc/src/math/generic/CMakeLists.txt (+1-5)
- (modified) libc/src/math/generic/cbrtbf16.cpp (+2-4)
- (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 (+15)
``````````diff
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 6add9628cabdd..619892259d9ae 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -62,6 +62,7 @@
#include "math/canonicalizef16.h"
#include "math/canonicalizel.h"
#include "math/cbrt.h"
+#include "math/cbrtbf16.h"
#include "math/cbrtf.h"
#include "math/ceil.h"
#include "math/ceilbf16.h"
diff --git a/libc/shared/math/cbrtbf16.h b/libc/shared/math/cbrtbf16.h
new file mode 100644
index 0000000000000..9b10b26e46f65
--- /dev/null
+++ b/libc/shared/math/cbrtbf16.h
@@ -0,0 +1,23 @@
+//===-- Shared cbrtbf16 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_CBRTBF16_H
+#define LLVM_LIBC_SHARED_MATH_CBRTBF16_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/cbrtbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::cbrtbf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_SHARED_MATH_CBRTBF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index fbef4b85f98a1..38256686a6015 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -682,6 +682,16 @@ add_header_library(
libc.src.__support.integer_literals
)
+add_header_library(
+ cbrtbf16
+ HDRS
+ cbrtbf16.h
+ DEPENDS
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.macros.config
+ libc.src.__support.math.cbrtf
+)
+
add_header_library(
cbrtf
HDRS
diff --git a/libc/src/__support/math/cbrtbf16.h b/libc/src/__support/math/cbrtbf16.h
new file mode 100644
index 0000000000000..75a08a32edfa1
--- /dev/null
+++ b/libc/src/__support/math/cbrtbf16.h
@@ -0,0 +1,26 @@
+//===-- Implementation header for cbrtbf16 ----------------------*- 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_CBRTBF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_CBRTBF16_H
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/math/cbrtf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE bfloat16 cbrtbf16(bfloat16 x) {
+ return fputil::cast<bfloat16>(math::cbrtf(static_cast<float>(x)));
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_CBRTBF16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 40d21ffaaa524..ae040faea3588 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4985,11 +4985,7 @@ add_entrypoint_object (
HDRS
../cbrtbf16.h
DEPENDS
- libc.src.__support.common
- libc.src.__support.FPUtil.bfloat16
- libc.src.math.generic.cbrt
- libc.src.__support.macros.config
- libc.src.__support.macros.properties.types
+ libc.src.__support.math.cbrtbf16
)
add_entrypoint_object(
diff --git a/libc/src/math/generic/cbrtbf16.cpp b/libc/src/math/generic/cbrtbf16.cpp
index 742cee8ae1429..db92aba17d172 100644
--- a/libc/src/math/generic/cbrtbf16.cpp
+++ b/libc/src/math/generic/cbrtbf16.cpp
@@ -7,12 +7,10 @@
//===----------------------------------------------------------------------===//
#include "src/math/cbrtbf16.h"
-#include "src/__support/FPUtil/bfloat16.h"
-#include "src/__support/macros/config.h"
-#include "src/__support/math/cbrtf.h"
+#include "src/__support/math/cbrtbf16.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(bfloat16, cbrtbf16, (bfloat16 x)) {
- return static_cast<bfloat16>(math::cbrtf(static_cast<float>(x)));
+ return math::cbrtbf16(x);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index dd5a9ea12a219..8584b21acd8b1 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -59,6 +59,7 @@ add_fp_unittest(
libc.src.__support.math.canonicalizef16
libc.src.__support.math.canonicalizel
libc.src.__support.math.cbrt
+ libc.src.__support.math.cbrtbf16
libc.src.__support.math.cbrtf
libc.src.__support.math.ceil
libc.src.__support.math.ceilbf16
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 71ea49ee32651..b714850fda9a9 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -450,6 +450,7 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) {
EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalizebf16(&canonicalizebf16_cx,
&canonicalizebf16_x));
EXPECT_FP_EQ(bfloat16(0.0), canonicalizebf16_cx);
+ EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::cbrtbf16(bfloat16(0.0)));
EXPECT_FP_EQ(bfloat16(5.0), LIBC_NAMESPACE::shared::bf16addf(2.0f, 3.0f));
EXPECT_FP_EQ(bfloat16(5.0), LIBC_NAMESPACE::shared::bf16addl(2L, 3L));
EXPECT_FP_EQ(bfloat16(10.0),
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 192e6f9eaa575..f23f422e9b679 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3488,6 +3488,16 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_cbrtbf16",
+ hdrs = ["src/__support/math/cbrtbf16.h"],
+ deps = [
+ ":__support_fputil_bfloat16",
+ ":__support_macros_config",
+ ":__support_math_cbrtf",
+ ],
+)
+
libc_support_library(
name = "__support_math_cbrtf",
hdrs = ["src/__support/math/cbrtf.h"],
@@ -6590,6 +6600,11 @@ libc_math_function(
additional_deps = [":__support_math_cbrt"],
)
+libc_math_function(
+ name = "cbrtbf16",
+ additional_deps = [":__support_math_cbrtbf16"],
+)
+
libc_math_function(
name = "cbrtf",
additional_deps = [":__support_math_cbrtf"],
``````````
</details>
https://github.com/llvm/llvm-project/pull/188204
More information about the libc-commits
mailing list