[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor cbrt implementation to header-only in src/__support/math folder. (PR #151837)
https://github.com/bassiounix ready_for_review https://github.com/llvm/llvm-project/pull/151837 ___ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor cbrt implementation to header-only in src/__support/math folder. (PR #151837)
llvmbot wrote:
@llvm/pr-subscribers-libc
Author: Muhammad Bassiouni (bassiounix)
Changes
---
Patch is 30.62 KiB, truncated to 20.00 KiB below, full version:
https://github.com/llvm/llvm-project/pull/151837.diff
9 Files Affected:
- (modified) libc/shared/math.h (+1)
- (added) libc/shared/math/cbrt.h (+23)
- (modified) libc/src/__support/math/CMakeLists.txt (+15)
- (added) libc/src/__support/math/cbrt.h (+349)
- (modified) libc/src/math/generic/CMakeLists.txt (+1-9)
- (modified) libc/src/math/generic/cbrt.cpp (+2-326)
- (modified) libc/test/shared/CMakeLists.txt (+1)
- (modified) libc/test/shared/shared_math_test.cpp (+1)
- (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+13-3)
``diff
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 7fb736b78efa5..3714f380a27dc 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -30,6 +30,7 @@
#include "math/atanf16.h"
#include "math/atanhf.h"
#include "math/atanhf16.h"
+#include "math/cbrt.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/cbrt.h b/libc/shared/math/cbrt.h
new file mode 100644
index 0..2f49dbd364328
--- /dev/null
+++ b/libc/shared/math/cbrt.h
@@ -0,0 +1,23 @@
+//===-- Shared cbrt 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_CBRT_H
+#define LLVM_LIBC_SHARED_MATH_CBRT_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/cbrt.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::cbrt;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_CBRT_H
\ No newline at end of file
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index 9631ab5be7d3b..e1076edf1e61c 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -331,6 +331,21 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ cbrt
+ HDRS
+cbrt.h
+ DEPENDS
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.dyadic_float
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.macros.optimization
+libc.src.__support.integer_literals
+)
+
add_header_library(
erff
HDRS
diff --git a/libc/src/__support/math/cbrt.h b/libc/src/__support/math/cbrt.h
new file mode 100644
index 0..2b9a73c823b14
--- /dev/null
+++ b/libc/src/__support/math/cbrt.h
@@ -0,0 +1,349 @@
+//===-- Implementation header for erff --*- 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 LIBC_SRC___SUPPORT_MATH_CBRT_H
+#define LIBC_SRC___SUPPORT_MATH_CBRT_H
+
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/double_double.h"
+#include "src/__support/FPUtil/dyadic_float.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/integer_literals.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+#if ((LIBC_MATH & LIBC_MATH_SKIP_ACCURATE_PASS) != 0)
+#define LIBC_MATH_CBRT_SKIP_ACCURATE_PASS
+#endif
+
+namespace cbrt_internal {
+using namespace fputil;
+
+// Initial approximation of x^(-2/3) for 1 <= x < 2.
+// Polynomial generated by Sollya with:
+// > P = fpminimax(x^(-2/3), 7, [|D...|], [1, 2]);
+// > dirtyinfnorm(P/x^(-2/3) - 1, [1, 2]);
+// 0x1.28...p-21
+LIBC_INLINE static double intial_approximation(double x) {
+ constexpr double COEFFS[8] = {
+ 0x1.bc52aedead5c6p1, -0x1.b52bfebf110b3p2, 0x1.1d8d71d53d126p3,
+ -0x1.de2db9e81cf87p2, 0x1.0154ca06153bdp2, -0x1.5973c66ee6da7p0,
+ 0x1.07bf6ac832552p-2, -0x1.5e53d9ce41cb8p-6,
+ };
+
+ double x_sq = x * x;
+
+ double c0 = fputil::multiply_add(x, COEFFS[1], COEFFS[0]);
+ double c1 = fputil::multiply_add(x, COEFFS[3], COEFFS[2]);
+ double c2 = fputil::multiply_add(x, COEFFS[5], COEFFS[4]);
+ double c3 = fputil::multiply_add(x, COEFFS[7], COEFFS[6]);
+
+ double x_4 = x_sq * x_sq;
+ double d0 = fputil::multiply_add(x_sq, c1, c0);
+ double d1 = fputil::multiply_add(x_sq, c3, c2);
+
+ return fputil:
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor cbrt implementation to header-only in src/__support/math folder. (PR #151837)
https://github.com/bassiounix edited https://github.com/llvm/llvm-project/pull/151837 ___ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor cbrt implementation to header-only in src/__support/math folder. (PR #151837)
bassiounix wrote: > [!WARNING] > This pull request is not mergeable via GitHub because a downstack PR is > open. Once all requirements are satisfied, merge this PR as a stack href="https://app.graphite.dev/github/pr/llvm/llvm-project/151837?utm_source=stack-comment-downstack-mergeability-warning"; > >on Graphite. > https://graphite.dev/docs/merge-pull-requests";>Learn more * **#151837** https://app.graphite.dev/github/pr/llvm/llvm-project/151837?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> 👈 https://app.graphite.dev/github/pr/llvm/llvm-project/151837?utm_source=stack-comment-view-in-graphite"; target="_blank">(View in Graphite) * **#151779** https://app.graphite.dev/github/pr/llvm/llvm-project/151779?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> * **#151399** https://app.graphite.dev/github/pr/llvm/llvm-project/151399?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> * **#151012** https://app.graphite.dev/github/pr/llvm/llvm-project/151012?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> * **#150993** https://app.graphite.dev/github/pr/llvm/llvm-project/150993?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> * **#150968** https://app.graphite.dev/github/pr/llvm/llvm-project/150968?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> * **#150868** https://app.graphite.dev/github/pr/llvm/llvm-project/150868?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> * **#150854** https://app.graphite.dev/github/pr/llvm/llvm-project/150854?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> * **#150852** https://app.graphite.dev/github/pr/llvm/llvm-project/150852?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> * **#150849** https://app.graphite.dev/github/pr/llvm/llvm-project/150849?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> * **#150843** https://app.graphite.dev/github/pr/llvm/llvm-project/150843?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> * `main` This stack of pull requests is managed by https://graphite.dev?utm-source=stack-comment";>Graphite. Learn more about https://stacking.dev/?utm_source=stack-comment";>stacking. https://github.com/llvm/llvm-project/pull/151837 ___ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor cbrt implementation to header-only in src/__support/math folder. (PR #151837)
https://github.com/bassiounix created
https://github.com/llvm/llvm-project/pull/151837
None
>From 16142801a79a1bbde7208deb738420829f0a1282 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Sat, 2 Aug 2025 22:51:43 +0300
Subject: [PATCH] [libc][math] Refactor cbrt implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/cbrt.h | 23 ++
libc/src/__support/math/CMakeLists.txt| 15 +
libc/src/__support/math/cbrt.h| 349 ++
libc/src/math/generic/CMakeLists.txt | 10 +-
libc/src/math/generic/cbrt.cpp| 328 +---
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 16 +-
9 files changed, 406 insertions(+), 338 deletions(-)
create mode 100644 libc/shared/math/cbrt.h
create mode 100644 libc/src/__support/math/cbrt.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 7fb736b78efa5..3714f380a27dc 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -30,6 +30,7 @@
#include "math/atanf16.h"
#include "math/atanhf.h"
#include "math/atanhf16.h"
+#include "math/cbrt.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/cbrt.h b/libc/shared/math/cbrt.h
new file mode 100644
index 0..2f49dbd364328
--- /dev/null
+++ b/libc/shared/math/cbrt.h
@@ -0,0 +1,23 @@
+//===-- Shared cbrt 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_CBRT_H
+#define LLVM_LIBC_SHARED_MATH_CBRT_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/cbrt.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::cbrt;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_CBRT_H
\ No newline at end of file
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index 9631ab5be7d3b..e1076edf1e61c 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -331,6 +331,21 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ cbrt
+ HDRS
+cbrt.h
+ DEPENDS
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.dyadic_float
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.macros.optimization
+libc.src.__support.integer_literals
+)
+
add_header_library(
erff
HDRS
diff --git a/libc/src/__support/math/cbrt.h b/libc/src/__support/math/cbrt.h
new file mode 100644
index 0..2b9a73c823b14
--- /dev/null
+++ b/libc/src/__support/math/cbrt.h
@@ -0,0 +1,349 @@
+//===-- Implementation header for erff --*- 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 LIBC_SRC___SUPPORT_MATH_CBRT_H
+#define LIBC_SRC___SUPPORT_MATH_CBRT_H
+
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/double_double.h"
+#include "src/__support/FPUtil/dyadic_float.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/integer_literals.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+#if ((LIBC_MATH & LIBC_MATH_SKIP_ACCURATE_PASS) != 0)
+#define LIBC_MATH_CBRT_SKIP_ACCURATE_PASS
+#endif
+
+namespace cbrt_internal {
+using namespace fputil;
+
+// Initial approximation of x^(-2/3) for 1 <= x < 2.
+// Polynomial generated by Sollya with:
+// > P = fpminimax(x^(-2/3), 7, [|D...|], [1, 2]);
+// > dirtyinfnorm(P/x^(-2/3) - 1, [1, 2]);
+// 0x1.28...p-21
+LIBC_INLINE static double intial_approximation(double x) {
+ constexpr double COEFFS[8] = {
+ 0x1.bc52aedead5c6p1, -0x1.b52bfebf110b3p2, 0x1.1d8d71d53d126p3,
+ -0x1.de2db9e81cf87p2, 0x1.0154ca06153bdp2, -0x1.5973c66ee6da7p0,
+ 0x1.07bf6ac832552p-2, -0x1.5e53d9ce41cb8p-6,
+ };
+
+ double x_sq = x * x;
+
+ double c0 = fputil::multiply_add(x, COEFFS[1], COEFFS[0]);
+ double c1 = fputil::multiply_add(x, COEFFS[3], COEFFS[2]);
+ d
