https://github.com/bassiounix updated https://github.com/llvm/llvm-project/pull/150868
>From e361d72613ded94fa78a5c1a4baa366c799a6d7b Mon Sep 17 00:00:00 2001 From: bassiounix <muhammad.m.bassio...@gmail.com> 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 0000000000000..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 0000000000000..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<float16, N_EXCEPTS> 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<float16>; + FPBits xbits(x); + + uint16_t x_u = xbits.uintval(); + uint16_t x_abs = x_u & 0x7fff; + bool x_sign = x_u >> 15; + float sign = (x_sign ? -1.0 : 1.0); + + // |x| >= +/-inf + if (LIBC_UNLIKELY(x_abs >= 0x7c00)) { + if (xbits.is_nan()) { + if (xbits.is_signaling_nan()) { + fputil::raise_except_if_required(FE_INVALID); + return FPBits::quiet_nan().get_val(); + } + return x; + } + + // atanf16(+/-inf) = +/-pi/2 + return fputil::cast<float16>(sign * PI_2); + } + + float xf = x; + float xsq = xf * xf; +#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS + // Handle exceptional values + if (auto r = ATANF16_EXCEPTS.lookup_odd(x_abs, x_sign); + LIBC_UNLIKELY(r.has_value())) + return r.value(); +#endif + + // |x| <= 0x1p0, |x| <= 1 + if (x_abs <= 0x3c00) { + // atanf16(+/-0) = +/-0 + if (LIBC_UNLIKELY(x_abs == 0)) + return x; + + // Degree-14 minimax odd polynomial of atan(x) generated by Sollya with: + // > P = fpminimax(atan(x)/x, [|0, 2, 4, 6, 8, 10, 12, 14|], [|SG...|], + // [0, 1]); + float result = fputil::polyeval( + xsq, 0x1.fffffcp-1f, -0x1.55519ep-2f, 0x1.98f6a8p-3f, -0x1.1f0a92p-3f, + 0x1.95b654p-4f, -0x1.e65492p-5f, 0x1.8c0c36p-6f, -0x1.32316ep-8f); + return fputil::cast<float16>(xf * result); + } + + // If |x| > 1 + // y = atan(x) = sign(x) * atan(|x|) + // atan(|x|) = pi/2 - atan(1/|x|) + // Recall, 1/|x| < 1 + float x_inv_sq = 1.0f / xsq; + float x_inv = fputil::sqrt<float>(x_inv_sq); + + // Degree-14 minimax odd polynomial of atan(x) generated by Sollya with: + // > P = fpminimax(atan(x)/x, [|0, 2, 4, 6, 8, 10, 12, 14|], [|SG...|], + // [0, 1]); + float interm = + fputil::polyeval(x_inv_sq, 0x1.fffffcp-1f, -0x1.55519ep-2f, + 0x1.98f6a8p-3f, -0x1.1f0a92p-3f, 0x1.95b654p-4f, + -0x1.e65492p-5f, 0x1.8c0c36p-6f, -0x1.32316ep-8f); + + return fputil::cast<float16>(sign * + fputil::multiply_add(x_inv, -interm, PI_2)); +} + +} // namespace math + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT16 + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ATANF16_H diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index 196e00841f2d1..007e5701b7538 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -4024,17 +4024,7 @@ add_entrypoint_object( HDRS ../atanf16.h DEPENDS - libc.hdr.errno_macros - libc.hdr.fenv_macros - 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 - libc.src.__support.macros.properties.types + libc.src.__support.math.atanf16 ) add_entrypoint_object( diff --git a/libc/src/math/generic/atanf16.cpp b/libc/src/math/generic/atanf16.cpp index 9b6ec657417a5..7191c4242160a 100644 --- a/libc/src/math/generic/atanf16.cpp +++ b/libc/src/math/generic/atanf16.cpp @@ -7,101 +7,10 @@ //===----------------------------------------------------------------------===// #include "src/math/atanf16.h" -#include "hdr/errno_macros.h" -#include "hdr/fenv_macros.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/sqrt.h" -#include "src/__support/macros/optimization.h" +#include "src/__support/math/atanf16.h" namespace LIBC_NAMESPACE_DECL { -// Generated by Solly using the following command: -// > round(pi/2, SG, RN); -static constexpr float PI_2 = 0x1.921fb6p0; - -#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS -static constexpr size_t N_EXCEPTS = 6; - -static constexpr fputil::ExceptValues<float16, N_EXCEPTS> 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 - -LLVM_LIBC_FUNCTION(float16, atanf16, (float16 x)) { - using FPBits = fputil::FPBits<float16>; - FPBits xbits(x); - - uint16_t x_u = xbits.uintval(); - uint16_t x_abs = x_u & 0x7fff; - bool x_sign = x_u >> 15; - float sign = (x_sign ? -1.0 : 1.0); - - // |x| >= +/-inf - if (LIBC_UNLIKELY(x_abs >= 0x7c00)) { - if (xbits.is_nan()) { - if (xbits.is_signaling_nan()) { - fputil::raise_except_if_required(FE_INVALID); - return FPBits::quiet_nan().get_val(); - } - return x; - } - - // atanf16(+/-inf) = +/-pi/2 - return fputil::cast<float16>(sign * PI_2); - } - - float xf = x; - float xsq = xf * xf; -#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS - // Handle exceptional values - if (auto r = ATANF16_EXCEPTS.lookup_odd(x_abs, x_sign); - LIBC_UNLIKELY(r.has_value())) - return r.value(); -#endif - - // |x| <= 0x1p0, |x| <= 1 - if (x_abs <= 0x3c00) { - // atanf16(+/-0) = +/-0 - if (LIBC_UNLIKELY(x_abs == 0)) - return x; - - // Degree-14 minimax odd polynomial of atan(x) generated by Sollya with: - // > P = fpminimax(atan(x)/x, [|0, 2, 4, 6, 8, 10, 12, 14|], [|SG...|], - // [0, 1]); - float result = fputil::polyeval( - xsq, 0x1.fffffcp-1f, -0x1.55519ep-2f, 0x1.98f6a8p-3f, -0x1.1f0a92p-3f, - 0x1.95b654p-4f, -0x1.e65492p-5f, 0x1.8c0c36p-6f, -0x1.32316ep-8f); - return fputil::cast<float16>(xf * result); - } - - // If |x| > 1 - // y = atan(x) = sign(x) * atan(|x|) - // atan(|x|) = pi/2 - atan(1/|x|) - // Recall, 1/|x| < 1 - float x_inv_sq = 1.0f / xsq; - float x_inv = fputil::sqrt<float>(x_inv_sq); - - // Degree-14 minimax odd polynomial of atan(x) generated by Sollya with: - // > P = fpminimax(atan(x)/x, [|0, 2, 4, 6, 8, 10, 12, 14|], [|SG...|], - // [0, 1]); - float interm = - fputil::polyeval(x_inv_sq, 0x1.fffffcp-1f, -0x1.55519ep-2f, - 0x1.98f6a8p-3f, -0x1.1f0a92p-3f, 0x1.95b654p-4f, - -0x1.e65492p-5f, 0x1.8c0c36p-6f, -0x1.32316ep-8f); - - return fputil::cast<float16>(sign * - fputil::multiply_add(x_inv, -interm, PI_2)); -} +LLVM_LIBC_FUNCTION(float16, atanf16, (float16 x)) { return math::atanf16(x); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt index 6d0601feda138..2dd5bdafe24f6 100644 --- a/libc/test/shared/CMakeLists.txt +++ b/libc/test/shared/CMakeLists.txt @@ -20,6 +20,7 @@ add_fp_unittest( libc.src.__support.math.asinhf16 libc.src.__support.math.atan libc.src.__support.math.atanf + libc.src.__support.math.atanf16 libc.src.__support.math.erff libc.src.__support.math.exp libc.src.__support.math.exp10 diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp index 228fa42ec834e..2b80068f991be 100644 --- a/libc/test/shared/shared_math_test.cpp +++ b/libc/test/shared/shared_math_test.cpp @@ -19,6 +19,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) { EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::acospif16(1.0f16)); EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::asinf16(0.0f16)); EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::asinhf16(0.0f16)); + EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::atanf16(0.0f16)); EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::exp10f16(0.0f16)); diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel index e8c59fa87d4c3..77e4829f44758 100644 --- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel @@ -2288,6 +2288,21 @@ libc_support_library( ], ) +libc_support_library( + name = "__support_math_atanf16", + hdrs = ["src/__support/math/atanf16.h"], + deps = [ + ":__support_fputil_cast", + ":__support_fputil_except_value_utils", + ":__support_fputil_fenv_impl", + ":__support_fputil_fp_bits", + ":__support_fputil_polyeval", + ":__support_fputil_multiply_add", + ":__support_fputil_sqrt", + ":__support_macros_optimization", + ], +) + libc_support_library( name = "__support_math_asinf", hdrs = ["src/__support/math/asinf.h"], @@ -2904,6 +2919,13 @@ libc_math_function( ], ) +libc_math_function( + name = "atanf16", + additional_deps = [ + ":__support_math_atanf16" + ], +) + libc_math_function( name = "atan", additional_deps = [ _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits