[libc-commits] [libc] [llvm] [libc][math] Refactor fdiv family to header-only (PR #182192)
Mohamed Emad via libc-commits
libc-commits at lists.llvm.org
Tue Mar 17 16:48:02 PDT 2026
https://github.com/hulxv updated https://github.com/llvm/llvm-project/pull/182192
>From 3c90b6b439f6a77f2746b8a347795fbae9e210ea Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Thu, 19 Feb 2026 02:10:54 +0200
Subject: [PATCH 1/3] [libc][math] Refactor fdiv family to header-only
Refactored functions:
- fdiv
- fdivf128
- fdivl
---
libc/shared/math.h | 3 ++
libc/shared/math/fdiv.h | 22 +++++++++
libc/shared/math/fdivf128.h | 28 +++++++++++
libc/shared/math/fdivl.h | 22 +++++++++
libc/src/__support/math/CMakeLists.txt | 28 +++++++++++
libc/src/__support/math/fdiv.h | 25 ++++++++++
libc/src/__support/math/fdivf128.h | 31 ++++++++++++
libc/src/__support/math/fdivl.h | 25 ++++++++++
libc/src/math/generic/CMakeLists.txt | 7 ++-
libc/src/math/generic/fdiv.cpp | 6 +--
libc/src/math/generic/fdivf128.cpp | 6 +--
libc/src/math/generic/fdivl.cpp | 6 +--
libc/test/shared/CMakeLists.txt | 3 ++
libc/test/shared/shared_math_test.cpp | 7 +++
.../llvm-project-overlay/libc/BUILD.bazel | 49 +++++++++++++++++--
15 files changed, 249 insertions(+), 19 deletions(-)
create mode 100644 libc/shared/math/fdiv.h
create mode 100644 libc/shared/math/fdivf128.h
create mode 100644 libc/shared/math/fdivl.h
create mode 100644 libc/src/__support/math/fdiv.h
create mode 100644 libc/src/__support/math/fdivf128.h
create mode 100644 libc/src/__support/math/fdivl.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 8a5aca82c6ec3..50d3dbfc206ad 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -83,6 +83,9 @@
#include "math/f16sqrt.h"
#include "math/f16sqrtf.h"
#include "math/f16sqrtl.h"
+#include "math/fdiv.h"
+#include "math/fdivf128.h"
+#include "math/fdivl.h"
#include "math/ffma.h"
#include "math/ffmal.h"
#include "math/frexpf.h"
diff --git a/libc/shared/math/fdiv.h b/libc/shared/math/fdiv.h
new file mode 100644
index 0000000000000..b30535e88c43c
--- /dev/null
+++ b/libc/shared/math/fdiv.h
@@ -0,0 +1,22 @@
+//===-- Shared fdiv 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_FDIV_H
+#define LLVM_LIBC_SHARED_MATH_FDIV_H
+
+#include "src/__support/math/fdiv.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::fdiv;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_FDIV_H
diff --git a/libc/shared/math/fdivf128.h b/libc/shared/math/fdivf128.h
new file mode 100644
index 0000000000000..07e44077da7b9
--- /dev/null
+++ b/libc/shared/math/fdivf128.h
@@ -0,0 +1,28 @@
+//===-- Shared fdivf128 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_FDIVF128_H
+#define LLVM_LIBC_SHARED_MATH_FDIVF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/math/fdivf128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::fdivf128;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_MATH_FDIVF128_H
diff --git a/libc/shared/math/fdivl.h b/libc/shared/math/fdivl.h
new file mode 100644
index 0000000000000..edc00132da43f
--- /dev/null
+++ b/libc/shared/math/fdivl.h
@@ -0,0 +1,22 @@
+//===-- Shared fdivl 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_FDIVL_H
+#define LLVM_LIBC_SHARED_MATH_FDIVL_H
+
+#include "src/__support/math/fdivl.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::fdivl;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_FDIVL_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index e21fe8ef0ab93..6266b952a414e 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -752,6 +752,34 @@ add_header_library(
libc.src.__support.macros.config
)
+add_header_library(
+ fdiv
+ HDRS
+ fdiv.h
+ DEPENDS
+ libc.src.__support.FPUtil.generic.div
+ libc.src.__support.macros.config
+)
+
+add_header_library(
+ fdivf128
+ HDRS
+ fdivf128.h
+ DEPENDS
+ libc.include.llvm-libc-types.float128
+ libc.src.__support.FPUtil.generic.div
+ libc.src.__support.macros.config
+)
+
+add_header_library(
+ fdivl
+ HDRS
+ fdivl.h
+ DEPENDS
+ libc.src.__support.FPUtil.generic.div
+ libc.src.__support.macros.config
+)
+
add_header_library(
ffmal
HDRS
diff --git a/libc/src/__support/math/fdiv.h b/libc/src/__support/math/fdiv.h
new file mode 100644
index 0000000000000..3993b04e1ecc8
--- /dev/null
+++ b/libc/src/__support/math/fdiv.h
@@ -0,0 +1,25 @@
+//===-- Implementation header for fdiv --------------------------*- 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_FDIV_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FDIV_H
+
+#include "src/__support/FPUtil/generic/div.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE float fdiv(double x, double y) {
+ return fputil::generic::div<float>(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FDIV_H
diff --git a/libc/src/__support/math/fdivf128.h b/libc/src/__support/math/fdivf128.h
new file mode 100644
index 0000000000000..002b931f97c52
--- /dev/null
+++ b/libc/src/__support/math/fdivf128.h
@@ -0,0 +1,31 @@
+//===-- Implementation header for fdivf128 ----------------------*- 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_FDIVF128_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FDIVF128_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 float fdivf128(float128 x, float128 y) {
+ return fputil::generic::div<float>(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FDIVF128_H
diff --git a/libc/src/__support/math/fdivl.h b/libc/src/__support/math/fdivl.h
new file mode 100644
index 0000000000000..36b4c2a719d45
--- /dev/null
+++ b/libc/src/__support/math/fdivl.h
@@ -0,0 +1,25 @@
+//===-- Implementation header for fdivl -------------------------*- 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_FDIVL_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FDIVL_H
+
+#include "src/__support/FPUtil/generic/div.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE float fdivl(long double x, long double y) {
+ return fputil::generic::div<float>(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FDIVL_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 57a29665318a3..4c0188993eb75 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3165,7 +3165,7 @@ add_entrypoint_object(
HDRS
../fdiv.h
DEPENDS
- libc.src.__support.FPUtil.generic.div
+ libc.src.__support.math.fdiv
)
add_entrypoint_object(
@@ -3175,7 +3175,7 @@ add_entrypoint_object(
HDRS
../fdivl.h
DEPENDS
- libc.src.__support.FPUtil.generic.div
+ libc.src.__support.math.fdivl
)
add_entrypoint_object(
@@ -3185,8 +3185,7 @@ add_entrypoint_object(
HDRS
../fdivf128.h
DEPENDS
- libc.src.__support.macros.properties.types
- libc.src.__support.FPUtil.generic.div
+ libc.src.__support.math.fdivf128
)
add_entrypoint_object(
diff --git a/libc/src/math/generic/fdiv.cpp b/libc/src/math/generic/fdiv.cpp
index 1d97fade22a37..da90852604c5f 100644
--- a/libc/src/math/generic/fdiv.cpp
+++ b/libc/src/math/generic/fdiv.cpp
@@ -7,14 +7,12 @@
//===----------------------------------------------------------------------===//
#include "src/math/fdiv.h"
-#include "src/__support/FPUtil/generic/div.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/fdiv.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(float, fdiv, (double x, double y)) {
- return fputil::generic::div<float>(x, y);
+ return math::fdiv(x, y);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/fdivf128.cpp b/libc/src/math/generic/fdivf128.cpp
index 192f13f5de10e..1ab0eb3003062 100644
--- a/libc/src/math/generic/fdivf128.cpp
+++ b/libc/src/math/generic/fdivf128.cpp
@@ -7,14 +7,12 @@
//===----------------------------------------------------------------------===//
#include "src/math/fdivf128.h"
-#include "src/__support/FPUtil/generic/div.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/fdivf128.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(float, fdivf128, (float128 x, float128 y)) {
- return fputil::generic::div<float>(x, y);
+ return math::fdivf128(x, y);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/fdivl.cpp b/libc/src/math/generic/fdivl.cpp
index dcd5debc2acd9..ac29aed5e04a0 100644
--- a/libc/src/math/generic/fdivl.cpp
+++ b/libc/src/math/generic/fdivl.cpp
@@ -7,14 +7,12 @@
//===----------------------------------------------------------------------===//
#include "src/math/fdivl.h"
-#include "src/__support/FPUtil/generic/div.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/fdivl.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(float, fdivl, (long double x, long double y)) {
- return fputil::generic::div<float>(x, y);
+ return math::fdivl(x, y);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 53ad309af4f30..f4536c986a938 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -79,6 +79,9 @@ add_fp_unittest(
libc.src.__support.math.f16sqrt
libc.src.__support.math.f16sqrtf
libc.src.__support.math.f16sqrtl
+ libc.src.__support.math.fdiv
+ libc.src.__support.math.fdivf128
+ libc.src.__support.math.fdivl
libc.src.__support.math.ffma
libc.src.__support.math.ffmal
libc.src.__support.math.frexpf
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 7f2f39ac7a285..ea505e6f709a4 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -187,6 +187,8 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalize(&canonicalize_cx,
&canonicalize_x));
EXPECT_FP_EQ(0.0, canonicalize_cx);
+
+ EXPECT_FP_EQ(1.0, LIBC_NAMESPACE::shared::fdiv(1.0, 1.0));
}
TEST(LlvmLibcSharedMathTest, AllLongDouble) {
@@ -203,6 +205,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(1.0L, LIBC_NAMESPACE::shared::fdivl(1.0L, 1.0L));
}
#ifdef LIBC_TYPES_HAS_FLOAT128
@@ -238,6 +242,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(1.0),
+ LIBC_NAMESPACE::shared::fdivf128(float128(1.0), float128(1.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..8e838b0128c8a 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3053,6 +3053,34 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_fdiv",
+ hdrs = ["src/__support/math/fdiv.h"],
+ deps = [
+ ":__support_fputil_basic_operations",
+ ":__support_macros_config",
+ ],
+)
+
+libc_support_library(
+ name = "__support_math_fdivf128",
+ hdrs = ["src/__support/math/fdivf128.h"],
+ deps = [
+ ":__support_fputil_basic_operations",
+ ":__support_macros_config",
+ ":llvm_libc_types_float128",
+ ],
+)
+
+libc_support_library(
+ name = "__support_math_fdivl",
+ hdrs = ["src/__support/math/fdivl.h"],
+ deps = [
+ ":__support_fputil_basic_operations",
+ ":__support_macros_config",
+ ],
+)
+
libc_support_library(
name = "__support_math_f16fma",
hdrs = ["src/__support/math/f16fma.h"],
@@ -4985,11 +5013,26 @@ libc_math_function(name = "fdimf128")
libc_math_function(name = "fdimf16")
-libc_math_function(name = "fdiv")
+libc_math_function(
+ name = "fdiv",
+ additional_deps = [
+ ":__support_math_fdiv",
+ ],
+)
-libc_math_function(name = "fdivl")
+libc_math_function(
+ name = "fdivl",
+ additional_deps = [
+ ":__support_math_fdivl",
+ ],
+)
-libc_math_function(name = "fdivf128")
+libc_math_function(
+ name = "fdivf128",
+ additional_deps = [
+ ":__support_math_fdivf128",
+ ],
+)
libc_math_function(
name = "ffma",
>From 4f161ee20f0772c726ba56881cbf6ad4b793b778 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Sat, 14 Mar 2026 02:25:14 +0200
Subject: [PATCH 2/3] missed libc_common
---
libc/shared/math/fdiv.h | 1 +
libc/shared/math/fdivf128.h | 1 +
libc/shared/math/fdivl.h | 1 +
3 files changed, 3 insertions(+)
diff --git a/libc/shared/math/fdiv.h b/libc/shared/math/fdiv.h
index b30535e88c43c..5a4455f6f2951 100644
--- a/libc/shared/math/fdiv.h
+++ b/libc/shared/math/fdiv.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SHARED_MATH_FDIV_H
#define LLVM_LIBC_SHARED_MATH_FDIV_H
+#include "shared/libc_common.h"
#include "src/__support/math/fdiv.h"
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/shared/math/fdivf128.h b/libc/shared/math/fdivf128.h
index 07e44077da7b9..cb96aebcd903b 100644
--- a/libc/shared/math/fdivf128.h
+++ b/libc/shared/math/fdivf128.h
@@ -13,6 +13,7 @@
#ifdef LIBC_TYPES_HAS_FLOAT128
+#include "shared/libc_common.h"
#include "src/__support/math/fdivf128.h"
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/shared/math/fdivl.h b/libc/shared/math/fdivl.h
index edc00132da43f..cb832923b823b 100644
--- a/libc/shared/math/fdivl.h
+++ b/libc/shared/math/fdivl.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SHARED_MATH_FDIVL_H
#define LLVM_LIBC_SHARED_MATH_FDIVL_H
+#include "shared/libc_common.h"
#include "src/__support/math/fdivl.h"
namespace LIBC_NAMESPACE_DECL {
>From 5dd4fee7af1b25bed027a820677b80c70d1c4ada Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Wed, 18 Mar 2026 01:44:26 +0200
Subject: [PATCH 3/3] correct returning type
---
libc/test/shared/shared_math_test.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index f8417fa921dae..e951d518c0843 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -215,7 +215,7 @@ TEST(LlvmLibcSharedMathTest, AllLongDouble) {
EXPECT_FP_EQ(0x0p+0L, canonicalizel_cx);
EXPECT_FP_EQ(0x0p+0L, LIBC_NAMESPACE::shared::ceill(0.0L));
- EXPECT_FP_EQ(1.0L, LIBC_NAMESPACE::shared::fdivl(1.0L, 1.0L));
+ EXPECT_FP_EQ(1.0, LIBC_NAMESPACE::shared::fdivl(1.0L, 1.0L));
EXPECT_FP_EQ(0x0p+0L, LIBC_NAMESPACE::shared::fmaxl(0.0L, 0.0L));
}
@@ -254,7 +254,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
EXPECT_FP_EQ(float128(0.0), canonicalizef128_cx);
EXPECT_FP_EQ(float128(0.0), LIBC_NAMESPACE::shared::ceilf128(float128(0.0)));
- EXPECT_FP_EQ(float128(1.0),
+ EXPECT_FP_EQ(1.0,
LIBC_NAMESPACE::shared::fdivf128(float128(1.0), float128(1.0)));
EXPECT_FP_EQ(float128(0.0),
LIBC_NAMESPACE::shared::fmaxf128(float128(0.0), float128(0.0)));
More information about the libc-commits
mailing list