[libc-commits] [libc] [llvm] Refactor bf16addf128 to header only (PR #181058)

via libc-commits libc-commits at lists.llvm.org
Fri Feb 13 02:54:47 PST 2026


https://github.com/abhijeetsharma200 updated https://github.com/llvm/llvm-project/pull/181058

>From c58ccfd4a1ab7d79921525540a2c73b68cc12525 Mon Sep 17 00:00:00 2001
From: Abhijeet Sharma <abhijeetsharma2002 at gmail.com>
Date: Thu, 12 Feb 2026 01:21:30 +0100
Subject: [PATCH 1/4] Refactor bf16addf128 to headers

---
 libc/shared/math.h                            |  1 +
 libc/shared/math/bf16addf128.h                | 29 +++++++++++++++++
 libc/src/__support/math/CMakeLists.txt        | 11 +++++++
 libc/src/__support/math/bf16addf128.h         | 32 +++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  4 +--
 libc/src/math/generic/bf16addf128.cpp         |  5 ++-
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  3 ++
 .../llvm-project-overlay/libc/BUILD.bazel     | 16 ++++++++++
 9 files changed, 96 insertions(+), 6 deletions(-)
 create mode 100644 libc/shared/math/bf16addf128.h
 create mode 100644 libc/src/__support/math/bf16addf128.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 4b0f2c67b0c86..7020b4ecfa891 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/bf16addf128.h"
 #include "math/cbrt.h"
 #include "math/cbrtf.h"
 #include "math/cos.h"
diff --git a/libc/shared/math/bf16addf128.h b/libc/shared/math/bf16addf128.h
new file mode 100644
index 0000000000000..2740899d3ece1
--- /dev/null
+++ b/libc/shared/math/bf16addf128.h
@@ -0,0 +1,29 @@
+//===-- Shared bf16addf128 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_BF16ADDF128_H
+#define LLVM_LIBC_SHARED_MATH_BF16ADDF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "shared/libc_common.h"
+#include "src/__support/math/bf16addf128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::bf16addf128;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_MATH_BF16ADDF128_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 6705a2a08f3a7..38acd7442b1fe 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -332,6 +332,17 @@ add_header_library(
     libc.src.__support.macros.optimization
 )
 
+add_header_library(
+  bf16addf128
+  HDRS
+    bf16addf128.h
+  DEPENDS
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.generic.add_sub
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
 add_header_library(
   cbrt
   HDRS
diff --git a/libc/src/__support/math/bf16addf128.h b/libc/src/__support/math/bf16addf128.h
new file mode 100644
index 0000000000000..2bd1d10e38d7d
--- /dev/null
+++ b/libc/src/__support/math/bf16addf128.h
@@ -0,0 +1,32 @@
+//===-- Implementation header for bf16addf128 -------------------*- 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_BF16ADDF128_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_BF16ADDF128_H
+
+#include "src/__support/macros/properties/types.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#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 bf16addf128(float128 x, float128 y) {
+  return fputil::generic::add<bfloat16>(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_BF16ADDF128_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 1a3588a71219b..094aa724c7616 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5209,10 +5209,8 @@ add_entrypoint_object(
     ../bf16addf128.h
   DEPENDS
     libc.src.__support.common
-    libc.src.__support.FPUtil.bfloat16
-    libc.src.__support.FPUtil.generic.add_sub
+    libc.src.__support.math.bf16addf128
     libc.src.__support.macros.config
-    libc.src.__support.macros.properties.types
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/bf16addf128.cpp b/libc/src/math/generic/bf16addf128.cpp
index 03f70af82e892..e02ae2a55f60f 100644
--- a/libc/src/math/generic/bf16addf128.cpp
+++ b/libc/src/math/generic/bf16addf128.cpp
@@ -7,15 +7,14 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/bf16addf128.h"
-#include "src/__support/FPUtil/bfloat16.h"
-#include "src/__support/FPUtil/generic/add_sub.h"
+#include "src/__support/math/bf16addf128.h"
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(bfloat16, bf16addf128, (float128 x, float128 y)) {
-  return fputil::generic::add<bfloat16>(x, y);
+  return math::bf16addf128(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index ec117e317d9bf..8bf44c30abd4c 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.bf16addf128
     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 b3a7dff4b6c4f..c63fa2f6c647d 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -184,6 +184,9 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
                LIBC_NAMESPACE::shared::sqrtf128(float128(1.0)));
 
   EXPECT_EQ(0L, LIBC_NAMESPACE::shared::llogbf128(float128(1.0)));
+
+  EXPECT_FP_EQ(bfloat16(5.0),
+               LIBC_NAMESPACE::shared::bf16addf128(float128(2.0), float128(3.0)));
 }
 
 #endif // LIBC_TYPES_HAS_FLOAT128
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 1c70361929887..c605da7c9d2b4 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2624,6 +2624,17 @@ 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",
+        ":__support_macros_properties_types",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_cbrt",
     hdrs = ["src/__support/math/cbrt.h"],
@@ -4242,6 +4253,11 @@ libc_math_function(
     additional_deps = [":__support_math_atanhf16"],
 )
 
+libc_math_function(
+    name = "bf16addf128",
+    additional_deps = [":__support_math_bf16addf128"],
+)
+
 libc_math_function(name = "canonicalize")
 
 libc_math_function(name = "canonicalizef")

>From af97cff13886726564feabe499803b1bf6c0e245 Mon Sep 17 00:00:00 2001
From: Abhijeet Sharma <abhijeetsharma2002 at gmail.com>
Date: Thu, 12 Feb 2026 20:46:49 +0100
Subject: [PATCH 2/4] Clang format fix and fixed based on code review

---
 libc/shared/math/bf16addf128.h                    | 2 +-
 libc/src/__support/math/CMakeLists.txt            | 1 -
 libc/src/math/generic/CMakeLists.txt              | 2 --
 libc/src/math/generic/bf16addf128.cpp             | 2 --
 libc/test/shared/shared_math_test.cpp             | 4 ++--
 utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 1 -
 6 files changed, 3 insertions(+), 9 deletions(-)

diff --git a/libc/shared/math/bf16addf128.h b/libc/shared/math/bf16addf128.h
index 2740899d3ece1..98b985de7a999 100644
--- a/libc/shared/math/bf16addf128.h
+++ b/libc/shared/math/bf16addf128.h
@@ -1,4 +1,4 @@
-//===-- Shared bf16addf128 function ------------------------------*- C++ -*-===//
+//===-- Shared bf16addf128 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.
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 38acd7442b1fe..26f0834890584 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -340,7 +340,6 @@ add_header_library(
     libc.src.__support.FPUtil.bfloat16
     libc.src.__support.FPUtil.generic.add_sub
     libc.src.__support.macros.config
-    libc.src.__support.macros.properties.types
 )
 
 add_header_library(
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 094aa724c7616..ddfeed239841f 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5208,9 +5208,7 @@ add_entrypoint_object(
   HDRS
     ../bf16addf128.h
   DEPENDS
-    libc.src.__support.common
     libc.src.__support.math.bf16addf128
-    libc.src.__support.macros.config
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/bf16addf128.cpp b/libc/src/math/generic/bf16addf128.cpp
index e02ae2a55f60f..cc76e0468f027 100644
--- a/libc/src/math/generic/bf16addf128.cpp
+++ b/libc/src/math/generic/bf16addf128.cpp
@@ -8,8 +8,6 @@
 
 #include "src/math/bf16addf128.h"
 #include "src/__support/math/bf16addf128.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index c63fa2f6c647d..d763c973fc04f 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -185,8 +185,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
 
   EXPECT_EQ(0L, LIBC_NAMESPACE::shared::llogbf128(float128(1.0)));
 
-  EXPECT_FP_EQ(bfloat16(5.0),
-               LIBC_NAMESPACE::shared::bf16addf128(float128(2.0), float128(3.0)));
+  EXPECT_FP_EQ(bfloat16(5.0), LIBC_NAMESPACE::shared::bf16addf128(
+                                  float128(2.0), float128(3.0)));
 }
 
 #endif // LIBC_TYPES_HAS_FLOAT128
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index c605da7c9d2b4..cfc7706ac6a67 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2631,7 +2631,6 @@ libc_support_library(
         ":__support_fputil_basic_operations",
         ":__support_fputil_bfloat16",
         ":__support_macros_config",
-        ":__support_macros_properties_types",
     ],
 )
 

>From ed8457e4c8e234ebf2599dc8a17e9e68db809fbc Mon Sep 17 00:00:00 2001
From: Abhijeet Sharma <abhijeetsharma2002 at gmail.com>
Date: Thu, 12 Feb 2026 21:00:10 +0100
Subject: [PATCH 3/4] Bazel dependency reorder to match cmake

---
 utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index cfc7706ac6a67..da501a03a6af8 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2628,8 +2628,8 @@ libc_support_library(
     name = "__support_math_bf16addf128",
     hdrs = ["src/__support/math/bf16addf128.h"],
     deps = [
-        ":__support_fputil_basic_operations",
         ":__support_fputil_bfloat16",
+        ":__support_fputil_basic_operations",
         ":__support_macros_config",
     ],
 )

>From e8abdafb77b5f88df167f8efaacea4c488978abe Mon Sep 17 00:00:00 2001
From: Abhijeet Sharma <abhijeetsharma2002 at gmail.com>
Date: Fri, 13 Feb 2026 11:54:30 +0100
Subject: [PATCH 4/4] Bazel reformat

---
 utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index da501a03a6af8..cfc7706ac6a67 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2628,8 +2628,8 @@ libc_support_library(
     name = "__support_math_bf16addf128",
     hdrs = ["src/__support/math/bf16addf128.h"],
     deps = [
-        ":__support_fputil_bfloat16",
         ":__support_fputil_basic_operations",
+        ":__support_fputil_bfloat16",
         ":__support_macros_config",
     ],
 )



More information about the libc-commits mailing list