[libc-commits] [libc] [llvm] [libc][math] Refactor dfmal to Header Only. (PR #175359)
via libc-commits
libc-commits at lists.llvm.org
Thu Jan 15 07:29:21 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 1/4] [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",
],
)
>From 92e69699e0e2e48c4ec548c235f433982303b645 Mon Sep 17 00:00:00 2001
From: anonmiraj <nabilmalek48 at gmail.com>
Date: Mon, 12 Jan 2026 07:37:28 +0200
Subject: [PATCH 2/4] fix formatting
---
libc/src/__support/math/dfmal.h | 5 ++---
libc/test/shared/CMakeLists.txt | 2 +-
2 files changed, 3 insertions(+), 4 deletions(-)
diff --git a/libc/src/__support/math/dfmal.h b/libc/src/__support/math/dfmal.h
index f975d172038e3..6c6d6b054a109 100644
--- a/libc/src/__support/math/dfmal.h
+++ b/libc/src/__support/math/dfmal.h
@@ -1,4 +1,4 @@
-//===-- Implementation headre for dfmal -----------------------------===//
+//===-- Implementation header 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.
@@ -14,14 +14,13 @@
#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/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 4c26687c1fdff..b62db2064187c 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -35,6 +35,7 @@ add_fp_unittest(
libc.src.__support.math.coshf16
libc.src.__support.math.cospif
libc.src.__support.math.cospif16
+ libc.src.__support.math.dfmal
libc.src.__support.math.dsqrtl
libc.src.__support.math.exp10m1f
libc.src.__support.math.exp10m1f16
@@ -62,5 +63,4 @@ add_fp_unittest(
libc.src.__support.math.rsqrtf
libc.src.__support.math.rsqrtf16
libc.src.__support.math.sin
- libc.src.__support.math.dfmal
)
>From 66b0032541eba7d729cc27dbbd9a3bb2b4cdc153 Mon Sep 17 00:00:00 2001
From: Anonmiraj <ezzibrahimx at gmail.com>
Date: Thu, 15 Jan 2026 16:13:50 +0200
Subject: [PATCH 3/4] Apply suggestion from @bassiounix
Co-authored-by: Muhammad Bassiouni <60100307+bassiounix at users.noreply.github.com>
---
libc/src/__support/math/dfmal.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/src/__support/math/dfmal.h b/libc/src/__support/math/dfmal.h
index 6c6d6b054a109..ffe94bc4abcc6 100644
--- a/libc/src/__support/math/dfmal.h
+++ b/libc/src/__support/math/dfmal.h
@@ -1,4 +1,4 @@
-//===-- Implementation header for dfmal -----------------------------===//
+//===-- Implementation header 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.
>From 522b01b5118bbdf605360fc7832335174eb3d797 Mon Sep 17 00:00:00 2001
From: anonmiraj <nabilmalek48 at gmail.com>
Date: Thu, 15 Jan 2026 17:29:06 +0200
Subject: [PATCH 4/4] put test under AllLongDouble
---
libc/test/shared/shared_math_test.cpp | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index cb23801c8cc04..9870327de8cbf 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -92,7 +92,10 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::expm1(0.0));
EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::sin(0.0));
}
-
+TEST(LlvmLibcSharedMathTest, AllLongDouble) {
+ EXPECT_FP_EQ(0x0p+0L,
+ LIBC_NAMESPACE::shared::dfmal(0x0.p+0L, 0x0.p+0L, 0x0.p+0L));
+}
#ifdef LIBC_TYPES_HAS_FLOAT128
TEST(LlvmLibcSharedMathTest, AllFloat128) {
More information about the libc-commits
mailing list