[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 08:46:19 PST 2026
https://github.com/ProfessionalMenace updated https://github.com/llvm/llvm-project/pull/175304
>From 7074da0848f8f69ab352f68a69b86a3a820312aa Mon Sep 17 00:00:00 2001
From: Menace <vondracekadam00 at gmail.com>
Date: Sat, 10 Jan 2026 11:28:35 +0100
Subject: [PATCH 1/4] [libc][math] Refactor ffma implementation to header-only
in src/__support/math folder.
---
libc/shared/math.h | 1 +
libc/shared/math/ffma.h | 23 ++++++++++++++++
libc/src/__support/math/CMakeLists.txt | 8 ++++++
libc/src/__support/math/ffma.h | 26 +++++++++++++++++++
libc/src/math/generic/ffma.cpp | 6 ++---
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 10 ++++++-
8 files changed, 71 insertions(+), 5 deletions(-)
create mode 100644 libc/shared/math/ffma.h
create mode 100644 libc/src/__support/math/ffma.h
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",
],
)
>From a223e700441eedf09db810cf8a96d14b438a981e Mon Sep 17 00:00:00 2001
From: Menace <vondracekadam00 at gmail.com>
Date: Sat, 10 Jan 2026 12:35:32 +0100
Subject: [PATCH 2/4] fix
---
libc/shared/math/ffma.h | 3 ++-
libc/src/math/generic/CMakeLists.txt | 2 +-
2 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/libc/shared/math/ffma.h b/libc/shared/math/ffma.h
index 1ff3e6fde76cf..cbd24ac0a186f 100644
--- a/libc/shared/math/ffma.h
+++ b/libc/shared/math/ffma.h
@@ -20,4 +20,5 @@ using math::ffma;
} // namespace shared
} // namespace LIBC_NAMESPACE_DECL
-#endif // LLVM_LIBC_SHARED_MATH_FFMA_H
\ No newline at end of file
+#endif // LLVM_LIBC_SHARED_MATH_FFMA_H
+
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index e343c1f15c3f1..930e6b6dc85f0 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3437,7 +3437,7 @@ add_entrypoint_object(
HDRS
../ffma.h
DEPENDS
- libc.src.__support.FPUtil.fma
+ libc.src.__support.math.fma
)
add_entrypoint_object(
>From 5d9b6c9080c809816d62a64d06fb90568de82eea Mon Sep 17 00:00:00 2001
From: Menace <155697298+ProfessionalMenace at users.noreply.github.com>
Date: Sat, 10 Jan 2026 17:24:43 +0100
Subject: [PATCH 3/4] Apply suggestions from code review
Co-authored-by: Muhammad Bassiouni <60100307+bassiounix at users.noreply.github.com>
---
libc/src/__support/math/CMakeLists.txt | 2 +-
libc/src/__support/math/ffma.h | 2 +-
libc/src/math/generic/CMakeLists.txt | 2 +-
3 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 3b429dd6c692e..8c1425d9c2081 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -595,7 +595,7 @@ add_header_library(
)
add_header_library(
- ffma.h
+ ffma
HDRS
ffma.h
DEPENDS
diff --git a/libc/src/__support/math/ffma.h b/libc/src/__support/math/ffma.h
index 9fb328c326361..9d4bcfe453362 100644
--- a/libc/src/__support/math/ffma.h
+++ b/libc/src/__support/math/ffma.h
@@ -16,7 +16,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE static float ffma(double x, double y, double z) {
+LIBC_INLINE static constexpr float ffma(double x, double y, double z) {
return fputil::fma<float>(x, y, z);
}
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 1fdfb49631755..fd61a978f16e7 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3417,7 +3417,7 @@ add_entrypoint_object(
HDRS
../ffma.h
DEPENDS
- libc.src.__support.math.fma
+ libc.src.__support.math.ffma
)
add_entrypoint_object(
>From bad7aaa6e08b2b2d5da7c7f711a72ba5a16c290c Mon Sep 17 00:00:00 2001
From: Menace <155697298+ProfessionalMenace at users.noreply.github.com>
Date: Sat, 10 Jan 2026 17:46:09 +0100
Subject: [PATCH 4/4] fix testing
---
libc/test/shared/shared_math_test.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 75daa1859b2fc..38e36f48f6074 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -90,7 +90,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));
+ EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::ffma(0.0, 0.0, 0.0));
EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::sin(0.0));
}
More information about the libc-commits
mailing list