[libc] [llvm] [libc][math] change bf16fmal to be header-only and constexpr-compat (PR #181666)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Feb 18 07:13:46 PST 2026
https://github.com/Serosh-commits updated https://github.com/llvm/llvm-project/pull/181666
>From 39e24180daedec97c6f109099d0894f7563b23cd Mon Sep 17 00:00:00 2001
From: Serosh-commits <janmejayapanda400 at gmail.com>
Date: Mon, 16 Feb 2026 19:13:42 +0530
Subject: [PATCH 1/2] addressed reviewer feedback
---
libc/shared/math.h | 1 +
libc/shared/math/bf16fmal.h | 25 +++++++++++++++++
libc/src/__support/math/CMakeLists.txt | 10 +++++++
libc/src/__support/math/bf16fmal.h | 26 +++++++++++++++++
libc/src/math/generic/CMakeLists.txt | 6 +---
libc/src/math/generic/bf16fmal.cpp | 9 ++----
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 2 ++
.../llvm-project-overlay/libc/BUILD.bazel | 28 +++++++++++++++----
9 files changed, 91 insertions(+), 17 deletions(-)
create mode 100644 libc/shared/math/bf16fmal.h
create mode 100644 libc/src/__support/math/bf16fmal.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 828980a3500df..89b9f78bec2b2 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -35,6 +35,7 @@
#include "math/bf16addf128.h"
#include "math/bf16divf.h"
#include "math/bf16fmaf.h"
+#include "math/bf16fmal.h"
#include "math/canonicalize.h"
#include "math/canonicalizebf16.h"
#include "math/canonicalizef.h"
diff --git a/libc/shared/math/bf16fmal.h b/libc/shared/math/bf16fmal.h
new file mode 100644
index 0000000000000..24aacc53c72a8
--- /dev/null
+++ b/libc/shared/math/bf16fmal.h
@@ -0,0 +1,25 @@
+//===-- Shared bf16fmal 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_BF16FMAL_H
+#define LLVM_LIBC_SHARED_MATH_BF16FMAL_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/bf16fmal.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace shared {
+
+using math::bf16fmal;
+
+} // namespace shared
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_BF16FMAL_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index af2c66597b75a..c8b75425389bc 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -845,6 +845,16 @@ add_header_library(
libc.include.llvm-libc-macros.float16_macros
)
+add_header_library(
+ bf16fmal
+ HDRS
+ bf16fmal.h
+ DEPENDS
+ libc.src.__support.macros.config
+ libc.src.__support.FPUtil.fma
+ libc.src.__support.FPUtil.bfloat16
+)
+
add_header_library(
ilogb
HDRS
diff --git a/libc/src/__support/math/bf16fmal.h b/libc/src/__support/math/bf16fmal.h
new file mode 100644
index 0000000000000..93a04d0ec8fac
--- /dev/null
+++ b/libc/src/__support/math/bf16fmal.h
@@ -0,0 +1,26 @@
+//===-- Implementation header for bf16fmal ----------------------*- 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_BF16FMAL_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_BF16FMAL_H
+
+#include "src/__support/FPUtil/FMA.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE bfloat16 bf16fmal(long double x, long double y, long double z) {
+ return fputil::fma<bfloat16>(x, y, z);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_BF16FMAL_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 47101706ce4c8..886c790b8e27a 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5241,11 +5241,7 @@ add_entrypoint_object(
HDRS
../bf16fmal.h
DEPENDS
- libc.src.__support.common
- libc.src.__support.FPUtil.bfloat16
- libc.src.__support.FPUtil.fma
- libc.src.__support.macros.config
- libc.src.__support.macros.properties.types
+ libc.src.__support.math.bf16fmal
)
add_entrypoint_object(
diff --git a/libc/src/math/generic/bf16fmal.cpp b/libc/src/math/generic/bf16fmal.cpp
index f31ec6904760b..0e8f1901c2093 100644
--- a/libc/src/math/generic/bf16fmal.cpp
+++ b/libc/src/math/generic/bf16fmal.cpp
@@ -7,16 +7,13 @@
//===----------------------------------------------------------------------===//
#include "src/math/bf16fmal.h"
-
-#include "src/__support/FPUtil/FMA.h"
-#include "src/__support/FPUtil/bfloat16.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/bf16fmal.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(bfloat16, bf16fmal,
(long double x, long double y, long double z)) {
- return fputil::fma<bfloat16>(x, y, z);
+ return math::bf16fmal(x, y, z);
}
+
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index dfe2378269921..89719a995a13d 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -31,6 +31,7 @@ add_fp_unittest(
libc.src.__support.math.bf16addf128
libc.src.__support.math.bf16divf
libc.src.__support.math.bf16fmaf
+ libc.src.__support.math.bf16fmal
libc.src.__support.math.canonicalize
libc.src.__support.math.canonicalizebf16
libc.src.__support.math.canonicalizef
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 8eba836538c41..70345ad2e5cfe 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -236,6 +236,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
TEST(LlvmLibcSharedMathTest, AllBFloat16) {
EXPECT_FP_EQ(bfloat16(5.0), LIBC_NAMESPACE::shared::bf16add(2.0, 3.0));
EXPECT_FP_EQ(bfloat16(2.0f), LIBC_NAMESPACE::shared::bf16divf(4.0f, 2.0f));
+ EXPECT_FP_EQ(bfloat16(10.0),
+ LIBC_NAMESPACE::shared::bf16fmal(2.0L, 3.0L, 4.0L));
bfloat16 canonicalizebf16_cx = bfloat16(0.0);
bfloat16 canonicalizebf16_x = bfloat16(0.0);
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index a5b6823b9ca3d..273e5a76b17b9 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2685,6 +2685,17 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_bf16fmal",
+ hdrs = ["src/__support/math/bf16fmal.h"],
+ deps = [
+ ":__support_common",
+ ":__support_fputil_bfloat16",
+ ":__support_fputil_fma",
+ ":__support_macros_config",
+ ],
+)
+
libc_support_library(
name = "__support_math_canonicalize",
hdrs = ["src/__support/math/canonicalize.h"],
@@ -3036,6 +3047,7 @@ libc_support_library(
],
)
+
libc_support_library(
name = "__support_math_ffma",
hdrs = ["src/__support/math/ffma.h"],
@@ -4483,25 +4495,29 @@ libc_math_function(
additional_deps = [":__support_math_bf16addf128"],
)
+libc_math_function(
+ name = "bf16divf",
+ additional_deps = [":__support_math_bf16divf"],
+)
+
libc_math_function(
name = "bf16fmaf",
additional_deps = [":__support_math_bf16fmaf"],
)
libc_math_function(
- name = "canonicalize",
- additional_deps = [
- ":__support_math_canonicalize",
- ],
+ name = "bf16fmal",
+ additional_deps = [":__support_math_bf16fmal"],
)
libc_math_function(
- name = "bf16divf",
+ name = "canonicalize",
additional_deps = [
- ":__support_math_bf16divf",
+ ":__support_math_canonicalize",
],
)
+
libc_math_function(
name = "canonicalizef",
additional_deps = [
>From 3011e3e99a600deccecbcf76705b1c259f88d394 Mon Sep 17 00:00:00 2001
From: Serosh-commits <janmejayapanda400 at gmail.com>
Date: Wed, 18 Feb 2026 20:21:38 +0530
Subject: [PATCH 2/2] bazel fixes
---
utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 273e5a76b17b9..b553a31532875 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3047,7 +3047,6 @@ libc_support_library(
],
)
-
libc_support_library(
name = "__support_math_ffma",
hdrs = ["src/__support/math/ffma.h"],
@@ -4495,11 +4494,6 @@ libc_math_function(
additional_deps = [":__support_math_bf16addf128"],
)
-libc_math_function(
- name = "bf16divf",
- additional_deps = [":__support_math_bf16divf"],
-)
-
libc_math_function(
name = "bf16fmaf",
additional_deps = [":__support_math_bf16fmaf"],
@@ -4517,6 +4511,12 @@ libc_math_function(
],
)
+libc_math_function(
+ name = "bf16divf",
+ additional_deps = [
+ ":__support_math_bf16divf",
+ ],
+)
libc_math_function(
name = "canonicalizef",
More information about the llvm-commits
mailing list