Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[libc][math] Refactor dsub family to header-only#182160

Open
hulxv wants to merge 5 commits intollvm:mainfrom
hulxv:refactor/header-only/dsub
Open

[libc][math] Refactor dsub family to header-only#182160
hulxv wants to merge 5 commits intollvm:mainfrom
hulxv:refactor/header-only/dsub

Conversation

@hulxv
Copy link
Copy Markdown
Member

@hulxv hulxv commented Feb 18, 2026

Refactors the dsub math family to be header-only.

Closes #182159

Target Functions:

  • dsubf128
  • dsubl

@llvmbot llvmbot added libc bazel "Peripheral" support tier build system: utils/bazel labels Feb 18, 2026
@llvmbot
Copy link
Copy Markdown
Member

llvmbot commented Feb 18, 2026

@llvm/pr-subscribers-libc

Author: Mohamed Emad (hulxv)

Changes

Refactors the dsub math family to be header-only.

Closes #182159

Target Functions:

  • dsubf128
  • dsubl

Full diff: https://github.com/llvm/llvm-project/pull/182160.diff

12 Files Affected:

  • (modified) libc/shared/math.h (+2)
  • (added) libc/shared/math/dsubf128.h (+28)
  • (added) libc/shared/math/dsubl.h (+22)
  • (modified) libc/src/__support/math/CMakeLists.txt (+17)
  • (added) libc/src/__support/math/dsubf128.h (+32)
  • (added) libc/src/__support/math/dsubl.h (+26)
  • (modified) libc/src/math/generic/CMakeLists.txt (+2-3)
  • (modified) libc/src/math/generic/dsubf128.cpp (+2-4)
  • (modified) libc/src/math/generic/dsubl.cpp (+2-4)
  • (modified) libc/test/shared/CMakeLists.txt (+2)
  • (modified) libc/test/shared/shared_math_test.cpp (+5)
  • (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+31-2)
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 8a5aca82c6ec3..7fea2537c4858 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -55,6 +55,8 @@
 #include "math/dfmaf128.h"
 #include "math/dfmal.h"
 #include "math/dsqrtl.h"
+#include "math/dsubf128.h"
+#include "math/dsubl.h"
 #include "math/erff.h"
 #include "math/exp.h"
 #include "math/exp10.h"
diff --git a/libc/shared/math/dsubf128.h b/libc/shared/math/dsubf128.h
new file mode 100644
index 0000000000000..4d401ddf023dc
--- /dev/null
+++ b/libc/shared/math/dsubf128.h
@@ -0,0 +1,28 @@
+//===-- Shared dsubf128 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_DSUBF128_H
+#define LLVM_LIBC_SHARED_MATH_DSUBF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/math/dsubf128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::dsubf128;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_MATH_DSUBF128_H
diff --git a/libc/shared/math/dsubl.h b/libc/shared/math/dsubl.h
new file mode 100644
index 0000000000000..a9b4fe5091ab4
--- /dev/null
+++ b/libc/shared/math/dsubl.h
@@ -0,0 +1,22 @@
+//===-- Shared dsubl 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_DSUBL_H
+#define LLVM_LIBC_SHARED_MATH_DSUBL_H
+
+#include "src/__support/math/dsubl.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::dsubl;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_DSUBL_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index e21fe8ef0ab93..d55f0887c34fd 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -591,6 +591,23 @@ add_header_library(
   DEPENDS
     libc.src.__support.FPUtil.generic.sqrt
 )
+add_header_library(
+  dsubf128
+  HDRS
+    dsubf128.h
+  DEPENDS
+    libc.include.llvm-libc-types.float128
+    libc.src.__support.FPUtil.generic.add_sub
+    libc.src.__support.macros.config
+)
+add_header_library(
+  dsubl
+  HDRS
+    dsubl.h
+  DEPENDS
+    libc.src.__support.FPUtil.generic.add_sub
+    libc.src.__support.macros.config
+)
 
 add_header_library(
   exp10m1f
diff --git a/libc/src/__support/math/dsubf128.h b/libc/src/__support/math/dsubf128.h
new file mode 100644
index 0000000000000..ed22808d4efe5
--- /dev/null
+++ b/libc/src/__support/math/dsubf128.h
@@ -0,0 +1,32 @@
+//===-- Implementation header for dsubf128 ----------------------*- 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_DSUBF128_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_DSUBF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/FPUtil/generic/add_sub.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE double dsubf128(float128 x, float128 y) {
+  return fputil::generic::sub<double>(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_DSUBF128_H
diff --git a/libc/src/__support/math/dsubl.h b/libc/src/__support/math/dsubl.h
new file mode 100644
index 0000000000000..ef46ab3444ee7
--- /dev/null
+++ b/libc/src/__support/math/dsubl.h
@@ -0,0 +1,26 @@
+//===-- Implementation header for dsubl -------------------------*- 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_DSUBL_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_DSUBL_H
+
+#include "src/__support/FPUtil/generic/add_sub.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE double dsubl(long double x, long double y) {
+  return fputil::generic::sub<double>(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_DSUBL_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 57a29665318a3..a6d42145e0dda 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -285,8 +285,7 @@ add_entrypoint_object(
   HDRS
     ../dsubf128.h
   DEPENDS
-    libc.src.__support.FPUtil.generic.add_sub
-    libc.src.__support.macros.properties.types
+    libc.src.__support.math.dsubf128
 )
 
 add_entrypoint_object(
@@ -296,7 +295,7 @@ add_entrypoint_object(
   HDRS
     ../dsubl.h
   DEPENDS
-    libc.src.__support.FPUtil.generic.add_sub
+    libc.src.__support.math.dsubl
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/dsubf128.cpp b/libc/src/math/generic/dsubf128.cpp
index 1b2f1214b3a6f..81510246b8b34 100644
--- a/libc/src/math/generic/dsubf128.cpp
+++ b/libc/src/math/generic/dsubf128.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/dsubf128.h"
-#include "src/__support/FPUtil/generic/add_sub.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/dsubf128.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(double, dsubf128, (float128 x, float128 y)) {
-  return fputil::generic::sub<double>(x, y);
+  return math::dsubf128(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/dsubl.cpp b/libc/src/math/generic/dsubl.cpp
index 8b567d0869d2a..27b24bb8c0c7d 100644
--- a/libc/src/math/generic/dsubl.cpp
+++ b/libc/src/math/generic/dsubl.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/dsubl.h"
-#include "src/__support/FPUtil/generic/add_sub.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/dsubl.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(double, dsubl, (long double x, long double y)) {
-  return fputil::generic::sub<double>(x, y);
+  return math::dsubl(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 53ad309af4f30..4143b2ffcd77c 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -51,6 +51,8 @@ add_fp_unittest(
     libc.src.__support.math.dfmaf128
     libc.src.__support.math.dfmal
     libc.src.__support.math.dsqrtl
+    libc.src.__support.math.dsubf128
+    libc.src.__support.math.dsubl
     libc.src.__support.math.exp10m1f
     libc.src.__support.math.exp10m1f16
     libc.src.__support.math.erff
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 7f2f39ac7a285..d013acc8401fb 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -203,6 +203,8 @@ TEST(LlvmLibcSharedMathTest, AllLongDouble) {
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalizel(&canonicalizel_cx,
                                                      &canonicalizel_x));
   EXPECT_FP_EQ(0x0p+0L, canonicalizel_cx);
+
+  EXPECT_FP_EQ(0x0p+0L, LIBC_NAMESPACE::shared::dsubl(0.0L, 0.0L));
 }
 
 #ifdef LIBC_TYPES_HAS_FLOAT128
@@ -238,6 +240,9 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalizef128(&canonicalizef128_cx,
                                                         &canonicalizef128_x));
   EXPECT_FP_EQ(float128(0.0), canonicalizef128_cx);
+
+  EXPECT_FP_EQ(float128(0.0),
+               LIBC_NAMESPACE::shared::dsubf128(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 b90688af7a1d2..17ed97cb225f9 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2896,6 +2896,25 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_dsubf128",
+    hdrs = ["src/__support/math/dsubf128.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_macros_config",
+        ":llvm_libc_types_float128",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_dsubl",
+    hdrs = ["src/__support/math/dsubl.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_macros_config",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_exp10m1f",
     hdrs = ["src/__support/math/exp10m1f.h"],
@@ -4765,9 +4784,19 @@ libc_math_function(
     additional_deps = [":__support_fputil_sqrt"],
 )
 
-libc_math_function(name = "dsubl")
+libc_math_function(
+    name = "dsubl",
+    additional_deps = [
+        ":__support_math_dsubl",
+    ],
+)
 
-libc_math_function(name = "dsubf128")
+libc_math_function(
+    name = "dsubf128",
+    additional_deps = [
+        ":__support_math_dsubf128",
+    ],
+)
 
 libc_math_function(
     name = "erff",

@hulxv hulxv force-pushed the refactor/header-only/dsub branch from 59b5558 to 71b791a Compare March 14, 2026 00:15
@hulxv hulxv requested a review from bassiounix March 17, 2026 23:25
Comment thread libc/test/shared/shared_math_test.cpp Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return type should be double not long double

Refactored functions:
  - dsubf128
  - dsubl
@hulxv hulxv requested a review from bassiounix April 13, 2026 14:47
@hulxv hulxv force-pushed the refactor/header-only/dsub branch from 71b791a to 691188b Compare April 13, 2026 14:47
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 13, 2026

🐧 Linux x64 Test Results

✅ The build succeeded and no tests ran. This is expected in some build configurations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bazel "Peripheral" support tier build system: utils/bazel libc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[libc][math] Refactor dsub Math Functions to Header Only

3 participants