[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor fmaf16 implementation to header-only in src/__support/math folder. (PR #163977)

Muhammad Bassiouni via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Mar 17 12:05:21 PDT 2026


https://github.com/bassiounix updated https://github.com/llvm/llvm-project/pull/163977

>From b1141ae5b6d9b64580b2ef3f5d6fabe7dffe1483 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Fri, 17 Oct 2025 18:09:02 +0300
Subject: [PATCH] [libc][math] Refactor fmaf16 implementation to header-only in
 src/__support/math folder.

---
 libc/shared/math.h                            |  1 +
 libc/shared/math/fmaf16.h                     | 29 ++++++++++++++++
 libc/src/__support/math/CMakeLists.txt        |  8 +++++
 libc/src/__support/math/fmaf16.h              | 33 +++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  3 +-
 libc/src/math/generic/fmaf16.cpp              |  6 ++--
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  2 ++
 .../llvm-project-overlay/libc/BUILD.bazel     | 10 +++++-
 9 files changed, 86 insertions(+), 7 deletions(-)
 create mode 100644 libc/shared/math/fmaf16.h
 create mode 100644 libc/src/__support/math/fmaf16.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 373a322435c11..75d7aa2ca35e7 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -140,6 +140,7 @@
 #include "math/fma.h"
 #include "math/fmabf16.h"
 #include "math/fmaf.h"
+#include "math/fmaf16.h"
 #include "math/fmax.h"
 #include "math/fmaxbf16.h"
 #include "math/fmaxf.h"
diff --git a/libc/shared/math/fmaf16.h b/libc/shared/math/fmaf16.h
new file mode 100644
index 0000000000000..a6da1bfa58a3a
--- /dev/null
+++ b/libc/shared/math/fmaf16.h
@@ -0,0 +1,29 @@
+//===-- Shared fmaf16 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_FMAF16_H
+#define LLVM_LIBC_SHARED_MATH_FMAF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+#include "shared/libc_common.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/math/fmaf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::fmaf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_FMAF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 2f6604705d449..797f21a273227 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1337,6 +1337,14 @@ add_header_library(
     libc.src.__support.FPUtil.fma
 )
 
+add_header_library(
+  fmaf16
+  HDRS
+    fmaf16.h
+  DEPENDS
+    libc.src.__support.FPUtil.fma
+)
+
 add_header_library(
   ffma
   HDRS
diff --git a/libc/src/__support/math/fmaf16.h b/libc/src/__support/math/fmaf16.h
new file mode 100644
index 0000000000000..2e061a97a2bd2
--- /dev/null
+++ b/libc/src/__support/math/fmaf16.h
@@ -0,0 +1,33 @@
+//===-- Implementation header for fmaf16 ------------------------*- 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_FMAF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FMAF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/FPUtil/FMA.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE float16 fmaf16(float16 x, float16 y, float16 z) {
+  return fputil::fma<float16>(x, y, z);
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FMAF16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 63b1fc9112b21..7f3972cb21cb6 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4388,8 +4388,7 @@ add_entrypoint_object(
   HDRS
     ../fmaf16.h
   DEPENDS
-    libc.src.__support.FPUtil.fma
-    libc.src.__support.macros.properties.types
+    libc.src.__support.math.fmaf16
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/fmaf16.cpp b/libc/src/math/generic/fmaf16.cpp
index 4f712f5de764f..e21240900c2f5 100644
--- a/libc/src/math/generic/fmaf16.cpp
+++ b/libc/src/math/generic/fmaf16.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/fmaf16.h"
-#include "src/__support/FPUtil/FMA.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/fmaf16.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float16, fmaf16, (float16 x, float16 y, float16 z)) {
-  return fputil::fma<float16>(x, y, z);
+  return math::fmaf16(x, y, z);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 2fa38df1cd82f..15b3ce128d385 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -136,6 +136,7 @@ add_fp_unittest(
     libc.src.__support.math.floorl
     libc.src.__support.math.fma
     libc.src.__support.math.fmaf
+    libc.src.__support.math.fmaf16
     libc.src.__support.math.fmabf16
     libc.src.__support.math.fmax
     libc.src.__support.math.fmaxbf16
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 8ed2a1c67d1eb..f443afd824a72 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -38,6 +38,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
   EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::exp2m1f16(0.0f16));
   EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::expf16(0.0f16));
   EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::expm1f16(0.0f16));
+  EXPECT_FP_EQ(0x0p+0f16,
+               LIBC_NAMESPACE::shared::fmaf16(0.0f16, 0.0f16, 0.0f16));
   EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::hypotf16(0.0f16, 0.0f16));
   EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::logf16(1.0f16));
   EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::sinhf16(0.0f16));
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index be68f75b88c8a..2e1863c12ecec 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -4247,6 +4247,14 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_fmaf16",
+    hdrs = ["src/__support/math/fmaf16.h"],
+    deps = [
+        ":__support_fputil_fma",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_frexpf128",
     hdrs = ["src/__support/math/frexpf128.h"],
@@ -7026,7 +7034,7 @@ libc_math_function(
 libc_math_function(
     name = "fmaf16",
     additional_deps = [
-        ":__support_math_fma",
+        ":__support_math_fmaf16",
     ],
 )
 



More information about the llvm-branch-commits mailing list