From bbd4d67f0ba75ce0cf20bc9a91ea632ebbb59636 Mon Sep 17 00:00:00 2001 From: hulxv Date: Sun, 26 Apr 2026 00:57:03 +0300 Subject: [PATCH] [libc][math] Refactor fdiv family to header-only (#182192) Refactors the fdiv math family to be header-only. Closes https://github.com/llvm/llvm-project/issues/182191 Target Functions: - fdiv - fdivf128 - fdivl --------- Co-authored-by: Muhammad Bassiouni <60100307+bassiounix@users.noreply.github.com> --- libc/shared/math.h | 3 ++ libc/shared/math/fdiv.h | 23 ++++++++++ libc/shared/math/fdivf128.h | 29 ++++++++++++ libc/shared/math/fdivl.h | 23 ++++++++++ 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 | 6 +++ .../shared/shared_math_constexpr_test.cpp | 4 ++ libc/test/shared/shared_math_test.cpp | 4 ++ .../llvm-project-overlay/libc/BUILD.bazel | 45 ++++++++++++++++++- 16 files changed, 253 insertions(+), 18 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 9192831afa3b..66b7af9b95eb 100644 --- a/libc/shared/math.h +++ b/libc/shared/math.h @@ -154,6 +154,9 @@ #include "math/fdimf128.h" #include "math/fdimf16.h" #include "math/fdiml.h" +#include "math/fdiv.h" +#include "math/fdivf128.h" +#include "math/fdivl.h" #include "math/ffma.h" #include "math/ffmaf128.h" #include "math/ffmal.h" diff --git a/libc/shared/math/fdiv.h b/libc/shared/math/fdiv.h new file mode 100644 index 000000000000..5a4455f6f295 --- /dev/null +++ b/libc/shared/math/fdiv.h @@ -0,0 +1,23 @@ +//===-- 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 "shared/libc_common.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 000000000000..cb96aebcd903 --- /dev/null +++ b/libc/shared/math/fdivf128.h @@ -0,0 +1,29 @@ +//===-- 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 "shared/libc_common.h" +#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 000000000000..cb832923b823 --- /dev/null +++ b/libc/shared/math/fdivl.h @@ -0,0 +1,23 @@ +//===-- 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 "shared/libc_common.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 095028c644d3..f72dd6772c8c 100644 --- a/libc/src/__support/math/CMakeLists.txt +++ b/libc/src/__support/math/CMakeLists.txt @@ -1546,6 +1546,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( ffmaf128 HDRS diff --git a/libc/src/__support/math/fdiv.h b/libc/src/__support/math/fdiv.h new file mode 100644 index 000000000000..be21838be826 --- /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 constexpr float fdiv(double x, double y) { + return fputil::generic::div(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 000000000000..2ab5ea31072c --- /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 constexpr float fdivf128(float128 x, float128 y) { + return fputil::generic::div(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 000000000000..a75f4fd82cbf --- /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 constexpr float fdivl(long double x, long double y) { + return fputil::generic::div(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 ff02cdec1ba0..eed816057b93 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -3116,7 +3116,7 @@ add_entrypoint_object( HDRS ../fdiv.h DEPENDS - libc.src.__support.FPUtil.generic.div + libc.src.__support.math.fdiv ) add_entrypoint_object( @@ -3126,7 +3126,7 @@ add_entrypoint_object( HDRS ../fdivl.h DEPENDS - libc.src.__support.FPUtil.generic.div + libc.src.__support.math.fdivl ) add_entrypoint_object( @@ -3136,8 +3136,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 1d97fade22a3..da90852604c5 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(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 192f13f5de10..1ab0eb300306 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(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 dcd5debc2acd..ac29aed5e04a 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(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 0aad31011378..4a210c81b7a0 100644 --- a/libc/test/shared/CMakeLists.txt +++ b/libc/test/shared/CMakeLists.txt @@ -151,6 +151,9 @@ add_fp_unittest( libc.src.__support.math.fdimf128 libc.src.__support.math.fdimf16 libc.src.__support.math.fdiml + libc.src.__support.math.fdiv + libc.src.__support.math.fdivf128 + libc.src.__support.math.fdivl libc.src.__support.math.ffma libc.src.__support.math.ffmaf128 libc.src.__support.math.ffmal @@ -306,6 +309,9 @@ add_fp_unittest( libc.src.__support.math.ddivl libc.src.__support.math.dmulf128 libc.src.__support.math.dmull + libc.src.__support.math.fdiv + libc.src.__support.math.fdivf128 + libc.src.__support.math.fdivl libc.src.__support.math.floor libc.src.__support.math.floorbf16 libc.src.__support.math.floorf diff --git a/libc/test/shared/shared_math_constexpr_test.cpp b/libc/test/shared/shared_math_constexpr_test.cpp index e9dbdd374084..93050d14c858 100644 --- a/libc/test/shared/shared_math_constexpr_test.cpp +++ b/libc/test/shared/shared_math_constexpr_test.cpp @@ -18,6 +18,7 @@ static_assert(0.0 == LIBC_NAMESPACE::shared::ceil(0.0)); static_assert(0.0 == LIBC_NAMESPACE::shared::copysign(0.0, 0.0)); static_assert(1.0 == LIBC_NAMESPACE::shared::fabs(-1.0)); +static_assert(0.0f == LIBC_NAMESPACE::shared::fdiv(0.0, 1.0)); static_assert(1.0 == LIBC_NAMESPACE::shared::floor(1.2)); static_assert(0.0 == LIBC_NAMESPACE::shared::log(1.0)); @@ -55,6 +56,7 @@ static_assert(0.0L == LIBC_NAMESPACE::shared::copysignl(0.0L, 0.0L)); static_assert(0.0 == LIBC_NAMESPACE::shared::ddivl(0.0L, 1.0L)); static_assert(0.0 == LIBC_NAMESPACE::shared::dmull(0.0L, 1.0L)); static_assert(1.0L == LIBC_NAMESPACE::shared::fabsl(-1.0L)); +static_assert(0.0f == LIBC_NAMESPACE::shared::fdivl(0.0L, 1.0L)); static_assert(0.0L == LIBC_NAMESPACE::shared::floorl(0.0L)); #endif @@ -75,6 +77,8 @@ static_assert(0.0 == LIBC_NAMESPACE::shared::ddivf128(float128(0.0), float128(1.0))); static_assert(0.0 == LIBC_NAMESPACE::shared::dmulf128(float128(0.0), float128(1.0))); +static_assert(0.0f == + LIBC_NAMESPACE::shared::fdivf128(float128(0.0), float128(1.0))); static_assert(float128(0.0) == LIBC_NAMESPACE::shared::floorf128(float128(0.0))); diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp index 7241f4fd3732..d31436267f75 100644 --- a/libc/test/shared/shared_math_test.cpp +++ b/libc/test/shared/shared_math_test.cpp @@ -264,6 +264,7 @@ TEST(LlvmLibcSharedMathTest, AllDouble) { EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::fabs(0.0)); EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::fadd(0.0, 0.0)); EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::fdim(0.0, 0.0)); + EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::shared::fdiv(1.0, 1.0)); EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::floor(0.0)); EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::fmax(0.0, 0.0)); double getpayload_x = 0.0; @@ -329,6 +330,7 @@ TEST(LlvmLibcSharedMathTest, AllLongDouble) { EXPECT_FP_EQ(0.0L, LIBC_NAMESPACE::shared::fabsl(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(1.0f, LIBC_NAMESPACE::shared::fdivl(1.0L, 1.0L)); EXPECT_FP_EQ(0.0L, LIBC_NAMESPACE::shared::floorl(0.0L)); EXPECT_FP_EQ(0.0L, LIBC_NAMESPACE::shared::fmaxl(0.0L, 0.0L)); long double getpayloadl_x = 0.0L; @@ -422,6 +424,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) { EXPECT_FP_EQ(float128(0.0), LIBC_NAMESPACE::shared::floorf128(float128(0.0))); EXPECT_FP_EQ(float128(0.0), LIBC_NAMESPACE::shared::fdimf128(float128(0.0), float128(0.0))); + EXPECT_FP_EQ(1.0f, + 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))); diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel index 9d1f4f9e6a49..b2355202d19c 100644 --- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel @@ -4493,6 +4493,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_f16mul", hdrs = ["src/__support/math/f16mul.h"], @@ -7721,12 +7749,25 @@ libc_math_function( ], ) -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", + additional_deps = [ + ":__support_math_fdivf128", + ], ) libc_math_function(