[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:00:05 PST 2026


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

builds correctly with both Clang and GCC 12.2.

Since `fma` is not `constexpr`, `dfmal` cannot be declared `constexpr` either.
Closes #175316.

>From 5fc9eee12d2bab42de654759c666086d546fba8c Mon Sep 17 00:00:00 2001
From: anonmiraj <nabilmalek48 at gmail.com>
Date: Sat, 10 Jan 2026 18:36:46 +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     | 17 ++++++++++++
 9 files changed, 85 insertions(+), 3 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..74e5b37e0e47b 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -40,6 +40,7 @@
 #include "math/cospif.h"
 #include "math/cospif16.h"
 #include "math/dsqrtl.h"
+#include "math/dfmal.h"
 #include "math/erff.h"
 #include "math/exp.h"
 #include "math/exp10.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..71724112e6433 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -91,6 +91,8 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
   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(0.0, LIBC_NAMESPACE::shared::sin(0.0));
+  EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::dfmal(
+                           float128(0.0), float128(0.0), float128(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 210c25dddd0b9..7fcf841a43b2a 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 = [
@@ -3594,6 +3604,13 @@ libc_math_function(
     ],
 )
 
+libc_math_function(
+    name = "dfmal",
+    additional_deps = [
+        ":__support_math_dfmal",
+    ],
+)
+
 libc_math_function(name = "canonicalize")
 
 libc_math_function(name = "canonicalizef")



More information about the libc-commits mailing list