[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 23:57:12 PST 2026
https://github.com/Atharva062006 updated https://github.com/llvm/llvm-project/pull/181392
>From 736d066c88db20e9dad96a8290f9f59bc6eb2555 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 1/2] 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 06cf83d0f3334..3ef31ecc01611 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/bf16addf128.h"
#include "math/cbrt.h"
#include "math/cbrtf.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 fe20faee176fb..ba3d8ac90cdba 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(
bf16addf128
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 b0a2e5a2a381f..83743f38ec958 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5154,11 +5154,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 6d04405e75163..53f2a91a4d484 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.bf16addf128
libc.src.__support.math.cbrt
libc.src.__support.math.cbrtf
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 4ed86b7b862b4..4c66192b2c014 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -135,6 +135,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 7f537270ddb3c..ee7b0a42c99ca 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_bf16addf128",
hdrs = ["src/__support/math/bf16addf128.h"],
@@ -4305,8 +4315,13 @@ libc_math_function(
)
libc_math_function(
+<<<<<<< HEAD
name = "bf16addf128",
additional_deps = [":__support_math_bf16addf128"],
+=======
+ name = "bf16add",
+ additional_deps = [":__support_math_bf16add"],
+>>>>>>> 18f1f1467e4e (initial commit)
)
libc_math_function(name = "canonicalize")
>From 2e0611117c87b9c170879a7459b9fcf75e3ec3ef Mon Sep 17 00:00:00 2001
From: Atharva062006 <72563240+Atharva062006 at users.noreply.github.com>
Date: Sat, 14 Feb 2026 13:26:38 +0530
Subject: [PATCH 2/2] resolved conflicts
---
utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index ee7b0a42c99ca..3cbb1ba2ac5af 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -4315,13 +4315,13 @@ libc_math_function(
)
libc_math_function(
-<<<<<<< HEAD
- name = "bf16addf128",
- additional_deps = [":__support_math_bf16addf128"],
-=======
name = "bf16add",
additional_deps = [":__support_math_bf16add"],
->>>>>>> 18f1f1467e4e (initial commit)
+)
+
+libc_math_function(
+ name = "bf16addf128",
+ additional_deps = [":__support_math_bf16addf128"],
)
libc_math_function(name = "canonicalize")
More information about the libc-commits
mailing list