[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
Wed Feb 18 16:11:52 PST 2026


https://github.com/hulxv created https://github.com/llvm/llvm-project/pull/182192

Refactors the fdiv math family to be header-only.

Closes https://github.com/llvm/llvm-project/issues/182191

Target Functions:
  - fdiv
  - fdivf128
  - fdivl

>From 00f1aea5e5900e59ff52dff39d0a1e925aeba00b Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Thu, 19 Feb 2026 02:10:54 +0200
Subject: [PATCH] [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        | 25 ++++++++++
 libc/src/__support/math/fdiv.h                | 26 ++++++++++
 libc/src/__support/math/fdivf128.h            | 32 ++++++++++++
 libc/src/__support/math/fdivl.h               | 26 ++++++++++
 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..ea885f22d1000 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -751,6 +751,31 @@ add_header_library(
     libc.src.__support.FPUtil.generic.add_sub
     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
diff --git a/libc/src/__support/math/fdiv.h b/libc/src/__support/math/fdiv.h
new file mode 100644
index 0000000000000..6c38b07733b9a
--- /dev/null
+++ b/libc/src/__support/math/fdiv.h
@@ -0,0 +1,26 @@
+//===-- 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..f45f7cdc7a510
--- /dev/null
+++ b/libc/src/__support/math/fdivf128.h
@@ -0,0 +1,32 @@
+//===-- 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..9cc6dae8fad15
--- /dev/null
+++ b/libc/src/__support/math/fdivl.h
@@ -0,0 +1,26 @@
+//===-- 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",



More information about the libc-commits mailing list