[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 llvm-branch-commits@lists.llvm.org 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 llvm-branch-commits@lists.llvm.org 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 llvm-branch-commits@lists.llvm.org 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 llvm-branch-commits@lists.llvm.org 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 llvm-branch-commits@lists.llvm.org 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 llvm-branch-commits@lists.llvm.org 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 llvm-branch-commits@lists.llvm.org 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 llvm-branch-commits@lists.llvm.org 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 llvm-branch-commits@lists.llvm.org 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 llvm-branch-commits@lists.llvm.org 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 llvm-branch-commits@lists.llvm.org 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 llvm-branch-commits@lists.llvm.org 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 llvm-branch-commits@lists.llvm.org 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 llvm-branch-commits@lists.llvm.org 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 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 llvm-branch-commits@lists.llvm.org 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 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 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