[libc-commits] [libc] [llvm] [libc][math] Refactor f16fma to header only (PR #176244)

Muhammad Bassiouni via libc-commits libc-commits at lists.llvm.org
Mon Jan 19 00:36:05 PST 2026


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

>From ef68a183b6a0564c7489e4233e76cf36315dcd88 Mon Sep 17 00:00:00 2001
From: Islam-Imad <islamimad404 at gmail.com>
Date: Thu, 15 Jan 2026 20:20:12 +0200
Subject: [PATCH 1/5] refactor f16fma to header only closes #175320

Part of #175313
---
 libc/shared/math.h                            |  1 +
 libc/shared/math/f16fma.h                     | 31 +++++++++++++++++
 libc/src/__support/math/CMakeLists.txt        | 11 +++++++
 libc/src/__support/math/f16fma.h              | 33 +++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  3 +-
 libc/src/math/generic/f16fma.cpp              |  6 ++--
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  2 ++
 .../llvm-project-overlay/libc/BUILD.bazel     | 14 +++++++-
 9 files changed, 95 insertions(+), 7 deletions(-)
 create mode 100644 libc/shared/math/f16fma.h
 create mode 100644 libc/src/__support/math/f16fma.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 287f3aea1565a..43d8e1d32f391 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -57,6 +57,7 @@
 #include "math/expm1.h"
 #include "math/expm1f.h"
 #include "math/expm1f16.h"
+#include "math/f16fma.h"
 #include "math/frexpf.h"
 #include "math/frexpf128.h"
 #include "math/frexpf16.h"
diff --git a/libc/shared/math/f16fma.h b/libc/shared/math/f16fma.h
new file mode 100644
index 0000000000000..e473bf96fc331
--- /dev/null
+++ b/libc/shared/math/f16fma.h
@@ -0,0 +1,31 @@
+//===-- Shared f16fma 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_F16FMA_H
+#define LLVM_LIBC_SHARED_MATH_F16FMA_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+#include "shared/libc_common.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/math/f16fma.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace shared {
+
+using math::f16fma;
+
+} // namespace shared
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_F16FMA_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 4139a1b1d3444..a1fdf37d57fd1 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -631,6 +631,17 @@ add_header_library(
     libc.src.__support.FPUtil.manipulation_functions
 )
 
+add_header_library(
+  f16fma
+  HDRS
+    f16fma.h
+  DEPENDS
+    libc.src.__support.FPUtil.fma
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+    libc.include.llvm-libc-macros.float16_macros
+)
+
 add_header_library(
   ilogbf16
   HDRS
diff --git a/libc/src/__support/math/f16fma.h b/libc/src/__support/math/f16fma.h
new file mode 100644
index 0000000000000..f7bb2fe33e963
--- /dev/null
+++ b/libc/src/__support/math/f16fma.h
@@ -0,0 +1,33 @@
+//===-- Implementation header for f16fma ------------------------*- 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_F16FMA_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_F16FMA_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 static float16 f16fma(double x, double y, double 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_F16FMA_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 6de4cf376bf02..505abfe695e4d 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5136,8 +5136,7 @@ add_entrypoint_object(
   HDRS
     ../f16fma.h
   DEPENDS
-    libc.src.__support.macros.properties.types
-    libc.src.__support.FPUtil.fma
+    libc.src.__support.math.f16fma
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/f16fma.cpp b/libc/src/math/generic/f16fma.cpp
index 70314b5420fad..e6673e71ad77e 100644
--- a/libc/src/math/generic/f16fma.cpp
+++ b/libc/src/math/generic/f16fma.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/f16fma.h"
-#include "src/__support/FPUtil/FMA.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/f16fma.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float16, f16fma, (double x, double y, double z)) {
-  return fputil::fma<float16>(x, y, z);
+  return math::f16fma(x, y, z);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index c5955ecfd54cb..b8ed33f325891 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -53,6 +53,7 @@ add_fp_unittest(
     libc.src.__support.math.exp10f16
     libc.src.__support.math.expf
     libc.src.__support.math.expf16
+    libc.src.__support.math.f16fma
     libc.src.__support.math.frexpf
     libc.src.__support.math.frexpf128
     libc.src.__support.math.frexpf16
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 20a98a1a9138c..078732afc7252 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -33,6 +33,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
   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(float16(10.0), LIBC_NAMESPACE::shared::f16fma(2.0, 3.0, 4.0));
+
   ASSERT_FP_EQ(float16(8 << 5), LIBC_NAMESPACE::shared::ldexpf16(8.0f16, 5));
   ASSERT_FP_EQ(float16(-1 * (8 << 5)),
                LIBC_NAMESPACE::shared::ldexpf16(-8.0f16, 5));
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index c03c21b834895..e475725864b33 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2804,6 +2804,17 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_f16fma",
+    hdrs = ["src/__support/math/f16fma.h"],
+    deps = [
+        ":__support_fputil_fma",
+        ":__support_macros_config",
+        ":__support_macros_properties_types",
+        ":llvm_libc_macros_float16_macros",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_frexpf128",
     hdrs = ["src/__support/math/frexpf128.h"],
@@ -2823,6 +2834,7 @@ libc_support_library(
     ],
 )
 
+
 libc_support_library(
     name = "__support_math_frexpf16",
     hdrs = ["src/__support/math/frexpf16.h"],
@@ -3923,7 +3935,7 @@ libc_math_function(name = "f16divl")
 libc_math_function(
     name = "f16fma",
     additional_deps = [
-        ":__support_fputil_fma",
+        ":__support_math_f16fma",
     ],
 )
 

>From ef13a2626dbc13827d7f05069f12746b4f562959 Mon Sep 17 00:00:00 2001
From: Islam-Imad <islamimad404 at gmail.com>
Date: Thu, 15 Jan 2026 23:33:01 +0200
Subject: [PATCH 2/5] format bazel

---
 utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 1 -
 1 file changed, 1 deletion(-)

diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index e475725864b33..cabd347a4d26c 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2834,7 +2834,6 @@ libc_support_library(
     ],
 )
 
-
 libc_support_library(
     name = "__support_math_frexpf16",
     hdrs = ["src/__support/math/frexpf16.h"],

>From 6e75b51262efc3f9dc502ecce101ebd86e22ff3a Mon Sep 17 00:00:00 2001
From: Islam-Imad <islamimad404 at gmail.com>
Date: Thu, 15 Jan 2026 23:41:50 +0200
Subject: [PATCH 3/5] fix bazel build faliure

---
 utils/bazel/llvm-project-overlay/clang/BUILD.bazel | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel
index 9d698d48f595d..b9f022207da65 100644
--- a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel
@@ -1807,8 +1807,8 @@ cc_library(
         "//llvm:AllTargetsAsmParsers",
         "//llvm:AllTargetsCodeGens",
         "//llvm:Core",
-        "//llvm:JITLink",
         "//llvm:ExecutionEngine",
+        "//llvm:JITLink",
         "//llvm:MC",
         "//llvm:Option",
         "//llvm:OrcDebugging",

>From d3567f9a1dbce41d72b73bee536b7d0a55dde93b Mon Sep 17 00:00:00 2001
From: Islam-Imad <islamimad404 at gmail.com>
Date: Fri, 16 Jan 2026 08:07:26 +0200
Subject: [PATCH 4/5] removed some depends in build files

---
 libc/src/__support/math/CMakeLists.txt             | 1 -
 utils/bazel/llvm-project-overlay/clang/BUILD.bazel | 1 -
 utils/bazel/llvm-project-overlay/libc/BUILD.bazel  | 1 -
 3 files changed, 3 deletions(-)

diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 2538330762d5e..4546edbfc8576 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -638,7 +638,6 @@ add_header_library(
   DEPENDS
     libc.src.__support.FPUtil.fma
     libc.src.__support.macros.config
-    libc.src.__support.macros.properties.types
     libc.include.llvm-libc-macros.float16_macros
 )
 
diff --git a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel
index b9f022207da65..ddd7f5a2d8451 100644
--- a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel
@@ -1808,7 +1808,6 @@ cc_library(
         "//llvm:AllTargetsCodeGens",
         "//llvm:Core",
         "//llvm:ExecutionEngine",
-        "//llvm:JITLink",
         "//llvm:MC",
         "//llvm:Option",
         "//llvm:OrcDebugging",
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 47342f2d5dd57..f8c209610785e 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2810,7 +2810,6 @@ libc_support_library(
     deps = [
         ":__support_fputil_fma",
         ":__support_macros_config",
-        ":__support_macros_properties_types",
         ":llvm_libc_macros_float16_macros",
     ],
 )

>From bc4225fc47963ed7c68baaf765fe5d82370d82d4 Mon Sep 17 00:00:00 2001
From: Islam-Imad <islamimad404 at gmail.com>
Date: Fri, 16 Jan 2026 08:43:13 +0200
Subject: [PATCH 5/5] fix clang bazel build

---
 utils/bazel/llvm-project-overlay/clang/BUILD.bazel | 1 +
 1 file changed, 1 insertion(+)

diff --git a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel
index ddd7f5a2d8451..9d698d48f595d 100644
--- a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel
@@ -1807,6 +1807,7 @@ cc_library(
         "//llvm:AllTargetsAsmParsers",
         "//llvm:AllTargetsCodeGens",
         "//llvm:Core",
+        "//llvm:JITLink",
         "//llvm:ExecutionEngine",
         "//llvm:MC",
         "//llvm:Option",



More information about the libc-commits mailing list