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

via libc-commits libc-commits at lists.llvm.org
Sat Jan 10 09:56:54 PST 2026


https://github.com/AnonMiraj updated https://github.com/llvm/llvm-project/pull/175359

>From d2f7039b64ad162443c9fb010afa27e7b76093a0 Mon Sep 17 00:00:00 2001
From: anonmiraj <nabilmalek48 at gmail.com>
Date: Sat, 10 Jan 2026 19:56:19 +0200
Subject: [PATCH] [libc][math] Refactor dfmal to Header Only.

---
 libc/shared/math.h                            |  1 +
 libc/shared/math/dfmal.h                      | 24 +++++++++++++++++
 libc/src/__support/math/CMakeLists.txt        | 10 +++++++
 libc/src/__support/math/dfmal.h               | 27 +++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  2 +-
 libc/src/math/generic/dfmal.cpp               |  4 +--
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  2 ++
 .../llvm-project-overlay/libc/BUILD.bazel     | 12 ++++++++-
 9 files changed, 79 insertions(+), 4 deletions(-)
 create mode 100644 libc/shared/math/dfmal.h
 create mode 100644 libc/src/__support/math/dfmal.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 7fb4c43f509c4..c06802dbb576e 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -39,6 +39,7 @@
 #include "math/coshf16.h"
 #include "math/cospif.h"
 #include "math/cospif16.h"
+#include "math/dfmal.h"
 #include "math/dsqrtl.h"
 #include "math/erff.h"
 #include "math/exp.h"
diff --git a/libc/shared/math/dfmal.h b/libc/shared/math/dfmal.h
new file mode 100644
index 0000000000000..05d83caad3926
--- /dev/null
+++ b/libc/shared/math/dfmal.h
@@ -0,0 +1,24 @@
+//===-- Shared dfmal 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_DFMAL_H
+#define LLVM_LIBC_SHARED_MATH_DFMAL_H
+
+#include "shared/libc_common.h"
+
+#include "src/__support/math/dfmal.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::dfmal;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_DFMAL_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 741da7432c94f..9f3e749615c1d 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1040,3 +1040,13 @@ add_header_library(
     libc.src.__support.math.sincos_eval
     libc.src.__support.macros.optimization
 )
+
+add_header_library(
+  dfmal
+  HDRS
+    dfmal.h
+  DEPENDS
+    libc.src.__support.FPUtil.fma
+    libc.src.__support.common
+    libc.src.__support.macros.config
+)
diff --git a/libc/src/__support/math/dfmal.h b/libc/src/__support/math/dfmal.h
new file mode 100644
index 0000000000000..f975d172038e3
--- /dev/null
+++ b/libc/src/__support/math/dfmal.h
@@ -0,0 +1,27 @@
+//===-- Implementation headre for dfmal       -----------------------------===//
+//
+// 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_DFMAL_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_DFMAL_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 double dfmal(long double x, long double y, long double z) {
+
+  return fputil::fma<double>(x, y, z);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_DFMAL_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 9c0da076b6cf0..02362ad10b2b9 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -260,7 +260,7 @@ add_entrypoint_object(
   HDRS
     ../dfmal.h
   DEPENDS
-    libc.src.__support.FPUtil.fma
+    libc.src.__support.math.dfmal
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/dfmal.cpp b/libc/src/math/generic/dfmal.cpp
index 02e0ce84ace83..626e5f1cc0e9b 100644
--- a/libc/src/math/generic/dfmal.cpp
+++ b/libc/src/math/generic/dfmal.cpp
@@ -7,15 +7,15 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/dfmal.h"
-#include "src/__support/FPUtil/FMA.h"
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
+#include "src/__support/math/dfmal.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(double, dfmal,
                    (long double x, long double y, long double z)) {
-  return fputil::fma<double>(x, y, z);
+  return math::dfmal(x, y, z);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 0f23162798a8b..4c26687c1fdff 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -62,4 +62,5 @@ add_fp_unittest(
     libc.src.__support.math.rsqrtf
     libc.src.__support.math.rsqrtf16
     libc.src.__support.math.sin
+    libc.src.__support.math.dfmal
 )
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index f823d414e2afd..cb23801c8cc04 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -108,6 +108,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
                LIBC_NAMESPACE::shared::ldexpf128(float128(8), 5));
   ASSERT_FP_EQ(float128(-1 * (8 << 5)),
                LIBC_NAMESPACE::shared::ldexpf128(float128(-8), 5));
+  EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::dfmal(
+                           float128(0.0), float128(0.0), float128(0.0)));
 }
 
 #endif // 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 210c25dddd0b9..e74f98fe9dba7 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3118,6 +3118,16 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_dfmal",
+    hdrs = ["src/__support/math/dfmal.h"],
+    deps = [
+        ":__support_common",
+        ":__support_fputil_fma",
+        ":__support_macros_config",
+    ],
+)
+
 libc_support_library(
     name = "__support_range_reduction_double",
     hdrs = [
@@ -3718,7 +3728,7 @@ libc_math_function(name = "ddivf128")
 libc_math_function(
     name = "dfmal",
     additional_deps = [
-        ":__support_fputil_fma",
+        ":__support_math_dfmal",
     ],
 )
 



More information about the libc-commits mailing list