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

Atharv Mane via libc-commits libc-commits at lists.llvm.org
Fri Feb 13 10:16:25 PST 2026


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

>From 18f1f1467e4ec56ccf58b348a5ad3de5262d8163 Mon Sep 17 00:00:00 2001
From: Atharva062006 <72563240+Atharva062006 at users.noreply.github.com>
Date: Fri, 13 Feb 2026 23:11:55 +0530
Subject: [PATCH] initial commit

---
 libc/shared/math.h                            |  1 +
 libc/shared/math/bf16add.h                    | 23 ++++++++++++++++
 libc/src/__support/math/CMakeLists.txt        | 10 +++++++
 libc/src/__support/math/bf16add.h             | 26 +++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  6 +----
 libc/src/math/generic/bf16add.cpp             |  7 ++---
 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(+), 10 deletions(-)
 create mode 100644 libc/shared/math/bf16add.h
 create mode 100644 libc/src/__support/math/bf16add.h

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..724ab99479b20
--- /dev/null
+++ b/libc/shared/math/bf16add.h
@@ -0,0 +1,23 @@
+//===-- 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
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..7e4ff3f54f32d 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..2c9400e53ec1b 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_bf16add",
+    hdrs = ["src/__support/math/bf16add.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")



More information about the libc-commits mailing list