[libc] [llvm] [libc][math] Refactor bf16fma to Header Only (PR #182572)

Anirudh Mathur via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 15 09:15:58 PDT 2026


https://github.com/AnirudhMathur12 updated https://github.com/llvm/llvm-project/pull/182572

>From 8a73aac7627bd0f7d717c1da9fc8745856a1efb6 Mon Sep 17 00:00:00 2001
From: AnirudhMathur12 <anirudhmathur12 at gmail.com>
Date: Sat, 21 Feb 2026 00:19:42 +0530
Subject: [PATCH 1/2] [libc][math] Refactor bf16fma to Header Only

---
 libc/shared/math/bf16fma.h                    | 23 ++++++++++++++++
 libc/src/__support/math/CMakeLists.txt        | 10 +++++++
 libc/src/__support/math/bf16fma.h             | 26 +++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  6 +----
 libc/src/math/generic/bf16fma.cpp             |  7 ++---
 .../llvm-project-overlay/libc/BUILD.bazel     | 15 +++++++++++
 6 files changed, 77 insertions(+), 10 deletions(-)
 create mode 100644 libc/shared/math/bf16fma.h
 create mode 100644 libc/src/__support/math/bf16fma.h

diff --git a/libc/shared/math/bf16fma.h b/libc/shared/math/bf16fma.h
new file mode 100644
index 0000000000000..d024062ec11ca
--- /dev/null
+++ b/libc/shared/math/bf16fma.h
@@ -0,0 +1,23 @@
+//===-- Shared bf16fma 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_BF16FMA_H
+#define LLVM_LIBC_SHARED_MATH_BF16FMA_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/bf16fma.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::bf16fma;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_BF16FMA_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 696993c535f68..dc47a0d5cfd2d 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -372,6 +372,16 @@ add_header_library(
     libc.src.__support.macros.config
 )
 
+add_header_library(
+  bf16fma
+  HDRS
+    bf16fma.h
+  DEPENDS
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.fma
+    libc.src.__support.macros.config
+)
+
 add_header_library(
   bf16fmaf
   HDRS
diff --git a/libc/src/__support/math/bf16fma.h b/libc/src/__support/math/bf16fma.h
new file mode 100644
index 0000000000000..da88d5753f6bb
--- /dev/null
+++ b/libc/src/__support/math/bf16fma.h
@@ -0,0 +1,26 @@
+//===-- Implementation header for bf16fma ----------------------*- 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_BF16FMA_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_BF16FMA_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 bf16fma(double x, double y, double z) {
+  return fputil::fma<bfloat16>(x, y, z);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_BF16FMA_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 29105974f5af2..dc1f63792788f 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5164,11 +5164,7 @@ add_entrypoint_object(
   HDRS
     ../bf16fma.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.bf16fma
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/bf16fma.cpp b/libc/src/math/generic/bf16fma.cpp
index 0f0fe8658dde0..a9719abc38c6d 100644
--- a/libc/src/math/generic/bf16fma.cpp
+++ b/libc/src/math/generic/bf16fma.cpp
@@ -7,15 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/bf16fma.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/bf16fma.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(bfloat16, bf16fma, (double x, double y, double z)) {
-  return fputil::fma<bfloat16>(x, y, z);
+  return math::bf16fma(x, y, z);
 }
 
 } // 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 38940c4113929..d1323751bca79 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2685,6 +2685,16 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_bf16fma",
+    hdrs = ["src/__support/math/bf16fma.h"],
+    deps = [
+        ":__support_fputil_bfloat16",
+        ":__support_fputil_fma",
+        ":__support_macros_config",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_bf16fmaf",
     hdrs = ["src/__support/math/bf16fmaf.h"],
@@ -4736,6 +4746,11 @@ libc_math_function(
     additional_deps = [":__support_math_bf16divl"],
 )
 
+libc_math_function(
+    name = "bf16fma",
+    additional_deps = [":__support_math_bf16fma"],
+)
+
 libc_math_function(
     name = "bf16fmaf",
     additional_deps = [":__support_math_bf16fmaf"],

>From ec6573e1ebad53aaceb530d9bb9a666c56af7df3 Mon Sep 17 00:00:00 2001
From: AnirudhMathur12 <anirudhmathur12 at gmail.com>
Date: Sat, 21 Feb 2026 00:19:42 +0530
Subject: [PATCH 2/2] [libc][math] Refactor bf16fma to Header Only

---
 libc/shared/math.h                            |  1 +
 libc/shared/math/bf16fma.h                    | 23 ++++++++++++++++
 libc/src/__support/math/CMakeLists.txt        | 10 +++++++
 libc/src/__support/math/bf16fma.h             | 26 +++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  6 +----
 libc/src/math/generic/bf16fma.cpp             |  7 ++---
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  3 +++
 .../llvm-project-overlay/libc/BUILD.bazel     | 15 +++++++++++
 9 files changed, 82 insertions(+), 10 deletions(-)
 create mode 100644 libc/shared/math/bf16fma.h
 create mode 100644 libc/src/__support/math/bf16fma.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 8737fc69d4d8d..8a54087a5cc1f 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/bf16divl.h"
+#include "math/bf16fma.h"
 #include "math/bf16fmaf.h"
 #include "math/bf16fmal.h"
 #include "math/canonicalize.h"
diff --git a/libc/shared/math/bf16fma.h b/libc/shared/math/bf16fma.h
new file mode 100644
index 0000000000000..d024062ec11ca
--- /dev/null
+++ b/libc/shared/math/bf16fma.h
@@ -0,0 +1,23 @@
+//===-- Shared bf16fma 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_BF16FMA_H
+#define LLVM_LIBC_SHARED_MATH_BF16FMA_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/bf16fma.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::bf16fma;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_BF16FMA_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 696993c535f68..dc47a0d5cfd2d 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -372,6 +372,16 @@ add_header_library(
     libc.src.__support.macros.config
 )
 
+add_header_library(
+  bf16fma
+  HDRS
+    bf16fma.h
+  DEPENDS
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.fma
+    libc.src.__support.macros.config
+)
+
 add_header_library(
   bf16fmaf
   HDRS
diff --git a/libc/src/__support/math/bf16fma.h b/libc/src/__support/math/bf16fma.h
new file mode 100644
index 0000000000000..da88d5753f6bb
--- /dev/null
+++ b/libc/src/__support/math/bf16fma.h
@@ -0,0 +1,26 @@
+//===-- Implementation header for bf16fma ----------------------*- 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_BF16FMA_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_BF16FMA_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 bf16fma(double x, double y, double z) {
+  return fputil::fma<bfloat16>(x, y, z);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_BF16FMA_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 29105974f5af2..dc1f63792788f 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5164,11 +5164,7 @@ add_entrypoint_object(
   HDRS
     ../bf16fma.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.bf16fma
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/bf16fma.cpp b/libc/src/math/generic/bf16fma.cpp
index 0f0fe8658dde0..a9719abc38c6d 100644
--- a/libc/src/math/generic/bf16fma.cpp
+++ b/libc/src/math/generic/bf16fma.cpp
@@ -7,15 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/bf16fma.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/bf16fma.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(bfloat16, bf16fma, (double x, double y, double z)) {
-  return fputil::fma<bfloat16>(x, y, z);
+  return math::bf16fma(x, y, z);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 14ab2eb3bc3d8..1ecab2b446369 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.bf16divl
+    libc.src.__support.math.bf16fma
     libc.src.__support.math.bf16fmaf
     libc.src.__support.math.bf16fmal
     libc.src.__support.math.canonicalize
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 8d736f618e5a9..0154c48d8f0e0 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -280,4 +280,7 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) {
   EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::ceilbf16(bfloat16(0.0)));
   EXPECT_FP_EQ(bfloat16(0.0),
                LIBC_NAMESPACE::shared::fmaxbf16(bfloat16(0.0), bfloat16(0.0)));
+
+  EXPECT_FP_EQ(bfloat16(10.0),
+               LIBC_NAMESPACE::shared::bf16fma(2.0, 3.0, 4.0));
 }
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 38940c4113929..d1323751bca79 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2685,6 +2685,16 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_bf16fma",
+    hdrs = ["src/__support/math/bf16fma.h"],
+    deps = [
+        ":__support_fputil_bfloat16",
+        ":__support_fputil_fma",
+        ":__support_macros_config",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_bf16fmaf",
     hdrs = ["src/__support/math/bf16fmaf.h"],
@@ -4736,6 +4746,11 @@ libc_math_function(
     additional_deps = [":__support_math_bf16divl"],
 )
 
+libc_math_function(
+    name = "bf16fma",
+    additional_deps = [":__support_math_bf16fma"],
+)
+
 libc_math_function(
     name = "bf16fmaf",
     additional_deps = [":__support_math_bf16fmaf"],



More information about the llvm-commits mailing list