[libc-commits] [libc] [llvm] [libc][math] Refactor dsub family to header-only (PR #182160)

Muhammad Bassiouni via libc-commits libc-commits at lists.llvm.org
Fri Apr 24 14:50:40 PDT 2026


https://github.com/bassiounix updated https://github.com/llvm/llvm-project/pull/182160

>From 691188b2987e96fac643d4143f217b1dc4f95588 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Wed, 18 Feb 2026 23:56:34 +0200
Subject: [PATCH 1/2] [libc][math] Refactor dsub family to header-only

Refactored functions:
  - dsubf128
  - dsubl
---
 libc/shared/math.h                            |  2 ++
 libc/shared/math/dsubf128.h                   | 29 +++++++++++++++++
 libc/shared/math/dsubl.h                      | 23 ++++++++++++++
 libc/src/__support/math/CMakeLists.txt        | 19 ++++++++++++
 libc/src/__support/math/dsubf128.h            | 31 +++++++++++++++++++
 libc/src/__support/math/dsubl.h               | 25 +++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  5 ++-
 libc/src/math/generic/dsubf128.cpp            |  6 ++--
 libc/src/math/generic/dsubl.cpp               |  6 ++--
 libc/test/shared/CMakeLists.txt               |  2 ++
 libc/test/shared/shared_math_test.cpp         |  3 ++
 .../llvm-project-overlay/libc/BUILD.bazel     | 29 ++++++++++++++++-
 12 files changed, 168 insertions(+), 12 deletions(-)
 create mode 100644 libc/shared/math/dsubf128.h
 create mode 100644 libc/shared/math/dsubl.h
 create mode 100644 libc/src/__support/math/dsubf128.h
 create mode 100644 libc/src/__support/math/dsubl.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 275b89db3179a..6e027f29c4cff 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -84,6 +84,8 @@
 #include "math/dfmaf128.h"
 #include "math/dfmal.h"
 #include "math/dsqrtl.h"
+#include "math/dsubf128.h"
+#include "math/dsubl.h"
 #include "math/erfcf16.h"
 #include "math/erff.h"
 #include "math/erff16.h"
diff --git a/libc/shared/math/dsubf128.h b/libc/shared/math/dsubf128.h
new file mode 100644
index 0000000000000..b08b941fb80a2
--- /dev/null
+++ b/libc/shared/math/dsubf128.h
@@ -0,0 +1,29 @@
+//===-- 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 "shared/libc_common.h"
+#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..6eb95b692d690
--- /dev/null
+++ b/libc/shared/math/dsubl.h
@@ -0,0 +1,23 @@
+//===-- 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 "shared/libc_common.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 987ecab8c57ef..760d9aa69a6c2 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -967,6 +967,25 @@ add_header_library(
     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
   HDRS
diff --git a/libc/src/__support/math/dsubf128.h b/libc/src/__support/math/dsubf128.h
new file mode 100644
index 0000000000000..e49c64a88b296
--- /dev/null
+++ b/libc/src/__support/math/dsubf128.h
@@ -0,0 +1,31 @@
+//===-- 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..f56e253af9aa1
--- /dev/null
+++ b/libc/src/__support/math/dsubl.h
@@ -0,0 +1,25 @@
+//===-- 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 5d265b89391e6..a96632fa7e21b 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -268,8 +268,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(
@@ -279,7 +278,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 9c4b7edb377e2..c15cbce2be56b 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -81,6 +81,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.erfcf16
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 4c58c834960ed..89f4d2955dd78 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -315,6 +315,7 @@ TEST(LlvmLibcSharedMathTest, AllLongDouble) {
 
   EXPECT_FP_EQ(0.0L, LIBC_NAMESPACE::shared::ceill(0.0L));
   EXPECT_FP_EQ(0.0L, LIBC_NAMESPACE::shared::daddl(0.0L, 0.0L));
+  EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::dsubl(0.0L, 0.0L));
   EXPECT_FP_EQ(0.0L, LIBC_NAMESPACE::shared::faddl(0.0L, 0.0L));
   EXPECT_FP_EQ(0.0L, LIBC_NAMESPACE::shared::fdiml(0.0L, 0.0L));
   EXPECT_FP_EQ(0.0L, LIBC_NAMESPACE::shared::floorl(0.0L));
@@ -396,6 +397,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
   EXPECT_FP_EQ(float128(0.0), LIBC_NAMESPACE::shared::ceilf128(float128(0.0)));
   EXPECT_FP_EQ(float128(0.0),
                LIBC_NAMESPACE::shared::daddf128(float128(0.0), float128(0.0)));
+  EXPECT_FP_EQ(0.0,
+               LIBC_NAMESPACE::shared::dsubf128(float128(0.0), float128(0.0)));
   EXPECT_FP_EQ(float128(0.0),
                LIBC_NAMESPACE::shared::faddf128(float128(0.0), float128(0.0)));
   EXPECT_FP_EQ(float128(0.0), LIBC_NAMESPACE::shared::floorf128(float128(0.0)));
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index d97d65e4ee43a..ffdc7111d0e13 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3982,6 +3982,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"],
@@ -7049,10 +7068,18 @@ libc_math_function(
     ],
 )
 
-libc_math_function(name = "dsubl")
+libc_math_function(
+    name = "dsubl",
+    additional_deps = [
+        ":__support_math_dsubl",
+    ],
+)
 
 libc_math_function(
     name = "dsubf128",
+    additional_deps = [
+        ":__support_math_dsubf128",
+    ],
 )
 
 libc_math_function(

>From d95eb8e95a94f71af9d6bef4767a63844c9ce4ec Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Mon, 13 Apr 2026 18:12:08 +0200
Subject: [PATCH 2/2] constexpr

---
 libc/src/__support/FPUtil/rounding_mode.h       | 4 ++--
 libc/src/__support/math/dsubf128.h              | 2 +-
 libc/src/__support/math/dsubl.h                 | 2 +-
 libc/test/shared/CMakeLists.txt                 | 2 ++
 libc/test/shared/shared_math_constexpr_test.cpp | 4 ++++
 5 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/libc/src/__support/FPUtil/rounding_mode.h b/libc/src/__support/FPUtil/rounding_mode.h
index 92061ea13e203..8e56baf422540 100644
--- a/libc/src/__support/FPUtil/rounding_mode.h
+++ b/libc/src/__support/FPUtil/rounding_mode.h
@@ -66,8 +66,8 @@ LIBC_INLINE bool fenv_is_round_to_zero() {
 }
 
 // Quick free standing get rounding mode based on the above observations.
-LIBC_INLINE int quick_get_round() {
-  static volatile float x = 0x1.0p-24f;
+LIBC_INLINE constexpr int quick_get_round() {
+  constexpr float x = 0x1.0p-24f;
   float y = x;
   float z = (0x1.000002p0f + y) + (-1.0f - y);
 
diff --git a/libc/src/__support/math/dsubf128.h b/libc/src/__support/math/dsubf128.h
index e49c64a88b296..db47024e702d0 100644
--- a/libc/src/__support/math/dsubf128.h
+++ b/libc/src/__support/math/dsubf128.h
@@ -19,7 +19,7 @@
 namespace LIBC_NAMESPACE_DECL {
 namespace math {
 
-LIBC_INLINE double dsubf128(float128 x, float128 y) {
+LIBC_INLINE constexpr double dsubf128(float128 x, float128 y) {
   return fputil::generic::sub<double>(x, y);
 }
 
diff --git a/libc/src/__support/math/dsubl.h b/libc/src/__support/math/dsubl.h
index f56e253af9aa1..a82a05214f620 100644
--- a/libc/src/__support/math/dsubl.h
+++ b/libc/src/__support/math/dsubl.h
@@ -15,7 +15,7 @@
 namespace LIBC_NAMESPACE_DECL {
 namespace math {
 
-LIBC_INLINE double dsubl(long double x, long double y) {
+LIBC_INLINE constexpr double dsubl(long double x, long double y) {
   return fputil::generic::sub<double>(x, y);
 }
 
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index c15cbce2be56b..f0feee9d78dc2 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -279,6 +279,8 @@ add_fp_unittest(
     libc.src.__support.math.ceilf128
     libc.src.__support.math.ceilf16
     libc.src.__support.math.ceill
+    libc.src.__support.math.dsubf128
+    libc.src.__support.math.dsubl
     libc.src.__support.math.log
 )
 
diff --git a/libc/test/shared/shared_math_constexpr_test.cpp b/libc/test/shared/shared_math_constexpr_test.cpp
index 53d416de30694..14991d0bcd235 100644
--- a/libc/test/shared/shared_math_constexpr_test.cpp
+++ b/libc/test/shared/shared_math_constexpr_test.cpp
@@ -42,6 +42,8 @@ static_assert(0.0f16 == LIBC_NAMESPACE::shared::ceilf16(0.0f16));
 #if 0 // Temporarily disable long double tests
 
 static_assert(0.0L == LIBC_NAMESPACE::shared::ceill(0.0L));
+static_assert(0.0 ==
+              LIBC_NAMESPACE::shared::dsubl(0.0L, 0.0L));
 
 #endif
 
@@ -52,6 +54,8 @@ static_assert(0.0L == LIBC_NAMESPACE::shared::ceill(0.0L));
 #ifdef LIBC_TYPES_HAS_FLOAT128
 
 static_assert(float128(0.0) == LIBC_NAMESPACE::shared::ceilf128(float128(0.0)));
+static_assert(0.0 ==
+              LIBC_NAMESPACE::shared::dsubf128(float128(0.0), float128(0.0)));
 
 #endif // LIBC_TYPES_HAS_FLOAT128
 



More information about the libc-commits mailing list