[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 03:06:09 PDT 2026


https://github.com/Sukumarsawant updated https://github.com/llvm/llvm-project/pull/188204

>From 86b8e5ca39f3a9de31efd3eaccba3d56b78d1445 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Tue, 24 Mar 2026 15:07:21 +0530
Subject: [PATCH 1/3] refactor: cbrtbf16

---
 libc/shared/math.h                            |  1 +
 libc/shared/math/cbrtbf16.h                   | 23 ++++++++++++++++
 libc/src/__support/math/CMakeLists.txt        | 10 +++++++
 libc/src/__support/math/cbrtbf16.h            | 26 +++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  6 +----
 libc/src/math/generic/cbrtbf16.cpp            |  6 ++---
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  1 +
 .../llvm-project-overlay/libc/BUILD.bazel     | 15 +++++++++++
 9 files changed, 80 insertions(+), 9 deletions(-)
 create mode 100644 libc/shared/math/cbrtbf16.h
 create mode 100644 libc/src/__support/math/cbrtbf16.h

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..a4926631faacf
--- /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 LIBC_SHARED_MATH_CBRTBF16_H
+#define 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"],

>From 17722a6c3da1aed487fad8825bfca561fa24239a Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Tue, 24 Mar 2026 15:17:08 +0530
Subject: [PATCH 2/3] nit

---
 libc/shared/math/cbrtbf16.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libc/shared/math/cbrtbf16.h b/libc/shared/math/cbrtbf16.h
index a4926631faacf..9b10b26e46f65 100644
--- a/libc/shared/math/cbrtbf16.h
+++ b/libc/shared/math/cbrtbf16.h
@@ -6,8 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef LIBC_SHARED_MATH_CBRTBF16_H
-#define LIBC_SHARED_MATH_CBRTBF16_H
+#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"

>From 82f0c893407353b465c7bfd6a7a717877e93172f Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Tue, 24 Mar 2026 15:35:44 +0530
Subject: [PATCH 3/3] nit

---
 libc/shared/math/cbrtbf16.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/shared/math/cbrtbf16.h b/libc/shared/math/cbrtbf16.h
index 9b10b26e46f65..70113ca6e6344 100644
--- a/libc/shared/math/cbrtbf16.h
+++ b/libc/shared/math/cbrtbf16.h
@@ -20,4 +20,4 @@ using math::cbrtbf16;
 } // namespace shared
 } // namespace LIBC_NAMESPACE_DECL
 
-#endif // LIBC_SHARED_MATH_CBRTBF16_H
+#endif // LLVM_LIBC_SHARED_MATH_CBRTBF16_H



More information about the libc-commits mailing list