[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atan implementation to header-only in src/__support/math folder. (PR #150852)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/150852
>From 41e35b0624ca02d1295272382e8e44ba8119ff53 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Sun, 27 Jul 2025 23:44:37 +0300
Subject: [PATCH] [libc][math] Refactor atan implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atan.h | 23 +++
libc/src/__support/math/CMakeLists.txt| 27 +++
libc/src/__support/math/atan.h| 189 ++
.../generic => __support/math}/atan_utils.h | 16 +-
libc/src/math/generic/CMakeLists.txt | 25 +--
libc/src/math/generic/atan.cpp| 167 +---
libc/src/math/generic/atan2.cpp | 3 +-
libc/src/math/generic/atan2f128.cpp | 3 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 44 ++--
12 files changed, 286 insertions(+), 214 deletions(-)
create mode 100644 libc/shared/math/atan.h
create mode 100644 libc/src/__support/math/atan.h
rename libc/src/{math/generic => __support/math}/atan_utils.h (96%)
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 26e33ecd45d73..70b1b7b0bef09 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -22,6 +22,7 @@
#include "math/asinf16.h"
#include "math/asinhf.h"
#include "math/asinhf16.h"
+#include "math/atan.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/atan.h b/libc/shared/math/atan.h
new file mode 100644
index 0..b9ba89b7e6225
--- /dev/null
+++ b/libc/shared/math/atan.h
@@ -0,0 +1,23 @@
+//===-- Shared atan 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_ATAN_H
+#define LLVM_LIBC_SHARED_MATH_ATAN_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atan.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atan;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATAN_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index be208f946024a..cc02920c2a1ef 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -172,6 +172,33 @@ DEPENDS
libc.src.__support.macros.optimization
)
+add_header_library(
+ atan_utils
+ HDRS
+atan_utils.h
+DEPENDS
+libc.src.__support.integer_literals
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.dyadic_float
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.macros.optimization
+)
+
+add_header_library(
+ atan
+ HDRS
+atan.h
+DEPENDS
+.atan_utils
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/atan.h b/libc/src/__support/math/atan.h
new file mode 100644
index 0..62190b092429a
--- /dev/null
+++ b/libc/src/__support/math/atan.h
@@ -0,0 +1,189 @@
+//===-- Implementation header for atan --*- 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_ATAN_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN_H
+
+#include "atan_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/double_double.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+// To compute atan(x), we divided it into the following cases:
+// * |x| < 2^-26:
+// Since |x| > atan(|x|) > |x| - |x|^3/3, and |x|^3/3 < ulp(x)/2, we
simply
+// return atan(x) = x - sign(x) * epsilon.
+// * 2^-26 <= |x| < 1:
+// We perform range reduction mod 2^-6 = 1/64 as follow:
+// Let k = 2^(-6) * round(|x| * 2^6), then
+//atan(x) = sign(x) * atan(|x|)
+//
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atanf implementation to header-only in src/__support/math folder. (PR #150854)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/150854
>From ca83f9dfa292c0aca3b61f9033a36df18350ed73 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Mon, 28 Jul 2025 00:37:42 +0300
Subject: [PATCH] [libc][math] Refactor atanf implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atanf.h | 23
libc/src/__support/math/CMakeLists.txt| 15 ++
libc/src/__support/math/atanf.h | 129 ++
libc/src/math/generic/CMakeLists.txt | 9 +-
libc/src/math/generic/atanf.cpp | 110 +--
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 22 ++-
9 files changed, 188 insertions(+), 123 deletions(-)
create mode 100644 libc/shared/math/atanf.h
create mode 100644 libc/src/__support/math/atanf.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 70b1b7b0bef09..21536647948f4 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -23,6 +23,7 @@
#include "math/asinhf.h"
#include "math/asinhf16.h"
#include "math/atan.h"
+#include "math/atanf.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/atanf.h b/libc/shared/math/atanf.h
new file mode 100644
index 0..858d727bd6698
--- /dev/null
+++ b/libc/shared/math/atanf.h
@@ -0,0 +1,23 @@
+//===-- Shared atanf 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_ATANF_H
+#define LLVM_LIBC_SHARED_MATH_ATANF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atanf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atanf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATANF_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index cc02920c2a1ef..95acc962cc885 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -199,6 +199,21 @@ DEPENDS
libc.src.__support.macros.optimization
)
+add_header_library(
+ atanf
+ HDRS
+atanf.h
+ DEPENDS
+.inv_trigf_utils
+libc.src.__support.FPUtil.except_value_utils
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.FPUtil.rounding_mode
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/atanf.h b/libc/src/__support/math/atanf.h
new file mode 100644
index 0..92799dc8db3cc
--- /dev/null
+++ b/libc/src/__support/math/atanf.h
@@ -0,0 +1,129 @@
+//===-- Implementation header for atanf -*- 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_ATANF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATANF_H
+
+#include "inv_trigf_utils.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float atanf(float x) {
+ using namespace inv_trigf_utils_internal;
+ using FPBits = typename fputil::FPBits;
+
+ constexpr double FINAL_SIGN[2] = {1.0, -1.0};
+ constexpr double SIGNED_PI_OVER_2[2] = {0x1.921fb54442d18p0,
+ -0x1.921fb54442d18p0};
+
+ FPBits x_bits(x);
+ Sign sign = x_bits.sign();
+ x_bits.set_sign(Sign::POS);
+ uint32_t x_abs = x_bits.uintval();
+
+ // x is inf or nan, |x| < 2^-4 or |x|= > 16.
+ if (LIBC_UNLIKELY(x_abs <= 0x3d80'U || x_abs >= 0x4180'U)) {
+double x_d = static_cast(x);
+double const_term = 0.0;
+if (LIBC_UNLIKELY(x_abs >= 0x4180')) {
+ // atan(+-Inf) = +-pi/2.
+ if (x_bits.is_inf()) {
+volatile double sign_pi_over_2 = SIGNED_PI_OVER_2[sign.is_neg()];
+return static_cast(sign_pi_over_2);
+ }
+ if (x_bits.is_nan())
+
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atanf16 implementation to header-only in src/__support/math folder. (PR #150868)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/150868
>From e361d72613ded94fa78a5c1a4baa366c799a6d7b Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Mon, 28 Jul 2025 05:26:38 +0300
Subject: [PATCH] [libc][math] Refactor atanf16 implementation to header-only
in src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atanf16.h| 28 +
libc/src/__support/math/CMakeLists.txt| 15 +++
libc/src/__support/math/atanf16.h | 119 ++
libc/src/math/generic/CMakeLists.txt | 12 +-
libc/src/math/generic/atanf16.cpp | 95 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 22
9 files changed, 190 insertions(+), 104 deletions(-)
create mode 100644 libc/shared/math/atanf16.h
create mode 100644 libc/src/__support/math/atanf16.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 21536647948f4..bcbe0de56170a 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -24,6 +24,7 @@
#include "math/asinhf16.h"
#include "math/atan.h"
#include "math/atanf.h"
+#include "math/atanf16.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/atanf16.h b/libc/shared/math/atanf16.h
new file mode 100644
index 0..f196907059e01
--- /dev/null
+++ b/libc/shared/math/atanf16.h
@@ -0,0 +1,28 @@
+//===-- Shared atanf16 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_ATANF16_H
+#define LLVM_LIBC_SHARED_MATH_ATANF16_H
+
+#include "shared/libc_common.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/math/atanf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atanf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_ATANF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index 95acc962cc885..04cbd3fd1cc01 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -214,6 +214,21 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ atanf16
+ HDRS
+atanf16.h
+ DEPENDS
+libc.src.__support.FPUtil.cast
+libc.src.__support.FPUtil.except_value_utils
+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.FPUtil.sqrt
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/atanf16.h
b/libc/src/__support/math/atanf16.h
new file mode 100644
index 0..f75d145f36852
--- /dev/null
+++ b/libc/src/__support/math/atanf16.h
@@ -0,0 +1,119 @@
+//===-- Implementation header for atanf16 ---*- 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_ATANF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATANF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/macros/optimization.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float16 atanf16(float16 x) {
+ // Generated by Solly using the following command:
+ // > round(pi/2, SG, RN);
+ constexpr float PI_2 = 0x1.921fb6p0;
+
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+ constexpr size_t N_EXCEPTS = 6;
+
+ constexpr fputil::ExceptValues ATANF16_EXCEPTS{{
+ // (input, RZ output, RU offset, RD offset, RN offset)
+ {0x2745, 0x2744, 1, 0, 1},
+ {0x3099, 0x3090, 1, 0, 1},
+ {0x3c6c, 0x3aae, 1, 0, 1},
+ {0x466e, 0x3daa, 1, 0, 1},
+ {0x48ae, 0x3ddb, 1, 0, 0},
+ {0x5619, 0x3e3d, 1, 0, 1},
+ }};
+#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+
+ using FPBits = fputil::FPBits;
+ FPBits xbits(x);
+
+ uint16_t x
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atan2 implementation to header-only in src/__support/math folder. (PR #150968)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/150968
>From d0488b8711b10233a6bb91b5d67b104837fd3eb7 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Mon, 28 Jul 2025 18:07:19 +0300
Subject: [PATCH] [libc][math] Refactor atan2 implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atan2.h | 23 ++
libc/src/__support/math/CMakeLists.txt| 20 +-
libc/src/__support/math/atan2.h | 209 ++
libc/src/math/generic/CMakeLists.txt | 8 +-
libc/src/math/generic/atan2.cpp | 187 +---
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 14 +-
9 files changed, 266 insertions(+), 198 deletions(-)
create mode 100644 libc/shared/math/atan2.h
create mode 100644 libc/src/__support/math/atan2.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index bcbe0de56170a..0605d918eb2af 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -23,6 +23,7 @@
#include "math/asinhf.h"
#include "math/asinhf16.h"
#include "math/atan.h"
+#include "math/atan2.h"
#include "math/atanf.h"
#include "math/atanf16.h"
#include "math/erff.h"
diff --git a/libc/shared/math/atan2.h b/libc/shared/math/atan2.h
new file mode 100644
index 0..894110838817c
--- /dev/null
+++ b/libc/shared/math/atan2.h
@@ -0,0 +1,23 @@
+//===-- Shared atan2 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_ATAN2_H
+#define LLVM_LIBC_SHARED_MATH_ATAN2_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atan2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atan2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATAN2_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index 04cbd3fd1cc01..bbb07b62552f6 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -158,7 +158,7 @@ add_header_library(
asinhf16
HDRS
asinhf16.h
-DEPENDS
+ DEPENDS
.acoshf_utils
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.fp_bits
@@ -176,7 +176,7 @@ add_header_library(
atan_utils
HDRS
atan_utils.h
-DEPENDS
+ DEPENDS
libc.src.__support.integer_literals
libc.src.__support.FPUtil.double_double
libc.src.__support.FPUtil.dyadic_float
@@ -189,7 +189,21 @@ add_header_library(
atan
HDRS
atan.h
-DEPENDS
+ DEPENDS
+.atan_utils
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.macros.optimization
+)
+
+add_header_library(
+ atan2
+ HDRS
+atan2.h
+ DEPENDS
.atan_utils
libc.src.__support.FPUtil.double_double
libc.src.__support.FPUtil.fenv_impl
diff --git a/libc/src/__support/math/atan2.h b/libc/src/__support/math/atan2.h
new file mode 100644
index 0..90ed926c8d75f
--- /dev/null
+++ b/libc/src/__support/math/atan2.h
@@ -0,0 +1,209 @@
+//===-- Implementation header for atan2 -*- 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_ATAN2_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN2_H
+
+#include "atan_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/double_double.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+// There are several range reduction steps we can take for atan2(y, x) as
+// follow:
+
+// * Range reduction 1: signness
+// atan2(y, x) will return a number between -PI and PI representing the angle
+// forming by the 0x axis and the vector (x, y) on the 0xy-plane.
+// In particular, we have that:
+// atan2(y, x) = atan( y/x ) if x >= 0 and y >= 0 (I-quadrant)
+// = pi + atan( y/x )if x < 0 and y >= 0 (II-quadrant)
+//
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atan2 implementation to header-only in src/__support/math folder. (PR #150968)
https://github.com/bassiounix created
https://github.com/llvm/llvm-project/pull/150968
Part of #147386
in preparation for:
https://discourse.llvm.org/t/rfc-make-clang-builtin-math-functions-constexpr-with-llvm-libc-to-support-c-23-constexpr-math-functions/86450
>From e6225547a4677563cd334a186fe01db4a5c4c9bc Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Mon, 28 Jul 2025 18:07:19 +0300
Subject: [PATCH] [libc][math] Refactor atan2 implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atan2.h | 23 ++
libc/src/__support/math/CMakeLists.txt| 20 +-
libc/src/__support/math/atan2.h | 209 ++
libc/src/math/generic/CMakeLists.txt | 8 +-
libc/src/math/generic/atan2.cpp | 187 +---
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 14 +-
9 files changed, 266 insertions(+), 198 deletions(-)
create mode 100644 libc/shared/math/atan2.h
create mode 100644 libc/src/__support/math/atan2.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index bcbe0de56170a..0605d918eb2af 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -23,6 +23,7 @@
#include "math/asinhf.h"
#include "math/asinhf16.h"
#include "math/atan.h"
+#include "math/atan2.h"
#include "math/atanf.h"
#include "math/atanf16.h"
#include "math/erff.h"
diff --git a/libc/shared/math/atan2.h b/libc/shared/math/atan2.h
new file mode 100644
index 0..894110838817c
--- /dev/null
+++ b/libc/shared/math/atan2.h
@@ -0,0 +1,23 @@
+//===-- Shared atan2 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_ATAN2_H
+#define LLVM_LIBC_SHARED_MATH_ATAN2_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atan2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atan2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATAN2_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index 04cbd3fd1cc01..bbb07b62552f6 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -158,7 +158,7 @@ add_header_library(
asinhf16
HDRS
asinhf16.h
-DEPENDS
+ DEPENDS
.acoshf_utils
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.fp_bits
@@ -176,7 +176,7 @@ add_header_library(
atan_utils
HDRS
atan_utils.h
-DEPENDS
+ DEPENDS
libc.src.__support.integer_literals
libc.src.__support.FPUtil.double_double
libc.src.__support.FPUtil.dyadic_float
@@ -189,7 +189,21 @@ add_header_library(
atan
HDRS
atan.h
-DEPENDS
+ DEPENDS
+.atan_utils
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.macros.optimization
+)
+
+add_header_library(
+ atan2
+ HDRS
+atan2.h
+ DEPENDS
.atan_utils
libc.src.__support.FPUtil.double_double
libc.src.__support.FPUtil.fenv_impl
diff --git a/libc/src/__support/math/atan2.h b/libc/src/__support/math/atan2.h
new file mode 100644
index 0..90ed926c8d75f
--- /dev/null
+++ b/libc/src/__support/math/atan2.h
@@ -0,0 +1,209 @@
+//===-- Implementation header for atan2 -*- 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_ATAN2_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN2_H
+
+#include "atan_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/double_double.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+// There are several range reduction steps we can take for atan2(y, x) as
+// follow:
+
+// * Range reduction 1: signness
+// atan2(y, x) will return a number between -PI and PI representing the angle
+// forming by the 0x axis and the vector (x, y) on the 0xy-plane.
+// In partic
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atan2 implementation to header-only in src/__support/math folder. (PR #150968)
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/150968?utm_source=stack-comment-downstack-mergeability-warning"; > >on Graphite. > https://graphite.dev/docs/merge-pull-requests";>Learn more * **#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"/> 👈 https://app.graphite.dev/github/pr/llvm/llvm-project/150968?utm_source=stack-comment-view-in-graphite"; target="_blank">(View in Graphite) * **#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/150968 ___ 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 atanf implementation to header-only in src/__support/math folder. (PR #150854)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/150854
>From 9cac4fc9c88100b253c67ada7767a5dba9c4c917 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Mon, 28 Jul 2025 00:37:42 +0300
Subject: [PATCH] [libc][math] Refactor atanf implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atanf.h | 23
libc/src/__support/math/CMakeLists.txt| 15 ++
libc/src/__support/math/atanf.h | 129 ++
libc/src/math/generic/CMakeLists.txt | 9 +-
libc/src/math/generic/atanf.cpp | 110 +--
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 22 ++-
9 files changed, 188 insertions(+), 123 deletions(-)
create mode 100644 libc/shared/math/atanf.h
create mode 100644 libc/src/__support/math/atanf.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 70b1b7b0bef09..21536647948f4 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -23,6 +23,7 @@
#include "math/asinhf.h"
#include "math/asinhf16.h"
#include "math/atan.h"
+#include "math/atanf.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/atanf.h b/libc/shared/math/atanf.h
new file mode 100644
index 0..858d727bd6698
--- /dev/null
+++ b/libc/shared/math/atanf.h
@@ -0,0 +1,23 @@
+//===-- Shared atanf 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_ATANF_H
+#define LLVM_LIBC_SHARED_MATH_ATANF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atanf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atanf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATANF_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index cc02920c2a1ef..95acc962cc885 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -199,6 +199,21 @@ DEPENDS
libc.src.__support.macros.optimization
)
+add_header_library(
+ atanf
+ HDRS
+atanf.h
+ DEPENDS
+.inv_trigf_utils
+libc.src.__support.FPUtil.except_value_utils
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.FPUtil.rounding_mode
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/atanf.h b/libc/src/__support/math/atanf.h
new file mode 100644
index 0..92799dc8db3cc
--- /dev/null
+++ b/libc/src/__support/math/atanf.h
@@ -0,0 +1,129 @@
+//===-- Implementation header for atanf -*- 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_ATANF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATANF_H
+
+#include "inv_trigf_utils.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float atanf(float x) {
+ using namespace inv_trigf_utils_internal;
+ using FPBits = typename fputil::FPBits;
+
+ constexpr double FINAL_SIGN[2] = {1.0, -1.0};
+ constexpr double SIGNED_PI_OVER_2[2] = {0x1.921fb54442d18p0,
+ -0x1.921fb54442d18p0};
+
+ FPBits x_bits(x);
+ Sign sign = x_bits.sign();
+ x_bits.set_sign(Sign::POS);
+ uint32_t x_abs = x_bits.uintval();
+
+ // x is inf or nan, |x| < 2^-4 or |x|= > 16.
+ if (LIBC_UNLIKELY(x_abs <= 0x3d80'U || x_abs >= 0x4180'U)) {
+double x_d = static_cast(x);
+double const_term = 0.0;
+if (LIBC_UNLIKELY(x_abs >= 0x4180')) {
+ // atan(+-Inf) = +-pi/2.
+ if (x_bits.is_inf()) {
+volatile double sign_pi_over_2 = SIGNED_PI_OVER_2[sign.is_neg()];
+return static_cast(sign_pi_over_2);
+ }
+ if (x_bits.is_nan())
+
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atanf16 implementation to header-only in src/__support/math folder. (PR #150868)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/150868
>From 5077f3f8f1c4d3db505f9aa74862607c2e6a32cf Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Mon, 28 Jul 2025 05:26:38 +0300
Subject: [PATCH] [libc][math] Refactor atanf16 implementation to header-only
in src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atanf16.h| 28 +
libc/src/__support/math/CMakeLists.txt| 15 +++
libc/src/__support/math/atanf16.h | 119 ++
libc/src/math/generic/CMakeLists.txt | 12 +-
libc/src/math/generic/atanf16.cpp | 95 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 22
9 files changed, 190 insertions(+), 104 deletions(-)
create mode 100644 libc/shared/math/atanf16.h
create mode 100644 libc/src/__support/math/atanf16.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 21536647948f4..bcbe0de56170a 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -24,6 +24,7 @@
#include "math/asinhf16.h"
#include "math/atan.h"
#include "math/atanf.h"
+#include "math/atanf16.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/atanf16.h b/libc/shared/math/atanf16.h
new file mode 100644
index 0..f196907059e01
--- /dev/null
+++ b/libc/shared/math/atanf16.h
@@ -0,0 +1,28 @@
+//===-- Shared atanf16 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_ATANF16_H
+#define LLVM_LIBC_SHARED_MATH_ATANF16_H
+
+#include "shared/libc_common.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/math/atanf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atanf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_ATANF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index 95acc962cc885..04cbd3fd1cc01 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -214,6 +214,21 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ atanf16
+ HDRS
+atanf16.h
+ DEPENDS
+libc.src.__support.FPUtil.cast
+libc.src.__support.FPUtil.except_value_utils
+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.FPUtil.sqrt
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/atanf16.h
b/libc/src/__support/math/atanf16.h
new file mode 100644
index 0..f75d145f36852
--- /dev/null
+++ b/libc/src/__support/math/atanf16.h
@@ -0,0 +1,119 @@
+//===-- Implementation header for atanf16 ---*- 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_ATANF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATANF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/macros/optimization.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float16 atanf16(float16 x) {
+ // Generated by Solly using the following command:
+ // > round(pi/2, SG, RN);
+ constexpr float PI_2 = 0x1.921fb6p0;
+
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+ constexpr size_t N_EXCEPTS = 6;
+
+ constexpr fputil::ExceptValues ATANF16_EXCEPTS{{
+ // (input, RZ output, RU offset, RD offset, RN offset)
+ {0x2745, 0x2744, 1, 0, 1},
+ {0x3099, 0x3090, 1, 0, 1},
+ {0x3c6c, 0x3aae, 1, 0, 1},
+ {0x466e, 0x3daa, 1, 0, 1},
+ {0x48ae, 0x3ddb, 1, 0, 0},
+ {0x5619, 0x3e3d, 1, 0, 1},
+ }};
+#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+
+ using FPBits = fputil::FPBits;
+ FPBits xbits(x);
+
+ uint16_t x
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atan2 implementation to header-only in src/__support/math folder. (PR #150968)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/150968
>From 079c38ecdd5a66b752a772213f76c2f514ea7fce Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Mon, 28 Jul 2025 18:07:19 +0300
Subject: [PATCH] [libc][math] Refactor atan2 implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atan2.h | 23 ++
libc/src/__support/math/CMakeLists.txt| 20 +-
libc/src/__support/math/atan2.h | 209 ++
libc/src/math/generic/CMakeLists.txt | 8 +-
libc/src/math/generic/atan2.cpp | 187 +---
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 14 +-
9 files changed, 266 insertions(+), 198 deletions(-)
create mode 100644 libc/shared/math/atan2.h
create mode 100644 libc/src/__support/math/atan2.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index bcbe0de56170a..0605d918eb2af 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -23,6 +23,7 @@
#include "math/asinhf.h"
#include "math/asinhf16.h"
#include "math/atan.h"
+#include "math/atan2.h"
#include "math/atanf.h"
#include "math/atanf16.h"
#include "math/erff.h"
diff --git a/libc/shared/math/atan2.h b/libc/shared/math/atan2.h
new file mode 100644
index 0..894110838817c
--- /dev/null
+++ b/libc/shared/math/atan2.h
@@ -0,0 +1,23 @@
+//===-- Shared atan2 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_ATAN2_H
+#define LLVM_LIBC_SHARED_MATH_ATAN2_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atan2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atan2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATAN2_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index 04cbd3fd1cc01..bbb07b62552f6 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -158,7 +158,7 @@ add_header_library(
asinhf16
HDRS
asinhf16.h
-DEPENDS
+ DEPENDS
.acoshf_utils
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.fp_bits
@@ -176,7 +176,7 @@ add_header_library(
atan_utils
HDRS
atan_utils.h
-DEPENDS
+ DEPENDS
libc.src.__support.integer_literals
libc.src.__support.FPUtil.double_double
libc.src.__support.FPUtil.dyadic_float
@@ -189,7 +189,21 @@ add_header_library(
atan
HDRS
atan.h
-DEPENDS
+ DEPENDS
+.atan_utils
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.macros.optimization
+)
+
+add_header_library(
+ atan2
+ HDRS
+atan2.h
+ DEPENDS
.atan_utils
libc.src.__support.FPUtil.double_double
libc.src.__support.FPUtil.fenv_impl
diff --git a/libc/src/__support/math/atan2.h b/libc/src/__support/math/atan2.h
new file mode 100644
index 0..90ed926c8d75f
--- /dev/null
+++ b/libc/src/__support/math/atan2.h
@@ -0,0 +1,209 @@
+//===-- Implementation header for atan2 -*- 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_ATAN2_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN2_H
+
+#include "atan_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/double_double.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+// There are several range reduction steps we can take for atan2(y, x) as
+// follow:
+
+// * Range reduction 1: signness
+// atan2(y, x) will return a number between -PI and PI representing the angle
+// forming by the 0x axis and the vector (x, y) on the 0xy-plane.
+// In particular, we have that:
+// atan2(y, x) = atan( y/x ) if x >= 0 and y >= 0 (I-quadrant)
+// = pi + atan( y/x )if x < 0 and y >= 0 (II-quadrant)
+//
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atanf implementation to header-only in src/__support/math folder. (PR #150854)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/150854
>From 9cac4fc9c88100b253c67ada7767a5dba9c4c917 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Mon, 28 Jul 2025 00:37:42 +0300
Subject: [PATCH] [libc][math] Refactor atanf implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atanf.h | 23
libc/src/__support/math/CMakeLists.txt| 15 ++
libc/src/__support/math/atanf.h | 129 ++
libc/src/math/generic/CMakeLists.txt | 9 +-
libc/src/math/generic/atanf.cpp | 110 +--
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 22 ++-
9 files changed, 188 insertions(+), 123 deletions(-)
create mode 100644 libc/shared/math/atanf.h
create mode 100644 libc/src/__support/math/atanf.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 70b1b7b0bef09..21536647948f4 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -23,6 +23,7 @@
#include "math/asinhf.h"
#include "math/asinhf16.h"
#include "math/atan.h"
+#include "math/atanf.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/atanf.h b/libc/shared/math/atanf.h
new file mode 100644
index 0..858d727bd6698
--- /dev/null
+++ b/libc/shared/math/atanf.h
@@ -0,0 +1,23 @@
+//===-- Shared atanf 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_ATANF_H
+#define LLVM_LIBC_SHARED_MATH_ATANF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atanf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atanf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATANF_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index cc02920c2a1ef..95acc962cc885 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -199,6 +199,21 @@ DEPENDS
libc.src.__support.macros.optimization
)
+add_header_library(
+ atanf
+ HDRS
+atanf.h
+ DEPENDS
+.inv_trigf_utils
+libc.src.__support.FPUtil.except_value_utils
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.FPUtil.rounding_mode
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/atanf.h b/libc/src/__support/math/atanf.h
new file mode 100644
index 0..92799dc8db3cc
--- /dev/null
+++ b/libc/src/__support/math/atanf.h
@@ -0,0 +1,129 @@
+//===-- Implementation header for atanf -*- 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_ATANF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATANF_H
+
+#include "inv_trigf_utils.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float atanf(float x) {
+ using namespace inv_trigf_utils_internal;
+ using FPBits = typename fputil::FPBits;
+
+ constexpr double FINAL_SIGN[2] = {1.0, -1.0};
+ constexpr double SIGNED_PI_OVER_2[2] = {0x1.921fb54442d18p0,
+ -0x1.921fb54442d18p0};
+
+ FPBits x_bits(x);
+ Sign sign = x_bits.sign();
+ x_bits.set_sign(Sign::POS);
+ uint32_t x_abs = x_bits.uintval();
+
+ // x is inf or nan, |x| < 2^-4 or |x|= > 16.
+ if (LIBC_UNLIKELY(x_abs <= 0x3d80'U || x_abs >= 0x4180'U)) {
+double x_d = static_cast(x);
+double const_term = 0.0;
+if (LIBC_UNLIKELY(x_abs >= 0x4180')) {
+ // atan(+-Inf) = +-pi/2.
+ if (x_bits.is_inf()) {
+volatile double sign_pi_over_2 = SIGNED_PI_OVER_2[sign.is_neg()];
+return static_cast(sign_pi_over_2);
+ }
+ if (x_bits.is_nan())
+
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atan implementation to header-only in src/__support/math folder. (PR #150852)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/150852
>From 8c60f834b0fd914b7d35b38ef89b0cd0f46c39e5 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Sun, 27 Jul 2025 23:44:37 +0300
Subject: [PATCH] [libc][math] Refactor atan implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atan.h | 23 +++
libc/src/__support/math/CMakeLists.txt| 27 +++
libc/src/__support/math/atan.h| 189 ++
.../generic => __support/math}/atan_utils.h | 16 +-
libc/src/math/generic/CMakeLists.txt | 25 +--
libc/src/math/generic/atan.cpp| 167 +---
libc/src/math/generic/atan2.cpp | 3 +-
libc/src/math/generic/atan2f128.cpp | 3 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 44 ++--
12 files changed, 286 insertions(+), 214 deletions(-)
create mode 100644 libc/shared/math/atan.h
create mode 100644 libc/src/__support/math/atan.h
rename libc/src/{math/generic => __support/math}/atan_utils.h (96%)
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 26e33ecd45d73..70b1b7b0bef09 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -22,6 +22,7 @@
#include "math/asinf16.h"
#include "math/asinhf.h"
#include "math/asinhf16.h"
+#include "math/atan.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/atan.h b/libc/shared/math/atan.h
new file mode 100644
index 0..b9ba89b7e6225
--- /dev/null
+++ b/libc/shared/math/atan.h
@@ -0,0 +1,23 @@
+//===-- Shared atan 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_ATAN_H
+#define LLVM_LIBC_SHARED_MATH_ATAN_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atan.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atan;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATAN_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index be208f946024a..cc02920c2a1ef 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -172,6 +172,33 @@ DEPENDS
libc.src.__support.macros.optimization
)
+add_header_library(
+ atan_utils
+ HDRS
+atan_utils.h
+DEPENDS
+libc.src.__support.integer_literals
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.dyadic_float
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.macros.optimization
+)
+
+add_header_library(
+ atan
+ HDRS
+atan.h
+DEPENDS
+.atan_utils
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/atan.h b/libc/src/__support/math/atan.h
new file mode 100644
index 0..62190b092429a
--- /dev/null
+++ b/libc/src/__support/math/atan.h
@@ -0,0 +1,189 @@
+//===-- Implementation header for atan --*- 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_ATAN_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN_H
+
+#include "atan_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/double_double.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+// To compute atan(x), we divided it into the following cases:
+// * |x| < 2^-26:
+// Since |x| > atan(|x|) > |x| - |x|^3/3, and |x|^3/3 < ulp(x)/2, we
simply
+// return atan(x) = x - sign(x) * epsilon.
+// * 2^-26 <= |x| < 1:
+// We perform range reduction mod 2^-6 = 1/64 as follow:
+// Let k = 2^(-6) * round(|x| * 2^6), then
+//atan(x) = sign(x) * atan(|x|)
+//
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atan2f implementation to header-only in src/__support/math folder. (PR #150993)
https://github.com/bassiounix created
https://github.com/llvm/llvm-project/pull/150993
Part of #147386
in preparation for:
https://discourse.llvm.org/t/rfc-make-clang-builtin-math-functions-constexpr-with-llvm-libc-to-support-c-23-constexpr-math-functions/86450
>From 938716d9098a91943153be870a7717e8a358988c Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Mon, 28 Jul 2025 19:35:03 +0300
Subject: [PATCH] [libc][math] Refactor atan2f implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atan2f.h | 23 ++
libc/src/__support/math/CMakeLists.txt| 17 +
libc/src/__support/math/atan2f.h | 351 ++
.../generic => __support/math}/atan2f_float.h | 21 +-
libc/src/math/generic/CMakeLists.txt | 12 +-
libc/src/math/generic/atan2f.cpp | 328 +---
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 20 +-
10 files changed, 427 insertions(+), 348 deletions(-)
create mode 100644 libc/shared/math/atan2f.h
create mode 100644 libc/src/__support/math/atan2f.h
rename libc/src/{math/generic => __support/math}/atan2f_float.h (95%)
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 0605d918eb2af..527bb8d6214ae 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -24,6 +24,7 @@
#include "math/asinhf16.h"
#include "math/atan.h"
#include "math/atan2.h"
+#include "math/atan2f.h"
#include "math/atanf.h"
#include "math/atanf16.h"
#include "math/erff.h"
diff --git a/libc/shared/math/atan2f.h b/libc/shared/math/atan2f.h
new file mode 100644
index 0..2de09d25e19f8
--- /dev/null
+++ b/libc/shared/math/atan2f.h
@@ -0,0 +1,23 @@
+//===-- Shared atan2f 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_ATAN2F_H
+#define LLVM_LIBC_SHARED_MATH_ATAN2F_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atan2f.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atan2f;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATAN2F_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index bbb07b62552f6..c197b19ed29de 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -213,6 +213,23 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ atan2f
+ HDRS
+atan2f_float.h
+atan2f.h
+ DEPENDS
+.inv_trigf_utils
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.macros.config
+libc.src.__support.macros.optimization
+)
+
add_header_library(
atanf
HDRS
diff --git a/libc/src/__support/math/atan2f.h b/libc/src/__support/math/atan2f.h
new file mode 100644
index 0..e3b19329126f4
--- /dev/null
+++ b/libc/src/__support/math/atan2f.h
@@ -0,0 +1,351 @@
+//===-- Implementation header for atan2f *- 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_ATAN2F_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN2F_H
+
+#include "inv_trigf_utils.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/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+#if defined(LIBC_MATH_HAS_SKIP_ACCURATE_PASS) &&
\
+defined(LIBC_MATH_HAS_INTERMEDIATE_COMP_IN_FLOAT)
+
+// We use float-float implementation to reduce size.
+#include "atan2f_float.h"
+
+#else
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+namespace atan2f_internal {
+
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+
+// Look up tables for accurate pass:
+
+// atan(i/16) with i = 0..16, generated by Sollya with:
+// > for i from 0 to 16 do {
+// a = round(atan(i/16), D, RN);
+/
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atan implementation to header-only in src/__support/math folder. (PR #150852)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/150852
>From 8c60f834b0fd914b7d35b38ef89b0cd0f46c39e5 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Sun, 27 Jul 2025 23:44:37 +0300
Subject: [PATCH] [libc][math] Refactor atan implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atan.h | 23 +++
libc/src/__support/math/CMakeLists.txt| 27 +++
libc/src/__support/math/atan.h| 189 ++
.../generic => __support/math}/atan_utils.h | 16 +-
libc/src/math/generic/CMakeLists.txt | 25 +--
libc/src/math/generic/atan.cpp| 167 +---
libc/src/math/generic/atan2.cpp | 3 +-
libc/src/math/generic/atan2f128.cpp | 3 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 44 ++--
12 files changed, 286 insertions(+), 214 deletions(-)
create mode 100644 libc/shared/math/atan.h
create mode 100644 libc/src/__support/math/atan.h
rename libc/src/{math/generic => __support/math}/atan_utils.h (96%)
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 26e33ecd45d73..70b1b7b0bef09 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -22,6 +22,7 @@
#include "math/asinf16.h"
#include "math/asinhf.h"
#include "math/asinhf16.h"
+#include "math/atan.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/atan.h b/libc/shared/math/atan.h
new file mode 100644
index 0..b9ba89b7e6225
--- /dev/null
+++ b/libc/shared/math/atan.h
@@ -0,0 +1,23 @@
+//===-- Shared atan 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_ATAN_H
+#define LLVM_LIBC_SHARED_MATH_ATAN_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atan.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atan;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATAN_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index be208f946024a..cc02920c2a1ef 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -172,6 +172,33 @@ DEPENDS
libc.src.__support.macros.optimization
)
+add_header_library(
+ atan_utils
+ HDRS
+atan_utils.h
+DEPENDS
+libc.src.__support.integer_literals
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.dyadic_float
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.macros.optimization
+)
+
+add_header_library(
+ atan
+ HDRS
+atan.h
+DEPENDS
+.atan_utils
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/atan.h b/libc/src/__support/math/atan.h
new file mode 100644
index 0..62190b092429a
--- /dev/null
+++ b/libc/src/__support/math/atan.h
@@ -0,0 +1,189 @@
+//===-- Implementation header for atan --*- 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_ATAN_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN_H
+
+#include "atan_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/double_double.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+// To compute atan(x), we divided it into the following cases:
+// * |x| < 2^-26:
+// Since |x| > atan(|x|) > |x| - |x|^3/3, and |x|^3/3 < ulp(x)/2, we
simply
+// return atan(x) = x - sign(x) * epsilon.
+// * 2^-26 <= |x| < 1:
+// We perform range reduction mod 2^-6 = 1/64 as follow:
+// Let k = 2^(-6) * round(|x| * 2^6), then
+//atan(x) = sign(x) * atan(|x|)
+//
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atan2f implementation to header-only in src/__support/math folder. (PR #150993)
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/150993?utm_source=stack-comment-downstack-mergeability-warning"; > >on Graphite. > https://graphite.dev/docs/merge-pull-requests";>Learn more * **#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"/> 👈 https://app.graphite.dev/github/pr/llvm/llvm-project/150993?utm_source=stack-comment-view-in-graphite"; target="_blank">(View in Graphite) * **#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/150993 ___ 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 atanf16 implementation to header-only in src/__support/math folder. (PR #150868)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/150868
>From 5077f3f8f1c4d3db505f9aa74862607c2e6a32cf Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Mon, 28 Jul 2025 05:26:38 +0300
Subject: [PATCH] [libc][math] Refactor atanf16 implementation to header-only
in src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atanf16.h| 28 +
libc/src/__support/math/CMakeLists.txt| 15 +++
libc/src/__support/math/atanf16.h | 119 ++
libc/src/math/generic/CMakeLists.txt | 12 +-
libc/src/math/generic/atanf16.cpp | 95 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 22
9 files changed, 190 insertions(+), 104 deletions(-)
create mode 100644 libc/shared/math/atanf16.h
create mode 100644 libc/src/__support/math/atanf16.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 21536647948f4..bcbe0de56170a 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -24,6 +24,7 @@
#include "math/asinhf16.h"
#include "math/atan.h"
#include "math/atanf.h"
+#include "math/atanf16.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/atanf16.h b/libc/shared/math/atanf16.h
new file mode 100644
index 0..f196907059e01
--- /dev/null
+++ b/libc/shared/math/atanf16.h
@@ -0,0 +1,28 @@
+//===-- Shared atanf16 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_ATANF16_H
+#define LLVM_LIBC_SHARED_MATH_ATANF16_H
+
+#include "shared/libc_common.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/math/atanf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atanf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_ATANF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index 95acc962cc885..04cbd3fd1cc01 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -214,6 +214,21 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ atanf16
+ HDRS
+atanf16.h
+ DEPENDS
+libc.src.__support.FPUtil.cast
+libc.src.__support.FPUtil.except_value_utils
+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.FPUtil.sqrt
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/atanf16.h
b/libc/src/__support/math/atanf16.h
new file mode 100644
index 0..f75d145f36852
--- /dev/null
+++ b/libc/src/__support/math/atanf16.h
@@ -0,0 +1,119 @@
+//===-- Implementation header for atanf16 ---*- 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_ATANF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATANF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/macros/optimization.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float16 atanf16(float16 x) {
+ // Generated by Solly using the following command:
+ // > round(pi/2, SG, RN);
+ constexpr float PI_2 = 0x1.921fb6p0;
+
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+ constexpr size_t N_EXCEPTS = 6;
+
+ constexpr fputil::ExceptValues ATANF16_EXCEPTS{{
+ // (input, RZ output, RU offset, RD offset, RN offset)
+ {0x2745, 0x2744, 1, 0, 1},
+ {0x3099, 0x3090, 1, 0, 1},
+ {0x3c6c, 0x3aae, 1, 0, 1},
+ {0x466e, 0x3daa, 1, 0, 1},
+ {0x48ae, 0x3ddb, 1, 0, 0},
+ {0x5619, 0x3e3d, 1, 0, 1},
+ }};
+#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+
+ using FPBits = fputil::FPBits;
+ FPBits xbits(x);
+
+ uint16_t x
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atan2 implementation to header-only in src/__support/math folder. (PR #150968)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/150968
>From 079c38ecdd5a66b752a772213f76c2f514ea7fce Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Mon, 28 Jul 2025 18:07:19 +0300
Subject: [PATCH] [libc][math] Refactor atan2 implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atan2.h | 23 ++
libc/src/__support/math/CMakeLists.txt| 20 +-
libc/src/__support/math/atan2.h | 209 ++
libc/src/math/generic/CMakeLists.txt | 8 +-
libc/src/math/generic/atan2.cpp | 187 +---
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 14 +-
9 files changed, 266 insertions(+), 198 deletions(-)
create mode 100644 libc/shared/math/atan2.h
create mode 100644 libc/src/__support/math/atan2.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index bcbe0de56170a..0605d918eb2af 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -23,6 +23,7 @@
#include "math/asinhf.h"
#include "math/asinhf16.h"
#include "math/atan.h"
+#include "math/atan2.h"
#include "math/atanf.h"
#include "math/atanf16.h"
#include "math/erff.h"
diff --git a/libc/shared/math/atan2.h b/libc/shared/math/atan2.h
new file mode 100644
index 0..894110838817c
--- /dev/null
+++ b/libc/shared/math/atan2.h
@@ -0,0 +1,23 @@
+//===-- Shared atan2 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_ATAN2_H
+#define LLVM_LIBC_SHARED_MATH_ATAN2_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atan2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atan2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATAN2_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index 04cbd3fd1cc01..bbb07b62552f6 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -158,7 +158,7 @@ add_header_library(
asinhf16
HDRS
asinhf16.h
-DEPENDS
+ DEPENDS
.acoshf_utils
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.fp_bits
@@ -176,7 +176,7 @@ add_header_library(
atan_utils
HDRS
atan_utils.h
-DEPENDS
+ DEPENDS
libc.src.__support.integer_literals
libc.src.__support.FPUtil.double_double
libc.src.__support.FPUtil.dyadic_float
@@ -189,7 +189,21 @@ add_header_library(
atan
HDRS
atan.h
-DEPENDS
+ DEPENDS
+.atan_utils
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.macros.optimization
+)
+
+add_header_library(
+ atan2
+ HDRS
+atan2.h
+ DEPENDS
.atan_utils
libc.src.__support.FPUtil.double_double
libc.src.__support.FPUtil.fenv_impl
diff --git a/libc/src/__support/math/atan2.h b/libc/src/__support/math/atan2.h
new file mode 100644
index 0..90ed926c8d75f
--- /dev/null
+++ b/libc/src/__support/math/atan2.h
@@ -0,0 +1,209 @@
+//===-- Implementation header for atan2 -*- 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_ATAN2_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN2_H
+
+#include "atan_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/double_double.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+// There are several range reduction steps we can take for atan2(y, x) as
+// follow:
+
+// * Range reduction 1: signness
+// atan2(y, x) will return a number between -PI and PI representing the angle
+// forming by the 0x axis and the vector (x, y) on the 0xy-plane.
+// In particular, we have that:
+// atan2(y, x) = atan( y/x ) if x >= 0 and y >= 0 (I-quadrant)
+// = pi + atan( y/x )if x < 0 and y >= 0 (II-quadrant)
+//
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atan2f implementation to header-only in src/__support/math folder. (PR #150993)
https://github.com/bassiounix ready_for_review https://github.com/llvm/llvm-project/pull/150993 ___ 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 asinhf16 implementation to header-only in src/__support/math folder. (PR #150849)
https://github.com/bassiounix created
https://github.com/llvm/llvm-project/pull/150849
None
>From 7be3b70ef8d3c8f39006dbee302fe5291491cb77 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Sun, 27 Jul 2025 22:21:00 +0300
Subject: [PATCH] [libc][math] Refactor asinhf16 implementation to header-only
in src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/asinf16.h| 2 +-
libc/shared/math/asinhf16.h | 28
libc/src/__support/math/CMakeLists.txt| 18 +++
libc/src/__support/math/asinhf16.h| 123 ++
libc/src/math/generic/CMakeLists.txt | 13 +-
libc/src/math/generic/asinhf16.cpp| 96 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 25
10 files changed, 201 insertions(+), 107 deletions(-)
create mode 100644 libc/shared/math/asinhf16.h
create mode 100644 libc/src/__support/math/asinhf16.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index e0f00f52e9dc3..26e33ecd45d73 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -21,6 +21,7 @@
#include "math/asinf.h"
#include "math/asinf16.h"
#include "math/asinhf.h"
+#include "math/asinhf16.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/asinf16.h b/libc/shared/math/asinf16.h
index d545e269a6402..af5b2ec179233 100644
--- a/libc/shared/math/asinf16.h
+++ b/libc/shared/math/asinf16.h
@@ -25,4 +25,4 @@ using math::asinf16;
#endif // LIBC_TYPES_HAS_FLOAT16
-#endif // LLVM_LIBC_SHARED_MATH_ASINF_H
+#endif // LLVM_LIBC_SHARED_MATH_ASINF16_H
diff --git a/libc/shared/math/asinhf16.h b/libc/shared/math/asinhf16.h
new file mode 100644
index 0..1a0525b6a9b8b
--- /dev/null
+++ b/libc/shared/math/asinhf16.h
@@ -0,0 +1,28 @@
+//===-- Shared asinhf16 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_ASINHF16_H
+#define LLVM_LIBC_SHARED_MATH_ASINHF16_H
+
+#include "shared/libc_common.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/math/asinhf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::asinhf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_ASINHF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index 13f46a13fe0d7..be208f946024a 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -154,6 +154,24 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ asinhf16
+ HDRS
+asinhf16.h
+DEPENDS
+.acoshf_utils
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.FPUtil.cast
+libc.src.__support.FPUtil.except_value_utils
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.rounding_mode
+libc.src.__support.FPUtil.sqrt
+libc.src.__support.macros.config
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/asinhf16.h
b/libc/src/__support/math/asinhf16.h
new file mode 100644
index 0..81657b8db97b0
--- /dev/null
+++ b/libc/src/__support/math/asinhf16.h
@@ -0,0 +1,123 @@
+//===-- Implementation header for asinhf16 --*- 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_ASINHF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ASINHF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "acoshf_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/rounding_mode.h"
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float16 asinhf16(float16 x) {
+
+#i
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor asinhf16 implementation to header-only in src/__support/math folder. (PR #150849)
https://github.com/bassiounix edited https://github.com/llvm/llvm-project/pull/150849 ___ 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 asinhf16 implementation to header-only in src/__support/math folder. (PR #150849)
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/150849?utm_source=stack-comment-downstack-mergeability-warning"; > >on Graphite. > https://graphite.dev/docs/merge-pull-requests";>Learn more * **#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"/> 👈 https://app.graphite.dev/github/pr/llvm/llvm-project/150849?utm_source=stack-comment-view-in-graphite"; target="_blank">(View in Graphite) * **#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/150849 ___ 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 asinhf16 implementation to header-only in src/__support/math folder. (PR #150849)
https://github.com/bassiounix ready_for_review https://github.com/llvm/llvm-project/pull/150849 ___ 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 atanf implementation to header-only in src/__support/math folder. (PR #150854)
https://github.com/bassiounix created
https://github.com/llvm/llvm-project/pull/150854
None
>From 19f366d6545cb91b34e5222c2b714abe88103748 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Mon, 28 Jul 2025 00:37:42 +0300
Subject: [PATCH] [libc][math] Refactor atanf implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atanf.h | 23
libc/src/__support/math/CMakeLists.txt| 15 ++
libc/src/__support/math/atanf.h | 129 ++
libc/src/math/generic/CMakeLists.txt | 9 +-
libc/src/math/generic/atanf.cpp | 110 +--
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 22 ++-
9 files changed, 188 insertions(+), 123 deletions(-)
create mode 100644 libc/shared/math/atanf.h
create mode 100644 libc/src/__support/math/atanf.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 70b1b7b0bef09..21536647948f4 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -23,6 +23,7 @@
#include "math/asinhf.h"
#include "math/asinhf16.h"
#include "math/atan.h"
+#include "math/atanf.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/atanf.h b/libc/shared/math/atanf.h
new file mode 100644
index 0..858d727bd6698
--- /dev/null
+++ b/libc/shared/math/atanf.h
@@ -0,0 +1,23 @@
+//===-- Shared atanf 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_ATANF_H
+#define LLVM_LIBC_SHARED_MATH_ATANF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atanf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atanf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATANF_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index cc02920c2a1ef..95acc962cc885 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -199,6 +199,21 @@ DEPENDS
libc.src.__support.macros.optimization
)
+add_header_library(
+ atanf
+ HDRS
+atanf.h
+ DEPENDS
+.inv_trigf_utils
+libc.src.__support.FPUtil.except_value_utils
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.FPUtil.rounding_mode
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/atanf.h b/libc/src/__support/math/atanf.h
new file mode 100644
index 0..92799dc8db3cc
--- /dev/null
+++ b/libc/src/__support/math/atanf.h
@@ -0,0 +1,129 @@
+//===-- Implementation header for atanf -*- 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_ATANF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATANF_H
+
+#include "inv_trigf_utils.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float atanf(float x) {
+ using namespace inv_trigf_utils_internal;
+ using FPBits = typename fputil::FPBits;
+
+ constexpr double FINAL_SIGN[2] = {1.0, -1.0};
+ constexpr double SIGNED_PI_OVER_2[2] = {0x1.921fb54442d18p0,
+ -0x1.921fb54442d18p0};
+
+ FPBits x_bits(x);
+ Sign sign = x_bits.sign();
+ x_bits.set_sign(Sign::POS);
+ uint32_t x_abs = x_bits.uintval();
+
+ // x is inf or nan, |x| < 2^-4 or |x|= > 16.
+ if (LIBC_UNLIKELY(x_abs <= 0x3d80'U || x_abs >= 0x4180'U)) {
+double x_d = static_cast(x);
+double const_term = 0.0;
+if (LIBC_UNLIKELY(x_abs >= 0x4180')) {
+ // atan(+-Inf) = +-pi/2.
+ if (x_bits.is_inf()) {
+volatile double sign_pi_over_2 = SIGNED_PI_OVER_2[sign.is_neg()];
+return static_cast(sign_pi_over_2);
+ }
+ if (x_bits.is_nan
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atanf implementation to header-only in src/__support/math folder. (PR #150854)
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/150854?utm_source=stack-comment-downstack-mergeability-warning"; > >on Graphite. > https://graphite.dev/docs/merge-pull-requests";>Learn more * **#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"/> 👈 https://app.graphite.dev/github/pr/llvm/llvm-project/150854?utm_source=stack-comment-view-in-graphite"; target="_blank">(View in Graphite) * **#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/150854 ___ 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 atanf implementation to header-only in src/__support/math folder. (PR #150854)
https://github.com/bassiounix edited https://github.com/llvm/llvm-project/pull/150854 ___ 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 atanf implementation to header-only in src/__support/math folder. (PR #150854)
https://github.com/bassiounix ready_for_review https://github.com/llvm/llvm-project/pull/150854 ___ 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 atan implementation to header-only in src/__support/math folder. (PR #150852)
https://github.com/bassiounix created
https://github.com/llvm/llvm-project/pull/150852
None
>From 8f3f2866eb6656784827084b7e1e1922ee0c8a56 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Sun, 27 Jul 2025 23:44:37 +0300
Subject: [PATCH] [libc][math] Refactor atan implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atan.h | 23 +++
libc/src/__support/math/CMakeLists.txt| 27 +++
libc/src/__support/math/atan.h| 190 ++
.../generic => __support/math}/atan_utils.h | 14 +-
libc/src/math/generic/CMakeLists.txt | 25 +--
libc/src/math/generic/atan.cpp| 167 +--
libc/src/math/generic/atan2.cpp | 3 +-
libc/src/math/generic/atan2f128.cpp | 3 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 44 ++--
12 files changed, 285 insertions(+), 214 deletions(-)
create mode 100644 libc/shared/math/atan.h
create mode 100644 libc/src/__support/math/atan.h
rename libc/src/{math/generic => __support/math}/atan_utils.h (96%)
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 26e33ecd45d73..70b1b7b0bef09 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -22,6 +22,7 @@
#include "math/asinf16.h"
#include "math/asinhf.h"
#include "math/asinhf16.h"
+#include "math/atan.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/atan.h b/libc/shared/math/atan.h
new file mode 100644
index 0..b9ba89b7e6225
--- /dev/null
+++ b/libc/shared/math/atan.h
@@ -0,0 +1,23 @@
+//===-- Shared atan 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_ATAN_H
+#define LLVM_LIBC_SHARED_MATH_ATAN_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atan.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atan;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATAN_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index be208f946024a..cc02920c2a1ef 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -172,6 +172,33 @@ DEPENDS
libc.src.__support.macros.optimization
)
+add_header_library(
+ atan_utils
+ HDRS
+atan_utils.h
+DEPENDS
+libc.src.__support.integer_literals
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.dyadic_float
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.macros.optimization
+)
+
+add_header_library(
+ atan
+ HDRS
+atan.h
+DEPENDS
+.atan_utils
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/atan.h b/libc/src/__support/math/atan.h
new file mode 100644
index 0..4f42b2948aea8
--- /dev/null
+++ b/libc/src/__support/math/atan.h
@@ -0,0 +1,190 @@
+//===-- Implementation header for atan --*- 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_ATAN_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN_H
+
+#include "atan_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/double_double.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+// To compute atan(x), we divided it into the following cases:
+// * |x| < 2^-26:
+// Since |x| > atan(|x|) > |x| - |x|^3/3, and |x|^3/3 < ulp(x)/2, we
simply
+// return atan(x) = x - sign(x) * epsilon.
+// * 2^-26 <= |x| < 1:
+// We perform range reduction mod 2^-6 = 1/64 as follow:
+// Let k = 2^(-6) * round(|x| * 2^6), then
+//atan(x) = sign(x) * atan(|x|)
+//
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atan implementation to header-only in src/__support/math folder. (PR #150852)
https://github.com/bassiounix edited https://github.com/llvm/llvm-project/pull/150852 ___ 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 atan implementation to header-only in src/__support/math folder. (PR #150852)
https://github.com/bassiounix ready_for_review https://github.com/llvm/llvm-project/pull/150852 ___ 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 atan implementation to header-only in src/__support/math folder. (PR #150852)
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/150852?utm_source=stack-comment-downstack-mergeability-warning"; > >on Graphite. > https://graphite.dev/docs/merge-pull-requests";>Learn more * **#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"/> 👈 https://app.graphite.dev/github/pr/llvm/llvm-project/150852?utm_source=stack-comment-view-in-graphite"; target="_blank">(View in Graphite) * **#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/150852 ___ 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 atan implementation to header-only in src/__support/math folder. (PR #150852)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/150852
>From 743428ec2ac87dd01da07be4a47199b7cbbec156 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Sun, 27 Jul 2025 23:44:37 +0300
Subject: [PATCH] [libc][math] Refactor atan implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atan.h | 23 +++
libc/src/__support/math/CMakeLists.txt| 27 +++
libc/src/__support/math/atan.h| 189 ++
.../generic => __support/math}/atan_utils.h | 16 +-
libc/src/math/generic/CMakeLists.txt | 25 +--
libc/src/math/generic/atan.cpp| 167 +---
libc/src/math/generic/atan2.cpp | 3 +-
libc/src/math/generic/atan2f128.cpp | 3 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 44 ++--
12 files changed, 286 insertions(+), 214 deletions(-)
create mode 100644 libc/shared/math/atan.h
create mode 100644 libc/src/__support/math/atan.h
rename libc/src/{math/generic => __support/math}/atan_utils.h (96%)
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 26e33ecd45d73..70b1b7b0bef09 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -22,6 +22,7 @@
#include "math/asinf16.h"
#include "math/asinhf.h"
#include "math/asinhf16.h"
+#include "math/atan.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/atan.h b/libc/shared/math/atan.h
new file mode 100644
index 0..b9ba89b7e6225
--- /dev/null
+++ b/libc/shared/math/atan.h
@@ -0,0 +1,23 @@
+//===-- Shared atan 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_ATAN_H
+#define LLVM_LIBC_SHARED_MATH_ATAN_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atan.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atan;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATAN_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index be208f946024a..cc02920c2a1ef 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -172,6 +172,33 @@ DEPENDS
libc.src.__support.macros.optimization
)
+add_header_library(
+ atan_utils
+ HDRS
+atan_utils.h
+DEPENDS
+libc.src.__support.integer_literals
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.dyadic_float
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.macros.optimization
+)
+
+add_header_library(
+ atan
+ HDRS
+atan.h
+DEPENDS
+.atan_utils
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/atan.h b/libc/src/__support/math/atan.h
new file mode 100644
index 0..62190b092429a
--- /dev/null
+++ b/libc/src/__support/math/atan.h
@@ -0,0 +1,189 @@
+//===-- Implementation header for atan --*- 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_ATAN_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN_H
+
+#include "atan_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/double_double.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+// To compute atan(x), we divided it into the following cases:
+// * |x| < 2^-26:
+// Since |x| > atan(|x|) > |x| - |x|^3/3, and |x|^3/3 < ulp(x)/2, we
simply
+// return atan(x) = x - sign(x) * epsilon.
+// * 2^-26 <= |x| < 1:
+// We perform range reduction mod 2^-6 = 1/64 as follow:
+// Let k = 2^(-6) * round(|x| * 2^6), then
+//atan(x) = sign(x) * atan(|x|)
+//
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atan implementation to header-only in src/__support/math folder. (PR #150852)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/150852
>From da6cf0f3700bcc5442c672800540ea4d68424b24 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Sun, 27 Jul 2025 23:44:37 +0300
Subject: [PATCH] [libc][math] Refactor atan implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atan.h | 23 +++
libc/src/__support/math/CMakeLists.txt| 27 +++
libc/src/__support/math/atan.h| 189 ++
.../generic => __support/math}/atan_utils.h | 16 +-
libc/src/math/generic/CMakeLists.txt | 25 +--
libc/src/math/generic/atan.cpp| 167 +---
libc/src/math/generic/atan2.cpp | 3 +-
libc/src/math/generic/atan2f128.cpp | 3 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 44 ++--
12 files changed, 286 insertions(+), 214 deletions(-)
create mode 100644 libc/shared/math/atan.h
create mode 100644 libc/src/__support/math/atan.h
rename libc/src/{math/generic => __support/math}/atan_utils.h (96%)
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 26e33ecd45d73..70b1b7b0bef09 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -22,6 +22,7 @@
#include "math/asinf16.h"
#include "math/asinhf.h"
#include "math/asinhf16.h"
+#include "math/atan.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/atan.h b/libc/shared/math/atan.h
new file mode 100644
index 0..b9ba89b7e6225
--- /dev/null
+++ b/libc/shared/math/atan.h
@@ -0,0 +1,23 @@
+//===-- Shared atan 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_ATAN_H
+#define LLVM_LIBC_SHARED_MATH_ATAN_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atan.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atan;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATAN_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index be208f946024a..cc02920c2a1ef 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -172,6 +172,33 @@ DEPENDS
libc.src.__support.macros.optimization
)
+add_header_library(
+ atan_utils
+ HDRS
+atan_utils.h
+DEPENDS
+libc.src.__support.integer_literals
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.dyadic_float
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.macros.optimization
+)
+
+add_header_library(
+ atan
+ HDRS
+atan.h
+DEPENDS
+.atan_utils
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/atan.h b/libc/src/__support/math/atan.h
new file mode 100644
index 0..62190b092429a
--- /dev/null
+++ b/libc/src/__support/math/atan.h
@@ -0,0 +1,189 @@
+//===-- Implementation header for atan --*- 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_ATAN_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN_H
+
+#include "atan_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/double_double.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+// To compute atan(x), we divided it into the following cases:
+// * |x| < 2^-26:
+// Since |x| > atan(|x|) > |x| - |x|^3/3, and |x|^3/3 < ulp(x)/2, we
simply
+// return atan(x) = x - sign(x) * epsilon.
+// * 2^-26 <= |x| < 1:
+// We perform range reduction mod 2^-6 = 1/64 as follow:
+// Let k = 2^(-6) * round(|x| * 2^6), then
+//atan(x) = sign(x) * atan(|x|)
+//
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atan implementation to header-only in src/__support/math folder. (PR #150852)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/150852
>From da6cf0f3700bcc5442c672800540ea4d68424b24 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Sun, 27 Jul 2025 23:44:37 +0300
Subject: [PATCH] [libc][math] Refactor atan implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atan.h | 23 +++
libc/src/__support/math/CMakeLists.txt| 27 +++
libc/src/__support/math/atan.h| 189 ++
.../generic => __support/math}/atan_utils.h | 16 +-
libc/src/math/generic/CMakeLists.txt | 25 +--
libc/src/math/generic/atan.cpp| 167 +---
libc/src/math/generic/atan2.cpp | 3 +-
libc/src/math/generic/atan2f128.cpp | 3 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 44 ++--
12 files changed, 286 insertions(+), 214 deletions(-)
create mode 100644 libc/shared/math/atan.h
create mode 100644 libc/src/__support/math/atan.h
rename libc/src/{math/generic => __support/math}/atan_utils.h (96%)
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 26e33ecd45d73..70b1b7b0bef09 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -22,6 +22,7 @@
#include "math/asinf16.h"
#include "math/asinhf.h"
#include "math/asinhf16.h"
+#include "math/atan.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/atan.h b/libc/shared/math/atan.h
new file mode 100644
index 0..b9ba89b7e6225
--- /dev/null
+++ b/libc/shared/math/atan.h
@@ -0,0 +1,23 @@
+//===-- Shared atan 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_ATAN_H
+#define LLVM_LIBC_SHARED_MATH_ATAN_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atan.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atan;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATAN_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index be208f946024a..cc02920c2a1ef 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -172,6 +172,33 @@ DEPENDS
libc.src.__support.macros.optimization
)
+add_header_library(
+ atan_utils
+ HDRS
+atan_utils.h
+DEPENDS
+libc.src.__support.integer_literals
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.dyadic_float
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.macros.optimization
+)
+
+add_header_library(
+ atan
+ HDRS
+atan.h
+DEPENDS
+.atan_utils
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/atan.h b/libc/src/__support/math/atan.h
new file mode 100644
index 0..62190b092429a
--- /dev/null
+++ b/libc/src/__support/math/atan.h
@@ -0,0 +1,189 @@
+//===-- Implementation header for atan --*- 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_ATAN_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN_H
+
+#include "atan_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/double_double.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+// To compute atan(x), we divided it into the following cases:
+// * |x| < 2^-26:
+// Since |x| > atan(|x|) > |x| - |x|^3/3, and |x|^3/3 < ulp(x)/2, we
simply
+// return atan(x) = x - sign(x) * epsilon.
+// * 2^-26 <= |x| < 1:
+// We perform range reduction mod 2^-6 = 1/64 as follow:
+// Let k = 2^(-6) * round(|x| * 2^6), then
+//atan(x) = sign(x) * atan(|x|)
+//
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor asinhf16 implementation to header-only in src/__support/math folder. (PR #150849)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/150849
>From 7be3b70ef8d3c8f39006dbee302fe5291491cb77 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Sun, 27 Jul 2025 22:21:00 +0300
Subject: [PATCH 1/2] [libc][math] Refactor asinhf16 implementation to
header-only in src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/asinf16.h| 2 +-
libc/shared/math/asinhf16.h | 28
libc/src/__support/math/CMakeLists.txt| 18 +++
libc/src/__support/math/asinhf16.h| 123 ++
libc/src/math/generic/CMakeLists.txt | 13 +-
libc/src/math/generic/asinhf16.cpp| 96 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 25
10 files changed, 201 insertions(+), 107 deletions(-)
create mode 100644 libc/shared/math/asinhf16.h
create mode 100644 libc/src/__support/math/asinhf16.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index e0f00f52e9dc3..26e33ecd45d73 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -21,6 +21,7 @@
#include "math/asinf.h"
#include "math/asinf16.h"
#include "math/asinhf.h"
+#include "math/asinhf16.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/asinf16.h b/libc/shared/math/asinf16.h
index d545e269a6402..af5b2ec179233 100644
--- a/libc/shared/math/asinf16.h
+++ b/libc/shared/math/asinf16.h
@@ -25,4 +25,4 @@ using math::asinf16;
#endif // LIBC_TYPES_HAS_FLOAT16
-#endif // LLVM_LIBC_SHARED_MATH_ASINF_H
+#endif // LLVM_LIBC_SHARED_MATH_ASINF16_H
diff --git a/libc/shared/math/asinhf16.h b/libc/shared/math/asinhf16.h
new file mode 100644
index 0..1a0525b6a9b8b
--- /dev/null
+++ b/libc/shared/math/asinhf16.h
@@ -0,0 +1,28 @@
+//===-- Shared asinhf16 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_ASINHF16_H
+#define LLVM_LIBC_SHARED_MATH_ASINHF16_H
+
+#include "shared/libc_common.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/math/asinhf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::asinhf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_ASINHF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index 13f46a13fe0d7..be208f946024a 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -154,6 +154,24 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ asinhf16
+ HDRS
+asinhf16.h
+DEPENDS
+.acoshf_utils
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.FPUtil.cast
+libc.src.__support.FPUtil.except_value_utils
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.rounding_mode
+libc.src.__support.FPUtil.sqrt
+libc.src.__support.macros.config
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/asinhf16.h
b/libc/src/__support/math/asinhf16.h
new file mode 100644
index 0..81657b8db97b0
--- /dev/null
+++ b/libc/src/__support/math/asinhf16.h
@@ -0,0 +1,123 @@
+//===-- Implementation header for asinhf16 --*- 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_ASINHF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ASINHF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "acoshf_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/rounding_mode.h"
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float16 asinhf16(float16 x) {
+
+#ifn
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atan implementation to header-only in src/__support/math folder. (PR #150852)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/150852
>From 5c608185f7c4ccd7746b2eef6b33fbbf945e8a2c Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Sun, 27 Jul 2025 23:44:37 +0300
Subject: [PATCH] [libc][math] Refactor atan implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atan.h | 23 +++
libc/src/__support/math/CMakeLists.txt| 27 +++
libc/src/__support/math/atan.h| 189 ++
.../generic => __support/math}/atan_utils.h | 16 +-
libc/src/math/generic/CMakeLists.txt | 25 +--
libc/src/math/generic/atan.cpp| 167 +---
libc/src/math/generic/atan2.cpp | 3 +-
libc/src/math/generic/atan2f128.cpp | 3 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 44 ++--
12 files changed, 286 insertions(+), 214 deletions(-)
create mode 100644 libc/shared/math/atan.h
create mode 100644 libc/src/__support/math/atan.h
rename libc/src/{math/generic => __support/math}/atan_utils.h (96%)
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 26e33ecd45d73..70b1b7b0bef09 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -22,6 +22,7 @@
#include "math/asinf16.h"
#include "math/asinhf.h"
#include "math/asinhf16.h"
+#include "math/atan.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/atan.h b/libc/shared/math/atan.h
new file mode 100644
index 0..b9ba89b7e6225
--- /dev/null
+++ b/libc/shared/math/atan.h
@@ -0,0 +1,23 @@
+//===-- Shared atan 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_ATAN_H
+#define LLVM_LIBC_SHARED_MATH_ATAN_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atan.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atan;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATAN_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index be208f946024a..cc02920c2a1ef 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -172,6 +172,33 @@ DEPENDS
libc.src.__support.macros.optimization
)
+add_header_library(
+ atan_utils
+ HDRS
+atan_utils.h
+DEPENDS
+libc.src.__support.integer_literals
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.dyadic_float
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.macros.optimization
+)
+
+add_header_library(
+ atan
+ HDRS
+atan.h
+DEPENDS
+.atan_utils
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/atan.h b/libc/src/__support/math/atan.h
new file mode 100644
index 0..62190b092429a
--- /dev/null
+++ b/libc/src/__support/math/atan.h
@@ -0,0 +1,189 @@
+//===-- Implementation header for atan --*- 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_ATAN_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN_H
+
+#include "atan_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/double_double.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+// To compute atan(x), we divided it into the following cases:
+// * |x| < 2^-26:
+// Since |x| > atan(|x|) > |x| - |x|^3/3, and |x|^3/3 < ulp(x)/2, we
simply
+// return atan(x) = x - sign(x) * epsilon.
+// * 2^-26 <= |x| < 1:
+// We perform range reduction mod 2^-6 = 1/64 as follow:
+// Let k = 2^(-6) * round(|x| * 2^6), then
+//atan(x) = sign(x) * atan(|x|)
+//
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor asinhf16 implementation to header-only in src/__support/math folder. (PR #150849)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/150849
>From 7be3b70ef8d3c8f39006dbee302fe5291491cb77 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Sun, 27 Jul 2025 22:21:00 +0300
Subject: [PATCH 1/2] [libc][math] Refactor asinhf16 implementation to
header-only in src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/asinf16.h| 2 +-
libc/shared/math/asinhf16.h | 28
libc/src/__support/math/CMakeLists.txt| 18 +++
libc/src/__support/math/asinhf16.h| 123 ++
libc/src/math/generic/CMakeLists.txt | 13 +-
libc/src/math/generic/asinhf16.cpp| 96 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 25
10 files changed, 201 insertions(+), 107 deletions(-)
create mode 100644 libc/shared/math/asinhf16.h
create mode 100644 libc/src/__support/math/asinhf16.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index e0f00f52e9dc3..26e33ecd45d73 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -21,6 +21,7 @@
#include "math/asinf.h"
#include "math/asinf16.h"
#include "math/asinhf.h"
+#include "math/asinhf16.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/asinf16.h b/libc/shared/math/asinf16.h
index d545e269a6402..af5b2ec179233 100644
--- a/libc/shared/math/asinf16.h
+++ b/libc/shared/math/asinf16.h
@@ -25,4 +25,4 @@ using math::asinf16;
#endif // LIBC_TYPES_HAS_FLOAT16
-#endif // LLVM_LIBC_SHARED_MATH_ASINF_H
+#endif // LLVM_LIBC_SHARED_MATH_ASINF16_H
diff --git a/libc/shared/math/asinhf16.h b/libc/shared/math/asinhf16.h
new file mode 100644
index 0..1a0525b6a9b8b
--- /dev/null
+++ b/libc/shared/math/asinhf16.h
@@ -0,0 +1,28 @@
+//===-- Shared asinhf16 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_ASINHF16_H
+#define LLVM_LIBC_SHARED_MATH_ASINHF16_H
+
+#include "shared/libc_common.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/math/asinhf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::asinhf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_ASINHF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index 13f46a13fe0d7..be208f946024a 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -154,6 +154,24 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ asinhf16
+ HDRS
+asinhf16.h
+DEPENDS
+.acoshf_utils
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.FPUtil.cast
+libc.src.__support.FPUtil.except_value_utils
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.rounding_mode
+libc.src.__support.FPUtil.sqrt
+libc.src.__support.macros.config
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/asinhf16.h
b/libc/src/__support/math/asinhf16.h
new file mode 100644
index 0..81657b8db97b0
--- /dev/null
+++ b/libc/src/__support/math/asinhf16.h
@@ -0,0 +1,123 @@
+//===-- Implementation header for asinhf16 --*- 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_ASINHF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ASINHF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "acoshf_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/rounding_mode.h"
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float16 asinhf16(float16 x) {
+
+#ifn
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atan implementation to header-only in src/__support/math folder. (PR #150852)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/150852
>From 5c608185f7c4ccd7746b2eef6b33fbbf945e8a2c Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Sun, 27 Jul 2025 23:44:37 +0300
Subject: [PATCH] [libc][math] Refactor atan implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atan.h | 23 +++
libc/src/__support/math/CMakeLists.txt| 27 +++
libc/src/__support/math/atan.h| 189 ++
.../generic => __support/math}/atan_utils.h | 16 +-
libc/src/math/generic/CMakeLists.txt | 25 +--
libc/src/math/generic/atan.cpp| 167 +---
libc/src/math/generic/atan2.cpp | 3 +-
libc/src/math/generic/atan2f128.cpp | 3 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 44 ++--
12 files changed, 286 insertions(+), 214 deletions(-)
create mode 100644 libc/shared/math/atan.h
create mode 100644 libc/src/__support/math/atan.h
rename libc/src/{math/generic => __support/math}/atan_utils.h (96%)
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 26e33ecd45d73..70b1b7b0bef09 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -22,6 +22,7 @@
#include "math/asinf16.h"
#include "math/asinhf.h"
#include "math/asinhf16.h"
+#include "math/atan.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/atan.h b/libc/shared/math/atan.h
new file mode 100644
index 0..b9ba89b7e6225
--- /dev/null
+++ b/libc/shared/math/atan.h
@@ -0,0 +1,23 @@
+//===-- Shared atan 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_ATAN_H
+#define LLVM_LIBC_SHARED_MATH_ATAN_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atan.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atan;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATAN_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index be208f946024a..cc02920c2a1ef 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -172,6 +172,33 @@ DEPENDS
libc.src.__support.macros.optimization
)
+add_header_library(
+ atan_utils
+ HDRS
+atan_utils.h
+DEPENDS
+libc.src.__support.integer_literals
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.dyadic_float
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.macros.optimization
+)
+
+add_header_library(
+ atan
+ HDRS
+atan.h
+DEPENDS
+.atan_utils
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/atan.h b/libc/src/__support/math/atan.h
new file mode 100644
index 0..62190b092429a
--- /dev/null
+++ b/libc/src/__support/math/atan.h
@@ -0,0 +1,189 @@
+//===-- Implementation header for atan --*- 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_ATAN_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN_H
+
+#include "atan_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/double_double.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+// To compute atan(x), we divided it into the following cases:
+// * |x| < 2^-26:
+// Since |x| > atan(|x|) > |x| - |x|^3/3, and |x|^3/3 < ulp(x)/2, we
simply
+// return atan(x) = x - sign(x) * epsilon.
+// * 2^-26 <= |x| < 1:
+// We perform range reduction mod 2^-6 = 1/64 as follow:
+// Let k = 2^(-6) * round(|x| * 2^6), then
+//atan(x) = sign(x) * atan(|x|)
+//
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atan2f128 implementation to header-only in src/__support/math folder. (PR #151012)
https://github.com/bassiounix created
https://github.com/llvm/llvm-project/pull/151012
None
>From 8d08d7c3bf19eb28d399d7ad431650b6a5462ea9 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Mon, 28 Jul 2025 21:14:48 +0300
Subject: [PATCH] [libc][math] Refactor atan2f128 implementation to header-only
in src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atan2f128.h | 29 +++
libc/src/__support/math/CMakeLists.txt| 15 ++
libc/src/__support/math/atan2f128.h | 212 ++
libc/src/math/generic/CMakeLists.txt | 10 +-
libc/src/math/generic/atan2f128.cpp | 190 +---
libc/test/shared/shared_math_test.cpp | 2 +
.../llvm-project-overlay/libc/BUILD.bazel | 27 ++-
8 files changed, 287 insertions(+), 199 deletions(-)
create mode 100644 libc/shared/math/atan2f128.h
create mode 100644 libc/src/__support/math/atan2f128.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 527bb8d6214ae..6cb583c08dedd 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -25,6 +25,7 @@
#include "math/atan.h"
#include "math/atan2.h"
#include "math/atan2f.h"
+#include "math/atan2f128.h"
#include "math/atanf.h"
#include "math/atanf16.h"
#include "math/erff.h"
diff --git a/libc/shared/math/atan2f128.h b/libc/shared/math/atan2f128.h
new file mode 100644
index 0..d7aee40c69527
--- /dev/null
+++ b/libc/shared/math/atan2f128.h
@@ -0,0 +1,29 @@
+//===-- Shared atan2f128 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_ATAN2F128_H
+#define LLVM_LIBC_SHARED_MATH_ATAN2F128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atan2f128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atan2f128;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_MATH_ATAN2F128_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index c197b19ed29de..caafdc2cbf1d6 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -230,6 +230,21 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ atan2f128
+ HDRS
+atan2f128.h
+ DEPENDS
+.atan_utils
+libc.src.__support.integer_literals
+libc.src.__support.uint128
+libc.src.__support.FPUtil.dyadic_float
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.macros.optimization
+)
+
add_header_library(
atanf
HDRS
diff --git a/libc/src/__support/math/atan2f128.h
b/libc/src/__support/math/atan2f128.h
new file mode 100644
index 0..89efaf1fd72a0
--- /dev/null
+++ b/libc/src/__support/math/atan2f128.h
@@ -0,0 +1,212 @@
+//===-- Implementation header for atan2f128 -*- 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_ATAN2F128_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN2F128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "atan_utils.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/dyadic_float.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/integer_literals.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+#include "src/__support/uint128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+// There are several range reduction steps we can take for atan2(y, x) as
+// follow:
+
+// * Range reduction 1: signness
+// atan2(y, x) will return a number between -PI and PI representing the angle
+// forming by the 0x axis and the vector (x, y) on the 0xy-plane.
+// In particular, we have that:
+// atan2(y, x) = atan( y/x ) if x >= 0 and y >= 0 (I-quadrant)
+// = pi + atan( y/x )if x < 0 and y >= 0 (II-quadrant)
+// = -pi + atan( y/x ) if x < 0 and y < 0 (III-quadrant)
+// = atan( y/x ) if x >= 0 and y < 0 (IV-quadrant)
+// Since atan function is odd, we can use the formula:
+// atan(-u) = -atan(u)
+// to adjust th
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atan2f128 implementation to header-only in src/__support/math folder. (PR #151012)
https://github.com/bassiounix edited https://github.com/llvm/llvm-project/pull/151012 ___ 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 atan2f128 implementation to header-only in src/__support/math folder. (PR #151012)
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/151012?utm_source=stack-comment-downstack-mergeability-warning"; > >on Graphite. > https://graphite.dev/docs/merge-pull-requests";>Learn more * **#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"/> 👈 https://app.graphite.dev/github/pr/llvm/llvm-project/151012?utm_source=stack-comment-view-in-graphite"; target="_blank">(View in Graphite) * **#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/151012 ___ 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 atanf implementation to header-only in src/__support/math folder. (PR #150854)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/150854
>From 3930b0f9886d2ec449e6f2126120f5ba3e7e48f7 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Mon, 28 Jul 2025 00:37:42 +0300
Subject: [PATCH] [libc][math] Refactor atanf implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atanf.h | 23
libc/src/__support/math/CMakeLists.txt| 15 ++
libc/src/__support/math/atanf.h | 129 ++
libc/src/math/generic/CMakeLists.txt | 9 +-
libc/src/math/generic/atanf.cpp | 110 +--
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 22 ++-
9 files changed, 188 insertions(+), 123 deletions(-)
create mode 100644 libc/shared/math/atanf.h
create mode 100644 libc/src/__support/math/atanf.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 70b1b7b0bef09..21536647948f4 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -23,6 +23,7 @@
#include "math/asinhf.h"
#include "math/asinhf16.h"
#include "math/atan.h"
+#include "math/atanf.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/atanf.h b/libc/shared/math/atanf.h
new file mode 100644
index 0..858d727bd6698
--- /dev/null
+++ b/libc/shared/math/atanf.h
@@ -0,0 +1,23 @@
+//===-- Shared atanf 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_ATANF_H
+#define LLVM_LIBC_SHARED_MATH_ATANF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atanf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atanf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATANF_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index cc02920c2a1ef..95acc962cc885 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -199,6 +199,21 @@ DEPENDS
libc.src.__support.macros.optimization
)
+add_header_library(
+ atanf
+ HDRS
+atanf.h
+ DEPENDS
+.inv_trigf_utils
+libc.src.__support.FPUtil.except_value_utils
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.FPUtil.rounding_mode
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/atanf.h b/libc/src/__support/math/atanf.h
new file mode 100644
index 0..92799dc8db3cc
--- /dev/null
+++ b/libc/src/__support/math/atanf.h
@@ -0,0 +1,129 @@
+//===-- Implementation header for atanf -*- 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_ATANF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATANF_H
+
+#include "inv_trigf_utils.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float atanf(float x) {
+ using namespace inv_trigf_utils_internal;
+ using FPBits = typename fputil::FPBits;
+
+ constexpr double FINAL_SIGN[2] = {1.0, -1.0};
+ constexpr double SIGNED_PI_OVER_2[2] = {0x1.921fb54442d18p0,
+ -0x1.921fb54442d18p0};
+
+ FPBits x_bits(x);
+ Sign sign = x_bits.sign();
+ x_bits.set_sign(Sign::POS);
+ uint32_t x_abs = x_bits.uintval();
+
+ // x is inf or nan, |x| < 2^-4 or |x|= > 16.
+ if (LIBC_UNLIKELY(x_abs <= 0x3d80'U || x_abs >= 0x4180'U)) {
+double x_d = static_cast(x);
+double const_term = 0.0;
+if (LIBC_UNLIKELY(x_abs >= 0x4180')) {
+ // atan(+-Inf) = +-pi/2.
+ if (x_bits.is_inf()) {
+volatile double sign_pi_over_2 = SIGNED_PI_OVER_2[sign.is_neg()];
+return static_cast(sign_pi_over_2);
+ }
+ if (x_bits.is_nan())
+
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atan2f128 implementation to header-only in src/__support/math folder. (PR #151012)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/151012
>From fa5283fdad6b26748a27ab6aa39b7e6c2a3d179d Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Mon, 28 Jul 2025 21:14:48 +0300
Subject: [PATCH 1/2] [libc][math] Refactor atan2f128 implementation to
header-only in src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atan2f128.h | 29 +++
libc/src/__support/math/CMakeLists.txt| 15 ++
libc/src/__support/math/atan2f128.h | 212 ++
libc/src/math/generic/CMakeLists.txt | 10 +-
libc/src/math/generic/atan2f128.cpp | 190 +---
libc/test/shared/shared_math_test.cpp | 2 +
.../llvm-project-overlay/libc/BUILD.bazel | 24 +-
8 files changed, 284 insertions(+), 199 deletions(-)
create mode 100644 libc/shared/math/atan2f128.h
create mode 100644 libc/src/__support/math/atan2f128.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 527bb8d6214ae..6cb583c08dedd 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -25,6 +25,7 @@
#include "math/atan.h"
#include "math/atan2.h"
#include "math/atan2f.h"
+#include "math/atan2f128.h"
#include "math/atanf.h"
#include "math/atanf16.h"
#include "math/erff.h"
diff --git a/libc/shared/math/atan2f128.h b/libc/shared/math/atan2f128.h
new file mode 100644
index 0..d7aee40c69527
--- /dev/null
+++ b/libc/shared/math/atan2f128.h
@@ -0,0 +1,29 @@
+//===-- Shared atan2f128 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_ATAN2F128_H
+#define LLVM_LIBC_SHARED_MATH_ATAN2F128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atan2f128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atan2f128;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_MATH_ATAN2F128_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index c197b19ed29de..caafdc2cbf1d6 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -230,6 +230,21 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ atan2f128
+ HDRS
+atan2f128.h
+ DEPENDS
+.atan_utils
+libc.src.__support.integer_literals
+libc.src.__support.uint128
+libc.src.__support.FPUtil.dyadic_float
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.macros.optimization
+)
+
add_header_library(
atanf
HDRS
diff --git a/libc/src/__support/math/atan2f128.h
b/libc/src/__support/math/atan2f128.h
new file mode 100644
index 0..89efaf1fd72a0
--- /dev/null
+++ b/libc/src/__support/math/atan2f128.h
@@ -0,0 +1,212 @@
+//===-- Implementation header for atan2f128 -*- 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_ATAN2F128_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN2F128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "atan_utils.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/dyadic_float.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/integer_literals.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+#include "src/__support/uint128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+// There are several range reduction steps we can take for atan2(y, x) as
+// follow:
+
+// * Range reduction 1: signness
+// atan2(y, x) will return a number between -PI and PI representing the angle
+// forming by the 0x axis and the vector (x, y) on the 0xy-plane.
+// In particular, we have that:
+// atan2(y, x) = atan( y/x ) if x >= 0 and y >= 0 (I-quadrant)
+// = pi + atan( y/x )if x < 0 and y >= 0 (II-quadrant)
+// = -pi + atan( y/x ) if x < 0 and y < 0 (III-quadrant)
+// = atan( y/x ) if x >= 0 and y < 0 (IV-quadrant)
+// Since atan function is odd, we can use the formula:
+// atan(-u) = -atan(u)
+// to adjust the a
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atan2f implementation to header-only in src/__support/math folder. (PR #150993)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/150993
>From a2300a69d17e0299bda72813c95b3eb5c8d7d883 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Mon, 28 Jul 2025 19:35:03 +0300
Subject: [PATCH] [libc][math] Refactor atan2f implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atan2f.h | 23 ++
libc/src/__support/math/CMakeLists.txt| 17 +
libc/src/__support/math/atan2f.h | 351 ++
.../generic => __support/math}/atan2f_float.h | 21 +-
libc/src/math/generic/CMakeLists.txt | 12 +-
libc/src/math/generic/atan2f.cpp | 328 +---
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 20 +-
10 files changed, 427 insertions(+), 348 deletions(-)
create mode 100644 libc/shared/math/atan2f.h
create mode 100644 libc/src/__support/math/atan2f.h
rename libc/src/{math/generic => __support/math}/atan2f_float.h (95%)
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 0605d918eb2af..527bb8d6214ae 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -24,6 +24,7 @@
#include "math/asinhf16.h"
#include "math/atan.h"
#include "math/atan2.h"
+#include "math/atan2f.h"
#include "math/atanf.h"
#include "math/atanf16.h"
#include "math/erff.h"
diff --git a/libc/shared/math/atan2f.h b/libc/shared/math/atan2f.h
new file mode 100644
index 0..2de09d25e19f8
--- /dev/null
+++ b/libc/shared/math/atan2f.h
@@ -0,0 +1,23 @@
+//===-- Shared atan2f 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_ATAN2F_H
+#define LLVM_LIBC_SHARED_MATH_ATAN2F_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atan2f.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atan2f;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATAN2F_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index bbb07b62552f6..c197b19ed29de 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -213,6 +213,23 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ atan2f
+ HDRS
+atan2f_float.h
+atan2f.h
+ DEPENDS
+.inv_trigf_utils
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.macros.config
+libc.src.__support.macros.optimization
+)
+
add_header_library(
atanf
HDRS
diff --git a/libc/src/__support/math/atan2f.h b/libc/src/__support/math/atan2f.h
new file mode 100644
index 0..e3b19329126f4
--- /dev/null
+++ b/libc/src/__support/math/atan2f.h
@@ -0,0 +1,351 @@
+//===-- Implementation header for atan2f *- 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_ATAN2F_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN2F_H
+
+#include "inv_trigf_utils.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/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+#if defined(LIBC_MATH_HAS_SKIP_ACCURATE_PASS) &&
\
+defined(LIBC_MATH_HAS_INTERMEDIATE_COMP_IN_FLOAT)
+
+// We use float-float implementation to reduce size.
+#include "atan2f_float.h"
+
+#else
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+namespace atan2f_internal {
+
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+
+// Look up tables for accurate pass:
+
+// atan(i/16) with i = 0..16, generated by Sollya with:
+// > for i from 0 to 16 do {
+// a = round(atan(i/16), D, RN);
+// b = round(atan(i/16) - a, D, RN);
+// print("{", b, ",", a, "},");
+// };
+static constexpr fputil::DoubleDouble ATAN_I[17] = {
+{0.0, 0.0},
+{-0x1.c934d86d23
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atanf16 implementation to header-only in src/__support/math folder. (PR #150868)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/150868
>From a4910961081ff7a6cc9aa0ea43aca57db23942c3 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Mon, 28 Jul 2025 05:26:38 +0300
Subject: [PATCH] [libc][math] Refactor atanf16 implementation to header-only
in src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atanf16.h| 28 +
libc/src/__support/math/CMakeLists.txt| 15 +++
libc/src/__support/math/atanf16.h | 119 ++
libc/src/math/generic/CMakeLists.txt | 12 +-
libc/src/math/generic/atanf16.cpp | 95 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 22
9 files changed, 190 insertions(+), 104 deletions(-)
create mode 100644 libc/shared/math/atanf16.h
create mode 100644 libc/src/__support/math/atanf16.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 21536647948f4..bcbe0de56170a 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -24,6 +24,7 @@
#include "math/asinhf16.h"
#include "math/atan.h"
#include "math/atanf.h"
+#include "math/atanf16.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/atanf16.h b/libc/shared/math/atanf16.h
new file mode 100644
index 0..f196907059e01
--- /dev/null
+++ b/libc/shared/math/atanf16.h
@@ -0,0 +1,28 @@
+//===-- Shared atanf16 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_ATANF16_H
+#define LLVM_LIBC_SHARED_MATH_ATANF16_H
+
+#include "shared/libc_common.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/math/atanf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atanf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_ATANF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index 95acc962cc885..04cbd3fd1cc01 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -214,6 +214,21 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ atanf16
+ HDRS
+atanf16.h
+ DEPENDS
+libc.src.__support.FPUtil.cast
+libc.src.__support.FPUtil.except_value_utils
+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.FPUtil.sqrt
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/atanf16.h
b/libc/src/__support/math/atanf16.h
new file mode 100644
index 0..f75d145f36852
--- /dev/null
+++ b/libc/src/__support/math/atanf16.h
@@ -0,0 +1,119 @@
+//===-- Implementation header for atanf16 ---*- 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_ATANF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATANF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/macros/optimization.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float16 atanf16(float16 x) {
+ // Generated by Solly using the following command:
+ // > round(pi/2, SG, RN);
+ constexpr float PI_2 = 0x1.921fb6p0;
+
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+ constexpr size_t N_EXCEPTS = 6;
+
+ constexpr fputil::ExceptValues ATANF16_EXCEPTS{{
+ // (input, RZ output, RU offset, RD offset, RN offset)
+ {0x2745, 0x2744, 1, 0, 1},
+ {0x3099, 0x3090, 1, 0, 1},
+ {0x3c6c, 0x3aae, 1, 0, 1},
+ {0x466e, 0x3daa, 1, 0, 1},
+ {0x48ae, 0x3ddb, 1, 0, 0},
+ {0x5619, 0x3e3d, 1, 0, 1},
+ }};
+#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+
+ using FPBits = fputil::FPBits;
+ FPBits xbits(x);
+
+ uint16_t x
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atan2 implementation to header-only in src/__support/math folder. (PR #150968)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/150968
>From d411e7849f1cf9d0b1f31def4bdd2b126363bd6a Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Mon, 28 Jul 2025 18:07:19 +0300
Subject: [PATCH] [libc][math] Refactor atan2 implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atan2.h | 23 ++
libc/src/__support/math/CMakeLists.txt| 20 +-
libc/src/__support/math/atan2.h | 209 ++
libc/src/math/generic/CMakeLists.txt | 8 +-
libc/src/math/generic/atan2.cpp | 187 +---
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 14 +-
9 files changed, 266 insertions(+), 198 deletions(-)
create mode 100644 libc/shared/math/atan2.h
create mode 100644 libc/src/__support/math/atan2.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index bcbe0de56170a..0605d918eb2af 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -23,6 +23,7 @@
#include "math/asinhf.h"
#include "math/asinhf16.h"
#include "math/atan.h"
+#include "math/atan2.h"
#include "math/atanf.h"
#include "math/atanf16.h"
#include "math/erff.h"
diff --git a/libc/shared/math/atan2.h b/libc/shared/math/atan2.h
new file mode 100644
index 0..894110838817c
--- /dev/null
+++ b/libc/shared/math/atan2.h
@@ -0,0 +1,23 @@
+//===-- Shared atan2 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_ATAN2_H
+#define LLVM_LIBC_SHARED_MATH_ATAN2_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atan2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atan2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATAN2_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index 04cbd3fd1cc01..bbb07b62552f6 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -158,7 +158,7 @@ add_header_library(
asinhf16
HDRS
asinhf16.h
-DEPENDS
+ DEPENDS
.acoshf_utils
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.fp_bits
@@ -176,7 +176,7 @@ add_header_library(
atan_utils
HDRS
atan_utils.h
-DEPENDS
+ DEPENDS
libc.src.__support.integer_literals
libc.src.__support.FPUtil.double_double
libc.src.__support.FPUtil.dyadic_float
@@ -189,7 +189,21 @@ add_header_library(
atan
HDRS
atan.h
-DEPENDS
+ DEPENDS
+.atan_utils
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.macros.optimization
+)
+
+add_header_library(
+ atan2
+ HDRS
+atan2.h
+ DEPENDS
.atan_utils
libc.src.__support.FPUtil.double_double
libc.src.__support.FPUtil.fenv_impl
diff --git a/libc/src/__support/math/atan2.h b/libc/src/__support/math/atan2.h
new file mode 100644
index 0..90ed926c8d75f
--- /dev/null
+++ b/libc/src/__support/math/atan2.h
@@ -0,0 +1,209 @@
+//===-- Implementation header for atan2 -*- 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_ATAN2_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN2_H
+
+#include "atan_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/double_double.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+// There are several range reduction steps we can take for atan2(y, x) as
+// follow:
+
+// * Range reduction 1: signness
+// atan2(y, x) will return a number between -PI and PI representing the angle
+// forming by the 0x axis and the vector (x, y) on the 0xy-plane.
+// In particular, we have that:
+// atan2(y, x) = atan( y/x ) if x >= 0 and y >= 0 (I-quadrant)
+// = pi + atan( y/x )if x < 0 and y >= 0 (II-quadrant)
+//
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atan2f implementation to header-only in src/__support/math folder. (PR #150993)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/150993
>From a2300a69d17e0299bda72813c95b3eb5c8d7d883 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Mon, 28 Jul 2025 19:35:03 +0300
Subject: [PATCH] [libc][math] Refactor atan2f implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atan2f.h | 23 ++
libc/src/__support/math/CMakeLists.txt| 17 +
libc/src/__support/math/atan2f.h | 351 ++
.../generic => __support/math}/atan2f_float.h | 21 +-
libc/src/math/generic/CMakeLists.txt | 12 +-
libc/src/math/generic/atan2f.cpp | 328 +---
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 20 +-
10 files changed, 427 insertions(+), 348 deletions(-)
create mode 100644 libc/shared/math/atan2f.h
create mode 100644 libc/src/__support/math/atan2f.h
rename libc/src/{math/generic => __support/math}/atan2f_float.h (95%)
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 0605d918eb2af..527bb8d6214ae 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -24,6 +24,7 @@
#include "math/asinhf16.h"
#include "math/atan.h"
#include "math/atan2.h"
+#include "math/atan2f.h"
#include "math/atanf.h"
#include "math/atanf16.h"
#include "math/erff.h"
diff --git a/libc/shared/math/atan2f.h b/libc/shared/math/atan2f.h
new file mode 100644
index 0..2de09d25e19f8
--- /dev/null
+++ b/libc/shared/math/atan2f.h
@@ -0,0 +1,23 @@
+//===-- Shared atan2f 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_ATAN2F_H
+#define LLVM_LIBC_SHARED_MATH_ATAN2F_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atan2f.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atan2f;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATAN2F_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index bbb07b62552f6..c197b19ed29de 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -213,6 +213,23 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ atan2f
+ HDRS
+atan2f_float.h
+atan2f.h
+ DEPENDS
+.inv_trigf_utils
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.macros.config
+libc.src.__support.macros.optimization
+)
+
add_header_library(
atanf
HDRS
diff --git a/libc/src/__support/math/atan2f.h b/libc/src/__support/math/atan2f.h
new file mode 100644
index 0..e3b19329126f4
--- /dev/null
+++ b/libc/src/__support/math/atan2f.h
@@ -0,0 +1,351 @@
+//===-- Implementation header for atan2f *- 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_ATAN2F_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN2F_H
+
+#include "inv_trigf_utils.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/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+#if defined(LIBC_MATH_HAS_SKIP_ACCURATE_PASS) &&
\
+defined(LIBC_MATH_HAS_INTERMEDIATE_COMP_IN_FLOAT)
+
+// We use float-float implementation to reduce size.
+#include "atan2f_float.h"
+
+#else
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+namespace atan2f_internal {
+
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+
+// Look up tables for accurate pass:
+
+// atan(i/16) with i = 0..16, generated by Sollya with:
+// > for i from 0 to 16 do {
+// a = round(atan(i/16), D, RN);
+// b = round(atan(i/16) - a, D, RN);
+// print("{", b, ",", a, "},");
+// };
+static constexpr fputil::DoubleDouble ATAN_I[17] = {
+{0.0, 0.0},
+{-0x1.c934d86d23
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atanf16 implementation to header-only in src/__support/math folder. (PR #150868)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/150868
>From a4910961081ff7a6cc9aa0ea43aca57db23942c3 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Mon, 28 Jul 2025 05:26:38 +0300
Subject: [PATCH] [libc][math] Refactor atanf16 implementation to header-only
in src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atanf16.h| 28 +
libc/src/__support/math/CMakeLists.txt| 15 +++
libc/src/__support/math/atanf16.h | 119 ++
libc/src/math/generic/CMakeLists.txt | 12 +-
libc/src/math/generic/atanf16.cpp | 95 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 22
9 files changed, 190 insertions(+), 104 deletions(-)
create mode 100644 libc/shared/math/atanf16.h
create mode 100644 libc/src/__support/math/atanf16.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 21536647948f4..bcbe0de56170a 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -24,6 +24,7 @@
#include "math/asinhf16.h"
#include "math/atan.h"
#include "math/atanf.h"
+#include "math/atanf16.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/atanf16.h b/libc/shared/math/atanf16.h
new file mode 100644
index 0..f196907059e01
--- /dev/null
+++ b/libc/shared/math/atanf16.h
@@ -0,0 +1,28 @@
+//===-- Shared atanf16 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_ATANF16_H
+#define LLVM_LIBC_SHARED_MATH_ATANF16_H
+
+#include "shared/libc_common.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/math/atanf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atanf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_ATANF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index 95acc962cc885..04cbd3fd1cc01 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -214,6 +214,21 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ atanf16
+ HDRS
+atanf16.h
+ DEPENDS
+libc.src.__support.FPUtil.cast
+libc.src.__support.FPUtil.except_value_utils
+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.FPUtil.sqrt
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/atanf16.h
b/libc/src/__support/math/atanf16.h
new file mode 100644
index 0..f75d145f36852
--- /dev/null
+++ b/libc/src/__support/math/atanf16.h
@@ -0,0 +1,119 @@
+//===-- Implementation header for atanf16 ---*- 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_ATANF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATANF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/macros/optimization.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float16 atanf16(float16 x) {
+ // Generated by Solly using the following command:
+ // > round(pi/2, SG, RN);
+ constexpr float PI_2 = 0x1.921fb6p0;
+
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+ constexpr size_t N_EXCEPTS = 6;
+
+ constexpr fputil::ExceptValues ATANF16_EXCEPTS{{
+ // (input, RZ output, RU offset, RD offset, RN offset)
+ {0x2745, 0x2744, 1, 0, 1},
+ {0x3099, 0x3090, 1, 0, 1},
+ {0x3c6c, 0x3aae, 1, 0, 1},
+ {0x466e, 0x3daa, 1, 0, 1},
+ {0x48ae, 0x3ddb, 1, 0, 0},
+ {0x5619, 0x3e3d, 1, 0, 1},
+ }};
+#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+
+ using FPBits = fputil::FPBits;
+ FPBits xbits(x);
+
+ uint16_t x
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atan2f128 implementation to header-only in src/__support/math folder. (PR #151012)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/151012
>From fa5283fdad6b26748a27ab6aa39b7e6c2a3d179d Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Mon, 28 Jul 2025 21:14:48 +0300
Subject: [PATCH 1/2] [libc][math] Refactor atan2f128 implementation to
header-only in src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atan2f128.h | 29 +++
libc/src/__support/math/CMakeLists.txt| 15 ++
libc/src/__support/math/atan2f128.h | 212 ++
libc/src/math/generic/CMakeLists.txt | 10 +-
libc/src/math/generic/atan2f128.cpp | 190 +---
libc/test/shared/shared_math_test.cpp | 2 +
.../llvm-project-overlay/libc/BUILD.bazel | 24 +-
8 files changed, 284 insertions(+), 199 deletions(-)
create mode 100644 libc/shared/math/atan2f128.h
create mode 100644 libc/src/__support/math/atan2f128.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 527bb8d6214ae..6cb583c08dedd 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -25,6 +25,7 @@
#include "math/atan.h"
#include "math/atan2.h"
#include "math/atan2f.h"
+#include "math/atan2f128.h"
#include "math/atanf.h"
#include "math/atanf16.h"
#include "math/erff.h"
diff --git a/libc/shared/math/atan2f128.h b/libc/shared/math/atan2f128.h
new file mode 100644
index 0..d7aee40c69527
--- /dev/null
+++ b/libc/shared/math/atan2f128.h
@@ -0,0 +1,29 @@
+//===-- Shared atan2f128 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_ATAN2F128_H
+#define LLVM_LIBC_SHARED_MATH_ATAN2F128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atan2f128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atan2f128;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_MATH_ATAN2F128_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index c197b19ed29de..caafdc2cbf1d6 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -230,6 +230,21 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ atan2f128
+ HDRS
+atan2f128.h
+ DEPENDS
+.atan_utils
+libc.src.__support.integer_literals
+libc.src.__support.uint128
+libc.src.__support.FPUtil.dyadic_float
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.macros.optimization
+)
+
add_header_library(
atanf
HDRS
diff --git a/libc/src/__support/math/atan2f128.h
b/libc/src/__support/math/atan2f128.h
new file mode 100644
index 0..89efaf1fd72a0
--- /dev/null
+++ b/libc/src/__support/math/atan2f128.h
@@ -0,0 +1,212 @@
+//===-- Implementation header for atan2f128 -*- 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_ATAN2F128_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN2F128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "atan_utils.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/dyadic_float.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/integer_literals.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+#include "src/__support/uint128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+// There are several range reduction steps we can take for atan2(y, x) as
+// follow:
+
+// * Range reduction 1: signness
+// atan2(y, x) will return a number between -PI and PI representing the angle
+// forming by the 0x axis and the vector (x, y) on the 0xy-plane.
+// In particular, we have that:
+// atan2(y, x) = atan( y/x ) if x >= 0 and y >= 0 (I-quadrant)
+// = pi + atan( y/x )if x < 0 and y >= 0 (II-quadrant)
+// = -pi + atan( y/x ) if x < 0 and y < 0 (III-quadrant)
+// = atan( y/x ) if x >= 0 and y < 0 (IV-quadrant)
+// Since atan function is odd, we can use the formula:
+// atan(-u) = -atan(u)
+// to adjust the a
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atan2 implementation to header-only in src/__support/math folder. (PR #150968)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/150968
>From d411e7849f1cf9d0b1f31def4bdd2b126363bd6a Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Mon, 28 Jul 2025 18:07:19 +0300
Subject: [PATCH] [libc][math] Refactor atan2 implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atan2.h | 23 ++
libc/src/__support/math/CMakeLists.txt| 20 +-
libc/src/__support/math/atan2.h | 209 ++
libc/src/math/generic/CMakeLists.txt | 8 +-
libc/src/math/generic/atan2.cpp | 187 +---
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 14 +-
9 files changed, 266 insertions(+), 198 deletions(-)
create mode 100644 libc/shared/math/atan2.h
create mode 100644 libc/src/__support/math/atan2.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index bcbe0de56170a..0605d918eb2af 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -23,6 +23,7 @@
#include "math/asinhf.h"
#include "math/asinhf16.h"
#include "math/atan.h"
+#include "math/atan2.h"
#include "math/atanf.h"
#include "math/atanf16.h"
#include "math/erff.h"
diff --git a/libc/shared/math/atan2.h b/libc/shared/math/atan2.h
new file mode 100644
index 0..894110838817c
--- /dev/null
+++ b/libc/shared/math/atan2.h
@@ -0,0 +1,23 @@
+//===-- Shared atan2 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_ATAN2_H
+#define LLVM_LIBC_SHARED_MATH_ATAN2_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atan2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atan2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATAN2_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index 04cbd3fd1cc01..bbb07b62552f6 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -158,7 +158,7 @@ add_header_library(
asinhf16
HDRS
asinhf16.h
-DEPENDS
+ DEPENDS
.acoshf_utils
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.fp_bits
@@ -176,7 +176,7 @@ add_header_library(
atan_utils
HDRS
atan_utils.h
-DEPENDS
+ DEPENDS
libc.src.__support.integer_literals
libc.src.__support.FPUtil.double_double
libc.src.__support.FPUtil.dyadic_float
@@ -189,7 +189,21 @@ add_header_library(
atan
HDRS
atan.h
-DEPENDS
+ DEPENDS
+.atan_utils
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.macros.optimization
+)
+
+add_header_library(
+ atan2
+ HDRS
+atan2.h
+ DEPENDS
.atan_utils
libc.src.__support.FPUtil.double_double
libc.src.__support.FPUtil.fenv_impl
diff --git a/libc/src/__support/math/atan2.h b/libc/src/__support/math/atan2.h
new file mode 100644
index 0..90ed926c8d75f
--- /dev/null
+++ b/libc/src/__support/math/atan2.h
@@ -0,0 +1,209 @@
+//===-- Implementation header for atan2 -*- 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_ATAN2_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN2_H
+
+#include "atan_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/double_double.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+// There are several range reduction steps we can take for atan2(y, x) as
+// follow:
+
+// * Range reduction 1: signness
+// atan2(y, x) will return a number between -PI and PI representing the angle
+// forming by the 0x axis and the vector (x, y) on the 0xy-plane.
+// In particular, we have that:
+// atan2(y, x) = atan( y/x ) if x >= 0 and y >= 0 (I-quadrant)
+// = pi + atan( y/x )if x < 0 and y >= 0 (II-quadrant)
+//
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atanf implementation to header-only in src/__support/math folder. (PR #150854)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/150854
>From 3930b0f9886d2ec449e6f2126120f5ba3e7e48f7 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Mon, 28 Jul 2025 00:37:42 +0300
Subject: [PATCH] [libc][math] Refactor atanf implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atanf.h | 23
libc/src/__support/math/CMakeLists.txt| 15 ++
libc/src/__support/math/atanf.h | 129 ++
libc/src/math/generic/CMakeLists.txt | 9 +-
libc/src/math/generic/atanf.cpp | 110 +--
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 22 ++-
9 files changed, 188 insertions(+), 123 deletions(-)
create mode 100644 libc/shared/math/atanf.h
create mode 100644 libc/src/__support/math/atanf.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 70b1b7b0bef09..21536647948f4 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -23,6 +23,7 @@
#include "math/asinhf.h"
#include "math/asinhf16.h"
#include "math/atan.h"
+#include "math/atanf.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/atanf.h b/libc/shared/math/atanf.h
new file mode 100644
index 0..858d727bd6698
--- /dev/null
+++ b/libc/shared/math/atanf.h
@@ -0,0 +1,23 @@
+//===-- Shared atanf 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_ATANF_H
+#define LLVM_LIBC_SHARED_MATH_ATANF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atanf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atanf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATANF_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index cc02920c2a1ef..95acc962cc885 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -199,6 +199,21 @@ DEPENDS
libc.src.__support.macros.optimization
)
+add_header_library(
+ atanf
+ HDRS
+atanf.h
+ DEPENDS
+.inv_trigf_utils
+libc.src.__support.FPUtil.except_value_utils
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.FPUtil.rounding_mode
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/atanf.h b/libc/src/__support/math/atanf.h
new file mode 100644
index 0..92799dc8db3cc
--- /dev/null
+++ b/libc/src/__support/math/atanf.h
@@ -0,0 +1,129 @@
+//===-- Implementation header for atanf -*- 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_ATANF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATANF_H
+
+#include "inv_trigf_utils.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float atanf(float x) {
+ using namespace inv_trigf_utils_internal;
+ using FPBits = typename fputil::FPBits;
+
+ constexpr double FINAL_SIGN[2] = {1.0, -1.0};
+ constexpr double SIGNED_PI_OVER_2[2] = {0x1.921fb54442d18p0,
+ -0x1.921fb54442d18p0};
+
+ FPBits x_bits(x);
+ Sign sign = x_bits.sign();
+ x_bits.set_sign(Sign::POS);
+ uint32_t x_abs = x_bits.uintval();
+
+ // x is inf or nan, |x| < 2^-4 or |x|= > 16.
+ if (LIBC_UNLIKELY(x_abs <= 0x3d80'U || x_abs >= 0x4180'U)) {
+double x_d = static_cast(x);
+double const_term = 0.0;
+if (LIBC_UNLIKELY(x_abs >= 0x4180')) {
+ // atan(+-Inf) = +-pi/2.
+ if (x_bits.is_inf()) {
+volatile double sign_pi_over_2 = SIGNED_PI_OVER_2[sign.is_neg()];
+return static_cast(sign_pi_over_2);
+ }
+ if (x_bits.is_nan())
+
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atanhf16 implementation to header-only in src/__support/math folder. (PR #151779)
https://github.com/bassiounix created
https://github.com/llvm/llvm-project/pull/151779
None
>From 135f49e154cc7e484ede397df8ff1591b4bc59eb Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Sat, 2 Aug 2025 02:04:26 +0300
Subject: [PATCH] [libc][math] Refactor atanhf16 implementation to header-only
in src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atanhf16.h | 28 +++
libc/src/__support/math/CMakeLists.txt| 15 ++
libc/src/__support/math/atanhf16.h| 234 ++
libc/src/math/generic/CMakeLists.txt | 12 +-
libc/src/math/generic/atanhf16.cpp| 86 +--
libc/src/math/generic/common_constants.cpp| 78 --
libc/src/math/generic/common_constants.h | 8 -
libc/src/math/generic/explogxf.h | 43
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 22 ++
12 files changed, 305 insertions(+), 224 deletions(-)
create mode 100644 libc/shared/math/atanhf16.h
create mode 100644 libc/src/__support/math/atanhf16.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index ddf219ece8ff1..7fb736b78efa5 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -29,6 +29,7 @@
#include "math/atanf.h"
#include "math/atanf16.h"
#include "math/atanhf.h"
+#include "math/atanhf16.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/atanhf16.h b/libc/shared/math/atanhf16.h
new file mode 100644
index 0..b7b5d77ae98c8
--- /dev/null
+++ b/libc/shared/math/atanhf16.h
@@ -0,0 +1,28 @@
+//===-- Shared atanhf16 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_ATANHF16_H
+#define LLVM_LIBC_SHARED_MATH_ATANHF16_H
+
+#include "shared/libc_common.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/math/atanhf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atanhf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_ATANHF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index 500dd9de2c555..9631ab5be7d3b 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -286,6 +286,21 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ atanhf16
+ HDRS
+atanhf16.h
+ DEPENDS
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.FPUtil.cast
+libc.src.__support.FPUtil.except_value_utils
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.macros.config
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/atanhf16.h
b/libc/src/__support/math/atanhf16.h
new file mode 100644
index 0..9146e1e31b815
--- /dev/null
+++ b/libc/src/__support/math/atanhf16.h
@@ -0,0 +1,234 @@
+//===-- Implementation header for atanhf16 --*- 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_ATANHF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATANHF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+namespace atanhf16_internal {
+
+// Lookup table for logf(f) = logf(1 + n*2^(-7)) where n = 0..127,
+// computed and stored as float precision constants.
+// Generated by Sollya with the following commands:
+// display = hexadecimal;
+// for n from 0 to 127 do { print(single(1 / (1 + n / 128.0))); };
+static constexpr float ONE_OVER_F_FLOAT[128] = {
+0x1p0f, 0x1.fc07fp-1f, 0x1.f81f82p-1f, 0x1.f4465ap-1f,
+0x1.f07c2p-1f, 0x1.ecc07cp-1f, 0x1.e9131ap-1f, 0x1.e573acp-1f,
+0x1.e1e1e2p-1
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atanhf16 implementation to header-only in src/__support/math folder. (PR #151779)
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/151779?utm_source=stack-comment-downstack-mergeability-warning"; > >on Graphite. > https://graphite.dev/docs/merge-pull-requests";>Learn more * **#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"/> 👈 https://app.graphite.dev/github/pr/llvm/llvm-project/151779?utm_source=stack-comment-view-in-graphite"; target="_blank">(View in Graphite) * **#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/151779 ___ 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 atanhf16 implementation to header-only in src/__support/math folder. (PR #151779)
https://github.com/bassiounix edited https://github.com/llvm/llvm-project/pull/151779 ___ 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 atanhf16 implementation to header-only in src/__support/math folder. (PR #151779)
https://github.com/bassiounix ready_for_review https://github.com/llvm/llvm-project/pull/151779 ___ 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 atanhf implementation to header-only in src/__support/math folder. (PR #151399)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/151399
>From 4c79b81484f25c99e8e840d5b994264e48a8b962 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Thu, 31 Jul 2025 00:41:13 +0300
Subject: [PATCH] [libc][math] Refactor atanhf implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atanhf.h | 23 ++
libc/src/__support/math/CMakeLists.txt| 11 +++
libc/src/__support/math/atanhf.h | 76 +++
libc/src/math/generic/CMakeLists.txt | 5 +-
libc/src/math/generic/atanhf.cpp | 56 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 20 +++--
9 files changed, 129 insertions(+), 65 deletions(-)
create mode 100644 libc/shared/math/atanhf.h
create mode 100644 libc/src/__support/math/atanhf.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 6cb583c08dedd..ddf219ece8ff1 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -28,6 +28,7 @@
#include "math/atan2f128.h"
#include "math/atanf.h"
#include "math/atanf16.h"
+#include "math/atanhf.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/atanhf.h b/libc/shared/math/atanhf.h
new file mode 100644
index 0..763fb3e00a659
--- /dev/null
+++ b/libc/shared/math/atanhf.h
@@ -0,0 +1,23 @@
+//===-- Shared atanhf 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_ATANHF_H
+#define LLVM_LIBC_SHARED_MATH_ATANHF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atanhf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atanhf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATANHF_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index caafdc2cbf1d6..500dd9de2c555 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -275,6 +275,17 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ atanhf
+ HDRS
+atanhf.h
+ DEPENDS
+.acoshf_utils
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/atanhf.h b/libc/src/__support/math/atanhf.h
new file mode 100644
index 0..b3ee5bbb4d408
--- /dev/null
+++ b/libc/src/__support/math/atanhf.h
@@ -0,0 +1,76 @@
+//===-- Implementation header for atanhf *- 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_ATANHF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATANHF_H
+
+#include "acoshf_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float atanhf(float x) {
+ using namespace acoshf_internal;
+ using FPBits = typename fputil::FPBits;
+
+ FPBits xbits(x);
+ Sign sign = xbits.sign();
+ uint32_t x_abs = xbits.abs().uintval();
+
+ // |x| >= 1.0
+ if (LIBC_UNLIKELY(x_abs >= 0x3F80'U)) {
+if (xbits.is_nan()) {
+ if (xbits.is_signaling_nan()) {
+fputil::raise_except_if_required(FE_INVALID);
+return FPBits::quiet_nan().get_val();
+ }
+ return x;
+}
+// |x| == 1.0
+if (x_abs == 0x3F80'U) {
+ fputil::set_errno_if_required(ERANGE);
+ fputil::raise_except_if_required(FE_DIVBYZERO);
+ return FPBits::inf(sign).get_val();
+} else {
+ fputil::set_errno_if_required(EDOM);
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+}
+ }
+
+ // |x| < ~0.10
+ if (LIBC_UNLIKELY(x_abs <= 0x3dcc'U)) {
+// |x| <= 2^-26
+if (LIBC_UNLIKELY(x_abs <= 0x3280'U)) {
+ return static_cast(LIBC_UNLIKELY(x_abs == 0)
+? x
+: (x + 0x1.5p-2 * x * x * x));
+}
+
+double xdbl = x;
+double x2
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atanhf implementation to header-only in src/__support/math folder. (PR #151399)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/151399
>From 4c79b81484f25c99e8e840d5b994264e48a8b962 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Thu, 31 Jul 2025 00:41:13 +0300
Subject: [PATCH] [libc][math] Refactor atanhf implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atanhf.h | 23 ++
libc/src/__support/math/CMakeLists.txt| 11 +++
libc/src/__support/math/atanhf.h | 76 +++
libc/src/math/generic/CMakeLists.txt | 5 +-
libc/src/math/generic/atanhf.cpp | 56 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 20 +++--
9 files changed, 129 insertions(+), 65 deletions(-)
create mode 100644 libc/shared/math/atanhf.h
create mode 100644 libc/src/__support/math/atanhf.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 6cb583c08dedd..ddf219ece8ff1 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -28,6 +28,7 @@
#include "math/atan2f128.h"
#include "math/atanf.h"
#include "math/atanf16.h"
+#include "math/atanhf.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/atanhf.h b/libc/shared/math/atanhf.h
new file mode 100644
index 0..763fb3e00a659
--- /dev/null
+++ b/libc/shared/math/atanhf.h
@@ -0,0 +1,23 @@
+//===-- Shared atanhf 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_ATANHF_H
+#define LLVM_LIBC_SHARED_MATH_ATANHF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atanhf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atanhf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATANHF_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index caafdc2cbf1d6..500dd9de2c555 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -275,6 +275,17 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ atanhf
+ HDRS
+atanhf.h
+ DEPENDS
+.acoshf_utils
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/atanhf.h b/libc/src/__support/math/atanhf.h
new file mode 100644
index 0..b3ee5bbb4d408
--- /dev/null
+++ b/libc/src/__support/math/atanhf.h
@@ -0,0 +1,76 @@
+//===-- Implementation header for atanhf *- 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_ATANHF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATANHF_H
+
+#include "acoshf_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float atanhf(float x) {
+ using namespace acoshf_internal;
+ using FPBits = typename fputil::FPBits;
+
+ FPBits xbits(x);
+ Sign sign = xbits.sign();
+ uint32_t x_abs = xbits.abs().uintval();
+
+ // |x| >= 1.0
+ if (LIBC_UNLIKELY(x_abs >= 0x3F80'U)) {
+if (xbits.is_nan()) {
+ if (xbits.is_signaling_nan()) {
+fputil::raise_except_if_required(FE_INVALID);
+return FPBits::quiet_nan().get_val();
+ }
+ return x;
+}
+// |x| == 1.0
+if (x_abs == 0x3F80'U) {
+ fputil::set_errno_if_required(ERANGE);
+ fputil::raise_except_if_required(FE_DIVBYZERO);
+ return FPBits::inf(sign).get_val();
+} else {
+ fputil::set_errno_if_required(EDOM);
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+}
+ }
+
+ // |x| < ~0.10
+ if (LIBC_UNLIKELY(x_abs <= 0x3dcc'U)) {
+// |x| <= 2^-26
+if (LIBC_UNLIKELY(x_abs <= 0x3280'U)) {
+ return static_cast(LIBC_UNLIKELY(x_abs == 0)
+? x
+: (x + 0x1.5p-2 * x * x * x));
+}
+
+double xdbl = x;
+double x2
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atanhf implementation to header-only in src/__support/math folder. (PR #151399)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/151399
>From 909f8e78ebe538dc929bbfa2d80c7e79df6a0194 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Thu, 31 Jul 2025 00:41:13 +0300
Subject: [PATCH] [libc][math] Refactor atanhf implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atanhf.h | 23 ++
libc/src/__support/math/CMakeLists.txt| 11 +++
libc/src/__support/math/atanhf.h | 76 +++
libc/src/math/generic/CMakeLists.txt | 5 +-
libc/src/math/generic/atanhf.cpp | 56 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 20 +++--
9 files changed, 129 insertions(+), 65 deletions(-)
create mode 100644 libc/shared/math/atanhf.h
create mode 100644 libc/src/__support/math/atanhf.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 6cb583c08dedd..ddf219ece8ff1 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -28,6 +28,7 @@
#include "math/atan2f128.h"
#include "math/atanf.h"
#include "math/atanf16.h"
+#include "math/atanhf.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/atanhf.h b/libc/shared/math/atanhf.h
new file mode 100644
index 0..763fb3e00a659
--- /dev/null
+++ b/libc/shared/math/atanhf.h
@@ -0,0 +1,23 @@
+//===-- Shared atanhf 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_ATANHF_H
+#define LLVM_LIBC_SHARED_MATH_ATANHF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atanhf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atanhf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATANHF_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index caafdc2cbf1d6..500dd9de2c555 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -275,6 +275,17 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ atanhf
+ HDRS
+atanhf.h
+ DEPENDS
+.acoshf_utils
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/atanhf.h b/libc/src/__support/math/atanhf.h
new file mode 100644
index 0..b3ee5bbb4d408
--- /dev/null
+++ b/libc/src/__support/math/atanhf.h
@@ -0,0 +1,76 @@
+//===-- Implementation header for atanhf *- 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_ATANHF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATANHF_H
+
+#include "acoshf_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float atanhf(float x) {
+ using namespace acoshf_internal;
+ using FPBits = typename fputil::FPBits;
+
+ FPBits xbits(x);
+ Sign sign = xbits.sign();
+ uint32_t x_abs = xbits.abs().uintval();
+
+ // |x| >= 1.0
+ if (LIBC_UNLIKELY(x_abs >= 0x3F80'U)) {
+if (xbits.is_nan()) {
+ if (xbits.is_signaling_nan()) {
+fputil::raise_except_if_required(FE_INVALID);
+return FPBits::quiet_nan().get_val();
+ }
+ return x;
+}
+// |x| == 1.0
+if (x_abs == 0x3F80'U) {
+ fputil::set_errno_if_required(ERANGE);
+ fputil::raise_except_if_required(FE_DIVBYZERO);
+ return FPBits::inf(sign).get_val();
+} else {
+ fputil::set_errno_if_required(EDOM);
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+}
+ }
+
+ // |x| < ~0.10
+ if (LIBC_UNLIKELY(x_abs <= 0x3dcc'U)) {
+// |x| <= 2^-26
+if (LIBC_UNLIKELY(x_abs <= 0x3280'U)) {
+ return static_cast(LIBC_UNLIKELY(x_abs == 0)
+? x
+: (x + 0x1.5p-2 * x * x * x));
+}
+
+double xdbl = x;
+double x2
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atanhf implementation to header-only in src/__support/math folder. (PR #151399)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/151399
>From 909f8e78ebe538dc929bbfa2d80c7e79df6a0194 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Thu, 31 Jul 2025 00:41:13 +0300
Subject: [PATCH] [libc][math] Refactor atanhf implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atanhf.h | 23 ++
libc/src/__support/math/CMakeLists.txt| 11 +++
libc/src/__support/math/atanhf.h | 76 +++
libc/src/math/generic/CMakeLists.txt | 5 +-
libc/src/math/generic/atanhf.cpp | 56 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 20 +++--
9 files changed, 129 insertions(+), 65 deletions(-)
create mode 100644 libc/shared/math/atanhf.h
create mode 100644 libc/src/__support/math/atanhf.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 6cb583c08dedd..ddf219ece8ff1 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -28,6 +28,7 @@
#include "math/atan2f128.h"
#include "math/atanf.h"
#include "math/atanf16.h"
+#include "math/atanhf.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/atanhf.h b/libc/shared/math/atanhf.h
new file mode 100644
index 0..763fb3e00a659
--- /dev/null
+++ b/libc/shared/math/atanhf.h
@@ -0,0 +1,23 @@
+//===-- Shared atanhf 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_ATANHF_H
+#define LLVM_LIBC_SHARED_MATH_ATANHF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atanhf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atanhf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATANHF_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index caafdc2cbf1d6..500dd9de2c555 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -275,6 +275,17 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ atanhf
+ HDRS
+atanhf.h
+ DEPENDS
+.acoshf_utils
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/atanhf.h b/libc/src/__support/math/atanhf.h
new file mode 100644
index 0..b3ee5bbb4d408
--- /dev/null
+++ b/libc/src/__support/math/atanhf.h
@@ -0,0 +1,76 @@
+//===-- Implementation header for atanhf *- 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_ATANHF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATANHF_H
+
+#include "acoshf_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float atanhf(float x) {
+ using namespace acoshf_internal;
+ using FPBits = typename fputil::FPBits;
+
+ FPBits xbits(x);
+ Sign sign = xbits.sign();
+ uint32_t x_abs = xbits.abs().uintval();
+
+ // |x| >= 1.0
+ if (LIBC_UNLIKELY(x_abs >= 0x3F80'U)) {
+if (xbits.is_nan()) {
+ if (xbits.is_signaling_nan()) {
+fputil::raise_except_if_required(FE_INVALID);
+return FPBits::quiet_nan().get_val();
+ }
+ return x;
+}
+// |x| == 1.0
+if (x_abs == 0x3F80'U) {
+ fputil::set_errno_if_required(ERANGE);
+ fputil::raise_except_if_required(FE_DIVBYZERO);
+ return FPBits::inf(sign).get_val();
+} else {
+ fputil::set_errno_if_required(EDOM);
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+}
+ }
+
+ // |x| < ~0.10
+ if (LIBC_UNLIKELY(x_abs <= 0x3dcc'U)) {
+// |x| <= 2^-26
+if (LIBC_UNLIKELY(x_abs <= 0x3280'U)) {
+ return static_cast(LIBC_UNLIKELY(x_abs == 0)
+? x
+: (x + 0x1.5p-2 * x * x * x));
+}
+
+double xdbl = x;
+double x2
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor cos implementation to header-only in src/__support/math folder. (PR #151883)
https://github.com/bassiounix created
https://github.com/llvm/llvm-project/pull/151883
None
>From a9b861153d10cc62444d1d893a23d0292848937e Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Sun, 3 Aug 2025 22:24:35 +0300
Subject: [PATCH] [libc][math] Refactor cos implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/cos.h| 23 +++
libc/src/__support/math/CMakeLists.txt| 47 +
libc/src/__support/math/cos.h | 173 ++
.../math}/range_reduction_double_common.h | 18 +-
.../math}/range_reduction_double_fma.h| 10 +-
.../math}/range_reduction_double_nofma.h | 10 +-
.../generic => __support/math}/sincos_eval.h | 8 +-
libc/src/math/generic/CMakeLists.txt | 50 +
libc/src/math/generic/cos.cpp | 155 +---
libc/src/math/generic/sin.cpp | 14 +-
libc/src/math/generic/sincos.cpp | 14 +-
libc/src/math/generic/tan.cpp | 7 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 3 +-
.../llvm-project-overlay/libc/BUILD.bazel | 101 +-
16 files changed, 368 insertions(+), 267 deletions(-)
create mode 100644 libc/shared/math/cos.h
create mode 100644 libc/src/__support/math/cos.h
rename libc/src/{math/generic =>
__support/math}/range_reduction_double_common.h (98%)
rename libc/src/{math/generic => __support/math}/range_reduction_double_fma.h
(98%)
rename libc/src/{math/generic =>
__support/math}/range_reduction_double_nofma.h (98%)
rename libc/src/{math/generic => __support/math}/sincos_eval.h (98%)
diff --git a/libc/shared/math.h b/libc/shared/math.h
index ea645f0afedbc..a5581ed4272a3 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -32,6 +32,7 @@
#include "math/atanhf16.h"
#include "math/cbrt.h"
#include "math/cbrtf.h"
+#include "math/cos.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/cos.h b/libc/shared/math/cos.h
new file mode 100644
index 0..c498550f098b4
--- /dev/null
+++ b/libc/shared/math/cos.h
@@ -0,0 +1,23 @@
+//===-- Shared cos 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_COS_H
+#define LLVM_LIBC_SHARED_MATH_COS_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/cos.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::cos;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_COS_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index fe928a8fadd5e..24844063fcd24 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -357,6 +357,24 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ cos
+ HDRS
+cos.h
+ DEPENDS
+libc.src.__support.math.sincos_eval
+libc.hdr.errno_macros
+libc.src.errno.errno
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.dyadic_float
+libc.src.__support.FPUtil.except_value_utils
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.math.range_reduction_double
+libc.src.__support.macros.optimization
+)
+
+
add_header_library(
erff
HDRS
@@ -613,3 +631,32 @@ add_header_library(
libc.src.__support.macros.optimization
libc.src.__support.macros.properties.cpu_features
)
+
+add_header_library(
+ range_reduction_double
+ HDRS
+range_reduction_double_common.h
+range_reduction_double_fma.h
+range_reduction_double_nofma.h
+ DEPENDS
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.dyadic_float
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.fma
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.common
+libc.src.__support.integer_literals
+)
+
+add_header_library(
+ sincos_eval
+ HDRS
+sincos_eval.h
+ DEPENDS
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.dyadic_float
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.integer_literals
+)
diff --git a/libc/src/__support/math/cos.h b/libc/src/__support/math/cos.h
new file mode 100644
index 0..0802f9e4f6e49
--- /dev/null
+++ b/libc/src/__support/math/cos.h
@@ -0,0 +1,173 @@
+//===-- Implementation header for cos --
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor cos implementation to header-only in src/__support/math folder. (PR #151883)
https://github.com/bassiounix ready_for_review https://github.com/llvm/llvm-project/pull/151883 ___ 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 cos implementation to header-only in src/__support/math folder. (PR #151883)
https://github.com/bassiounix edited https://github.com/llvm/llvm-project/pull/151883 ___ 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 cos implementation to header-only in src/__support/math folder. (PR #151883)
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/151883?utm_source=stack-comment-downstack-mergeability-warning"; > >on Graphite. > https://graphite.dev/docs/merge-pull-requests";>Learn more * **#151883** https://app.graphite.dev/github/pr/llvm/llvm-project/151883?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/151883?utm_source=stack-comment-view-in-graphite"; target="_blank">(View in Graphite) * **#151846** https://app.graphite.dev/github/pr/llvm/llvm-project/151846?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> * **#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"/> * **#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/151883 ___ 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 cbrtf implementation to header-only in src/__support/math folder. (PR #151846)
https://github.com/bassiounix edited https://github.com/llvm/llvm-project/pull/151846 ___ 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 cbrtf implementation to header-only in src/__support/math folder. (PR #151846)
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/151846?utm_source=stack-comment-downstack-mergeability-warning"; > >on Graphite. > https://graphite.dev/docs/merge-pull-requests";>Learn more * **#151846** https://app.graphite.dev/github/pr/llvm/llvm-project/151846?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/151846?utm_source=stack-comment-view-in-graphite"; target="_blank">(View in Graphite) * **#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"/> * **#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/151846 ___ 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 cbrtf implementation to header-only in src/__support/math folder. (PR #151846)
https://github.com/bassiounix ready_for_review https://github.com/llvm/llvm-project/pull/151846 ___ 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 cbrtf implementation to header-only in src/__support/math folder. (PR #151846)
https://github.com/bassiounix created
https://github.com/llvm/llvm-project/pull/151846
None
>From ea293895fc08b57dd21e46744298e4631c4c9bc7 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Sun, 3 Aug 2025 07:05:00 +0300
Subject: [PATCH] [libc][math] Refactor cbrtf implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/cbrtf.h | 23 +++
libc/src/__support/math/CMakeLists.txt| 11 ++
libc/src/__support/math/cbrtf.h | 161 ++
libc/src/math/generic/CMakeLists.txt | 6 +-
libc/src/math/generic/cbrtf.cpp | 147 +---
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 14 +-
9 files changed, 214 insertions(+), 151 deletions(-)
create mode 100644 libc/shared/math/cbrtf.h
create mode 100644 libc/src/__support/math/cbrtf.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 3714f380a27dc..ea645f0afedbc 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -31,6 +31,7 @@
#include "math/atanhf.h"
#include "math/atanhf16.h"
#include "math/cbrt.h"
+#include "math/cbrtf.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/cbrtf.h b/libc/shared/math/cbrtf.h
new file mode 100644
index 0..09b86bed3fb7e
--- /dev/null
+++ b/libc/shared/math/cbrtf.h
@@ -0,0 +1,23 @@
+//===-- Shared cbrtf 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 LIBC_SHARED_MATH_CBRTF_H
+#define LIBC_SHARED_MATH_CBRTF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/cbrtf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::cbrtf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_SHARED_MATH_CBRTF_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index e1076edf1e61c..fe928a8fadd5e 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -346,6 +346,17 @@ add_header_library(
libc.src.__support.integer_literals
)
+add_header_library(
+ cbrtf
+ HDRS
+cbrtf.h
+ DEPENDS
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.macros.optimization
+)
+
add_header_library(
erff
HDRS
diff --git a/libc/src/__support/math/cbrtf.h b/libc/src/__support/math/cbrtf.h
new file mode 100644
index 0..f82892bbbe61b
--- /dev/null
+++ b/libc/src/__support/math/cbrtf.h
@@ -0,0 +1,161 @@
+//===-- Implementation header for cbrtf -*- 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_CBRTF_H
+#define LIBC_SRC___SUPPORT_MATH_CBRTF_H
+
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float cbrtf(float x) {
+ // Look up table for 2^(i/3) for i = 0, 1, 2.
+ constexpr double CBRT2[3] = {1.0, 0x1.428a2f98d728bp0, 0x1.965fea53d6e3dp0};
+
+ // Degree-7 polynomials approximation of ((1 + x)^(1/3) - 1)/x for 0 <= x <=
1
+ // generated by Sollya with:
+ // > for i from 0 to 15 do {
+ // P = fpminimax(((1 + x)^(1/3) - 1)/x, 6, [|D...|], [i/16, (i + 1)/16]);
+ // print("{", coeff(P, 0), ",", coeff(P, 1), ",", coeff(P, 2), ",",
+ // coeff(P, 3), ",", coeff(P, 4), ",", coeff(P, 5), ",",
+ // coeff(P, 6), "},");
+ // };
+ // Then (1 + x)^(1/3) ~ 1 + x * P(x).
+ constexpr double COEFFS[16][7] = {
+ {0x1.554ebp-2, -0x1.c71c71c678c0cp-4, 0x1.f9add2776de81p-5,
+ -0x1.511e10aa964a7p-5, 0x1.ee44165937fa2p-6, -0x1.7c5c9e059345dp-6,
+ 0x1.047f75e0aff14p-6},
+ {0x1.554d1149ap-2, -0x1.c71c676fcb5bp-4, 0x1.f9ab127dc57ebp-5,
+ -0x1.50ea8fd1d4c15p-5, 0x1.e9d68f28ced43p-6, -0x1.60e0e1e661311p-6,
+ 0x1.716eca1d6e3bcp-7},
+ {0x1.546377d45p-2, -0x1.c71bc1c6d49d2p-4, 0x1.f9924cc0ed24dp-5,
+ -0x1.4fea3beb53b3bp-5, 0x1.de028a9a07b1bp-6, -0x1.3b090d2233524p-6,
+ 0x1.0ae
[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
[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)
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 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 atanhf implementation to header-only in src/__support/math folder. (PR #151399)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/151399
>From 2fd12f451dbc98bc078fc3d86e71227e66950e3d Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Thu, 31 Jul 2025 00:41:13 +0300
Subject: [PATCH] [libc][math] Refactor atanhf implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atanhf.h | 23 ++
libc/src/__support/math/CMakeLists.txt| 11 +++
libc/src/__support/math/atanhf.h | 76 +++
libc/src/math/generic/CMakeLists.txt | 5 +-
libc/src/math/generic/atanhf.cpp | 56 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 20 +++--
9 files changed, 129 insertions(+), 65 deletions(-)
create mode 100644 libc/shared/math/atanhf.h
create mode 100644 libc/src/__support/math/atanhf.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 6cb583c08dedd..ddf219ece8ff1 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -28,6 +28,7 @@
#include "math/atan2f128.h"
#include "math/atanf.h"
#include "math/atanf16.h"
+#include "math/atanhf.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/atanhf.h b/libc/shared/math/atanhf.h
new file mode 100644
index 0..763fb3e00a659
--- /dev/null
+++ b/libc/shared/math/atanhf.h
@@ -0,0 +1,23 @@
+//===-- Shared atanhf 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_ATANHF_H
+#define LLVM_LIBC_SHARED_MATH_ATANHF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atanhf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atanhf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATANHF_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index caafdc2cbf1d6..500dd9de2c555 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -275,6 +275,17 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ atanhf
+ HDRS
+atanhf.h
+ DEPENDS
+.acoshf_utils
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/atanhf.h b/libc/src/__support/math/atanhf.h
new file mode 100644
index 0..b3ee5bbb4d408
--- /dev/null
+++ b/libc/src/__support/math/atanhf.h
@@ -0,0 +1,76 @@
+//===-- Implementation header for atanhf *- 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_ATANHF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATANHF_H
+
+#include "acoshf_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float atanhf(float x) {
+ using namespace acoshf_internal;
+ using FPBits = typename fputil::FPBits;
+
+ FPBits xbits(x);
+ Sign sign = xbits.sign();
+ uint32_t x_abs = xbits.abs().uintval();
+
+ // |x| >= 1.0
+ if (LIBC_UNLIKELY(x_abs >= 0x3F80'U)) {
+if (xbits.is_nan()) {
+ if (xbits.is_signaling_nan()) {
+fputil::raise_except_if_required(FE_INVALID);
+return FPBits::quiet_nan().get_val();
+ }
+ return x;
+}
+// |x| == 1.0
+if (x_abs == 0x3F80'U) {
+ fputil::set_errno_if_required(ERANGE);
+ fputil::raise_except_if_required(FE_DIVBYZERO);
+ return FPBits::inf(sign).get_val();
+} else {
+ fputil::set_errno_if_required(EDOM);
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+}
+ }
+
+ // |x| < ~0.10
+ if (LIBC_UNLIKELY(x_abs <= 0x3dcc'U)) {
+// |x| <= 2^-26
+if (LIBC_UNLIKELY(x_abs <= 0x3280'U)) {
+ return static_cast(LIBC_UNLIKELY(x_abs == 0)
+? x
+: (x + 0x1.5p-2 * x * x * x));
+}
+
+double xdbl = x;
+double x2
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atanhf implementation to header-only in src/__support/math folder. (PR #151399)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/151399
>From afec079b2ce7810f9e7b968eb06ab83d48c6c84d Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Thu, 31 Jul 2025 00:41:13 +0300
Subject: [PATCH] [libc][math] Refactor atanhf implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atanhf.h | 23 ++
libc/src/__support/math/CMakeLists.txt| 11 +++
libc/src/__support/math/atanhf.h | 76 +++
libc/src/math/generic/CMakeLists.txt | 5 +-
libc/src/math/generic/atanhf.cpp | 56 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 20 +++--
9 files changed, 129 insertions(+), 65 deletions(-)
create mode 100644 libc/shared/math/atanhf.h
create mode 100644 libc/src/__support/math/atanhf.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 6cb583c08dedd..ddf219ece8ff1 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -28,6 +28,7 @@
#include "math/atan2f128.h"
#include "math/atanf.h"
#include "math/atanf16.h"
+#include "math/atanhf.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/atanhf.h b/libc/shared/math/atanhf.h
new file mode 100644
index 0..763fb3e00a659
--- /dev/null
+++ b/libc/shared/math/atanhf.h
@@ -0,0 +1,23 @@
+//===-- Shared atanhf 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_ATANHF_H
+#define LLVM_LIBC_SHARED_MATH_ATANHF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atanhf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atanhf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATANHF_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index caafdc2cbf1d6..500dd9de2c555 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -275,6 +275,17 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ atanhf
+ HDRS
+atanhf.h
+ DEPENDS
+.acoshf_utils
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/atanhf.h b/libc/src/__support/math/atanhf.h
new file mode 100644
index 0..b3ee5bbb4d408
--- /dev/null
+++ b/libc/src/__support/math/atanhf.h
@@ -0,0 +1,76 @@
+//===-- Implementation header for atanhf *- 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_ATANHF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATANHF_H
+
+#include "acoshf_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float atanhf(float x) {
+ using namespace acoshf_internal;
+ using FPBits = typename fputil::FPBits;
+
+ FPBits xbits(x);
+ Sign sign = xbits.sign();
+ uint32_t x_abs = xbits.abs().uintval();
+
+ // |x| >= 1.0
+ if (LIBC_UNLIKELY(x_abs >= 0x3F80'U)) {
+if (xbits.is_nan()) {
+ if (xbits.is_signaling_nan()) {
+fputil::raise_except_if_required(FE_INVALID);
+return FPBits::quiet_nan().get_val();
+ }
+ return x;
+}
+// |x| == 1.0
+if (x_abs == 0x3F80'U) {
+ fputil::set_errno_if_required(ERANGE);
+ fputil::raise_except_if_required(FE_DIVBYZERO);
+ return FPBits::inf(sign).get_val();
+} else {
+ fputil::set_errno_if_required(EDOM);
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+}
+ }
+
+ // |x| < ~0.10
+ if (LIBC_UNLIKELY(x_abs <= 0x3dcc'U)) {
+// |x| <= 2^-26
+if (LIBC_UNLIKELY(x_abs <= 0x3280'U)) {
+ return static_cast(LIBC_UNLIKELY(x_abs == 0)
+? x
+: (x + 0x1.5p-2 * x * x * x));
+}
+
+double xdbl = x;
+double x2
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atanf16 implementation to header-only in src/__support/math folder. (PR #150868)
https://github.com/bassiounix created
https://github.com/llvm/llvm-project/pull/150868
Part of #147386
in preparation for:
https://discourse.llvm.org/t/rfc-make-clang-builtin-math-functions-constexpr-with-llvm-libc-to-support-c-23-constexpr-math-functions/86450
>From 65ffa9615224d4ffe094b95ab197bf5ee5d1d529 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Mon, 28 Jul 2025 05:26:38 +0300
Subject: [PATCH] [libc][math] Refactor atanf16 implementation to header-only
in src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atanf16.h| 28 +
libc/src/__support/math/CMakeLists.txt| 15 +++
libc/src/__support/math/atanf16.h | 119 ++
libc/src/math/generic/CMakeLists.txt | 12 +-
libc/src/math/generic/atanf16.cpp | 95 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 22
9 files changed, 190 insertions(+), 104 deletions(-)
create mode 100644 libc/shared/math/atanf16.h
create mode 100644 libc/src/__support/math/atanf16.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 21536647948f4..bcbe0de56170a 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -24,6 +24,7 @@
#include "math/asinhf16.h"
#include "math/atan.h"
#include "math/atanf.h"
+#include "math/atanf16.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/atanf16.h b/libc/shared/math/atanf16.h
new file mode 100644
index 0..f196907059e01
--- /dev/null
+++ b/libc/shared/math/atanf16.h
@@ -0,0 +1,28 @@
+//===-- Shared atanf16 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_ATANF16_H
+#define LLVM_LIBC_SHARED_MATH_ATANF16_H
+
+#include "shared/libc_common.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/math/atanf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atanf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_ATANF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index 95acc962cc885..04cbd3fd1cc01 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -214,6 +214,21 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ atanf16
+ HDRS
+atanf16.h
+ DEPENDS
+libc.src.__support.FPUtil.cast
+libc.src.__support.FPUtil.except_value_utils
+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.FPUtil.sqrt
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/atanf16.h
b/libc/src/__support/math/atanf16.h
new file mode 100644
index 0..f75d145f36852
--- /dev/null
+++ b/libc/src/__support/math/atanf16.h
@@ -0,0 +1,119 @@
+//===-- Implementation header for atanf16 ---*- 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_ATANF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATANF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/macros/optimization.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float16 atanf16(float16 x) {
+ // Generated by Solly using the following command:
+ // > round(pi/2, SG, RN);
+ constexpr float PI_2 = 0x1.921fb6p0;
+
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+ constexpr size_t N_EXCEPTS = 6;
+
+ constexpr fputil::ExceptValues ATANF16_EXCEPTS{{
+ // (input, RZ output, RU offset, RD offset, RN offset)
+ {0x2745, 0x2744, 1, 0, 1},
+ {0x3099, 0x3090, 1, 0, 1},
+ {0x3c6c, 0x3aae, 1, 0, 1},
+ {0x466e, 0x3daa, 1, 0, 1},
+ {0x48a
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atanf16 implementation to header-only in src/__support/math folder. (PR #150868)
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/150868?utm_source=stack-comment-downstack-mergeability-warning"; > >on Graphite. > https://graphite.dev/docs/merge-pull-requests";>Learn more * **#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"/> 👈 https://app.graphite.dev/github/pr/llvm/llvm-project/150868?utm_source=stack-comment-view-in-graphite"; target="_blank">(View in Graphite) * **#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/150868 ___ 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 atan2f128 implementation to header-only in src/__support/math folder. (PR #151012)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/151012
>From a4bd4ed9b3ce4b833cad7421816ff03fb7df9fab Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Mon, 28 Jul 2025 21:14:48 +0300
Subject: [PATCH 1/2] [libc][math] Refactor atan2f128 implementation to
header-only in src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atan2f128.h | 29 +++
libc/src/__support/math/CMakeLists.txt| 15 ++
libc/src/__support/math/atan2f128.h | 212 ++
libc/src/math/generic/CMakeLists.txt | 10 +-
libc/src/math/generic/atan2f128.cpp | 190 +---
libc/test/shared/shared_math_test.cpp | 2 +
.../llvm-project-overlay/libc/BUILD.bazel | 24 +-
8 files changed, 284 insertions(+), 199 deletions(-)
create mode 100644 libc/shared/math/atan2f128.h
create mode 100644 libc/src/__support/math/atan2f128.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 527bb8d6214ae..6cb583c08dedd 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -25,6 +25,7 @@
#include "math/atan.h"
#include "math/atan2.h"
#include "math/atan2f.h"
+#include "math/atan2f128.h"
#include "math/atanf.h"
#include "math/atanf16.h"
#include "math/erff.h"
diff --git a/libc/shared/math/atan2f128.h b/libc/shared/math/atan2f128.h
new file mode 100644
index 0..d7aee40c69527
--- /dev/null
+++ b/libc/shared/math/atan2f128.h
@@ -0,0 +1,29 @@
+//===-- Shared atan2f128 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_ATAN2F128_H
+#define LLVM_LIBC_SHARED_MATH_ATAN2F128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atan2f128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atan2f128;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_MATH_ATAN2F128_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index c197b19ed29de..caafdc2cbf1d6 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -230,6 +230,21 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ atan2f128
+ HDRS
+atan2f128.h
+ DEPENDS
+.atan_utils
+libc.src.__support.integer_literals
+libc.src.__support.uint128
+libc.src.__support.FPUtil.dyadic_float
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.macros.optimization
+)
+
add_header_library(
atanf
HDRS
diff --git a/libc/src/__support/math/atan2f128.h
b/libc/src/__support/math/atan2f128.h
new file mode 100644
index 0..89efaf1fd72a0
--- /dev/null
+++ b/libc/src/__support/math/atan2f128.h
@@ -0,0 +1,212 @@
+//===-- Implementation header for atan2f128 -*- 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_ATAN2F128_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN2F128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "atan_utils.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/dyadic_float.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/integer_literals.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+#include "src/__support/uint128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+// There are several range reduction steps we can take for atan2(y, x) as
+// follow:
+
+// * Range reduction 1: signness
+// atan2(y, x) will return a number between -PI and PI representing the angle
+// forming by the 0x axis and the vector (x, y) on the 0xy-plane.
+// In particular, we have that:
+// atan2(y, x) = atan( y/x ) if x >= 0 and y >= 0 (I-quadrant)
+// = pi + atan( y/x )if x < 0 and y >= 0 (II-quadrant)
+// = -pi + atan( y/x ) if x < 0 and y < 0 (III-quadrant)
+// = atan( y/x ) if x >= 0 and y < 0 (IV-quadrant)
+// Since atan function is odd, we can use the formula:
+// atan(-u) = -atan(u)
+// to adjust the a
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atan2f implementation to header-only in src/__support/math folder. (PR #150993)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/150993
>From 37d0403d9fbb96d117cc8ce90cdee667ee9f86b2 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Mon, 28 Jul 2025 19:35:03 +0300
Subject: [PATCH] [libc][math] Refactor atan2f implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atan2f.h | 23 ++
libc/src/__support/math/CMakeLists.txt| 17 +
libc/src/__support/math/atan2f.h | 351 ++
.../generic => __support/math}/atan2f_float.h | 21 +-
libc/src/math/generic/CMakeLists.txt | 12 +-
libc/src/math/generic/atan2f.cpp | 328 +---
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 20 +-
10 files changed, 427 insertions(+), 348 deletions(-)
create mode 100644 libc/shared/math/atan2f.h
create mode 100644 libc/src/__support/math/atan2f.h
rename libc/src/{math/generic => __support/math}/atan2f_float.h (95%)
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 0605d918eb2af..527bb8d6214ae 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -24,6 +24,7 @@
#include "math/asinhf16.h"
#include "math/atan.h"
#include "math/atan2.h"
+#include "math/atan2f.h"
#include "math/atanf.h"
#include "math/atanf16.h"
#include "math/erff.h"
diff --git a/libc/shared/math/atan2f.h b/libc/shared/math/atan2f.h
new file mode 100644
index 0..2de09d25e19f8
--- /dev/null
+++ b/libc/shared/math/atan2f.h
@@ -0,0 +1,23 @@
+//===-- Shared atan2f 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_ATAN2F_H
+#define LLVM_LIBC_SHARED_MATH_ATAN2F_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atan2f.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atan2f;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATAN2F_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index bbb07b62552f6..c197b19ed29de 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -213,6 +213,23 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ atan2f
+ HDRS
+atan2f_float.h
+atan2f.h
+ DEPENDS
+.inv_trigf_utils
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.macros.config
+libc.src.__support.macros.optimization
+)
+
add_header_library(
atanf
HDRS
diff --git a/libc/src/__support/math/atan2f.h b/libc/src/__support/math/atan2f.h
new file mode 100644
index 0..e3b19329126f4
--- /dev/null
+++ b/libc/src/__support/math/atan2f.h
@@ -0,0 +1,351 @@
+//===-- Implementation header for atan2f *- 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_ATAN2F_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN2F_H
+
+#include "inv_trigf_utils.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/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+#if defined(LIBC_MATH_HAS_SKIP_ACCURATE_PASS) &&
\
+defined(LIBC_MATH_HAS_INTERMEDIATE_COMP_IN_FLOAT)
+
+// We use float-float implementation to reduce size.
+#include "atan2f_float.h"
+
+#else
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+namespace atan2f_internal {
+
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+
+// Look up tables for accurate pass:
+
+// atan(i/16) with i = 0..16, generated by Sollya with:
+// > for i from 0 to 16 do {
+// a = round(atan(i/16), D, RN);
+// b = round(atan(i/16) - a, D, RN);
+// print("{", b, ",", a, "},");
+// };
+static constexpr fputil::DoubleDouble ATAN_I[17] = {
+{0.0, 0.0},
+{-0x1.c934d86d23
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atan2f128 implementation to header-only in src/__support/math folder. (PR #151012)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/151012
>From a4bd4ed9b3ce4b833cad7421816ff03fb7df9fab Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Mon, 28 Jul 2025 21:14:48 +0300
Subject: [PATCH 1/2] [libc][math] Refactor atan2f128 implementation to
header-only in src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atan2f128.h | 29 +++
libc/src/__support/math/CMakeLists.txt| 15 ++
libc/src/__support/math/atan2f128.h | 212 ++
libc/src/math/generic/CMakeLists.txt | 10 +-
libc/src/math/generic/atan2f128.cpp | 190 +---
libc/test/shared/shared_math_test.cpp | 2 +
.../llvm-project-overlay/libc/BUILD.bazel | 24 +-
8 files changed, 284 insertions(+), 199 deletions(-)
create mode 100644 libc/shared/math/atan2f128.h
create mode 100644 libc/src/__support/math/atan2f128.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 527bb8d6214ae..6cb583c08dedd 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -25,6 +25,7 @@
#include "math/atan.h"
#include "math/atan2.h"
#include "math/atan2f.h"
+#include "math/atan2f128.h"
#include "math/atanf.h"
#include "math/atanf16.h"
#include "math/erff.h"
diff --git a/libc/shared/math/atan2f128.h b/libc/shared/math/atan2f128.h
new file mode 100644
index 0..d7aee40c69527
--- /dev/null
+++ b/libc/shared/math/atan2f128.h
@@ -0,0 +1,29 @@
+//===-- Shared atan2f128 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_ATAN2F128_H
+#define LLVM_LIBC_SHARED_MATH_ATAN2F128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atan2f128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atan2f128;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_MATH_ATAN2F128_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index c197b19ed29de..caafdc2cbf1d6 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -230,6 +230,21 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ atan2f128
+ HDRS
+atan2f128.h
+ DEPENDS
+.atan_utils
+libc.src.__support.integer_literals
+libc.src.__support.uint128
+libc.src.__support.FPUtil.dyadic_float
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.macros.optimization
+)
+
add_header_library(
atanf
HDRS
diff --git a/libc/src/__support/math/atan2f128.h
b/libc/src/__support/math/atan2f128.h
new file mode 100644
index 0..89efaf1fd72a0
--- /dev/null
+++ b/libc/src/__support/math/atan2f128.h
@@ -0,0 +1,212 @@
+//===-- Implementation header for atan2f128 -*- 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_ATAN2F128_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN2F128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "atan_utils.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/dyadic_float.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/integer_literals.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+#include "src/__support/uint128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+// There are several range reduction steps we can take for atan2(y, x) as
+// follow:
+
+// * Range reduction 1: signness
+// atan2(y, x) will return a number between -PI and PI representing the angle
+// forming by the 0x axis and the vector (x, y) on the 0xy-plane.
+// In particular, we have that:
+// atan2(y, x) = atan( y/x ) if x >= 0 and y >= 0 (I-quadrant)
+// = pi + atan( y/x )if x < 0 and y >= 0 (II-quadrant)
+// = -pi + atan( y/x ) if x < 0 and y < 0 (III-quadrant)
+// = atan( y/x ) if x >= 0 and y < 0 (IV-quadrant)
+// Since atan function is odd, we can use the formula:
+// atan(-u) = -atan(u)
+// to adjust the a
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atan2f implementation to header-only in src/__support/math folder. (PR #150993)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/150993
>From 37d0403d9fbb96d117cc8ce90cdee667ee9f86b2 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Mon, 28 Jul 2025 19:35:03 +0300
Subject: [PATCH] [libc][math] Refactor atan2f implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atan2f.h | 23 ++
libc/src/__support/math/CMakeLists.txt| 17 +
libc/src/__support/math/atan2f.h | 351 ++
.../generic => __support/math}/atan2f_float.h | 21 +-
libc/src/math/generic/CMakeLists.txt | 12 +-
libc/src/math/generic/atan2f.cpp | 328 +---
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 20 +-
10 files changed, 427 insertions(+), 348 deletions(-)
create mode 100644 libc/shared/math/atan2f.h
create mode 100644 libc/src/__support/math/atan2f.h
rename libc/src/{math/generic => __support/math}/atan2f_float.h (95%)
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 0605d918eb2af..527bb8d6214ae 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -24,6 +24,7 @@
#include "math/asinhf16.h"
#include "math/atan.h"
#include "math/atan2.h"
+#include "math/atan2f.h"
#include "math/atanf.h"
#include "math/atanf16.h"
#include "math/erff.h"
diff --git a/libc/shared/math/atan2f.h b/libc/shared/math/atan2f.h
new file mode 100644
index 0..2de09d25e19f8
--- /dev/null
+++ b/libc/shared/math/atan2f.h
@@ -0,0 +1,23 @@
+//===-- Shared atan2f 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_ATAN2F_H
+#define LLVM_LIBC_SHARED_MATH_ATAN2F_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atan2f.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atan2f;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATAN2F_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index bbb07b62552f6..c197b19ed29de 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -213,6 +213,23 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ atan2f
+ HDRS
+atan2f_float.h
+atan2f.h
+ DEPENDS
+.inv_trigf_utils
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.macros.config
+libc.src.__support.macros.optimization
+)
+
add_header_library(
atanf
HDRS
diff --git a/libc/src/__support/math/atan2f.h b/libc/src/__support/math/atan2f.h
new file mode 100644
index 0..e3b19329126f4
--- /dev/null
+++ b/libc/src/__support/math/atan2f.h
@@ -0,0 +1,351 @@
+//===-- Implementation header for atan2f *- 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_ATAN2F_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN2F_H
+
+#include "inv_trigf_utils.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/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+#if defined(LIBC_MATH_HAS_SKIP_ACCURATE_PASS) &&
\
+defined(LIBC_MATH_HAS_INTERMEDIATE_COMP_IN_FLOAT)
+
+// We use float-float implementation to reduce size.
+#include "atan2f_float.h"
+
+#else
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+namespace atan2f_internal {
+
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+
+// Look up tables for accurate pass:
+
+// atan(i/16) with i = 0..16, generated by Sollya with:
+// > for i from 0 to 16 do {
+// a = round(atan(i/16), D, RN);
+// b = round(atan(i/16) - a, D, RN);
+// print("{", b, ",", a, "},");
+// };
+static constexpr fputil::DoubleDouble ATAN_I[17] = {
+{0.0, 0.0},
+{-0x1.c934d86d23
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atanhf implementation to header-only in src/__support/math folder. (PR #151399)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/151399
>From 0eaa3d23851ef27288dffd45be2b3e5914292cc5 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Thu, 31 Jul 2025 00:41:13 +0300
Subject: [PATCH] [libc][math] Refactor atanhf implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atanhf.h | 23 ++
libc/src/__support/math/CMakeLists.txt| 11 +++
libc/src/__support/math/atanhf.h | 76 +++
libc/src/math/generic/CMakeLists.txt | 5 +-
libc/src/math/generic/atanhf.cpp | 56 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 20 +++--
9 files changed, 129 insertions(+), 65 deletions(-)
create mode 100644 libc/shared/math/atanhf.h
create mode 100644 libc/src/__support/math/atanhf.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 6cb583c08dedd..ddf219ece8ff1 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -28,6 +28,7 @@
#include "math/atan2f128.h"
#include "math/atanf.h"
#include "math/atanf16.h"
+#include "math/atanhf.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/atanhf.h b/libc/shared/math/atanhf.h
new file mode 100644
index 0..763fb3e00a659
--- /dev/null
+++ b/libc/shared/math/atanhf.h
@@ -0,0 +1,23 @@
+//===-- Shared atanhf 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_ATANHF_H
+#define LLVM_LIBC_SHARED_MATH_ATANHF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atanhf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atanhf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATANHF_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index caafdc2cbf1d6..500dd9de2c555 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -275,6 +275,17 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ atanhf
+ HDRS
+atanhf.h
+ DEPENDS
+.acoshf_utils
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/atanhf.h b/libc/src/__support/math/atanhf.h
new file mode 100644
index 0..b3ee5bbb4d408
--- /dev/null
+++ b/libc/src/__support/math/atanhf.h
@@ -0,0 +1,76 @@
+//===-- Implementation header for atanhf *- 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_ATANHF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATANHF_H
+
+#include "acoshf_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float atanhf(float x) {
+ using namespace acoshf_internal;
+ using FPBits = typename fputil::FPBits;
+
+ FPBits xbits(x);
+ Sign sign = xbits.sign();
+ uint32_t x_abs = xbits.abs().uintval();
+
+ // |x| >= 1.0
+ if (LIBC_UNLIKELY(x_abs >= 0x3F80'U)) {
+if (xbits.is_nan()) {
+ if (xbits.is_signaling_nan()) {
+fputil::raise_except_if_required(FE_INVALID);
+return FPBits::quiet_nan().get_val();
+ }
+ return x;
+}
+// |x| == 1.0
+if (x_abs == 0x3F80'U) {
+ fputil::set_errno_if_required(ERANGE);
+ fputil::raise_except_if_required(FE_DIVBYZERO);
+ return FPBits::inf(sign).get_val();
+} else {
+ fputil::set_errno_if_required(EDOM);
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+}
+ }
+
+ // |x| < ~0.10
+ if (LIBC_UNLIKELY(x_abs <= 0x3dcc'U)) {
+// |x| <= 2^-26
+if (LIBC_UNLIKELY(x_abs <= 0x3280'U)) {
+ return static_cast(LIBC_UNLIKELY(x_abs == 0)
+? x
+: (x + 0x1.5p-2 * x * x * x));
+}
+
+double xdbl = x;
+double x2
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atan2f128 implementation to header-only in src/__support/math folder. (PR #151012)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/151012
>From 13af55c51d38d055926c2e4725ba92c460e146c0 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Mon, 28 Jul 2025 21:14:48 +0300
Subject: [PATCH 1/2] [libc][math] Refactor atan2f128 implementation to
header-only in src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atan2f128.h | 29 +++
libc/src/__support/math/CMakeLists.txt| 15 ++
libc/src/__support/math/atan2f128.h | 212 ++
libc/src/math/generic/CMakeLists.txt | 10 +-
libc/src/math/generic/atan2f128.cpp | 190 +---
libc/test/shared/shared_math_test.cpp | 2 +
.../llvm-project-overlay/libc/BUILD.bazel | 24 +-
8 files changed, 284 insertions(+), 199 deletions(-)
create mode 100644 libc/shared/math/atan2f128.h
create mode 100644 libc/src/__support/math/atan2f128.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 527bb8d6214ae..6cb583c08dedd 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -25,6 +25,7 @@
#include "math/atan.h"
#include "math/atan2.h"
#include "math/atan2f.h"
+#include "math/atan2f128.h"
#include "math/atanf.h"
#include "math/atanf16.h"
#include "math/erff.h"
diff --git a/libc/shared/math/atan2f128.h b/libc/shared/math/atan2f128.h
new file mode 100644
index 0..d7aee40c69527
--- /dev/null
+++ b/libc/shared/math/atan2f128.h
@@ -0,0 +1,29 @@
+//===-- Shared atan2f128 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_ATAN2F128_H
+#define LLVM_LIBC_SHARED_MATH_ATAN2F128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atan2f128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atan2f128;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_MATH_ATAN2F128_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index c197b19ed29de..caafdc2cbf1d6 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -230,6 +230,21 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ atan2f128
+ HDRS
+atan2f128.h
+ DEPENDS
+.atan_utils
+libc.src.__support.integer_literals
+libc.src.__support.uint128
+libc.src.__support.FPUtil.dyadic_float
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.macros.optimization
+)
+
add_header_library(
atanf
HDRS
diff --git a/libc/src/__support/math/atan2f128.h
b/libc/src/__support/math/atan2f128.h
new file mode 100644
index 0..89efaf1fd72a0
--- /dev/null
+++ b/libc/src/__support/math/atan2f128.h
@@ -0,0 +1,212 @@
+//===-- Implementation header for atan2f128 -*- 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_ATAN2F128_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN2F128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "atan_utils.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/dyadic_float.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/integer_literals.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+#include "src/__support/uint128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+// There are several range reduction steps we can take for atan2(y, x) as
+// follow:
+
+// * Range reduction 1: signness
+// atan2(y, x) will return a number between -PI and PI representing the angle
+// forming by the 0x axis and the vector (x, y) on the 0xy-plane.
+// In particular, we have that:
+// atan2(y, x) = atan( y/x ) if x >= 0 and y >= 0 (I-quadrant)
+// = pi + atan( y/x )if x < 0 and y >= 0 (II-quadrant)
+// = -pi + atan( y/x ) if x < 0 and y < 0 (III-quadrant)
+// = atan( y/x ) if x >= 0 and y < 0 (IV-quadrant)
+// Since atan function is odd, we can use the formula:
+// atan(-u) = -atan(u)
+// to adjust the a
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atan2f128 implementation to header-only in src/__support/math folder. (PR #151012)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/151012
>From 13af55c51d38d055926c2e4725ba92c460e146c0 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Mon, 28 Jul 2025 21:14:48 +0300
Subject: [PATCH 1/2] [libc][math] Refactor atan2f128 implementation to
header-only in src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atan2f128.h | 29 +++
libc/src/__support/math/CMakeLists.txt| 15 ++
libc/src/__support/math/atan2f128.h | 212 ++
libc/src/math/generic/CMakeLists.txt | 10 +-
libc/src/math/generic/atan2f128.cpp | 190 +---
libc/test/shared/shared_math_test.cpp | 2 +
.../llvm-project-overlay/libc/BUILD.bazel | 24 +-
8 files changed, 284 insertions(+), 199 deletions(-)
create mode 100644 libc/shared/math/atan2f128.h
create mode 100644 libc/src/__support/math/atan2f128.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 527bb8d6214ae..6cb583c08dedd 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -25,6 +25,7 @@
#include "math/atan.h"
#include "math/atan2.h"
#include "math/atan2f.h"
+#include "math/atan2f128.h"
#include "math/atanf.h"
#include "math/atanf16.h"
#include "math/erff.h"
diff --git a/libc/shared/math/atan2f128.h b/libc/shared/math/atan2f128.h
new file mode 100644
index 0..d7aee40c69527
--- /dev/null
+++ b/libc/shared/math/atan2f128.h
@@ -0,0 +1,29 @@
+//===-- Shared atan2f128 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_ATAN2F128_H
+#define LLVM_LIBC_SHARED_MATH_ATAN2F128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atan2f128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atan2f128;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_MATH_ATAN2F128_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index c197b19ed29de..caafdc2cbf1d6 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -230,6 +230,21 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ atan2f128
+ HDRS
+atan2f128.h
+ DEPENDS
+.atan_utils
+libc.src.__support.integer_literals
+libc.src.__support.uint128
+libc.src.__support.FPUtil.dyadic_float
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.macros.optimization
+)
+
add_header_library(
atanf
HDRS
diff --git a/libc/src/__support/math/atan2f128.h
b/libc/src/__support/math/atan2f128.h
new file mode 100644
index 0..89efaf1fd72a0
--- /dev/null
+++ b/libc/src/__support/math/atan2f128.h
@@ -0,0 +1,212 @@
+//===-- Implementation header for atan2f128 -*- 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_ATAN2F128_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN2F128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "atan_utils.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/dyadic_float.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/integer_literals.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+#include "src/__support/uint128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+// There are several range reduction steps we can take for atan2(y, x) as
+// follow:
+
+// * Range reduction 1: signness
+// atan2(y, x) will return a number between -PI and PI representing the angle
+// forming by the 0x axis and the vector (x, y) on the 0xy-plane.
+// In particular, we have that:
+// atan2(y, x) = atan( y/x ) if x >= 0 and y >= 0 (I-quadrant)
+// = pi + atan( y/x )if x < 0 and y >= 0 (II-quadrant)
+// = -pi + atan( y/x ) if x < 0 and y < 0 (III-quadrant)
+// = atan( y/x ) if x >= 0 and y < 0 (IV-quadrant)
+// Since atan function is odd, we can use the formula:
+// atan(-u) = -atan(u)
+// to adjust the a
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor atanhf implementation to header-only in src/__support/math folder. (PR #151399)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/151399
>From 0eaa3d23851ef27288dffd45be2b3e5914292cc5 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Thu, 31 Jul 2025 00:41:13 +0300
Subject: [PATCH] [libc][math] Refactor atanhf implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/atanhf.h | 23 ++
libc/src/__support/math/CMakeLists.txt| 11 +++
libc/src/__support/math/atanhf.h | 76 +++
libc/src/math/generic/CMakeLists.txt | 5 +-
libc/src/math/generic/atanhf.cpp | 56 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 20 +++--
9 files changed, 129 insertions(+), 65 deletions(-)
create mode 100644 libc/shared/math/atanhf.h
create mode 100644 libc/src/__support/math/atanhf.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 6cb583c08dedd..ddf219ece8ff1 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -28,6 +28,7 @@
#include "math/atan2f128.h"
#include "math/atanf.h"
#include "math/atanf16.h"
+#include "math/atanhf.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/atanhf.h b/libc/shared/math/atanhf.h
new file mode 100644
index 0..763fb3e00a659
--- /dev/null
+++ b/libc/shared/math/atanhf.h
@@ -0,0 +1,23 @@
+//===-- Shared atanhf 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_ATANHF_H
+#define LLVM_LIBC_SHARED_MATH_ATANHF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/atanhf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::atanhf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ATANHF_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index caafdc2cbf1d6..500dd9de2c555 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -275,6 +275,17 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ atanhf
+ HDRS
+atanhf.h
+ DEPENDS
+.acoshf_utils
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.macros.optimization
+)
+
add_header_library(
asinf
HDRS
diff --git a/libc/src/__support/math/atanhf.h b/libc/src/__support/math/atanhf.h
new file mode 100644
index 0..b3ee5bbb4d408
--- /dev/null
+++ b/libc/src/__support/math/atanhf.h
@@ -0,0 +1,76 @@
+//===-- Implementation header for atanhf *- 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_ATANHF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATANHF_H
+
+#include "acoshf_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float atanhf(float x) {
+ using namespace acoshf_internal;
+ using FPBits = typename fputil::FPBits;
+
+ FPBits xbits(x);
+ Sign sign = xbits.sign();
+ uint32_t x_abs = xbits.abs().uintval();
+
+ // |x| >= 1.0
+ if (LIBC_UNLIKELY(x_abs >= 0x3F80'U)) {
+if (xbits.is_nan()) {
+ if (xbits.is_signaling_nan()) {
+fputil::raise_except_if_required(FE_INVALID);
+return FPBits::quiet_nan().get_val();
+ }
+ return x;
+}
+// |x| == 1.0
+if (x_abs == 0x3F80'U) {
+ fputil::set_errno_if_required(ERANGE);
+ fputil::raise_except_if_required(FE_DIVBYZERO);
+ return FPBits::inf(sign).get_val();
+} else {
+ fputil::set_errno_if_required(EDOM);
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+}
+ }
+
+ // |x| < ~0.10
+ if (LIBC_UNLIKELY(x_abs <= 0x3dcc'U)) {
+// |x| <= 2^-26
+if (LIBC_UNLIKELY(x_abs <= 0x3280'U)) {
+ return static_cast(LIBC_UNLIKELY(x_abs == 0)
+? x
+: (x + 0x1.5p-2 * x * x * x));
+}
+
+double xdbl = x;
+double x2
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor cos implementation to header-only in src/__support/math folder. (PR #151883)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/151883
>From 37848d777f74c5052a8332033e51446a3f7a152f Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Sun, 3 Aug 2025 22:24:35 +0300
Subject: [PATCH] [libc][math] Refactor cos implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/cos.h| 23 +++
libc/src/__support/math/CMakeLists.txt| 47 +
libc/src/__support/math/cos.h | 173 ++
.../math}/range_reduction_double_common.h | 24 ++-
.../math}/range_reduction_double_fma.h| 18 +-
.../math}/range_reduction_double_nofma.h | 16 +-
.../generic => __support/math}/sincos_eval.h | 8 +-
libc/src/math/generic/CMakeLists.txt | 50 +
libc/src/math/generic/cos.cpp | 155 +---
libc/src/math/generic/sin.cpp | 14 +-
libc/src/math/generic/sincos.cpp | 14 +-
libc/src/math/generic/tan.cpp | 7 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 3 +-
.../llvm-project-overlay/libc/BUILD.bazel | 101 +-
16 files changed, 377 insertions(+), 278 deletions(-)
create mode 100644 libc/shared/math/cos.h
create mode 100644 libc/src/__support/math/cos.h
rename libc/src/{math/generic =>
__support/math}/range_reduction_double_common.h (97%)
rename libc/src/{math/generic => __support/math}/range_reduction_double_fma.h
(97%)
rename libc/src/{math/generic =>
__support/math}/range_reduction_double_nofma.h (97%)
rename libc/src/{math/generic => __support/math}/sincos_eval.h (98%)
diff --git a/libc/shared/math.h b/libc/shared/math.h
index ea645f0afedbc..a5581ed4272a3 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -32,6 +32,7 @@
#include "math/atanhf16.h"
#include "math/cbrt.h"
#include "math/cbrtf.h"
+#include "math/cos.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/cos.h b/libc/shared/math/cos.h
new file mode 100644
index 0..c498550f098b4
--- /dev/null
+++ b/libc/shared/math/cos.h
@@ -0,0 +1,23 @@
+//===-- Shared cos 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_COS_H
+#define LLVM_LIBC_SHARED_MATH_COS_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/cos.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::cos;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_COS_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index fe928a8fadd5e..24844063fcd24 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -357,6 +357,24 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ cos
+ HDRS
+cos.h
+ DEPENDS
+libc.src.__support.math.sincos_eval
+libc.hdr.errno_macros
+libc.src.errno.errno
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.dyadic_float
+libc.src.__support.FPUtil.except_value_utils
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.math.range_reduction_double
+libc.src.__support.macros.optimization
+)
+
+
add_header_library(
erff
HDRS
@@ -613,3 +631,32 @@ add_header_library(
libc.src.__support.macros.optimization
libc.src.__support.macros.properties.cpu_features
)
+
+add_header_library(
+ range_reduction_double
+ HDRS
+range_reduction_double_common.h
+range_reduction_double_fma.h
+range_reduction_double_nofma.h
+ DEPENDS
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.dyadic_float
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.fma
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.common
+libc.src.__support.integer_literals
+)
+
+add_header_library(
+ sincos_eval
+ HDRS
+sincos_eval.h
+ DEPENDS
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.dyadic_float
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.integer_literals
+)
diff --git a/libc/src/__support/math/cos.h b/libc/src/__support/math/cos.h
new file mode 100644
index 0..0802f9e4f6e49
--- /dev/null
+++ b/libc/src/__support/math/cos.h
@@ -0,0 +1,173 @@
+//===-- Implementation header for cos ---*- C
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor cos implementation to header-only in src/__support/math folder. (PR #151883)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/151883
>From 14a8e8add569e7e423cfc57ecf85c722063f0835 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Sun, 3 Aug 2025 22:24:35 +0300
Subject: [PATCH] [libc][math] Refactor cos implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/cos.h| 23 +++
libc/src/__support/math/CMakeLists.txt| 47 +
libc/src/__support/math/cos.h | 173 ++
.../math}/range_reduction_double_common.h | 24 ++-
.../math}/range_reduction_double_fma.h| 18 +-
.../math}/range_reduction_double_nofma.h | 16 +-
.../generic => __support/math}/sincos_eval.h | 8 +-
libc/src/math/generic/CMakeLists.txt | 50 +
libc/src/math/generic/cos.cpp | 155 +---
libc/src/math/generic/sin.cpp | 14 +-
libc/src/math/generic/sincos.cpp | 14 +-
libc/src/math/generic/tan.cpp | 7 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 3 +-
.../llvm-project-overlay/libc/BUILD.bazel | 101 +-
16 files changed, 377 insertions(+), 278 deletions(-)
create mode 100644 libc/shared/math/cos.h
create mode 100644 libc/src/__support/math/cos.h
rename libc/src/{math/generic =>
__support/math}/range_reduction_double_common.h (97%)
rename libc/src/{math/generic => __support/math}/range_reduction_double_fma.h
(97%)
rename libc/src/{math/generic =>
__support/math}/range_reduction_double_nofma.h (97%)
rename libc/src/{math/generic => __support/math}/sincos_eval.h (98%)
diff --git a/libc/shared/math.h b/libc/shared/math.h
index ea645f0afedbc..a5581ed4272a3 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -32,6 +32,7 @@
#include "math/atanhf16.h"
#include "math/cbrt.h"
#include "math/cbrtf.h"
+#include "math/cos.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/cos.h b/libc/shared/math/cos.h
new file mode 100644
index 0..c498550f098b4
--- /dev/null
+++ b/libc/shared/math/cos.h
@@ -0,0 +1,23 @@
+//===-- Shared cos 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_COS_H
+#define LLVM_LIBC_SHARED_MATH_COS_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/cos.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::cos;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_COS_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index fe928a8fadd5e..24844063fcd24 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -357,6 +357,24 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ cos
+ HDRS
+cos.h
+ DEPENDS
+libc.src.__support.math.sincos_eval
+libc.hdr.errno_macros
+libc.src.errno.errno
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.dyadic_float
+libc.src.__support.FPUtil.except_value_utils
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.math.range_reduction_double
+libc.src.__support.macros.optimization
+)
+
+
add_header_library(
erff
HDRS
@@ -613,3 +631,32 @@ add_header_library(
libc.src.__support.macros.optimization
libc.src.__support.macros.properties.cpu_features
)
+
+add_header_library(
+ range_reduction_double
+ HDRS
+range_reduction_double_common.h
+range_reduction_double_fma.h
+range_reduction_double_nofma.h
+ DEPENDS
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.dyadic_float
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.fma
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.common
+libc.src.__support.integer_literals
+)
+
+add_header_library(
+ sincos_eval
+ HDRS
+sincos_eval.h
+ DEPENDS
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.dyadic_float
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.integer_literals
+)
diff --git a/libc/src/__support/math/cos.h b/libc/src/__support/math/cos.h
new file mode 100644
index 0..0802f9e4f6e49
--- /dev/null
+++ b/libc/src/__support/math/cos.h
@@ -0,0 +1,173 @@
+//===-- Implementation header for cos ---*- C
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor cosf implementation to header-only in src/__support/math folder. (PR #152069)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/152069
>From 2ff880d6bf304f919b92ae62cac84e4b9789bcf1 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Tue, 5 Aug 2025 06:30:16 +0300
Subject: [PATCH] [libc][math] Refactor cosf implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/cosf.h | 23 +++
libc/src/__support/math/CMakeLists.txt| 39 +
libc/src/__support/math/cosf.h| 152 ++
.../math}/range_reduction.h | 6 +-
.../math}/range_reduction_fma.h | 6 +-
.../math}/sincosf_utils.h | 6 +-
libc/src/math/generic/CMakeLists.txt | 52 ++
libc/src/math/generic/cosf.cpp| 133 +--
libc/src/math/generic/cospif.cpp | 2 +-
libc/src/math/generic/sincosf.cpp | 2 +-
libc/src/math/generic/sinf.cpp| 6 +-
libc/src/math/generic/sinpif.cpp | 2 +-
libc/src/math/generic/tanf.cpp| 2 +-
libc/src/math/generic/tanpif.cpp | 2 +-
.../llvm-project-overlay/libc/BUILD.bazel | 84 +-
16 files changed, 290 insertions(+), 228 deletions(-)
create mode 100644 libc/shared/math/cosf.h
create mode 100644 libc/src/__support/math/cosf.h
rename libc/src/{math/generic => __support/math}/range_reduction.h (95%)
rename libc/src/{math/generic => __support/math}/range_reduction_fma.h (95%)
rename libc/src/{math/generic => __support/math}/sincosf_utils.h (97%)
diff --git a/libc/shared/math.h b/libc/shared/math.h
index a5581ed4272a3..0c11640101563 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -33,6 +33,7 @@
#include "math/cbrt.h"
#include "math/cbrtf.h"
#include "math/cos.h"
+#include "math/cosf.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/cosf.h b/libc/shared/math/cosf.h
new file mode 100644
index 0..06182207a82f2
--- /dev/null
+++ b/libc/shared/math/cosf.h
@@ -0,0 +1,23 @@
+//===-- Shared cosf 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_COSF_H
+#define LLVM_LIBC_SHARED_MATH_COSF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/cosf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::cosf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_COSF_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index 24844063fcd24..450d56acafe53 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -374,6 +374,21 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ cosf
+ HDRS
+cosf.h
+ DEPENDS
+.sincosf_utils
+libc.src.errno.errno
+libc.src.__support.FPUtil.basic_operations
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.except_value_utils
+libc.src.__support.FPUtil.fma
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.macros.optimization
+)
add_header_library(
erff
@@ -649,6 +664,19 @@ add_header_library(
libc.src.__support.integer_literals
)
+add_header_library(
+ range_reduction
+ HDRS
+range_reduction.h
+range_reduction_fma.h
+ DEPENDS
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.fma
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.common
+)
+
add_header_library(
sincos_eval
HDRS
@@ -660,3 +688,14 @@ add_header_library(
libc.src.__support.FPUtil.polyeval
libc.src.__support.integer_literals
)
+
+add_header_library(
+ sincosf_utils
+ HDRS
+sincosf_utils.h
+ DEPENDS
+.range_reduction
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.common
+)
diff --git a/libc/src/__support/math/cosf.h b/libc/src/__support/math/cosf.h
new file mode 100644
index 0..074be0b314637
--- /dev/null
+++ b/libc/src/__support/math/cosf.h
@@ -0,0 +1,152 @@
+//===-- Implementation header for cosf --*- 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___SU
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor cos implementation to header-only in src/__support/math folder. (PR #151883)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/151883
>From 14a8e8add569e7e423cfc57ecf85c722063f0835 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Sun, 3 Aug 2025 22:24:35 +0300
Subject: [PATCH] [libc][math] Refactor cos implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/cos.h| 23 +++
libc/src/__support/math/CMakeLists.txt| 47 +
libc/src/__support/math/cos.h | 173 ++
.../math}/range_reduction_double_common.h | 24 ++-
.../math}/range_reduction_double_fma.h| 18 +-
.../math}/range_reduction_double_nofma.h | 16 +-
.../generic => __support/math}/sincos_eval.h | 8 +-
libc/src/math/generic/CMakeLists.txt | 50 +
libc/src/math/generic/cos.cpp | 155 +---
libc/src/math/generic/sin.cpp | 14 +-
libc/src/math/generic/sincos.cpp | 14 +-
libc/src/math/generic/tan.cpp | 7 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 3 +-
.../llvm-project-overlay/libc/BUILD.bazel | 101 +-
16 files changed, 377 insertions(+), 278 deletions(-)
create mode 100644 libc/shared/math/cos.h
create mode 100644 libc/src/__support/math/cos.h
rename libc/src/{math/generic =>
__support/math}/range_reduction_double_common.h (97%)
rename libc/src/{math/generic => __support/math}/range_reduction_double_fma.h
(97%)
rename libc/src/{math/generic =>
__support/math}/range_reduction_double_nofma.h (97%)
rename libc/src/{math/generic => __support/math}/sincos_eval.h (98%)
diff --git a/libc/shared/math.h b/libc/shared/math.h
index ea645f0afedbc..a5581ed4272a3 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -32,6 +32,7 @@
#include "math/atanhf16.h"
#include "math/cbrt.h"
#include "math/cbrtf.h"
+#include "math/cos.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/cos.h b/libc/shared/math/cos.h
new file mode 100644
index 0..c498550f098b4
--- /dev/null
+++ b/libc/shared/math/cos.h
@@ -0,0 +1,23 @@
+//===-- Shared cos 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_COS_H
+#define LLVM_LIBC_SHARED_MATH_COS_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/cos.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::cos;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_COS_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index fe928a8fadd5e..24844063fcd24 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -357,6 +357,24 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ cos
+ HDRS
+cos.h
+ DEPENDS
+libc.src.__support.math.sincos_eval
+libc.hdr.errno_macros
+libc.src.errno.errno
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.dyadic_float
+libc.src.__support.FPUtil.except_value_utils
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.math.range_reduction_double
+libc.src.__support.macros.optimization
+)
+
+
add_header_library(
erff
HDRS
@@ -613,3 +631,32 @@ add_header_library(
libc.src.__support.macros.optimization
libc.src.__support.macros.properties.cpu_features
)
+
+add_header_library(
+ range_reduction_double
+ HDRS
+range_reduction_double_common.h
+range_reduction_double_fma.h
+range_reduction_double_nofma.h
+ DEPENDS
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.dyadic_float
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.fma
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.common
+libc.src.__support.integer_literals
+)
+
+add_header_library(
+ sincos_eval
+ HDRS
+sincos_eval.h
+ DEPENDS
+libc.src.__support.FPUtil.double_double
+libc.src.__support.FPUtil.dyadic_float
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.integer_literals
+)
diff --git a/libc/src/__support/math/cos.h b/libc/src/__support/math/cos.h
new file mode 100644
index 0..0802f9e4f6e49
--- /dev/null
+++ b/libc/src/__support/math/cos.h
@@ -0,0 +1,173 @@
+//===-- Implementation header for cos ---*- C
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor cosf implementation to header-only in src/__support/math folder. (PR #152069)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/152069
>From 2ff880d6bf304f919b92ae62cac84e4b9789bcf1 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Tue, 5 Aug 2025 06:30:16 +0300
Subject: [PATCH] [libc][math] Refactor cosf implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/cosf.h | 23 +++
libc/src/__support/math/CMakeLists.txt| 39 +
libc/src/__support/math/cosf.h| 152 ++
.../math}/range_reduction.h | 6 +-
.../math}/range_reduction_fma.h | 6 +-
.../math}/sincosf_utils.h | 6 +-
libc/src/math/generic/CMakeLists.txt | 52 ++
libc/src/math/generic/cosf.cpp| 133 +--
libc/src/math/generic/cospif.cpp | 2 +-
libc/src/math/generic/sincosf.cpp | 2 +-
libc/src/math/generic/sinf.cpp| 6 +-
libc/src/math/generic/sinpif.cpp | 2 +-
libc/src/math/generic/tanf.cpp| 2 +-
libc/src/math/generic/tanpif.cpp | 2 +-
.../llvm-project-overlay/libc/BUILD.bazel | 84 +-
16 files changed, 290 insertions(+), 228 deletions(-)
create mode 100644 libc/shared/math/cosf.h
create mode 100644 libc/src/__support/math/cosf.h
rename libc/src/{math/generic => __support/math}/range_reduction.h (95%)
rename libc/src/{math/generic => __support/math}/range_reduction_fma.h (95%)
rename libc/src/{math/generic => __support/math}/sincosf_utils.h (97%)
diff --git a/libc/shared/math.h b/libc/shared/math.h
index a5581ed4272a3..0c11640101563 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -33,6 +33,7 @@
#include "math/cbrt.h"
#include "math/cbrtf.h"
#include "math/cos.h"
+#include "math/cosf.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/cosf.h b/libc/shared/math/cosf.h
new file mode 100644
index 0..06182207a82f2
--- /dev/null
+++ b/libc/shared/math/cosf.h
@@ -0,0 +1,23 @@
+//===-- Shared cosf 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_COSF_H
+#define LLVM_LIBC_SHARED_MATH_COSF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/cosf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::cosf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_COSF_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index 24844063fcd24..450d56acafe53 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -374,6 +374,21 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ cosf
+ HDRS
+cosf.h
+ DEPENDS
+.sincosf_utils
+libc.src.errno.errno
+libc.src.__support.FPUtil.basic_operations
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.except_value_utils
+libc.src.__support.FPUtil.fma
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.macros.optimization
+)
add_header_library(
erff
@@ -649,6 +664,19 @@ add_header_library(
libc.src.__support.integer_literals
)
+add_header_library(
+ range_reduction
+ HDRS
+range_reduction.h
+range_reduction_fma.h
+ DEPENDS
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.fma
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.common
+)
+
add_header_library(
sincos_eval
HDRS
@@ -660,3 +688,14 @@ add_header_library(
libc.src.__support.FPUtil.polyeval
libc.src.__support.integer_literals
)
+
+add_header_library(
+ sincosf_utils
+ HDRS
+sincosf_utils.h
+ DEPENDS
+.range_reduction
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.common
+)
diff --git a/libc/src/__support/math/cosf.h b/libc/src/__support/math/cosf.h
new file mode 100644
index 0..074be0b314637
--- /dev/null
+++ b/libc/src/__support/math/cosf.h
@@ -0,0 +1,152 @@
+//===-- Implementation header for cosf --*- 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___SU
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor cosf implementation to header-only in src/__support/math folder. (PR #152069)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/152069
>From b30366fb8f68e1103ea57a475e7e76638f0cc83f Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Tue, 5 Aug 2025 06:30:16 +0300
Subject: [PATCH] [libc][math] Refactor cosf implementation to header-only in
src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/cosf.h | 23 +++
libc/src/__support/math/CMakeLists.txt| 39 +
libc/src/__support/math/cosf.h| 152 ++
.../math}/range_reduction.h | 6 +-
.../math}/range_reduction_fma.h | 6 +-
.../math}/sincosf_utils.h | 6 +-
libc/src/math/generic/CMakeLists.txt | 52 ++
libc/src/math/generic/cosf.cpp| 133 +--
libc/src/math/generic/cospif.cpp | 2 +-
libc/src/math/generic/sincosf.cpp | 2 +-
libc/src/math/generic/sinf.cpp| 6 +-
libc/src/math/generic/sinpif.cpp | 2 +-
libc/src/math/generic/tanf.cpp| 2 +-
libc/src/math/generic/tanpif.cpp | 2 +-
.../llvm-project-overlay/libc/BUILD.bazel | 84 +-
16 files changed, 290 insertions(+), 228 deletions(-)
create mode 100644 libc/shared/math/cosf.h
create mode 100644 libc/src/__support/math/cosf.h
rename libc/src/{math/generic => __support/math}/range_reduction.h (95%)
rename libc/src/{math/generic => __support/math}/range_reduction_fma.h (95%)
rename libc/src/{math/generic => __support/math}/sincosf_utils.h (97%)
diff --git a/libc/shared/math.h b/libc/shared/math.h
index a5581ed4272a3..0c11640101563 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -33,6 +33,7 @@
#include "math/cbrt.h"
#include "math/cbrtf.h"
#include "math/cos.h"
+#include "math/cosf.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/cosf.h b/libc/shared/math/cosf.h
new file mode 100644
index 0..06182207a82f2
--- /dev/null
+++ b/libc/shared/math/cosf.h
@@ -0,0 +1,23 @@
+//===-- Shared cosf 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_COSF_H
+#define LLVM_LIBC_SHARED_MATH_COSF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/cosf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::cosf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_COSF_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index 24844063fcd24..450d56acafe53 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -374,6 +374,21 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ cosf
+ HDRS
+cosf.h
+ DEPENDS
+.sincosf_utils
+libc.src.errno.errno
+libc.src.__support.FPUtil.basic_operations
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.except_value_utils
+libc.src.__support.FPUtil.fma
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.macros.optimization
+)
add_header_library(
erff
@@ -649,6 +664,19 @@ add_header_library(
libc.src.__support.integer_literals
)
+add_header_library(
+ range_reduction
+ HDRS
+range_reduction.h
+range_reduction_fma.h
+ DEPENDS
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.fma
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.common
+)
+
add_header_library(
sincos_eval
HDRS
@@ -660,3 +688,14 @@ add_header_library(
libc.src.__support.FPUtil.polyeval
libc.src.__support.integer_literals
)
+
+add_header_library(
+ sincosf_utils
+ HDRS
+sincosf_utils.h
+ DEPENDS
+.range_reduction
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.polyeval
+libc.src.__support.common
+)
diff --git a/libc/src/__support/math/cosf.h b/libc/src/__support/math/cosf.h
new file mode 100644
index 0..074be0b314637
--- /dev/null
+++ b/libc/src/__support/math/cosf.h
@@ -0,0 +1,152 @@
+//===-- Implementation header for cosf --*- 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___SU
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor coshf16 implementation to header-only in src/__support/math folder. (PR #153582)
https://github.com/bassiounix created
https://github.com/llvm/llvm-project/pull/153582
None
>From cb0396544fe8b78e3f082ea46df08010cd32bb16 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Thu, 14 Aug 2025 17:13:46 +0300
Subject: [PATCH] [libc][math] Refactor coshf16 implementation to header-only
in src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/coshf16.h| 28
libc/src/__support/math/CMakeLists.txt| 28
libc/src/__support/math/coshf16.h | 124 ++
.../math/expxf16_utils.h} | 21 ++-
libc/src/math/generic/CMakeLists.txt | 40 ++
libc/src/math/generic/coshf16.cpp | 99 +-
libc/src/math/generic/exp2f16.cpp | 3 +-
libc/src/math/generic/exp2m1f16.cpp | 3 +-
libc/src/math/generic/expm1f16.cpp| 3 +-
libc/src/math/generic/log10f16.cpp| 3 +-
libc/src/math/generic/log2f16.cpp | 3 +-
libc/src/math/generic/logf16.cpp | 3 +-
libc/src/math/generic/sinhf16.cpp | 3 +-
libc/src/math/generic/tanhf16.cpp | 3 +-
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 58
18 files changed, 261 insertions(+), 164 deletions(-)
create mode 100644 libc/shared/math/coshf16.h
create mode 100644 libc/src/__support/math/coshf16.h
rename libc/src/{math/generic/expxf16.h => __support/math/expxf16_utils.h}
(95%)
diff --git a/libc/shared/math.h b/libc/shared/math.h
index c582877563f98..a89aa90ee1ef8 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -36,6 +36,7 @@
#include "math/cosf.h"
#include "math/cosf16.h"
#include "math/coshf.h"
+#include "math/coshf16.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
diff --git a/libc/shared/math/coshf16.h b/libc/shared/math/coshf16.h
new file mode 100644
index 0..66e8d1431df65
--- /dev/null
+++ b/libc/shared/math/coshf16.h
@@ -0,0 +1,28 @@
+//===-- Shared coshf16 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_COSHF16_H
+#define LLVM_LIBC_SHARED_MATH_COSHF16_H
+
+#include "shared/libc_common.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/math/coshf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::coshf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_COSHF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index e249af93b36c1..cddf336369973 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -419,6 +419,19 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ coshf16
+ HDRS
+coshf16.h
+ DEPENDS
+.expxf16_utils
+libc.src.__support.FPUtil.except_value_utils
+libc.src.__support.FPUtil.fenv_impl
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.rounding_mode
+libc.src.__support.macros.optimization
+)
+
add_header_library(
erff
HDRS
@@ -489,6 +502,21 @@ add_header_library(
libc.include.llvm-libc-macros.float16_macros
)
+add_header_library(
+ expxf16_utils
+ HDRS
+expxf16_utils.h
+ DEPENDS
+libc.hdr.stdint_proxy
+libc.src.__support.FPUtil.fp_bits
+libc.src.__support.FPUtil.cast
+libc.src.__support.FPUtil.multiply_add
+libc.src.__support.FPUtil.nearest_integer
+libc.src.__support.macros.attributes
+libc.src.__support.math.expf16_utils
+libc.src.__support.math.exp10_float16_constants
+)
+
add_header_library(
frexpf128
HDRS
diff --git a/libc/src/__support/math/coshf16.h
b/libc/src/__support/math/coshf16.h
new file mode 100644
index 0..4c96a78fa5254
--- /dev/null
+++ b/libc/src/__support/math/coshf16.h
@@ -0,0 +1,124 @@
+//===-- Implementation header for coshf16 ---*- 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_COSHF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_COSHF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "expxf16_utils.h"
+#include "src/__support/FPUtil/F
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor coshf16 implementation to header-only in src/__support/math folder. (PR #153582)
https://github.com/bassiounix edited https://github.com/llvm/llvm-project/pull/153582 ___ 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 coshf16 implementation to header-only in src/__support/math folder. (PR #153582)
https://github.com/bassiounix ready_for_review https://github.com/llvm/llvm-project/pull/153582 ___ 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 coshf16 implementation to header-only in src/__support/math folder. (PR #153582)
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/153582?utm_source=stack-comment-downstack-mergeability-warning"; > >on Graphite. > https://graphite.dev/docs/merge-pull-requests";>Learn more * **#153582** https://app.graphite.dev/github/pr/llvm/llvm-project/153582?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/153582?utm_source=stack-comment-view-in-graphite"; target="_blank">(View in Graphite) * **#153427** https://app.graphite.dev/github/pr/llvm/llvm-project/153427?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> * **#152871** https://app.graphite.dev/github/pr/llvm/llvm-project/152871?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> * **#152069** https://app.graphite.dev/github/pr/llvm/llvm-project/152069?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> * **#151883** https://app.graphite.dev/github/pr/llvm/llvm-project/151883?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> * **#151846** https://app.graphite.dev/github/pr/llvm/llvm-project/151846?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> * **#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"/> * **#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/153582 ___ 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 exp10m1f implementation to header-only in src/__support/math folder. (PR #159897)
https://github.com/bassiounix ready_for_review https://github.com/llvm/llvm-project/pull/159897 ___ 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 exp10m1f implementation to header-only in src/__support/math folder. (PR #159897)
https://github.com/bassiounix edited https://github.com/llvm/llvm-project/pull/159897 ___ 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 exp10m1f implementation to header-only in src/__support/math folder. (PR #159897)
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/159897?utm_source=stack-comment-downstack-mergeability-warning"; > >on Graphite. > https://graphite.dev/docs/merge-pull-requests";>Learn more * **#159897** https://app.graphite.dev/github/pr/llvm/llvm-project/159897?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/159897?utm_source=stack-comment-view-in-graphite"; target="_blank">(View in Graphite) * **#154868** https://app.graphite.dev/github/pr/llvm/llvm-project/154868?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> * **#154222** https://app.graphite.dev/github/pr/llvm/llvm-project/154222?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> * **#154215** https://app.graphite.dev/github/pr/llvm/llvm-project/154215?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> * **#153582** https://app.graphite.dev/github/pr/llvm/llvm-project/153582?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> * **#153427** https://app.graphite.dev/github/pr/llvm/llvm-project/153427?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> * **#152871** https://app.graphite.dev/github/pr/llvm/llvm-project/152871?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> * **#152069** https://app.graphite.dev/github/pr/llvm/llvm-project/152069?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> * **#151883** https://app.graphite.dev/github/pr/llvm/llvm-project/151883?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> * **#151846** https://app.graphite.dev/github/pr/llvm/llvm-project/151846?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> * **#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"/> * **#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** h
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor exp10m1f implementation to header-only in src/__support/math folder. (PR #159897)
https://github.com/bassiounix created
https://github.com/llvm/llvm-project/pull/159897
None
>From 38ba1330197517cf460530eb7410917cd0db11d8 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Sat, 20 Sep 2025 07:15:51 +0300
Subject: [PATCH] [libc][math] Refactor exp10m1f implementation to header-only
in src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/exp10m1f.h | 23 ++
libc/src/__support/math/CMakeLists.txt| 17 ++
libc/src/__support/math/exp10m1f.h| 234 ++
libc/src/math/generic/CMakeLists.txt | 11 +-
libc/src/math/generic/exp10m1f.cpp| 209 +---
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 18 +-
9 files changed, 297 insertions(+), 218 deletions(-)
create mode 100644 libc/shared/math/exp10m1f.h
create mode 100644 libc/src/__support/math/exp10m1f.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 210aa65463a2c..987ae8c340ea2 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -45,6 +45,7 @@
#include "math/exp10.h"
#include "math/exp10f.h"
#include "math/exp10f16.h"
+#include "math/exp10m1f.h"
#include "math/expf.h"
#include "math/expf16.h"
#include "math/frexpf.h"
diff --git a/libc/shared/math/exp10m1f.h b/libc/shared/math/exp10m1f.h
new file mode 100644
index 0..9093705ce801b
--- /dev/null
+++ b/libc/shared/math/exp10m1f.h
@@ -0,0 +1,23 @@
+//===-- Shared exp10m1f 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_EXP10M1F_H
+#define LLVM_LIBC_SHARED_MATH_EXP10M1F_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/exp10m1f.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::exp10m1f;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_EXP10M1F_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index 648f33d347ce1..351b03259f2f6 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -465,6 +465,23 @@ add_header_library(
libc.src.__support.FPUtil.generic.sqrt
)
+add_header_library(
+ exp10m1f
+ HDRS
+exp10m1f.h
+ DEPENDS
+.exp10f_utils
+libc.src.errno.errno
+libc.src.__support.common
+libc.src.__support.FPUtil.except_value_utils
+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.FPUtil.rounding_mode
+libc.src.__support.macros.optimization
+)
+
add_header_library(
erff
HDRS
diff --git a/libc/src/__support/math/exp10m1f.h
b/libc/src/__support/math/exp10m1f.h
new file mode 100644
index 0..9fe4ff774ec68
--- /dev/null
+++ b/libc/src/__support/math/exp10m1f.h
@@ -0,0 +1,234 @@
+//===-- Implementation header for exp10m1f --*- 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_EXP10M1F_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_EXP10M1F_H
+
+#include "exp10f_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/rounding_mode.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+namespace exp10m1f_internal {
+
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+static constexpr size_t N_EXCEPTS_LO = 11;
+
+static constexpr fputil::ExceptValues EXP10M1F_EXCEPTS_LO
=
+{{
+// x = 0x1.0fe54ep-11, exp10m1f(x) = 0x1.3937eep-10 (RZ)
+{0x3a07'f2a7U, 0x3a9c'9bf7U, 1U, 0U, 1U},
+// x = 0x1.80e6eap-11, exp10m1f(x) = 0x1.bb8272p-10 (RZ)
+{0x3a40'7375U, 0x3add'c139U, 1U, 0U, 1U},
+// x = -0x1.2a33bcp-51, exp10m1f(x) = -0x1.57515ep-50 (RZ)
+{0xa615'19deU, 0xa6ab'a8afU, 0U, 1U, 0U},
+// x = -0x0p+0, exp10m1f(x) = -0x0p+0 (RZ)
+{0x8000'U, 0x8000'U, 0U, 0U, 0U},
+// x = -0x1.b59e08p-3
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor exp2m1f implementation to header-only in src/__support/math folder. (PR #162017)
https://github.com/bassiounix updated
https://github.com/llvm/llvm-project/pull/162017
>From f2e2010a4077d9bf0f2cb55e05f0273c3c6fe10b Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Sun, 5 Oct 2025 17:52:54 +0300
Subject: [PATCH] [libc][math] Refactor exp2m1f implementation to header-only
in src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/exp2m1f.h| 23 +++
libc/src/__support/math/CMakeLists.txt| 18 ++
libc/src/__support/math/exp2m1f.h | 195 ++
libc/src/math/generic/CMakeLists.txt | 12 +-
libc/src/math/generic/exp2m1f.cpp | 177 +---
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 19 +-
9 files changed, 259 insertions(+), 188 deletions(-)
create mode 100644 libc/shared/math/exp2m1f.h
create mode 100644 libc/src/__support/math/exp2m1f.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 8bff70f1c5336..0c0e8560bd40f 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -50,6 +50,7 @@
#include "math/exp2.h"
#include "math/exp2f.h"
#include "math/exp2f16.h"
+#include "math/exp2m1f.h"
#include "math/expf.h"
#include "math/expf16.h"
#include "math/frexpf.h"
diff --git a/libc/shared/math/exp2m1f.h b/libc/shared/math/exp2m1f.h
new file mode 100644
index 0..ca9754774f0fc
--- /dev/null
+++ b/libc/shared/math/exp2m1f.h
@@ -0,0 +1,23 @@
+//===-- Shared exp2m1f 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_EXP2M1F_H
+#define LLVM_LIBC_SHARED_MATH_EXP2M1F_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/exp2m1f.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::exp2m1f;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_EXP2M1F_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index 185900efa7354..ec2d8948f615b 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -767,6 +767,24 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ exp2m1f
+ HDRS
+exp2m1f.h
+ DEPENDS
+.exp10f_utils
+libc.src.errno.errno
+libc.src.__support.common
+libc.src.__support.FPUtil.except_value_utils
+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.FPUtil.rounding_mode
+libc.src.__support.macros.optimization
+libc.src.__support.macros.properties.cpu_features
+)
+
add_header_library(
exp10
HDRS
diff --git a/libc/src/__support/math/exp2m1f.h
b/libc/src/__support/math/exp2m1f.h
new file mode 100644
index 0..e95076c9eac22
--- /dev/null
+++ b/libc/src/__support/math/exp2m1f.h
@@ -0,0 +1,195 @@
+//===-- Implementation header for exp2m1f *-
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_EXP2M1F_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_EXP2M1F_H
+
+#include "exp10f_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/rounding_mode.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
+#include "src/__support/macros/properties/cpu_features.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float exp2m1f(float x) {
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+ constexpr size_t N_EXCEPTS_LO = 8;
+
+ constexpr fputil::ExceptValues EXP2M1F_EXCEPTS_LO = {{
+ // (input, RZ output, RU offset, RD offset, RN offset)
+ // x = 0x1.36dc8ep-36, exp2m1f(x) = 0x1.aef212p-37 (RZ)
+ {0x2d9b'6e47U, 0x2d57'7909U, 1U, 0U, 0U},
+ // x = 0x1.224936p-19, exp2m1f(x) = 0x1.926c0ep-20 (RZ)
+ {0x3611'249bU, 0x35c9'3607U, 1U, 0U, 1U},
+ // x = 0x1.d16d2p-20, exp2m1f(x) = 0x1.429becp-20 (RZ)
+ {0x35e8'b690U, 0x35a1'4df6U, 1U, 0U, 1U}
[llvm-branch-commits] [libc] [llvm] [libc][math] Refactor exp2m1f16 implementation to header-only in src/__support/math folder. (PR #162019)
https://github.com/bassiounix created
https://github.com/llvm/llvm-project/pull/162019
None
>From 8400f595f9cf58fbb1769c42d2bdd91a75995cd5 Mon Sep 17 00:00:00 2001
From: bassiounix
Date: Sun, 5 Oct 2025 18:08:23 +0300
Subject: [PATCH] [libc][math] Refactor exp2m1f16 implementation to header-only
in src/__support/math folder.
---
libc/shared/math.h| 1 +
libc/shared/math/exp2m1f16.h | 29 +++
libc/src/__support/math/CMakeLists.txt| 18 ++
libc/src/__support/math/exp2m1f16.h | 180 ++
libc/src/math/generic/CMakeLists.txt | 14 +-
libc/src/math/generic/exp2m1f16.cpp | 155 +--
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 18 +-
9 files changed, 250 insertions(+), 167 deletions(-)
create mode 100644 libc/shared/math/exp2m1f16.h
create mode 100644 libc/src/__support/math/exp2m1f16.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 0c0e8560bd40f..bcddb39021a9c 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -51,6 +51,7 @@
#include "math/exp2f.h"
#include "math/exp2f16.h"
#include "math/exp2m1f.h"
+#include "math/exp2m1f16.h"
#include "math/expf.h"
#include "math/expf16.h"
#include "math/frexpf.h"
diff --git a/libc/shared/math/exp2m1f16.h b/libc/shared/math/exp2m1f16.h
new file mode 100644
index 0..96a404708be18
--- /dev/null
+++ b/libc/shared/math/exp2m1f16.h
@@ -0,0 +1,29 @@
+//===-- Shared exp2m1f16 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_EXP2M1F16_H
+#define LLVM_LIBC_SHARED_MATH_EXP2M1F16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+#include "shared/libc_common.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/math/exp2m1f16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::exp2m1f16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_EXP2M1F16_H
diff --git a/libc/src/__support/math/CMakeLists.txt
b/libc/src/__support/math/CMakeLists.txt
index ec2d8948f615b..aaace44d04d3b 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -785,6 +785,24 @@ add_header_library(
libc.src.__support.macros.properties.cpu_features
)
+add_header_library(
+ exp2m1f16
+ HDRS
+exp2m1f16.h
+ DEPENDS
+.expxf16_utils
+libc.src.__support.common
+libc.src.__support.FPUtil.cast
+libc.src.__support.FPUtil.except_value_utils
+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.FPUtil.rounding_mode
+libc.src.__support.macros.optimization
+libc.src.__support.macros.properties.cpu_features
+)
+
add_header_library(
exp10
HDRS
diff --git a/libc/src/__support/math/exp2m1f16.h
b/libc/src/__support/math/exp2m1f16.h
new file mode 100644
index 0..0424af4aa953d
--- /dev/null
+++ b/libc/src/__support/math/exp2m1f16.h
@@ -0,0 +1,180 @@
+//===-- Implementation header for exp2m1f16 --*-
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_EXP2M1F16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_EXP2M1F16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/rounding_mode.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
+#include "src/__support/macros/properties/cpu_features.h"
+#include "src/__support/math/expxf16_utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float16 exp2m1f16(float16 x) {
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+ constexpr fputil::ExceptValues EXP2M1F16_EXCEPTS_LO = {{
+ // (input, RZ output, RU offset, RD offset, RN offset)
+ // x = 0x1.cf4p-13, exp2m1f16(x) = 0x1.41p-13 (RZ)
+ {0x0b3dU, 0x0904U, 1U, 0U, 1U},
[llvm-branch-commits] [libc] [libc][annex_k] Add errno_t. (PR #163094)
@@ -0,0 +1,21 @@ +//===-- Definition of type errno_t ===// +// +// 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_INCLUDE_LLVM_LIBC_TYPES_ERRNO_T_H +#define LLVM_LIBC_INCLUDE_LLVM_LIBC_TYPES_ERRNO_T_H + +// LIBC_HAS_ANNEX_K is a necessary check guard here because errno_t is only +// defined when Annex K is enabled. We use LIBC_HAS_ANNEX_K internally to +// indicate whether Annex K is enabled or not. bassiounix wrote: done. https://github.com/llvm/llvm-project/pull/163094 ___ 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 exp2f implementation to header-only in src/__support/math folder. (PR #161992)
https://github.com/bassiounix ready_for_review https://github.com/llvm/llvm-project/pull/161992 ___ 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 exp2f16 implementation to header-only in src/__support/math folder. (PR #161993)
https://github.com/bassiounix edited https://github.com/llvm/llvm-project/pull/161993 ___ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
