[libc][math] Refactor atanhf implementation to header-only in src/__support/math folder. (#151399)

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
This commit is contained in:
Muhammad Bassiouni 2025-08-02 02:54:13 +03:00 committed by GitHub
parent 154354e60e
commit 99cd1d8aef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 129 additions and 65 deletions

View File

@ -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"

23
libc/shared/math/atanhf.h Normal file
View File

@ -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

View File

@ -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

View File

@ -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<float>;
FPBits xbits(x);
Sign sign = xbits.sign();
uint32_t x_abs = xbits.abs().uintval();
// |x| >= 1.0
if (LIBC_UNLIKELY(x_abs >= 0x3F80'0000U)) {
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'0000U) {
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'0000U)) {
// |x| <= 2^-26
if (LIBC_UNLIKELY(x_abs <= 0x3280'0000U)) {
return static_cast<float>(LIBC_UNLIKELY(x_abs == 0)
? x
: (x + 0x1.5555555555555p-2 * x * x * x));
}
double xdbl = x;
double x2 = xdbl * xdbl;
// Pure Taylor series.
double pe = fputil::polyeval(x2, 0.0, 0x1.5555555555555p-2,
0x1.999999999999ap-3, 0x1.2492492492492p-3,
0x1.c71c71c71c71cp-4, 0x1.745d1745d1746p-4);
return static_cast<float>(fputil::multiply_add(xdbl, pe, xdbl));
}
double xdbl = x;
return static_cast<float>(0.5 * log_eval((xdbl + 1.0) / (xdbl - 1.0)));
}
} // namespace math
} // namespace LIBC_NAMESPACE_DECL
#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ATANHF_H

View File

@ -3922,10 +3922,7 @@ add_entrypoint_object(
HDRS
../atanhf.h
DEPENDS
.explogxf
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.macros.optimization
libc.src.__support.math.atanhf
)
add_entrypoint_object(

View File

@ -7,62 +7,10 @@
//===----------------------------------------------------------------------===//
#include "src/math/atanhf.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
#include "src/math/generic/explogxf.h"
#include "src/__support/math/atanhf.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(float, atanhf, (float x)) {
using namespace acoshf_internal;
using FPBits = typename fputil::FPBits<float>;
FPBits xbits(x);
Sign sign = xbits.sign();
uint32_t x_abs = xbits.abs().uintval();
// |x| >= 1.0
if (LIBC_UNLIKELY(x_abs >= 0x3F80'0000U)) {
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'0000U) {
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'0000U)) {
// |x| <= 2^-26
if (LIBC_UNLIKELY(x_abs <= 0x3280'0000U)) {
return static_cast<float>(LIBC_UNLIKELY(x_abs == 0)
? x
: (x + 0x1.5555555555555p-2 * x * x * x));
}
double xdbl = x;
double x2 = xdbl * xdbl;
// Pure Taylor series.
double pe = fputil::polyeval(x2, 0.0, 0x1.5555555555555p-2,
0x1.999999999999ap-3, 0x1.2492492492492p-3,
0x1.c71c71c71c71cp-4, 0x1.745d1745d1746p-4);
return static_cast<float>(fputil::multiply_add(xdbl, pe, xdbl));
}
double xdbl = x;
return static_cast<float>(0.5 * log_eval((xdbl + 1.0) / (xdbl - 1.0)));
}
LLVM_LIBC_FUNCTION(float, atanhf, (float x)) { return math::atanhf(x); }
} // namespace LIBC_NAMESPACE_DECL

View File

@ -24,6 +24,7 @@ add_fp_unittest(
libc.src.__support.math.atan2f128
libc.src.__support.math.atanf
libc.src.__support.math.atanf16
libc.src.__support.math.atanhf
libc.src.__support.math.erff
libc.src.__support.math.exp
libc.src.__support.math.exp10

View File

@ -47,6 +47,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::asinhf(0.0f));
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::atan2f(0.0f, 0.0f));
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::atanf(0.0f));
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::atanhf(0.0f));
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::erff(0.0f));
EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::exp10f(0.0f));
EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::expf(0.0f));

View File

@ -2373,6 +2373,18 @@ libc_support_library(
],
)
libc_support_library(
name = "__support_math_atanhf",
hdrs = ["src/__support/math/atanhf.h"],
deps = [
":__support_math_acoshf_utils",
":__support_fputil_fenv_impl",
":__support_fputil_fp_bits",
":__support_macros_config",
":__support_macros_optimization",
],
)
libc_support_library(
name = "__support_math_erff",
hdrs = ["src/__support/math/erff.h"],
@ -2998,13 +3010,7 @@ libc_math_function(
libc_math_function(
name = "atanhf",
additional_deps = [
":__support_fputil_fma",
":__support_fputil_multiply_add",
":__support_fputil_nearest_integer",
":__support_fputil_polyeval",
":__support_macros_optimization",
":common_constants",
":explogxf",
":__support_math_atanhf",
],
)