[libc-commits] [libc] [llvm] [libc][math] Refactor `bf16fmaf128` to header-only (PR #182009)

Mohamed Emad via libc-commits libc-commits at lists.llvm.org
Wed Feb 18 11:46:48 PST 2026


https://github.com/hulxv updated https://github.com/llvm/llvm-project/pull/182009

>From b677ae491050ede7792f8b96cc9f1c7787b7dc84 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Wed, 18 Feb 2026 14:38:08 +0200
Subject: [PATCH] [libc][math] Refactor `bf16fmaf128` to header-only

---
 libc/shared/math.h                            |  1 +
 libc/shared/math/bf16fmaf128.h                | 28 ++++++++++++++++
 libc/src/__support/math/CMakeLists.txt        | 10 ++++++
 libc/src/__support/math/bf16fmaf128.h         | 32 +++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  6 +---
 libc/src/math/generic/bf16fmaf128.cpp         |  7 ++--
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  3 ++
 .../llvm-project-overlay/libc/BUILD.bazel     | 16 ++++++++++
 9 files changed, 94 insertions(+), 10 deletions(-)
 create mode 100644 libc/shared/math/bf16fmaf128.h
 create mode 100644 libc/src/__support/math/bf16fmaf128.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 828980a3500df..050083bbedd88 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/bf16fmaf128.h"
 #include "math/canonicalize.h"
 #include "math/canonicalizebf16.h"
 #include "math/canonicalizef.h"
diff --git a/libc/shared/math/bf16fmaf128.h b/libc/shared/math/bf16fmaf128.h
new file mode 100644
index 0000000000000..ee5a098b22dea
--- /dev/null
+++ b/libc/shared/math/bf16fmaf128.h
@@ -0,0 +1,28 @@
+//===-- Shared bf16fmaf128 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_BF16FMAF128_H
+#define LLVM_LIBC_SHARED_MATH_BF16FMAF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/math/bf16fmaf128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::bf16fmaf128;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_MATH_BF16FMAF128_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index af2c66597b75a..0cdb9aac77dbb 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -381,6 +381,16 @@ add_header_library(
     libc.src.__support.FPUtil.fma
     libc.src.__support.macros.config
 )
+add_header_library(
+  bf16fmaf128
+  HDRS
+    bf16fmaf128.h
+  DEPENDS
+    libc.include.llvm-libc-types.float128
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.fma
+    libc.src.__support.macros.config
+)
 
 add_header_library(
   canonicalize
diff --git a/libc/src/__support/math/bf16fmaf128.h b/libc/src/__support/math/bf16fmaf128.h
new file mode 100644
index 0000000000000..46a72b1646d2b
--- /dev/null
+++ b/libc/src/__support/math/bf16fmaf128.h
@@ -0,0 +1,32 @@
+//===-- Implementation header for bf16fmaf128 -------------------*- 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_BF16FMAF128_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_BF16FMAF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#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 bf16fmaf128(float128 x, float128 y, float128 z) {
+  return fputil::fma<bfloat16>(x, y, z);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_BF16FMAF128_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 47101706ce4c8..a7fa6f9522765 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5255,11 +5255,7 @@ add_entrypoint_object(
   HDRS
     ../bf16fmaf128.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.bf16fmaf128
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/bf16fmaf128.cpp b/libc/src/math/generic/bf16fmaf128.cpp
index a29a0b0b27276..96e20e723a942 100644
--- a/libc/src/math/generic/bf16fmaf128.cpp
+++ b/libc/src/math/generic/bf16fmaf128.cpp
@@ -7,16 +7,13 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/bf16fmaf128.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/bf16fmaf128.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(bfloat16, bf16fmaf128,
                    (float128 x, float128 y, float128 z)) {
-  return fputil::fma<bfloat16>(x, y, z);
+  return math::bf16fmaf128(x, y, z);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index dfe2378269921..bde597a1598fa 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.bf16fmaf128
     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..68a89e21a2006 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -229,6 +229,9 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalizef128(&canonicalizef128_cx,
                                                         &canonicalizef128_x));
   EXPECT_FP_EQ(float128(0.0), canonicalizef128_cx);
+
+  EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::bf16fmaf128(
+                                  float128(0.0), float128(0.0), float128(0.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 a5b6823b9ca3d..b953f83de696a 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_bf16fmaf128",
+    hdrs = ["src/__support/math/bf16fmaf128.h"],
+    deps = [
+        ":__support_fputil_bfloat16",
+        ":__support_fputil_fma",
+        ":__support_macros_config",
+        ":llvm_libc_types_float128",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_canonicalize",
     hdrs = ["src/__support/math/canonicalize.h"],
@@ -4488,6 +4499,11 @@ libc_math_function(
     additional_deps = [":__support_math_bf16fmaf"],
 )
 
+libc_math_function(
+    name = "bf16fmaf128",
+    additional_deps = [":__support_math_bf16fmaf128"],
+)
+
 libc_math_function(
     name = "canonicalize",
     additional_deps = [



More information about the libc-commits mailing list