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

Muhammad Bassiouni via libc-commits libc-commits at lists.llvm.org
Tue Mar 17 11:59:47 PDT 2026


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

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

---
 libc/shared/math.h                            |  1 +
 libc/shared/math/fma.h                        | 23 ++++++++++++++++
 libc/src/__support/math/CMakeLists.txt        |  8 ++++++
 libc/src/__support/math/fma.h                 | 27 +++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  2 +-
 libc/src/math/generic/fma.cpp                 |  7 ++---
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  1 +
 .../llvm-project-overlay/libc/BUILD.bazel     | 10 ++++++-
 9 files changed, 73 insertions(+), 7 deletions(-)
 create mode 100644 libc/shared/math/fma.h
 create mode 100644 libc/src/__support/math/fma.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index c110f6a696b79..bb880f8be80e4 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -137,6 +137,7 @@
 #include "math/floorf128.h"
 #include "math/floorf16.h"
 #include "math/floorl.h"
+#include "math/fma.h"
 #include "math/fmabf16.h"
 #include "math/fmax.h"
 #include "math/fmaxbf16.h"
diff --git a/libc/shared/math/fma.h b/libc/shared/math/fma.h
new file mode 100644
index 0000000000000..82f1dac61dda2
--- /dev/null
+++ b/libc/shared/math/fma.h
@@ -0,0 +1,23 @@
+//===-- Shared fma 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_FMA_H
+#define LLVM_LIBC_SHARED_MATH_FMA_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/fma.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::fma;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_FMA_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index a8bd09e925b38..3e5ce04bb4f61 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1320,6 +1320,14 @@ add_header_library(
     libc.src.__support.macros.properties.types
 )
 
+add_header_library(
+  fma
+  HDRS
+    fma.h
+  DEPENDS
+    libc.src.__support.FPUtil.fma
+)
+
 add_header_library(
   ffma
   HDRS
diff --git a/libc/src/__support/math/fma.h b/libc/src/__support/math/fma.h
new file mode 100644
index 0000000000000..d996610167a19
--- /dev/null
+++ b/libc/src/__support/math/fma.h
@@ -0,0 +1,27 @@
+//===-- Implementation header for fma ---------------------------*- 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_FMA_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FMA_H
+
+#include "src/__support/FPUtil/FMA.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static double fma(double x, double y, double z) {
+  return fputil::fma<double>(x, y, z);
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FMA_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 1c41a8f4980b0..eee96b09f03ad 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4399,7 +4399,7 @@ add_entrypoint_object(
   HDRS
     ../fma.h
   DEPENDS
-    libc.src.__support.FPUtil.fma
+    libc.src.__support.math.fma
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/fma.cpp b/libc/src/math/generic/fma.cpp
index 2ea4ae9961150..3ccdb78846e34 100644
--- a/libc/src/math/generic/fma.cpp
+++ b/libc/src/math/generic/fma.cpp
@@ -7,15 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/fma.h"
-#include "src/__support/common.h"
-
-#include "src/__support/FPUtil/FMA.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/fma.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(double, fma, (double x, double y, double z)) {
-  return fputil::fma<double>(x, y, z);
+  return math::fma(x, y, z);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 789286c25444f..6914d547b5d85 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -134,6 +134,7 @@ add_fp_unittest(
     libc.src.__support.math.floorf128
     libc.src.__support.math.floorf16
     libc.src.__support.math.floorl
+    libc.src.__support.math.fma
     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 c67078f4d5ac4..ab2733742cdd9 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -247,6 +247,7 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
   EXPECT_FP_EQ(0x1p+0, LIBC_NAMESPACE::shared::exp2(0.0));
   EXPECT_FP_EQ(0x1p+0, LIBC_NAMESPACE::shared::exp10(0.0));
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::expm1(0.0));
+  EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::fma(0.0, 0.0, 0.0));
   EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::ffma(0.0, 0.0, 0.0));
   EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::hypot(0.0, 0.0));
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::fsqrt(0.0));
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 381c57b18c8e2..e722875da39e8 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -4231,6 +4231,14 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_fma",
+    hdrs = ["src/__support/math/fma.h"],
+    deps = [
+        ":__support_fputil_fma",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_frexpf128",
     hdrs = ["src/__support/math/frexpf128.h"],
@@ -7010,7 +7018,7 @@ libc_math_function(
 libc_math_function(
     name = "fmaf16",
     additional_deps = [
-        ":__support_fputil_fma",
+        ":__support_math_fma",
     ],
 )
 

>From bfcc555eb2fcc3a38e3ce96f1056a3802c4b01f7 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Tue, 17 Mar 2026 20:53:00 +0200
Subject: [PATCH 2/2] unstatic

---
 libc/src/__support/math/fma.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/__support/math/fma.h b/libc/src/__support/math/fma.h
index d996610167a19..723ff9fecd9f0 100644
--- a/libc/src/__support/math/fma.h
+++ b/libc/src/__support/math/fma.h
@@ -16,7 +16,7 @@ namespace LIBC_NAMESPACE_DECL {
 
 namespace math {
 
-LIBC_INLINE static double fma(double x, double y, double z) {
+LIBC_INLINE double fma(double x, double y, double z) {
   return fputil::fma<double>(x, y, z);
 }
 



More information about the libc-commits mailing list