[libc-commits] [libc] [llvm] [libc][math] Refactor f16fmaf to Header Only. (PR #178851)

via libc-commits libc-commits at lists.llvm.org
Thu Feb 5 08:07:29 PST 2026


https://github.com/Sukumarsawant updated https://github.com/llvm/llvm-project/pull/178851

>From f3c81384f6d58c209a1231967e09767d8ad7dbb8 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Thu, 5 Feb 2026 21:32:27 +0530
Subject: [PATCH 1/3] rebased with upstream + reflected 823e3e0 changes

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

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 32b4bdf599f2a..110b4bfe81073 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -60,6 +60,7 @@
 #include "math/expm1f.h"
 #include "math/expm1f16.h"
 #include "math/f16fma.h"
+#include "math/f16fmaf.h"
 #include "math/f16fmaf128.h"
 #include "math/f16fmal.h"
 #include "math/f16sqrt.h"
diff --git a/libc/shared/math/f16fmaf.h b/libc/shared/math/f16fmaf.h
new file mode 100644
index 0000000000000..bafbb4b4803b6
--- /dev/null
+++ b/libc/shared/math/f16fmaf.h
@@ -0,0 +1,31 @@
+//===-- Shared f16fmaf 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_F16FMAF_H
+#define LLVM_LIBC_SHARED_MATH_F16FMAF_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+#include "shared/libc_common.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/math/f16fmaf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace shared {
+
+using math::f16fmaf;
+
+} // namespace shared
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_F16FMAF_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index a520ede69419a..3d43eccfdbe2c 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -675,6 +675,16 @@ add_header_library(
     libc.include.llvm-libc-macros.float16_macros
 )
 
+add_header_library(
+  f16fmaf
+  HDRS
+    f16fmaf.h
+  DEPENDS
+    libc.src.__support.macros.config
+    libc.src.__support.FPUtil.fma
+    libc.include.llvm-libc-macros.float16_macros
+)
+
 add_header_library(
   f16fmaf128
   HDRS
diff --git a/libc/src/__support/math/f16fmaf.h b/libc/src/__support/math/f16fmaf.h
new file mode 100644
index 0000000000000..92f94a9721e7d
--- /dev/null
+++ b/libc/src/__support/math/f16fmaf.h
@@ -0,0 +1,33 @@
+//===-- Implementation header for f16fmaf -----------------------*- 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_F16FMAF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_F16FMAF_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 f16fmaf(float x, float y, float 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_F16FMAF_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 97ab64e79055f..be3f724e10a8b 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5038,8 +5038,7 @@ add_entrypoint_object(
   HDRS
     ../f16fmaf.h
   DEPENDS
-    libc.src.__support.macros.properties.types
-    libc.src.__support.FPUtil.fma
+    libc.src.__support.math.f16fmaf
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/f16fmaf.cpp b/libc/src/math/generic/f16fmaf.cpp
index bd0b56b1ab31b..1c2c6d80934c8 100644
--- a/libc/src/math/generic/f16fmaf.cpp
+++ b/libc/src/math/generic/f16fmaf.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/f16fmaf.h"
-#include "src/__support/FPUtil/FMA.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/f16fmaf.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float16, f16fmaf, (float x, float y, float z)) {
-  return fputil::fma<float16>(x, y, z);
+  return math::f16fmaf(x, y, z);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 77bdca8ad8aec..7143ce6e84458 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -56,6 +56,7 @@ add_fp_unittest(
     libc.src.__support.math.expf
     libc.src.__support.math.expf16
     libc.src.__support.math.f16fma
+    libc.src.__support.math.f16fmaf
     libc.src.__support.math.f16fmaf128
     libc.src.__support.math.f16fmal
     libc.src.__support.math.f16sqrt
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index c00076deee8be..84734f818241a 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(float16(10.0), LIBC_NAMESPACE::shared::f16fma(2.0, 3.0, 4.0));
 
+  EXPECT_FP_EQ(float16(10.0), LIBC_NAMESPACE::shared::f16fmaf(2.0f, 3.0f, 4.0f));
+  
 #ifdef LIBC_TYPES_HAS_FLOAT128
 
   EXPECT_FP_EQ(10.0f16, LIBC_NAMESPACE::shared::f16fmaf128(
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index ba484c26b2ef4..3dcd63c60923a 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2908,6 +2908,17 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_f16fmaf",
+    hdrs = ["src/__support/math/f16fmaf.h"],
+    deps = [
+        ":__support_common",
+        ":__support_fputil_fma",
+        ":__support_macros_config",
+        ":llvm_libc_macros_float16_macros",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_f16fmaf128",
     hdrs = ["src/__support/math/f16fmaf128.h"],
@@ -4450,7 +4461,7 @@ libc_math_function(
 libc_math_function(
     name = "f16fmaf",
     additional_deps = [
-        ":__support_fputil_fma",
+        ":__support_math_f16fmaf",
     ],
 )
 

>From 19e01885fe1821a55e018898039c4f88aadb472c Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Fri, 30 Jan 2026 14:15:12 +0530
Subject: [PATCH 2/3] initial commit for f16fmaf

---
 libc/test/shared/shared_math_test.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 84734f818241a..79105a8c420c4 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -39,7 +39,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
   EXPECT_FP_EQ(float16(10.0), LIBC_NAMESPACE::shared::f16fma(2.0, 3.0, 4.0));
 
   EXPECT_FP_EQ(float16(10.0), LIBC_NAMESPACE::shared::f16fmaf(2.0f, 3.0f, 4.0f));
-  
+
 #ifdef LIBC_TYPES_HAS_FLOAT128
 
   EXPECT_FP_EQ(10.0f16, LIBC_NAMESPACE::shared::f16fmaf128(

>From 56fd43c4fad3a7523d49fba8c77cb2d6b0f859c0 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Fri, 30 Jan 2026 14:45:15 +0530
Subject: [PATCH 3/3] formatted including bazel buidl files

---
 libc/test/shared/shared_math_test.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 79105a8c420c4..73d6d836227b3 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -38,7 +38,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
 
   EXPECT_FP_EQ(float16(10.0), LIBC_NAMESPACE::shared::f16fma(2.0, 3.0, 4.0));
 
-  EXPECT_FP_EQ(float16(10.0), LIBC_NAMESPACE::shared::f16fmaf(2.0f, 3.0f, 4.0f));
+  EXPECT_FP_EQ(float16(10.0),
+               LIBC_NAMESPACE::shared::f16fmaf(2.0f, 3.0f, 4.0f));
 
 #ifdef LIBC_TYPES_HAS_FLOAT128
 



More information about the libc-commits mailing list