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

via libc-commits libc-commits at lists.llvm.org
Fri Feb 13 22:08:40 PST 2026


Author: Menace
Date: 2026-02-14T06:08:36Z
New Revision: bcf0ecf7865942738c180f2dbbe01cec671b4b23

URL: https://github.com/llvm/llvm-project/commit/bcf0ecf7865942738c180f2dbbe01cec671b4b23
DIFF: https://github.com/llvm/llvm-project/commit/bcf0ecf7865942738c180f2dbbe01cec671b4b23.diff

LOG: [libc][math] Refactor ffma implementation to header-only in src/__support/math folder (#175304).

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

Added: 
    libc/shared/math/ffma.h
    libc/src/__support/math/ffma.h

Modified: 
    libc/shared/math.h
    libc/src/__support/math/CMakeLists.txt
    libc/src/math/generic/CMakeLists.txt
    libc/src/math/generic/ffma.cpp
    libc/test/shared/CMakeLists.txt
    libc/test/shared/shared_math_test.cpp
    utils/bazel/llvm-project-overlay/libc/BUILD.bazel

Removed: 
    


################################################################################
diff  --git a/libc/shared/math.h b/libc/shared/math.h
index ee979f298ffcf..bfbfb61e59e41 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -67,6 +67,7 @@
 #include "math/f16sqrt.h"
 #include "math/f16sqrtf.h"
 #include "math/f16sqrtl.h"
+#include "math/ffma.h"
 #include "math/ffmal.h"
 #include "math/frexpf.h"
 #include "math/frexpf128.h"

diff  --git a/libc/shared/math/ffma.h b/libc/shared/math/ffma.h
new file mode 100644
index 0000000000000..b8b0b97867ef9
--- /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

diff  --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 0da1c395e2a6a..76c468620a899 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -643,6 +643,15 @@ add_header_library(
     libc.src.__support.macros.properties.types
 )
 
+add_header_library(
+  ffma
+  HDRS
+    ffma.h
+  DEPENDS
+    libc.src.__support.FPUtil.fma
+    libc.src.__support.macros.config
+)
+
 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..64431e8bec407
--- /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 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/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 026d0c6e6e8fa..b7f140ae31f53 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3238,7 +3238,7 @@ add_entrypoint_object(
   HDRS
     ../ffma.h
   DEPENDS
-    libc.src.__support.FPUtil.fma
+    libc.src.__support.math.ffma
 )
 
 add_entrypoint_object(

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 41c114936a987..43fa3b77698cb 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -63,6 +63,7 @@ add_fp_unittest(
     libc.src.__support.math.f16sqrt
     libc.src.__support.math.f16sqrtf
     libc.src.__support.math.f16sqrtl
+    libc.src.__support.math.ffma
     libc.src.__support.math.ffmal
     libc.src.__support.math.frexpf
     libc.src.__support.math.frexpf128

diff  --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 8afb80acb8a5e..27a800cc7d09d 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -141,6 +141,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+0f, LIBC_NAMESPACE::shared::ffma(0.0, 0.0, 0.0));
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::fsqrt(0.0));
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::log(1.0));
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::log10(1.0));

diff  --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index a4dcb3ea81b1c..0d1e07e4374b7 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2939,6 +2939,15 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_ffma",
+    hdrs = ["src/__support/math/ffma.h"],
+    deps = [
+        ":__support_fputil_fma",
+        ":__support_macros_config",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_ffmal",
     hdrs = ["src/__support/math/ffmal.h"],
@@ -4629,7 +4638,9 @@ libc_math_function(name = "fdivf128")
 
 libc_math_function(
     name = "ffma",
-    additional_deps = [":__support_fputil_fma"],
+    additional_deps = [
+        ":__support_math_ffma",
+    ],
 )
 
 libc_math_function(


        


More information about the libc-commits mailing list