[libc-commits] [libc] [llvm] [libc][math] Refactor bf16add to header only (PR #181392)

via libc-commits libc-commits at lists.llvm.org
Fri Feb 13 09:55:35 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Atharv Mane (Atharva062006)

<details>
<summary>Changes</summary>

Resolves #<!-- -->181016
Part of #<!-- -->147386

---
Full diff: https://github.com/llvm/llvm-project/pull/181392.diff


9 Files Affected:

- (modified) libc/shared/math.h (+1) 
- (added) libc/shared/math/bf16add.h (+24) 
- (modified) libc/src/__support/math/CMakeLists.txt (+10) 
- (added) libc/src/__support/math/bf16add.h (+26) 
- (modified) libc/src/math/generic/CMakeLists.txt (+1-5) 
- (modified) libc/src/math/generic/bf16add.cpp (+3-6) 
- (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 eab584aea4e60..3530d563f0af9 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -30,6 +30,7 @@
 #include "math/atanf16.h"
 #include "math/atanhf.h"
 #include "math/atanhf16.h"
+#include "math/bf16add.h"
 #include "math/cbrt.h"
 #include "math/cbrtf.h"
 #include "math/cos.h"
diff --git a/libc/shared/math/bf16add.h b/libc/shared/math/bf16add.h
new file mode 100644
index 0000000000000..02a60765e3c95
--- /dev/null
+++ b/libc/shared/math/bf16add.h
@@ -0,0 +1,24 @@
+//===-- Shared bf16add 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_BF16ADD_H
+#define LLVM_LIBC_SHARED_MATH_BF16ADD_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/bf16add.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::bf16add;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_BF16ADD_H
+	
\ No newline at end of file
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 4e6e9bd6233ec..48ac3a5f538f3 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -332,6 +332,16 @@ add_header_library(
     libc.src.__support.macros.optimization
 )
 
+add_header_library(
+  bf16add
+  HDRS
+    bf16add.h
+  DEPENDS
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.generic.add_sub
+    libc.src.__support.macros.config
+)
+
 add_header_library(
   cbrt
   HDRS
diff --git a/libc/src/__support/math/bf16add.h b/libc/src/__support/math/bf16add.h
new file mode 100644
index 0000000000000..7d7a24d9bc134
--- /dev/null
+++ b/libc/src/__support/math/bf16add.h
@@ -0,0 +1,26 @@
+//===-- Implementation header for bf16add -----------------------*- 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_BF16ADD_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_BF16ADD_H
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/FPUtil/generic/add_sub.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE bfloat16 bf16add(double x, double y) {
+  return fputil::generic::add<bfloat16>(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_BF16ADD_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 98d79e3b3826d..4ce3de6c1f0da 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5165,11 +5165,7 @@ add_entrypoint_object(
   HDRS
     ../bf16add.h
   DEPENDS
-    libc.src.__support.common
-    libc.src.__support.FPUtil.bfloat16
-    libc.src.__support.FPUtil.generic.add_sub
-    libc.src.__support.macros.config
-    libc.src.__support.macros.properties.types
+    libc.src.__support.math.bf16add
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/bf16add.cpp b/libc/src/math/generic/bf16add.cpp
index 257596afe66cf..df262a8365f1a 100644
--- a/libc/src/math/generic/bf16add.cpp
+++ b/libc/src/math/generic/bf16add.cpp
@@ -7,15 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/bf16add.h"
-#include "src/__support/FPUtil/bfloat16.h"
-#include "src/__support/FPUtil/generic/add_sub.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/bf16add.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(bfloat16, bf16add, (double x, double y)) {
-  return fputil::generic::add<bfloat16>(x, y);
-}
+  return math::bf16add(x, y);
+}	
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index ef4d78584ef77..c1e7dba89dd5e 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -26,6 +26,7 @@ add_fp_unittest(
     libc.src.__support.math.atanf16
     libc.src.__support.math.atanhf
     libc.src.__support.math.atanhf16
+    libc.src.__support.math.bf16add
     libc.src.__support.math.cbrt
     libc.src.__support.math.cbrtf
     libc.src.__support.math.cos
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 1c8463d9321fc..4abff0c4db4fe 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -133,6 +133,7 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::asin(0.0));
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::atan(0.0));
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::atan2(0.0, 0.0));
+  EXPECT_FP_EQ(bfloat16(5.0), LIBC_NAMESPACE::shared::bf16add(2.0, 3.0));
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::cbrt(0.0));
   EXPECT_FP_EQ(0x1p+0, LIBC_NAMESPACE::shared::cos(0.0));
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::dsqrtl(0.0));
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index e08d759f063b0..d68d1621222f7 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2635,6 +2635,16 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_bf16addf128",
+    hdrs = ["src/__support/math/bf16addf128.h"],
+    deps = [    
+        ":__support_fputil_basic_operations",
+        ":__support_fputil_bfloat16",
+        ":__support_macros_config",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_cbrt",
     hdrs = ["src/__support/math/cbrt.h"],
@@ -4253,6 +4263,11 @@ libc_math_function(
     additional_deps = [":__support_math_atanhf16"],
 )
 
+libc_math_function(
+    name = "bf16add",
+    additional_deps = [":__support_math_bf16add"],
+)
+
 libc_math_function(name = "canonicalize")
 
 libc_math_function(name = "canonicalizef")

``````````

</details>


https://github.com/llvm/llvm-project/pull/181392


More information about the libc-commits mailing list