Skip to content

Commit 909f8e7

Browse files
committed
[libc][math] Refactor atanhf implementation to header-only in src/__support/math folder.
1 parent 69398d2 commit 909f8e7

File tree

9 files changed

+129
-65
lines changed

9 files changed

+129
-65
lines changed

libc/shared/math.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "math/atan2f128.h"
2929
#include "math/atanf.h"
3030
#include "math/atanf16.h"
31+
#include "math/atanhf.h"
3132
#include "math/erff.h"
3233
#include "math/exp.h"
3334
#include "math/exp10.h"

libc/shared/math/atanhf.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Shared atanhf function ----------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SHARED_MATH_ATANHF_H
10+
#define LLVM_LIBC_SHARED_MATH_ATANHF_H
11+
12+
#include "shared/libc_common.h"
13+
#include "src/__support/math/atanhf.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
namespace shared {
17+
18+
using math::atanhf;
19+
20+
} // namespace shared
21+
} // namespace LIBC_NAMESPACE_DECL
22+
23+
#endif // LLVM_LIBC_SHARED_MATH_ATANHF_H

libc/src/__support/math/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,17 @@ add_header_library(
275275
libc.src.__support.macros.optimization
276276
)
277277

278+
add_header_library(
279+
atanhf
280+
HDRS
281+
atanhf.h
282+
DEPENDS
283+
.acoshf_utils
284+
libc.src.__support.FPUtil.fp_bits
285+
libc.src.__support.FPUtil.fenv_impl
286+
libc.src.__support.macros.optimization
287+
)
288+
278289
add_header_library(
279290
asinf
280291
HDRS

libc/src/__support/math/atanhf.h

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//===-- Implementation header for atanhf ------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_ATANHF_H
10+
#define LLVM_LIBC_SRC___SUPPORT_MATH_ATANHF_H
11+
12+
#include "acoshf_utils.h"
13+
#include "src/__support/FPUtil/FEnvImpl.h"
14+
#include "src/__support/FPUtil/FPBits.h"
15+
#include "src/__support/macros/config.h"
16+
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
17+
18+
namespace LIBC_NAMESPACE_DECL {
19+
20+
namespace math {
21+
22+
LIBC_INLINE static constexpr float atanhf(float x) {
23+
using namespace acoshf_internal;
24+
using FPBits = typename fputil::FPBits<float>;
25+
26+
FPBits xbits(x);
27+
Sign sign = xbits.sign();
28+
uint32_t x_abs = xbits.abs().uintval();
29+
30+
// |x| >= 1.0
31+
if (LIBC_UNLIKELY(x_abs >= 0x3F80'0000U)) {
32+
if (xbits.is_nan()) {
33+
if (xbits.is_signaling_nan()) {
34+
fputil::raise_except_if_required(FE_INVALID);
35+
return FPBits::quiet_nan().get_val();
36+
}
37+
return x;
38+
}
39+
// |x| == 1.0
40+
if (x_abs == 0x3F80'0000U) {
41+
fputil::set_errno_if_required(ERANGE);
42+
fputil::raise_except_if_required(FE_DIVBYZERO);
43+
return FPBits::inf(sign).get_val();
44+
} else {
45+
fputil::set_errno_if_required(EDOM);
46+
fputil::raise_except_if_required(FE_INVALID);
47+
return FPBits::quiet_nan().get_val();
48+
}
49+
}
50+
51+
// |x| < ~0.10
52+
if (LIBC_UNLIKELY(x_abs <= 0x3dcc'0000U)) {
53+
// |x| <= 2^-26
54+
if (LIBC_UNLIKELY(x_abs <= 0x3280'0000U)) {
55+
return static_cast<float>(LIBC_UNLIKELY(x_abs == 0)
56+
? x
57+
: (x + 0x1.5555555555555p-2 * x * x * x));
58+
}
59+
60+
double xdbl = x;
61+
double x2 = xdbl * xdbl;
62+
// Pure Taylor series.
63+
double pe = fputil::polyeval(x2, 0.0, 0x1.5555555555555p-2,
64+
0x1.999999999999ap-3, 0x1.2492492492492p-3,
65+
0x1.c71c71c71c71cp-4, 0x1.745d1745d1746p-4);
66+
return static_cast<float>(fputil::multiply_add(xdbl, pe, xdbl));
67+
}
68+
double xdbl = x;
69+
return static_cast<float>(0.5 * log_eval((xdbl + 1.0) / (xdbl - 1.0)));
70+
}
71+
72+
} // namespace math
73+
74+
} // namespace LIBC_NAMESPACE_DECL
75+
76+
#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ATANHF_H

libc/src/math/generic/CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3922,10 +3922,7 @@ add_entrypoint_object(
39223922
HDRS
39233923
../atanhf.h
39243924
DEPENDS
3925-
.explogxf
3926-
libc.src.__support.FPUtil.fp_bits
3927-
libc.src.__support.FPUtil.fenv_impl
3928-
libc.src.__support.macros.optimization
3925+
libc.src.__support.math.atanhf
39293926
)
39303927

39313928
add_entrypoint_object(

libc/src/math/generic/atanhf.cpp

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,62 +7,10 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/math/atanhf.h"
10-
#include "src/__support/FPUtil/FEnvImpl.h"
11-
#include "src/__support/FPUtil/FPBits.h"
12-
#include "src/__support/macros/config.h"
13-
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
14-
#include "src/math/generic/explogxf.h"
10+
#include "src/__support/math/atanhf.h"
1511

1612
namespace LIBC_NAMESPACE_DECL {
1713

18-
LLVM_LIBC_FUNCTION(float, atanhf, (float x)) {
19-
using namespace acoshf_internal;
20-
using FPBits = typename fputil::FPBits<float>;
21-
22-
FPBits xbits(x);
23-
Sign sign = xbits.sign();
24-
uint32_t x_abs = xbits.abs().uintval();
25-
26-
// |x| >= 1.0
27-
if (LIBC_UNLIKELY(x_abs >= 0x3F80'0000U)) {
28-
if (xbits.is_nan()) {
29-
if (xbits.is_signaling_nan()) {
30-
fputil::raise_except_if_required(FE_INVALID);
31-
return FPBits::quiet_nan().get_val();
32-
}
33-
return x;
34-
}
35-
// |x| == 1.0
36-
if (x_abs == 0x3F80'0000U) {
37-
fputil::set_errno_if_required(ERANGE);
38-
fputil::raise_except_if_required(FE_DIVBYZERO);
39-
return FPBits::inf(sign).get_val();
40-
} else {
41-
fputil::set_errno_if_required(EDOM);
42-
fputil::raise_except_if_required(FE_INVALID);
43-
return FPBits::quiet_nan().get_val();
44-
}
45-
}
46-
47-
// |x| < ~0.10
48-
if (LIBC_UNLIKELY(x_abs <= 0x3dcc'0000U)) {
49-
// |x| <= 2^-26
50-
if (LIBC_UNLIKELY(x_abs <= 0x3280'0000U)) {
51-
return static_cast<float>(LIBC_UNLIKELY(x_abs == 0)
52-
? x
53-
: (x + 0x1.5555555555555p-2 * x * x * x));
54-
}
55-
56-
double xdbl = x;
57-
double x2 = xdbl * xdbl;
58-
// Pure Taylor series.
59-
double pe = fputil::polyeval(x2, 0.0, 0x1.5555555555555p-2,
60-
0x1.999999999999ap-3, 0x1.2492492492492p-3,
61-
0x1.c71c71c71c71cp-4, 0x1.745d1745d1746p-4);
62-
return static_cast<float>(fputil::multiply_add(xdbl, pe, xdbl));
63-
}
64-
double xdbl = x;
65-
return static_cast<float>(0.5 * log_eval((xdbl + 1.0) / (xdbl - 1.0)));
66-
}
14+
LLVM_LIBC_FUNCTION(float, atanhf, (float x)) { return math::atanhf(x); }
6715

6816
} // namespace LIBC_NAMESPACE_DECL

libc/test/shared/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ add_fp_unittest(
2424
libc.src.__support.math.atan2f128
2525
libc.src.__support.math.atanf
2626
libc.src.__support.math.atanf16
27+
libc.src.__support.math.atanhf
2728
libc.src.__support.math.erff
2829
libc.src.__support.math.exp
2930
libc.src.__support.math.exp10

libc/test/shared/shared_math_test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
4747
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::asinhf(0.0f));
4848
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::atan2f(0.0f, 0.0f));
4949
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::atanf(0.0f));
50+
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::atanhf(0.0f));
5051
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::erff(0.0f));
5152
EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::exp10f(0.0f));
5253
EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::expf(0.0f));

utils/bazel/llvm-project-overlay/libc/BUILD.bazel

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2373,6 +2373,18 @@ libc_support_library(
23732373
],
23742374
)
23752375

2376+
libc_support_library(
2377+
name = "__support_math_atanhf",
2378+
hdrs = ["src/__support/math/atanhf.h"],
2379+
deps = [
2380+
":__support_math_acoshf_utils",
2381+
":__support_fputil_fenv_impl",
2382+
":__support_fputil_fp_bits",
2383+
":__support_macros_config",
2384+
":__support_macros_optimization",
2385+
],
2386+
)
2387+
23762388
libc_support_library(
23772389
name = "__support_math_erff",
23782390
hdrs = ["src/__support/math/erff.h"],
@@ -2998,13 +3010,7 @@ libc_math_function(
29983010
libc_math_function(
29993011
name = "atanhf",
30003012
additional_deps = [
3001-
":__support_fputil_fma",
3002-
":__support_fputil_multiply_add",
3003-
":__support_fputil_nearest_integer",
3004-
":__support_fputil_polyeval",
3005-
":__support_macros_optimization",
3006-
":common_constants",
3007-
":explogxf",
3013+
":__support_math_atanhf",
30083014
],
30093015
)
30103016

0 commit comments

Comments
 (0)