Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/shared/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
23 changes: 23 additions & 0 deletions libc/shared/math/atanhf.h
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions libc/src/__support/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
76 changes: 76 additions & 0 deletions libc/src/__support/math/atanhf.h
Original file line number Diff line number Diff line change
@@ -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
5 changes: 1 addition & 4 deletions libc/src/math/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
56 changes: 2 additions & 54 deletions libc/src/math/generic/atanhf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions libc/test/shared/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions libc/test/shared/shared_math_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
20 changes: 13 additions & 7 deletions utils/bazel/llvm-project-overlay/libc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -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",
],
)

Expand Down
Loading