[libc-commits] [libc] [llvm] [libc][math] Refactor ddiv family to header-only (PR #182149)
via libc-commits
libc-commits at lists.llvm.org
Mon Apr 13 07:39:12 PDT 2026
https://github.com/hulxv updated https://github.com/llvm/llvm-project/pull/182149
>From d527d6bd142f190017991f47096652bb24c82aa1 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Wed, 18 Feb 2026 23:16:50 +0200
Subject: [PATCH 1/5] [libc][math] Refactor ddiv family to header-only
Refactored functions:
- ddivf128
- ddivl
---
libc/shared/math.h | 2 ++
libc/shared/math/ddivf128.h | 28 ++++++++++++++++
libc/shared/math/ddivl.h | 22 +++++++++++++
libc/src/__support/math/CMakeLists.txt | 17 ++++++++++
libc/src/__support/math/ddivf128.h | 32 +++++++++++++++++++
libc/src/__support/math/ddivl.h | 26 +++++++++++++++
libc/src/math/generic/CMakeLists.txt | 5 ++-
libc/src/math/generic/ddivf128.cpp | 6 ++--
libc/src/math/generic/ddivl.cpp | 6 ++--
libc/test/shared/CMakeLists.txt | 2 ++
libc/test/shared/shared_math_test.cpp | 2 ++
.../llvm-project-overlay/libc/BUILD.bazel | 29 ++++++++++++++++-
12 files changed, 165 insertions(+), 12 deletions(-)
create mode 100644 libc/shared/math/ddivf128.h
create mode 100644 libc/shared/math/ddivl.h
create mode 100644 libc/src/__support/math/ddivf128.h
create mode 100644 libc/src/__support/math/ddivl.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 275b89db3179a..d71c257d00eaa 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -81,6 +81,8 @@
#include "math/cospif16.h"
#include "math/daddf128.h"
#include "math/daddl.h"
+#include "math/ddivf128.h"
+#include "math/ddivl.h"
#include "math/dfmaf128.h"
#include "math/dfmal.h"
#include "math/dsqrtl.h"
diff --git a/libc/shared/math/ddivf128.h b/libc/shared/math/ddivf128.h
new file mode 100644
index 0000000000000..8766382b72484
--- /dev/null
+++ b/libc/shared/math/ddivf128.h
@@ -0,0 +1,28 @@
+//===-- Shared ddivf128 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_DDIVF128_H
+#define LLVM_LIBC_SHARED_MATH_DDIVF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/math/ddivf128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::ddivf128;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_MATH_DDIVF128_H
diff --git a/libc/shared/math/ddivl.h b/libc/shared/math/ddivl.h
new file mode 100644
index 0000000000000..772adf9023218
--- /dev/null
+++ b/libc/shared/math/ddivl.h
@@ -0,0 +1,22 @@
+//===-- Shared ddivl 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_DDIVL_H
+#define LLVM_LIBC_SHARED_MATH_DDIVL_H
+
+#include "src/__support/math/ddivl.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::ddivl;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_DDIVL_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 987ecab8c57ef..843d263523712 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -939,6 +939,23 @@ add_header_library(
libc.src.__support.FPUtil.multiply_add
libc.src.__support.macros.optimization
)
+add_header_library(
+ ddivf128
+ HDRS
+ ddivf128.h
+ DEPENDS
+ libc.include.llvm-libc-types.float128
+ libc.src.__support.FPUtil.generic.div
+ libc.src.__support.macros.config
+)
+add_header_library(
+ ddivl
+ HDRS
+ ddivl.h
+ DEPENDS
+ libc.src.__support.FPUtil.generic.div
+ libc.src.__support.macros.config
+)
add_header_library(
daddf128
diff --git a/libc/src/__support/math/ddivf128.h b/libc/src/__support/math/ddivf128.h
new file mode 100644
index 0000000000000..a6b6871d5431c
--- /dev/null
+++ b/libc/src/__support/math/ddivf128.h
@@ -0,0 +1,32 @@
+//===-- Implementation header for ddivf128 ----------------------*- 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_DDIVF128_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_DDIVF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/FPUtil/generic/div.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE constexpr double ddivf128(float128 x, float128 y) {
+ return fputil::generic::div<double>(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_DDIVF128_H
diff --git a/libc/src/__support/math/ddivl.h b/libc/src/__support/math/ddivl.h
new file mode 100644
index 0000000000000..aeb34442923c3
--- /dev/null
+++ b/libc/src/__support/math/ddivl.h
@@ -0,0 +1,26 @@
+//===-- Implementation header for ddivl -------------------------*- 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_DDIVL_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_DDIVL_H
+
+#include "src/__support/FPUtil/generic/div.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE constexpr double ddivl(long double x, long double y) {
+ return fputil::generic::div<double>(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_DDIVL_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 5d265b89391e6..1b991be9df0de 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -205,7 +205,7 @@ add_entrypoint_object(
HDRS
../ddivl.h
DEPENDS
- libc.src.__support.FPUtil.generic.div
+ libc.src.__support.math.ddivl
)
add_entrypoint_object(
@@ -215,8 +215,7 @@ add_entrypoint_object(
HDRS
../ddivf128.h
DEPENDS
- libc.src.__support.macros.properties.types
- libc.src.__support.FPUtil.generic.div
+ libc.src.__support.math.ddivf128
)
add_entrypoint_object(
diff --git a/libc/src/math/generic/ddivf128.cpp b/libc/src/math/generic/ddivf128.cpp
index 1ce4fd66b42cb..e7df2fa8be08a 100644
--- a/libc/src/math/generic/ddivf128.cpp
+++ b/libc/src/math/generic/ddivf128.cpp
@@ -7,14 +7,12 @@
//===----------------------------------------------------------------------===//
#include "src/math/ddivf128.h"
-#include "src/__support/FPUtil/generic/div.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/ddivf128.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(double, ddivf128, (float128 x, float128 y)) {
- return fputil::generic::div<double>(x, y);
+ return math::ddivf128(x, y);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/ddivl.cpp b/libc/src/math/generic/ddivl.cpp
index 18fc44d6f1648..5fa8e997bb0e0 100644
--- a/libc/src/math/generic/ddivl.cpp
+++ b/libc/src/math/generic/ddivl.cpp
@@ -7,14 +7,12 @@
//===----------------------------------------------------------------------===//
#include "src/math/ddivl.h"
-#include "src/__support/FPUtil/generic/div.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/ddivl.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(double, ddivl, (long double x, long double y)) {
- return fputil::generic::div<double>(x, y);
+ return math::ddivl(x, y);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 9c4b7edb377e2..a0c7e199840b9 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -78,6 +78,8 @@ add_fp_unittest(
libc.src.__support.math.cospif16
libc.src.__support.math.daddf128
libc.src.__support.math.daddl
+ libc.src.__support.math.ddivf128
+ libc.src.__support.math.ddivl
libc.src.__support.math.dfmaf128
libc.src.__support.math.dfmal
libc.src.__support.math.dsqrtl
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 4c58c834960ed..995a547a7594b 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(1.0L, LIBC_NAMESPACE::shared::ddivl(1.0L, 1.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,7 @@ 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(float128(1.0), LIBC_NAMESPACE::shared::ddivf128(1.0, 1.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..b99ff9765fa8f 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3974,6 +3974,25 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_ddivf128",
+ hdrs = ["src/__support/math/ddivf128.h"],
+ deps = [
+ ":__support_fputil_basic_operations",
+ ":__support_macros_config",
+ ":llvm_libc_types_float128",
+ ],
+)
+
+libc_support_library(
+ name = "__support_math_ddivl",
+ hdrs = ["src/__support/math/ddivl.h"],
+ deps = [
+ ":__support_fputil_basic_operations",
+ ":__support_macros_config",
+ ],
+)
+
libc_support_library(
name = "__support_math_dsqrtl",
hdrs = ["src/__support/math/dsqrtl.h"],
@@ -7012,10 +7031,18 @@ libc_math_function(
],
)
-libc_math_function(name = "ddivl")
+libc_math_function(
+ name = "ddivl",
+ additional_deps = [
+ ":__support_math_ddivl",
+ ],
+)
libc_math_function(
name = "ddivf128",
+ additional_deps = [
+ ":__support_math_ddivf128",
+ ],
)
libc_math_function(
>From 22ae1e2b09f36cfe711571441d1bf49cdc2cc18f Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Sat, 14 Mar 2026 02:09:52 +0200
Subject: [PATCH 2/5] fix: format
---
libc/src/__support/math/CMakeLists.txt | 2 ++
libc/src/__support/math/ddivf128.h | 1 -
libc/src/__support/math/ddivl.h | 1 -
3 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 843d263523712..d3586c1eaa8b4 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -939,6 +939,7 @@ add_header_library(
libc.src.__support.FPUtil.multiply_add
libc.src.__support.macros.optimization
)
+
add_header_library(
ddivf128
HDRS
@@ -948,6 +949,7 @@ add_header_library(
libc.src.__support.FPUtil.generic.div
libc.src.__support.macros.config
)
+
add_header_library(
ddivl
HDRS
diff --git a/libc/src/__support/math/ddivf128.h b/libc/src/__support/math/ddivf128.h
index a6b6871d5431c..359b77d740605 100644
--- a/libc/src/__support/math/ddivf128.h
+++ b/libc/src/__support/math/ddivf128.h
@@ -17,7 +17,6 @@
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
-
namespace math {
LIBC_INLINE constexpr double ddivf128(float128 x, float128 y) {
diff --git a/libc/src/__support/math/ddivl.h b/libc/src/__support/math/ddivl.h
index aeb34442923c3..3b7ce586ee636 100644
--- a/libc/src/__support/math/ddivl.h
+++ b/libc/src/__support/math/ddivl.h
@@ -13,7 +13,6 @@
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
-
namespace math {
LIBC_INLINE constexpr double ddivl(long double x, long double y) {
>From fc4fb1f4dadcabeb8e93396ae1c678a58126437f Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Sat, 14 Mar 2026 02:15:55 +0200
Subject: [PATCH 3/5] missed libc_common
---
libc/shared/math/ddivf128.h | 1 +
libc/shared/math/ddivl.h | 1 +
2 files changed, 2 insertions(+)
diff --git a/libc/shared/math/ddivf128.h b/libc/shared/math/ddivf128.h
index 8766382b72484..01c59481d8a96 100644
--- a/libc/shared/math/ddivf128.h
+++ b/libc/shared/math/ddivf128.h
@@ -13,6 +13,7 @@
#ifdef LIBC_TYPES_HAS_FLOAT128
+#include "shared/libc_common.h"
#include "src/__support/math/ddivf128.h"
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/shared/math/ddivl.h b/libc/shared/math/ddivl.h
index 772adf9023218..b1e2d341a3069 100644
--- a/libc/shared/math/ddivl.h
+++ b/libc/shared/math/ddivl.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SHARED_MATH_DDIVL_H
#define LLVM_LIBC_SHARED_MATH_DDIVL_H
+#include "shared/libc_common.h"
#include "src/__support/math/ddivl.h"
namespace LIBC_NAMESPACE_DECL {
>From dbabdd73066e52b601298909a796f6a4af23ae48 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Wed, 18 Mar 2026 01:22:19 +0200
Subject: [PATCH 4/5] remove constexpr
---
libc/src/__support/math/ddivf128.h | 2 +-
libc/src/__support/math/ddivl.h | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/libc/src/__support/math/ddivf128.h b/libc/src/__support/math/ddivf128.h
index 359b77d740605..5f318fe8ebcbb 100644
--- a/libc/src/__support/math/ddivf128.h
+++ b/libc/src/__support/math/ddivf128.h
@@ -19,7 +19,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE constexpr double ddivf128(float128 x, float128 y) {
+LIBC_INLINE double ddivf128(float128 x, float128 y) {
return fputil::generic::div<double>(x, y);
}
diff --git a/libc/src/__support/math/ddivl.h b/libc/src/__support/math/ddivl.h
index 3b7ce586ee636..c62abd3070da5 100644
--- a/libc/src/__support/math/ddivl.h
+++ b/libc/src/__support/math/ddivl.h
@@ -15,7 +15,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE constexpr double ddivl(long double x, long double y) {
+LIBC_INLINE double ddivl(long double x, long double y) {
return fputil::generic::div<double>(x, y);
}
>From cd0259e41c0e994abc5190ef395f25486ffca3fd Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Mon, 13 Apr 2026 16:38:51 +0200
Subject: [PATCH 5/5] correct return type
---
libc/test/shared/shared_math_test.cpp | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 995a547a7594b..f5cb68c2ab477 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -315,7 +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(1.0L, LIBC_NAMESPACE::shared::ddivl(1.0L, 1.0L));
+ EXPECT_FP_EQ(1.0, LIBC_NAMESPACE::shared::ddivl(1.0L, 1.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));
@@ -397,7 +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(float128(1.0), LIBC_NAMESPACE::shared::ddivf128(1.0, 1.0));
+ EXPECT_FP_EQ(1.0,
+ LIBC_NAMESPACE::shared::ddivf128(float128(1.0), float128(1.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)));
More information about the libc-commits
mailing list