[libc-commits] [libc] [llvm] [libc][math] Refactor dfmaf128 to Header Only (PR #176480)

Madhur Kumar via libc-commits libc-commits at lists.llvm.org
Mon Jan 19 05:42:04 PST 2026


https://github.com/MadhurKumar004 updated https://github.com/llvm/llvm-project/pull/176480

>From 05c85243999e85dc87115ed6400af44100c71c73 Mon Sep 17 00:00:00 2001
From: Madhur Kumar <madhurkumar004 at gmail.com>
Date: Sat, 17 Jan 2026 03:05:51 +0530
Subject: [PATCH] [libc][math] Refactor dfmaf128 to Header Only

---
 libc/shared/math.h                            |  1 +
 libc/shared/math/dfmaf128.h                   | 28 +++++++++++++++++
 libc/src/__support/math/CMakeLists.txt        | 10 ++++++
 libc/src/__support/math/dfmaf128.h            | 31 +++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  4 +--
 libc/src/math/generic/dfmaf128.cpp            | 11 ++-----
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  2 ++
 .../llvm-project-overlay/libc/BUILD.bazel     | 13 +++++++-
 9 files changed, 89 insertions(+), 12 deletions(-)
 create mode 100644 libc/shared/math/dfmaf128.h
 create mode 100644 libc/src/__support/math/dfmaf128.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 2e7bbe1ef13be..9069d516cb244 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/dfmaf128.h"
 #include "math/dfmal.h"
 #include "math/dsqrtl.h"
 #include "math/erff.h"
diff --git a/libc/shared/math/dfmaf128.h b/libc/shared/math/dfmaf128.h
new file mode 100644
index 0000000000000..840ceac768934
--- /dev/null
+++ b/libc/shared/math/dfmaf128.h
@@ -0,0 +1,28 @@
+//===-- Shared dfmaf128 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_DFMAF128_H
+#define LLVM_LIBC_SHARED_MATH_DFMAF128_H
+
+#include "include/llvm-libc-types/float128.h"
+#include "shared/libc_common.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/math/dfmaf128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::dfmaf128;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+#endif // LLVM_LIBC_SHARED_MATH_DFMAF128_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index b45ccfc0eb3cb..c3e88170676a4 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1096,3 +1096,13 @@ add_header_library(
     libc.src.__support.common
     libc.src.__support.macros.config
 )
+
+add_header_library(
+  dfmaf128
+  HDRS
+    dfmaf128.h
+  DEPENDS
+    libc.src.__support.FPUtil.fma
+    libc.src.__support.common
+    libc.src.__support.macros.config
+)
diff --git a/libc/src/__support/math/dfmaf128.h b/libc/src/__support/math/dfmaf128.h
new file mode 100644
index 0000000000000..9bb3a6f418904
--- /dev/null
+++ b/libc/src/__support/math/dfmaf128.h
@@ -0,0 +1,31 @@
+//===-- Implementation header for dfmaf128 ---------------------------------===//
+//
+// 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_DFMAF128_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_DFMAF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#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 dfmaf128(float128 x, float128 y, float128 z) {
+  return fputil::fma<double>(x, y, z);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_DFMAF128_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 009a3033083bd..fd8a3684d59d5 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -249,8 +249,8 @@ add_entrypoint_object(
   HDRS
     ../dfmaf128.h
   DEPENDS
-    libc.src.__support.FPUtil.fma
-    libc.src.__support.macros.properties.types
+    libc.src.__support.math.dfmaf128
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/dfmaf128.cpp b/libc/src/math/generic/dfmaf128.cpp
index b6e1bdb085cf7..b8069c6597c64 100644
--- a/libc/src/math/generic/dfmaf128.cpp
+++ b/libc/src/math/generic/dfmaf128.cpp
@@ -6,20 +6,13 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef LLVM_LIBC_SRC_MATH_DFMAf128_H
-#define LLVM_LIBC_SRC_MATH_DFMAf128_H
-
 #include "src/math/dfmaf128.h"
-#include "src/__support/FPUtil/FMA.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/dfmaf128.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(double, dfmaf128, (float128 x, float128 y, float128 z)) {
-  return fputil::fma<double>(x, y, z);
+  return math::dfmaf128(x, y, z);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
-
-#endif // LLVM_LIBC_SRC_MATH_DFMAf128_H
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 58214bf6f88eb..170fff1716c60 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.dfmaf128
     libc.src.__support.math.dfmal
     libc.src.__support.math.dsqrtl
     libc.src.__support.math.exp10m1f
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 6b78de0281347..ae134063f26de 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -115,6 +115,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(0.0, LIBC_NAMESPACE::shared::dfmaf128(
+                        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 4251f20a5dfcf..13d64c10d345f 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3164,6 +3164,16 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_dfmaf128",
+    hdrs = ["src/__support/math/dfmaf128.h"],
+    deps = [
+        ":__support_common",
+        ":__support_fputil_fma",
+        ":__support_macros_config",
+    ],
+)
+
 libc_support_library(
     name = "__support_range_reduction_double",
     hdrs = [
@@ -3771,7 +3781,8 @@ libc_math_function(
 libc_math_function(
     name = "dfmaf128",
     additional_deps = [
-        ":__support_fputil_fma",
+        ":__support_math_dfmaf128",
+        ":errno",
     ],
 )
 



More information about the libc-commits mailing list