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

via libc-commits libc-commits at lists.llvm.org
Sat Jan 10 02:44:16 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Menace (ProfessionalMenace)

<details>
<summary>Changes</summary>

Part of #<!-- -->147386

in preparation for: https://discourse.llvm.org/t/rfc-make-clang-builtin-math-functions-constexpr-with-llvm-libc-to-support-c-23-constexpr-math-functions/86450


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


8 Files Affected:

- (modified) libc/shared/math.h (+1) 
- (added) libc/shared/math/ffma.h (+23) 
- (modified) libc/src/__support/math/CMakeLists.txt (+8) 
- (added) libc/src/__support/math/ffma.h (+26) 
- (modified) libc/src/math/generic/ffma.cpp (+2-4) 
- (modified) libc/test/shared/CMakeLists.txt (+1) 
- (modified) libc/test/shared/shared_math_test.cpp (+1) 
- (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+9-1) 


``````````diff
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 70c6d375c22de..7d2adfe62a98f 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -56,6 +56,7 @@
 #include "math/expf16.h"
 #include "math/expm1.h"
 #include "math/expm1f.h"
+#include "math/ffma.h"
 #include "math/frexpf.h"
 #include "math/frexpf128.h"
 #include "math/frexpf16.h"
diff --git a/libc/shared/math/ffma.h b/libc/shared/math/ffma.h
new file mode 100644
index 0000000000000..1ff3e6fde76cf
--- /dev/null
+++ b/libc/shared/math/ffma.h
@@ -0,0 +1,23 @@
+//===-- Shared ffma 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_FFMA_H
+#define LLVM_LIBC_SHARED_MATH_FFMA_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/ffma.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::ffma;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_FFMA_H
\ No newline at end of file
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index f8622da52d983..e8e9a8eb532b3 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -594,6 +594,14 @@ add_header_library(
     libc.src.__support.math.exp10_float16_constants
 )
 
+add_header_library(
+  ffma.h
+  HDRS
+    ffma.h
+  DEPENDS
+    libc.src.__support.FPUtil.fma
+)
+
 add_header_library(
   frexpf128
   HDRS
diff --git a/libc/src/__support/math/ffma.h b/libc/src/__support/math/ffma.h
new file mode 100644
index 0000000000000..9fb328c326361
--- /dev/null
+++ b/libc/src/__support/math/ffma.h
@@ -0,0 +1,26 @@
+//===-- Implementation header for ffma --------------------------*- 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_FFMA_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FFMA_H
+
+#include "src/__support/FPUtil/FMA.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static float ffma(double x, double y, double z) {
+  return fputil::fma<float>(x, y, z);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FFMA_H
diff --git a/libc/src/math/generic/ffma.cpp b/libc/src/math/generic/ffma.cpp
index a4c834ddd7986..20d374ba3463c 100644
--- a/libc/src/math/generic/ffma.cpp
+++ b/libc/src/math/generic/ffma.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/ffma.h"
-#include "src/__support/FPUtil/FMA.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/ffma.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float, ffma, (double x, double y, double z)) {
-  return fputil::fma<float>(x, y, z);
+  return math::ffma(x, y, z);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 4482d6344ae03..e3ec8c8a50815 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -52,6 +52,7 @@ add_fp_unittest(
     libc.src.__support.math.exp10f16
     libc.src.__support.math.expf
     libc.src.__support.math.expf16
+    libc.src.__support.math.ffma
     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 f09b0dcd4ef9f..c8c998a7bc615 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -89,6 +89,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::ffma(0.0));
 }
 
 #ifdef LIBC_TYPES_HAS_FLOAT128
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 913e913572b14..e990c3272e3f2 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2804,6 +2804,14 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_ffma",
+    hdrs = ["src/__support/math/ffma.h"],
+    deps = [
+        ":__support_fputil_fma",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_frexpf128",
     hdrs = ["src/__support/math/frexpf128.h"],
@@ -3971,7 +3979,7 @@ libc_math_function(name = "fdivf128")
 libc_math_function(
     name = "ffma",
     additional_deps = [
-        ":__support_fputil_fma",
+        ":__support_math_ffma",
     ],
 )
 

``````````

</details>


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


More information about the libc-commits mailing list