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

via libc-commits libc-commits at lists.llvm.org
Thu Jan 15 13:06:58 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Islam Imad (Islam-Imad)

<details>
<summary>Changes</summary>

closes #<!-- -->175320

Part of #<!-- -->175313

---
Full diff: https://github.com/llvm/llvm-project/pull/176244.diff


9 Files Affected:

- (modified) libc/shared/math.h (+1) 
- (added) libc/shared/math/f16fma.h (+31) 
- (modified) libc/src/__support/math/CMakeLists.txt (+11) 
- (added) libc/src/__support/math/f16fma.h (+33) 
- (modified) libc/src/math/generic/CMakeLists.txt (+1-2) 
- (modified) libc/src/math/generic/f16fma.cpp (+2-4) 
- (modified) libc/test/shared/CMakeLists.txt (+1) 
- (modified) libc/test/shared/shared_math_test.cpp (+2) 
- (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+13-1) 


``````````diff
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",
     ],
 )
 

``````````

</details>


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


More information about the libc-commits mailing list