[libc-commits] [libc] [llvm] [libc][math] Refactor ffmal to Header Only. (PR #179069)

via libc-commits libc-commits at lists.llvm.org
Sat Jan 31 12:57:30 PST 2026


https://github.com/Sukumarsawant created https://github.com/llvm/llvm-project/pull/179069

closes #175326

Part of #147386 

@bassiounix 


>From 1d8d84b7743489d8b4d535aa55870ececeb96c10 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sun, 1 Feb 2026 01:36:05 +0530
Subject: [PATCH 1/4] intial commit without build/format/test

---
 libc/shared/math.h                            |  1 +
 libc/shared/math/ffmal.h                      | 25 ++++++++++++
 libc/src/__support/math/CMakeLists.txt        | 10 +++++
 libc/src/__support/math/ffmal.h               | 28 +++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  2 +-
 libc/src/math/generic/ffmal.cpp               | 40 +++++++++----------
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  4 ++
 .../llvm-project-overlay/libc/BUILD.bazel     | 12 +++++-
 9 files changed, 100 insertions(+), 23 deletions(-)
 create mode 100644 libc/shared/math/ffmal.h
 create mode 100644 libc/src/__support/math/ffmal.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index b58d29d1ee480..460a56ef510a7 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -63,6 +63,7 @@
 #include "math/f16fmal.h"
 #include "math/f16sqrt.h"
 #include "math/f16sqrtl.h"
+#include "math/ffmal.h"
 #include "math/frexpf.h"
 #include "math/frexpf128.h"
 #include "math/frexpf16.h"
diff --git a/libc/shared/math/ffmal.h b/libc/shared/math/ffmal.h
new file mode 100644
index 0000000000000..51e20cbed70f5
--- /dev/null
+++ b/libc/shared/math/ffmal.h
@@ -0,0 +1,25 @@
+//===-- Shared ffmal 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_FFMAL_H
+#define LLVM_LIBC_SHARED_MATH_FFMAL_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/ffmal.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace shared {
+
+using math::ffmal;
+
+} // namespace shared
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_FFMAL_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 58e4040911f8e..a157529229650 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -593,6 +593,16 @@ add_header_library(
     libc.src.__support.math.exp10_float16_constants
 )
 
+add_header_library(
+  ffmal
+  HDRS
+    ffmal.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.FPUtil.fma
+    libc.src.__support.macros.config
+)
+
 add_header_library(
   f16sqrt
   HDRS
diff --git a/libc/src/__support/math/ffmal.h b/libc/src/__support/math/ffmal.h
new file mode 100644
index 0000000000000..c3311f6977f53
--- /dev/null
+++ b/libc/src/__support/math/ffmal.h
@@ -0,0 +1,28 @@
+//===-- Implementation header for ffmal -------------------------*- 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_FFMAL_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FFMAL_H
+
+#include "src/__support/FPUtil/FMA.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static float f16fmal(long double x, long double y,long double z) {
+  return fputil::fma<float>(x, y, z);
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FFMAL_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 1b18388ed60f8..c69bc674f41d3 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3309,7 +3309,7 @@ add_entrypoint_object(
   HDRS
     ../ffmal.h
   DEPENDS
-    libc.src.__support.FPUtil.fma
+    libc.src.__support.math.ffmal
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/ffmal.cpp b/libc/src/math/generic/ffmal.cpp
index d5cd4f763cbe5..6ad1f7da563dc 100644
--- a/libc/src/math/generic/ffmal.cpp
+++ b/libc/src/math/generic/ffmal.cpp
@@ -1,21 +1,19 @@
-//===-- Implementation of ffmal function ----------------------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-#include "src/math/ffmal.h"
-#include "src/__support/FPUtil/FMA.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
-
-namespace LIBC_NAMESPACE_DECL {
-
-LLVM_LIBC_FUNCTION(float, ffmal,
-                   (long double x, long double y, long double z)) {
-  return fputil::fma<float>(x, y, z);
-}
-
-} // namespace LIBC_NAMESPACE_DECL
+//===-- Implementation of ffmal function ----------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/ffmal.h"
+#include "src/__support/math/ffmal.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float, ffmal,
+                   (long double x, long double y, long double z)) {
+  return math::ffmal(x, y, z);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 07ccd0ca1bc4b..1ad3ab930f759 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -59,6 +59,7 @@ add_fp_unittest(
     libc.src.__support.math.f16fmal
     libc.src.__support.math.f16sqrt
     libc.src.__support.math.f16sqrtl
+    libc.src.__support.math.ffmal
     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 ec70025f0f4ea..471bc7d1fad0f 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -38,6 +38,9 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
 
   EXPECT_FP_EQ(float16(10.0), LIBC_NAMESPACE::shared::f16fma(2.0, 3.0, 4.0));
 
+  EXPECT_FP_EQ(float16(10.0),
+               LIBC_NAMESPACE::shared::f16fmaf(2.0f, 3.0f, 4.0f));
+
   EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::f16sqrt(0.0));
 
   EXPECT_FP_EQ(float16(10.0),
@@ -84,6 +87,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(10.0f, LIBC_NAMESPACE::shared::ffmal(2.0L, 3.0, 4.0L));
   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 83b752ce1214b..e8e72ffa81a10 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2919,6 +2919,16 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_ffmal",
+    hdrs = ["src/__support/math/ffmal.h"],
+    deps = [
+        ":__support_common",
+        ":__support_fputil_fma",
+        ":__support_macros_config",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_frexpf128",
     hdrs = ["src/__support/math/frexpf128.h"],
@@ -4543,7 +4553,7 @@ libc_math_function(
 libc_math_function(
     name = "ffmal",
     additional_deps = [
-        ":__support_fputil_fma",
+        ":__support_math_ffmal",
     ],
 )
 

>From 3291f8ac9d8888f19e041b23858ba36b53880726 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sun, 1 Feb 2026 01:42:00 +0530
Subject: [PATCH 2/4] fixed 1 crlf and 1 overwritten

---
 libc/src/math/generic/ffmal.cpp       | 38 +++++++++++++--------------
 libc/test/shared/shared_math_test.cpp |  3 ---
 2 files changed, 19 insertions(+), 22 deletions(-)

diff --git a/libc/src/math/generic/ffmal.cpp b/libc/src/math/generic/ffmal.cpp
index 6ad1f7da563dc..c5d0d421c42a4 100644
--- a/libc/src/math/generic/ffmal.cpp
+++ b/libc/src/math/generic/ffmal.cpp
@@ -1,19 +1,19 @@
-//===-- Implementation of ffmal function ----------------------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-#include "src/math/ffmal.h"
-#include "src/__support/math/ffmal.h"
-
-namespace LIBC_NAMESPACE_DECL {
-
-LLVM_LIBC_FUNCTION(float, ffmal,
-                   (long double x, long double y, long double z)) {
-  return math::ffmal(x, y, z);
-}
-
-} // namespace LIBC_NAMESPACE_DECL
+//===-- Implementation of ffmal function ----------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/ffmal.h"
+#include "src/__support/math/ffmal.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float, ffmal,
+                   (long double x, long double y, long double z)) {
+  return math::ffmal(x, y, z);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 471bc7d1fad0f..97b07503cd64d 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -38,9 +38,6 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
 
   EXPECT_FP_EQ(float16(10.0), LIBC_NAMESPACE::shared::f16fma(2.0, 3.0, 4.0));
 
-  EXPECT_FP_EQ(float16(10.0),
-               LIBC_NAMESPACE::shared::f16fmaf(2.0f, 3.0f, 4.0f));
-
   EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::f16sqrt(0.0));
 
   EXPECT_FP_EQ(float16(10.0),

>From fcaa4ee59d49376957fede28ff182818a45a25fb Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sun, 1 Feb 2026 01:51:56 +0530
Subject: [PATCH 3/4] spell err

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

diff --git a/libc/src/__support/math/ffmal.h b/libc/src/__support/math/ffmal.h
index c3311f6977f53..f69a605ba90cb 100644
--- a/libc/src/__support/math/ffmal.h
+++ b/libc/src/__support/math/ffmal.h
@@ -17,7 +17,7 @@ namespace LIBC_NAMESPACE_DECL {
 
 namespace math {
 
-LIBC_INLINE static float f16fmal(long double x, long double y,long double z) {
+LIBC_INLINE static float ffmal(long double x, long double y,long double z) {
   return fputil::fma<float>(x, y, z);
 }
 

>From cfeaaa333f4ea041c4357401c86c86af5d8e7ac7 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sun, 1 Feb 2026 02:15:30 +0530
Subject: [PATCH 4/4] formatted clang/bazel build

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

diff --git a/libc/src/__support/math/ffmal.h b/libc/src/__support/math/ffmal.h
index f69a605ba90cb..d99e98afbda84 100644
--- a/libc/src/__support/math/ffmal.h
+++ b/libc/src/__support/math/ffmal.h
@@ -17,7 +17,7 @@ namespace LIBC_NAMESPACE_DECL {
 
 namespace math {
 
-LIBC_INLINE static float ffmal(long double x, long double y,long double z) {
+LIBC_INLINE static float ffmal(long double x, long double y, long double z) {
   return fputil::fma<float>(x, y, z);
 }
 



More information about the libc-commits mailing list