[libc-commits] [libc] [llvm] [libc][math] Refactor bf16addf to header only (PR #181230)
Viktor Moros via libc-commits
libc-commits at lists.llvm.org
Sat Feb 14 05:50:24 PST 2026
https://github.com/vmoros updated https://github.com/llvm/llvm-project/pull/181230
>From 9c60c390ffff865f057d972e223edb2b0b470f34 Mon Sep 17 00:00:00 2001
From: vgm <vgm at google.com>
Date: Thu, 12 Feb 2026 11:47:08 -0800
Subject: [PATCH 1/7] Initial commit for refactoring bf16addf to header-only
---
libc/shared/math.h | 1 +
libc/shared/math/bf16addf.h | 23 ++++++++++++++++
libc/src/__support/math/CMakeLists.txt | 11 ++++++++
libc/src/__support/math/bf16addf.h | 27 +++++++++++++++++++
libc/src/math/generic/CMakeLists.txt | 3 +--
libc/src/math/generic/bf16addf.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, 85 insertions(+), 5 deletions(-)
create mode 100644 libc/shared/math/bf16addf.h
create mode 100644 libc/src/__support/math/bf16addf.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index eab584aea4e60..6a44a69133ec0 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/bf16addf.h"
#include "math/cbrt.h"
#include "math/cbrtf.h"
#include "math/cos.h"
diff --git a/libc/shared/math/bf16addf.h b/libc/shared/math/bf16addf.h
new file mode 100644
index 0000000000000..d63de966272c4
--- /dev/null
+++ b/libc/shared/math/bf16addf.h
@@ -0,0 +1,23 @@
+//===-- Shared bf16addf 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_BF16ADDF_H
+#define LLVM_LIBC_SHARED_MATH_BF16ADDF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/bf16addf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::bf16addf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_BF16ADDF_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 4e6e9bd6233ec..4d3663640c179 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(
+ bf16addf
+ HDRS
+ bf16addf.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/bf16addf.h b/libc/src/__support/math/bf16addf.h
new file mode 100644
index 0000000000000..ba7528eb092e1
--- /dev/null
+++ b/libc/src/__support/math/bf16addf.h
@@ -0,0 +1,27 @@
+//===-- Implementation header for bf16addf -------------------*- 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_BF16ADDF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_BF16ADDF_H
+
+#include "src/__support/macros/properties/types.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 bf16addf(float x, float y) {
+ return fputil::generic::add<bfloat16>(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_BF16ADDF_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 98d79e3b3826d..3978f6b6dea9e 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5180,8 +5180,7 @@ add_entrypoint_object(
../bf16addf.h
DEPENDS
libc.src.__support.common
- libc.src.__support.FPUtil.bfloat16
- libc.src.__support.FPUtil.generic.add_sub
+ libc.src.__support.math.bf16addf
libc.src.__support.macros.config
libc.src.__support.macros.properties.types
)
diff --git a/libc/src/math/generic/bf16addf.cpp b/libc/src/math/generic/bf16addf.cpp
index 65e6cbf6b1507..1635eadae3962 100644
--- a/libc/src/math/generic/bf16addf.cpp
+++ b/libc/src/math/generic/bf16addf.cpp
@@ -7,15 +7,14 @@
//===----------------------------------------------------------------------===//
#include "src/math/bf16addf.h"
-#include "src/__support/FPUtil/bfloat16.h"
-#include "src/__support/FPUtil/generic/add_sub.h"
+#include "src/__support/math/bf16addf.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(bfloat16, bf16addf, (float x, float y)) {
- return fputil::generic::add<bfloat16>(x, y);
+ return math::bf16addf(x, y);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index ef4d78584ef77..b4f9826251672 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.bf16addf
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..718ef8b247025 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -72,6 +72,9 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
EXPECT_FP_EQ(0x1.921fb6p+0f16, LIBC_NAMESPACE::shared::acosf16(0.0f16));
EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::f16sqrtl(1.0L));
EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::shared::sinf16(0.0f16));
+
+ EXPECT_FP_EQ(bfloat16{5.0},
+ LIBC_NAMESPACE::shared::bf16addf(2.0f, 3.0f));
}
#endif // LIBC_TYPES_HAS_FLOAT16
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index e08d759f063b0..4b6f65876d8db 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2635,6 +2635,17 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_bf16addf",
+ hdrs = ["src/__support/math/bf16addf.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"],
@@ -4253,6 +4264,11 @@ libc_math_function(
additional_deps = [":__support_math_atanhf16"],
)
+libc_math_function(
+ name = "bf16addf",
+ additional_deps = [":__support_math_bf16addf"],
+)
+
libc_math_function(name = "canonicalize")
libc_math_function(name = "canonicalizef")
>From cc9793a28d2641aad7eb3381caab9613d76dc2e9 Mon Sep 17 00:00:00 2001
From: vgm <vgm at google.com>
Date: Thu, 12 Feb 2026 11:48:10 -0800
Subject: [PATCH 2/7] Formatted code
---
libc/src/__support/math/bf16addf.h | 2 +-
libc/src/math/generic/bf16addf.cpp | 2 +-
libc/test/shared/shared_math_test.cpp | 3 +--
3 files changed, 3 insertions(+), 4 deletions(-)
diff --git a/libc/src/__support/math/bf16addf.h b/libc/src/__support/math/bf16addf.h
index ba7528eb092e1..0172c0644b554 100644
--- a/libc/src/__support/math/bf16addf.h
+++ b/libc/src/__support/math/bf16addf.h
@@ -9,10 +9,10 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_BF16ADDF_H
#define LLVM_LIBC_SRC___SUPPORT_MATH_BF16ADDF_H
-#include "src/__support/macros/properties/types.h"
#include "src/__support/FPUtil/bfloat16.h"
#include "src/__support/FPUtil/generic/add_sub.h"
#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
namespace LIBC_NAMESPACE_DECL {
namespace math {
diff --git a/libc/src/math/generic/bf16addf.cpp b/libc/src/math/generic/bf16addf.cpp
index 1635eadae3962..7518fe501236e 100644
--- a/libc/src/math/generic/bf16addf.cpp
+++ b/libc/src/math/generic/bf16addf.cpp
@@ -7,9 +7,9 @@
//===----------------------------------------------------------------------===//
#include "src/math/bf16addf.h"
-#include "src/__support/math/bf16addf.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
+#include "src/__support/math/bf16addf.h"
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 718ef8b247025..fb6cd9086da49 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -73,8 +73,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::f16sqrtl(1.0L));
EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::shared::sinf16(0.0f16));
- EXPECT_FP_EQ(bfloat16{5.0},
- LIBC_NAMESPACE::shared::bf16addf(2.0f, 3.0f));
+ EXPECT_FP_EQ(bfloat16{5.0}, LIBC_NAMESPACE::shared::bf16addf(2.0f, 3.0f));
}
#endif // LIBC_TYPES_HAS_FLOAT16
>From bb821ba0444838c7a724f1a6953503d651bd140d Mon Sep 17 00:00:00 2001
From: vgm <vgm at google.com>
Date: Thu, 12 Feb 2026 12:23:24 -0800
Subject: [PATCH 3/7] Clean up dependencies
---
libc/src/__support/math/CMakeLists.txt | 1 -
libc/src/math/generic/CMakeLists.txt | 3 ---
libc/src/math/generic/bf16addf.cpp | 2 --
utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 1 -
4 files changed, 7 deletions(-)
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 4d3663640c179..ae4bd36c69c01 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 3978f6b6dea9e..400e7a714dbc4 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5179,10 +5179,7 @@ add_entrypoint_object(
HDRS
../bf16addf.h
DEPENDS
- libc.src.__support.common
libc.src.__support.math.bf16addf
- libc.src.__support.macros.config
- libc.src.__support.macros.properties.types
)
add_entrypoint_object(
diff --git a/libc/src/math/generic/bf16addf.cpp b/libc/src/math/generic/bf16addf.cpp
index 7518fe501236e..5447d947fd1a2 100644
--- a/libc/src/math/generic/bf16addf.cpp
+++ b/libc/src/math/generic/bf16addf.cpp
@@ -7,8 +7,6 @@
//===----------------------------------------------------------------------===//
#include "src/math/bf16addf.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
#include "src/__support/math/bf16addf.h"
namespace LIBC_NAMESPACE_DECL {
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 4b6f65876d8db..4d5f33e4af72a 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2642,7 +2642,6 @@ libc_support_library(
":__support_fputil_basic_operations",
":__support_fputil_bfloat16",
":__support_macros_config",
- ":__support_macros_properties_types",
],
)
>From bff950288f5153d6b550eca00695c077e5b98c64 Mon Sep 17 00:00:00 2001
From: Viktor Moros <vmoros at users.noreply.github.com>
Date: Fri, 13 Feb 2026 09:43:10 -0500
Subject: [PATCH 4/7] Use parens instead of curly brackets for bloat16
initialization
Co-authored-by: Muhammad Bassiouni <60100307+bassiounix at users.noreply.github.com>
---
libc/test/shared/shared_math_test.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index fb6cd9086da49..36104a3481aca 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -73,7 +73,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::f16sqrtl(1.0L));
EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::shared::sinf16(0.0f16));
- EXPECT_FP_EQ(bfloat16{5.0}, LIBC_NAMESPACE::shared::bf16addf(2.0f, 3.0f));
+ EXPECT_FP_EQ(bfloat16(5.0), LIBC_NAMESPACE::shared::bf16addf(2.0f, 3.0f));
}
#endif // LIBC_TYPES_HAS_FLOAT16
>From d9f0a60f3880b3010cb898f762b52ff052429a80 Mon Sep 17 00:00:00 2001
From: Viktor Moros <vmoros at users.noreply.github.com>
Date: Fri, 13 Feb 2026 09:43:54 -0500
Subject: [PATCH 5/7] Remove unused dep from libc/src/__support/math/bf16addf.h
Co-authored-by: Muhammad Bassiouni <60100307+bassiounix at users.noreply.github.com>
---
libc/src/__support/math/bf16addf.h | 1 -
1 file changed, 1 deletion(-)
diff --git a/libc/src/__support/math/bf16addf.h b/libc/src/__support/math/bf16addf.h
index 0172c0644b554..755746698ef2b 100644
--- a/libc/src/__support/math/bf16addf.h
+++ b/libc/src/__support/math/bf16addf.h
@@ -12,7 +12,6 @@
#include "src/__support/FPUtil/bfloat16.h"
#include "src/__support/FPUtil/generic/add_sub.h"
#include "src/__support/macros/config.h"
-#include "src/__support/macros/properties/types.h"
namespace LIBC_NAMESPACE_DECL {
namespace math {
>From 0df991b3c9d06c8ed7331ee6f14d3a65aac68f54 Mon Sep 17 00:00:00 2001
From: Viktor Moros <vmoros at users.noreply.github.com>
Date: Fri, 13 Feb 2026 09:44:11 -0500
Subject: [PATCH 6/7] Fix header comment in libc/shared/math/bf16addf.h
Co-authored-by: Muhammad Bassiouni <60100307+bassiounix at users.noreply.github.com>
---
libc/shared/math/bf16addf.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/shared/math/bf16addf.h b/libc/shared/math/bf16addf.h
index d63de966272c4..f952e83f01f5a 100644
--- a/libc/shared/math/bf16addf.h
+++ b/libc/shared/math/bf16addf.h
@@ -1,4 +1,4 @@
-//===-- Shared bf16addf function ------------------------------*- C++ -*-===//
+//===-- Shared bf16addf 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.
>From 55d6986c9c454150d3b3f4e158fd0f2f1e43b098 Mon Sep 17 00:00:00 2001
From: Viktor Moros <vmoros at users.noreply.github.com>
Date: Fri, 13 Feb 2026 09:47:15 -0500
Subject: [PATCH 7/7] Fix header comment in libc/src/__support/math/bf16addf.h
---
libc/src/__support/math/bf16addf.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/src/__support/math/bf16addf.h b/libc/src/__support/math/bf16addf.h
index 755746698ef2b..28a836030d127 100644
--- a/libc/src/__support/math/bf16addf.h
+++ b/libc/src/__support/math/bf16addf.h
@@ -1,4 +1,4 @@
-//===-- Implementation header for bf16addf -------------------*- C++ -*-===//
+//===-- Implementation header for bf16addf ----------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
More information about the libc-commits
mailing list