[libc-commits] [libc] [llvm] [libc][math] Refactor bf16addl implementation to header-only in src/__support/math folder. (PR #182561)
via libc-commits
libc-commits at lists.llvm.org
Fri Feb 20 10:23:28 PST 2026
https://github.com/Armillus created https://github.com/llvm/llvm-project/pull/182561
Resolves https://github.com/llvm/llvm-project/issues/181019
Part of https://github.com/llvm/llvm-project/issues/147386
>From 5e959a09f504f1ba1addd2304fcada13a8462f30 Mon Sep 17 00:00:00 2001
From: Armillus <millotaxel71 at gmail.com>
Date: Fri, 20 Feb 2026 19:15:11 +0100
Subject: [PATCH] [libc][math] Refactor bf16addl implementation to header-only
in src/__support/math folder.
---
libc/shared/math.h | 1 +
libc/shared/math/bf16addl.h | 23 ++++++++++++++++
libc/src/__support/math/CMakeLists.txt | 12 ++++++++-
libc/src/__support/math/bf16addl.h | 26 +++++++++++++++++++
libc/src/math/generic/CMakeLists.txt | 6 +----
libc/src/math/generic/bf16addl.cpp | 7 ++---
libc/test/shared/CMakeLists.txt | 3 ++-
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 15 +++++++++++
9 files changed, 82 insertions(+), 12 deletions(-)
create mode 100644 libc/shared/math/bf16addl.h
create mode 100644 libc/src/__support/math/bf16addl.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 8737fc69d4d8d..e001dbfc1305d 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -32,6 +32,7 @@
#include "math/atanhf16.h"
#include "math/bf16add.h"
#include "math/bf16addf.h"
+#include "math/bf16addl.h"
#include "math/bf16addf128.h"
#include "math/bf16divf.h"
#include "math/bf16divl.h"
diff --git a/libc/shared/math/bf16addl.h b/libc/shared/math/bf16addl.h
new file mode 100644
index 0000000000000..66dccde75df38
--- /dev/null
+++ b/libc/shared/math/bf16addl.h
@@ -0,0 +1,23 @@
+//===-- Shared bf16addl 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_BF16ADDL_H
+#define LLVM_LIBC_SHARED_MATH_BF16ADDL_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/bf16addl.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::bf16addl;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_BF16ADDL_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 696993c535f68..3373621b0e553 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -352,6 +352,16 @@ add_header_library(
libc.src.__support.macros.config
)
+add_header_library(
+ bf16addl
+ HDRS
+ bf16addl.h
+ DEPENDS
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.generic.add_sub
+ libc.src.__support.macros.config
+)
+
add_header_library(
bf16addf128
HDRS
@@ -809,7 +819,7 @@ add_header_library(
)
add_header_library(
- f16addl
+ f16addl
HDRS
f16addl.h
DEPENDS
diff --git a/libc/src/__support/math/bf16addl.h b/libc/src/__support/math/bf16addl.h
new file mode 100644
index 0000000000000..f9e27cb48ea09
--- /dev/null
+++ b/libc/src/__support/math/bf16addl.h
@@ -0,0 +1,26 @@
+//===-- Implementation header for bf16addl ----------------------*- 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_BF16ADDL_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_BF16ADDL_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 bf16addl(long double x, long double y) {
+ return fputil::generic::add<bfloat16>(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_BF16ADDL_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 29105974f5af2..f70385c232f13 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5092,11 +5092,7 @@ add_entrypoint_object(
HDRS
../bf16addl.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.bf16addl
)
add_entrypoint_object(
diff --git a/libc/src/math/generic/bf16addl.cpp b/libc/src/math/generic/bf16addl.cpp
index c212195c2b7c8..1ddd614276ef7 100644
--- a/libc/src/math/generic/bf16addl.cpp
+++ b/libc/src/math/generic/bf16addl.cpp
@@ -7,15 +7,12 @@
//===----------------------------------------------------------------------===//
#include "src/math/bf16addl.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/bf16addl.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(bfloat16, bf16addl, (long double x, long double y)) {
- return fputil::generic::add<bfloat16>(x, y);
+ return math::bf16addl(x, y);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 14ab2eb3bc3d8..d14655a3a6099 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -28,6 +28,7 @@ add_fp_unittest(
libc.src.__support.math.atanhf16
libc.src.__support.math.bf16add
libc.src.__support.math.bf16addf
+ libc.src.__support.math.bf16addl
libc.src.__support.math.bf16addf128
libc.src.__support.math.bf16divf
libc.src.__support.math.bf16divl
@@ -132,7 +133,7 @@ add_fp_unittest(
libc.src.__support.math.llogbf128
libc.src.__support.math.llogbf16
libc.src.__support.math.logbl
- libc.src.__support.math.logf16
+ libc.src.__support.math.logf16
libc.src.__support.math.llogbl
libc.src.__support.math.pow
libc.src.__support.math.powf
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 8d736f618e5a9..62845bc625ca2 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -274,6 +274,7 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) {
&canonicalizebf16_x));
EXPECT_FP_EQ(bfloat16(0.0), canonicalizebf16_cx);
EXPECT_FP_EQ(bfloat16(5.0), LIBC_NAMESPACE::shared::bf16addf(2.0f, 3.0f));
+ EXPECT_FP_EQ(bfloat16(5.0), LIBC_NAMESPACE::shared::bf16addl(2L, 3L));
EXPECT_FP_EQ(bfloat16(10.0),
LIBC_NAMESPACE::shared::bf16fmaf(2.0f, 3.0f, 4.0f));
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 38940c4113929..c7b7ffe06d8de 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2655,6 +2655,16 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_bf16addl",
+ hdrs = ["src/__support/math/bf16addl.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"],
@@ -4726,6 +4736,11 @@ libc_math_function(
additional_deps = [":__support_math_bf16addf"],
)
+libc_math_function(
+ name = "bf16addl",
+ additional_deps = [":__support_math_bf16addl"],
+)
+
libc_math_function(
name = "bf16addf128",
additional_deps = [":__support_math_bf16addf128"],
More information about the libc-commits
mailing list