[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor fmaf implementation to header-only in src/__support/math folder. (PR #163970)
Muhammad Bassiouni via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Mar 17 12:05:22 PDT 2026
https://github.com/bassiounix updated https://github.com/llvm/llvm-project/pull/163970
>From 1977fb9d6a16fb6b8952def9ad5254726e9f1aaf Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Fri, 17 Oct 2025 17:19:30 +0300
Subject: [PATCH] [libc][math] Refactor fmaf implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h | 1 +
libc/shared/math/fmaf.h | 23 ++++++++++++++++
libc/src/__support/math/CMakeLists.txt | 8 ++++++
libc/src/__support/math/fmaf.h | 27 +++++++++++++++++++
libc/src/math/generic/CMakeLists.txt | 2 +-
libc/src/math/generic/fmaf.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/fmaf.h
create mode 100644 libc/src/__support/math/fmaf.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index bb880f8be80e4..373a322435c11 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -139,6 +139,7 @@
#include "math/floorl.h"
#include "math/fma.h"
#include "math/fmabf16.h"
+#include "math/fmaf.h"
#include "math/fmax.h"
#include "math/fmaxbf16.h"
#include "math/fmaxf.h"
diff --git a/libc/shared/math/fmaf.h b/libc/shared/math/fmaf.h
new file mode 100644
index 0000000000000..eef75b05d4a18
--- /dev/null
+++ b/libc/shared/math/fmaf.h
@@ -0,0 +1,23 @@
+//===-- Shared fmaf 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_FMAF_H
+#define LLVM_LIBC_SHARED_MATH_FMAF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/fmaf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::fmaf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_FMAF_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 22fe851cd9088..2f6604705d449 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1329,6 +1329,14 @@ add_header_library(
libc.src.__support.macros.config
)
+add_header_library(
+ fmaf
+ HDRS
+ fmaf.h
+ DEPENDS
+ libc.src.__support.FPUtil.fma
+)
+
add_header_library(
ffma
HDRS
diff --git a/libc/src/__support/math/fmaf.h b/libc/src/__support/math/fmaf.h
new file mode 100644
index 0000000000000..0437bb0ff44d6
--- /dev/null
+++ b/libc/src/__support/math/fmaf.h
@@ -0,0 +1,27 @@
+//===-- Implementation header for fmaf --------------------------*- 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_FMAF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FMAF_H
+
+#include "src/__support/FPUtil/FMA.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE float fmaf(float x, float y, float z) {
+ return fputil::fma<float>(x, y, z);
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FMAF_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index eee96b09f03ad..63b1fc9112b21 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4378,7 +4378,7 @@ add_entrypoint_object(
HDRS
../fmaf.h
DEPENDS
- libc.src.__support.FPUtil.fma
+ libc.src.__support.math.fmaf
)
add_entrypoint_object(
diff --git a/libc/src/math/generic/fmaf.cpp b/libc/src/math/generic/fmaf.cpp
index dad85b4e9d1e6..069c7c4605da7 100644
--- a/libc/src/math/generic/fmaf.cpp
+++ b/libc/src/math/generic/fmaf.cpp
@@ -7,15 +7,12 @@
//===----------------------------------------------------------------------===//
#include "src/math/fmaf.h"
-#include "src/__support/common.h"
-
-#include "src/__support/FPUtil/FMA.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/fmaf.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(float, fmaf, (float x, float y, float z)) {
- return fputil::fma<float>(x, y, z);
+ return math::fmaf(x, y, z);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 6914d547b5d85..2fa38df1cd82f 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -135,6 +135,7 @@ add_fp_unittest(
libc.src.__support.math.floorf16
libc.src.__support.math.floorl
libc.src.__support.math.fma
+ libc.src.__support.math.fmaf
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 ab2733742cdd9..8ed2a1c67d1eb 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -169,6 +169,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::expf(0.0f));
EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::exp2f(0.0f));
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::expm1f(0.0f));
+ EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::fmaf(0.0f, 0.0f, 0.0f));
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::hypotf(0.0f, 0.0f));
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::logf(1.0f));
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::sinhf(0.0f));
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index e722875da39e8..be68f75b88c8a 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -4239,6 +4239,14 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_fmaf",
+ hdrs = ["src/__support/math/fmaf.h"],
+ deps = [
+ ":__support_fputil_fma",
+ ],
+)
+
libc_support_library(
name = "__support_math_frexpf128",
hdrs = ["src/__support/math/frexpf128.h"],
@@ -7012,7 +7020,7 @@ libc_math_function(
libc_math_function(
name = "fmaf",
- additional_deps = [":__support_fputil_fma"],
+ additional_deps = [":__support_math_fmaf"],
)
libc_math_function(
More information about the llvm-branch-commits
mailing list