[libc-commits] [libc] [llvm] [libc][math] Refactor dmul family to header-only (PR #182151)
via libc-commits
libc-commits at lists.llvm.org
Wed Apr 22 15:49:10 PDT 2026
https://github.com/hulxv updated https://github.com/llvm/llvm-project/pull/182151
>From d58d805028417bc6b23885b8af0259d3a647bb8a Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Wed, 18 Feb 2026 23:26:46 +0200
Subject: [PATCH 1/3] [libc][math] Refactor dmul family to header-only
Refactored functions:
- dmulf128
- dmull
---
libc/shared/math.h | 2 ++
libc/shared/math/dmulf128.h | 28 ++++++++++++++++
libc/shared/math/dmull.h | 22 +++++++++++++
libc/src/__support/math/CMakeLists.txt | 17 ++++++++++
libc/src/__support/math/dmulf128.h | 32 +++++++++++++++++++
libc/src/__support/math/dmull.h | 26 +++++++++++++++
libc/src/math/generic/CMakeLists.txt | 5 ++-
libc/src/math/generic/dmulf128.cpp | 6 ++--
libc/src/math/generic/dmull.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, 166 insertions(+), 12 deletions(-)
create mode 100644 libc/shared/math/dmulf128.h
create mode 100644 libc/shared/math/dmull.h
create mode 100644 libc/src/__support/math/dmulf128.h
create mode 100644 libc/src/__support/math/dmull.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 275b89db3179a..bdfe97389881e 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -83,6 +83,8 @@
#include "math/daddl.h"
#include "math/dfmaf128.h"
#include "math/dfmal.h"
+#include "math/dmulf128.h"
+#include "math/dmull.h"
#include "math/dsqrtl.h"
#include "math/erfcf16.h"
#include "math/erff.h"
diff --git a/libc/shared/math/dmulf128.h b/libc/shared/math/dmulf128.h
new file mode 100644
index 0000000000000..7a9f88cca6600
--- /dev/null
+++ b/libc/shared/math/dmulf128.h
@@ -0,0 +1,28 @@
+//===-- Shared dmulf128 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_DMULF128_H
+#define LLVM_LIBC_SHARED_MATH_DMULF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/math/dmulf128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::dmulf128;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_MATH_DMULF128_H
diff --git a/libc/shared/math/dmull.h b/libc/shared/math/dmull.h
new file mode 100644
index 0000000000000..494f57074bdb4
--- /dev/null
+++ b/libc/shared/math/dmull.h
@@ -0,0 +1,22 @@
+//===-- Shared dmull 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_DMULL_H
+#define LLVM_LIBC_SHARED_MATH_DMULL_H
+
+#include "src/__support/math/dmull.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::dmull;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_DMULL_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 987ecab8c57ef..16b87d0328e17 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(
+ dmulf128
+ HDRS
+ dmulf128.h
+ DEPENDS
+ libc.include.llvm-libc-types.float128
+ libc.src.__support.FPUtil.generic.mul
+ libc.src.__support.macros.config
+)
+add_header_library(
+ dmull
+ HDRS
+ dmull.h
+ DEPENDS
+ libc.src.__support.FPUtil.generic.mul
+ libc.src.__support.macros.config
+)
add_header_library(
daddf128
diff --git a/libc/src/__support/math/dmulf128.h b/libc/src/__support/math/dmulf128.h
new file mode 100644
index 0000000000000..519b12b4bf93e
--- /dev/null
+++ b/libc/src/__support/math/dmulf128.h
@@ -0,0 +1,32 @@
+//===-- Implementation header for dmulf128 ----------------------*- 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_DMULF128_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_DMULF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/FPUtil/generic/mul.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE double dmulf128(float128 x, float128 y) {
+ return fputil::generic::mul<double>(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_DMULF128_H
diff --git a/libc/src/__support/math/dmull.h b/libc/src/__support/math/dmull.h
new file mode 100644
index 0000000000000..dacce3ad492e6
--- /dev/null
+++ b/libc/src/__support/math/dmull.h
@@ -0,0 +1,26 @@
+//===-- Implementation header for dmull -------------------------*- 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_DMULL_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_DMULL_H
+
+#include "src/__support/FPUtil/generic/mul.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE double dmull(long double x, long double y) {
+ return fputil::generic::mul<double>(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_DMULL_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 5d265b89391e6..e6ad1fc02eee2 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5035,7 +5035,7 @@ add_entrypoint_object(
HDRS
../dmull.h
DEPENDS
- libc.src.__support.FPUtil.generic.mul
+ libc.src.__support.math.dmull
)
add_entrypoint_object(
@@ -5045,8 +5045,7 @@ add_entrypoint_object(
HDRS
../dmulf128.h
DEPENDS
- libc.src.__support.macros.properties.types
- libc.src.__support.FPUtil.generic.mul
+ libc.src.__support.math.dmulf128
)
add_entrypoint_object(
diff --git a/libc/src/math/generic/dmulf128.cpp b/libc/src/math/generic/dmulf128.cpp
index 7e6ef95362c09..0e05d8ffaafd9 100644
--- a/libc/src/math/generic/dmulf128.cpp
+++ b/libc/src/math/generic/dmulf128.cpp
@@ -7,14 +7,12 @@
//===----------------------------------------------------------------------===//
#include "src/math/dmulf128.h"
-#include "src/__support/FPUtil/generic/mul.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/dmulf128.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(double, dmulf128, (float128 x, float128 y)) {
- return fputil::generic::mul<double>(x, y);
+ return math::dmulf128(x, y);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/dmull.cpp b/libc/src/math/generic/dmull.cpp
index 428caa84a9977..36be06f7585a2 100644
--- a/libc/src/math/generic/dmull.cpp
+++ b/libc/src/math/generic/dmull.cpp
@@ -7,14 +7,12 @@
//===----------------------------------------------------------------------===//
#include "src/math/dmull.h"
-#include "src/__support/FPUtil/generic/mul.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/dmull.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(double, dmull, (long double x, long double y)) {
- return fputil::generic::mul<double>(x, y);
+ return math::dmull(x, y);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 9c4b7edb377e2..7045971e41689 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -80,6 +80,8 @@ add_fp_unittest(
libc.src.__support.math.daddl
libc.src.__support.math.dfmaf128
libc.src.__support.math.dfmal
+ libc.src.__support.math.dmulf128
+ libc.src.__support.math.dmull
libc.src.__support.math.dsqrtl
libc.src.__support.math.exp10m1f
libc.src.__support.math.exp10m1f16
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 4c58c834960ed..3f43b5cf12d89 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::dmull(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::dmulf128(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..67489d420e606 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_dmulf128",
+ hdrs = ["src/__support/math/dmulf128.h"],
+ deps = [
+ ":__support_fputil_basic_operations",
+ ":__support_macros_config",
+ ":llvm_libc_types_float128",
+ ],
+)
+
+libc_support_library(
+ name = "__support_math_dmull",
+ hdrs = ["src/__support/math/dmull.h"],
+ deps = [
+ ":__support_fputil_basic_operations",
+ ":__support_macros_config",
+ ],
+)
+
libc_support_library(
name = "__support_math_dsqrtl",
hdrs = ["src/__support/math/dsqrtl.h"],
@@ -7031,10 +7050,18 @@ libc_math_function(
],
)
-libc_math_function(name = "dmull")
+libc_math_function(
+ name = "dmull",
+ additional_deps = [
+ ":__support_math_dmull",
+ ],
+)
libc_math_function(
name = "dmulf128",
+ additional_deps = [
+ ":__support_math_dmulf128",
+ ],
)
libc_math_function(
>From b4cc8e4a14a4d4f369e025aad6905bc30f2d8ecb Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Thu, 12 Mar 2026 03:41:49 +0200
Subject: [PATCH 2/3] comments
---
libc/shared/math/dmulf128.h | 1 +
libc/shared/math/dmull.h | 1 +
libc/src/__support/math/CMakeLists.txt | 2 ++
libc/src/__support/math/dmulf128.h | 1 -
libc/src/__support/math/dmull.h | 1 -
5 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/libc/shared/math/dmulf128.h b/libc/shared/math/dmulf128.h
index 7a9f88cca6600..f255117689daf 100644
--- a/libc/shared/math/dmulf128.h
+++ b/libc/shared/math/dmulf128.h
@@ -13,6 +13,7 @@
#ifdef LIBC_TYPES_HAS_FLOAT128
+#include "shared/libc_common.h"
#include "src/__support/math/dmulf128.h"
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/shared/math/dmull.h b/libc/shared/math/dmull.h
index 494f57074bdb4..a1320ca3339f8 100644
--- a/libc/shared/math/dmull.h
+++ b/libc/shared/math/dmull.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SHARED_MATH_DMULL_H
#define LLVM_LIBC_SHARED_MATH_DMULL_H
+#include "shared/libc_common.h"
#include "src/__support/math/dmull.h"
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 16b87d0328e17..c7f9aaec4d637 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(
dmulf128
HDRS
@@ -948,6 +949,7 @@ add_header_library(
libc.src.__support.FPUtil.generic.mul
libc.src.__support.macros.config
)
+
add_header_library(
dmull
HDRS
diff --git a/libc/src/__support/math/dmulf128.h b/libc/src/__support/math/dmulf128.h
index 519b12b4bf93e..efb5e0de2ab4a 100644
--- a/libc/src/__support/math/dmulf128.h
+++ b/libc/src/__support/math/dmulf128.h
@@ -17,7 +17,6 @@
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
-
namespace math {
LIBC_INLINE double dmulf128(float128 x, float128 y) {
diff --git a/libc/src/__support/math/dmull.h b/libc/src/__support/math/dmull.h
index dacce3ad492e6..88cc47d0acf9b 100644
--- a/libc/src/__support/math/dmull.h
+++ b/libc/src/__support/math/dmull.h
@@ -13,7 +13,6 @@
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
-
namespace math {
LIBC_INLINE double dmull(long double x, long double y) {
>From a27ce55d417134fb1fba0b22585f3dbaf66571ae Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Wed, 22 Apr 2026 22:43:17 +0200
Subject: [PATCH 3/3] make constexpr
---
libc/src/__support/math/dmulf128.h | 2 +-
libc/src/__support/math/dmull.h | 2 +-
libc/test/shared/CMakeLists.txt | 2 ++
libc/test/shared/shared_math_constexpr_test.cpp | 2 ++
4 files changed, 6 insertions(+), 2 deletions(-)
diff --git a/libc/src/__support/math/dmulf128.h b/libc/src/__support/math/dmulf128.h
index efb5e0de2ab4a..a76f2f1925599 100644
--- a/libc/src/__support/math/dmulf128.h
+++ b/libc/src/__support/math/dmulf128.h
@@ -19,7 +19,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE double dmulf128(float128 x, float128 y) {
+LIBC_INLINE constexpr double dmulf128(float128 x, float128 y) {
return fputil::generic::mul<double>(x, y);
}
diff --git a/libc/src/__support/math/dmull.h b/libc/src/__support/math/dmull.h
index 88cc47d0acf9b..bb9529cfe0241 100644
--- a/libc/src/__support/math/dmull.h
+++ b/libc/src/__support/math/dmull.h
@@ -15,7 +15,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE double dmull(long double x, long double y) {
+LIBC_INLINE constexpr double dmull(long double x, long double y) {
return fputil::generic::mul<double>(x, y);
}
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 7045971e41689..164c97926905e 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.dmulf128
+ libc.src.__support.math.dmull
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..edbe42e45539d 100644
--- a/libc/test/shared/shared_math_constexpr_test.cpp
+++ b/libc/test/shared/shared_math_constexpr_test.cpp
@@ -42,6 +42,7 @@ 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::dmull(0.0L, 1.0L));
#endif
@@ -52,6 +53,7 @@ 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::dmulf128(float128(0.0), float128(1.0)));
#endif // LIBC_TYPES_HAS_FLOAT128
More information about the libc-commits
mailing list