Skip to content

Commit c07ca89

Browse files
committed
[libc][math][c23] Update for comments.
1 parent c1f0d01 commit c07ca89

File tree

4 files changed

+24
-34
lines changed

4 files changed

+24
-34
lines changed

libc/src/math/generic/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4003,6 +4003,7 @@ add_entrypoint_object(
40034003
libc.hdr.errno_macros
40044004
libc.hdr.fenv_macros
40054005
libc.src.__support.FPUtil.cast
4006+
libc.src.__support.FPUtil.fenv_impl
40064007
libc.src.__support.FPUtil.fp_bits
40074008
libc.src.__support.FPUtil.multiply_add
40084009
libc.src.__support.FPUtil.polyeval

libc/src/math/generic/atanhf16.cpp

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===-- Implementation of atanh(x) function -------------------------------===//
1+
//===-- Half-precision atanh(x) function ----------------------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -10,6 +10,7 @@
1010
#include "explogxf.h"
1111
#include "hdr/errno_macros.h"
1212
#include "hdr/fenv_macros.h"
13+
#include "src/__support/FPUtil/FEnvImpl.h"
1314
#include "src/__support/FPUtil/FPBits.h"
1415
#include "src/__support/FPUtil/PolyEval.h"
1516
#include "src/__support/FPUtil/cast.h"
@@ -21,26 +22,27 @@
2122
namespace LIBC_NAMESPACE_DECL {
2223

2324
LLVM_LIBC_FUNCTION(float16, atanhf16, (float16 x)) {
24-
using FPBits = typename fputil::FPBits<float16>;
25+
using FPBits = fputil::FPBits<float16>;
2526

2627
FPBits xbits(x);
2728
Sign sign = xbits.sign();
2829
uint16_t x_abs = xbits.abs().uintval();
2930

31+
// |x| >= 1
3032
if (LIBC_UNLIKELY(x_abs >= 0x3c00U)) {
31-
if (xbits.is_nan()) {
33+
if (xbits.is_nan())
3234
return x;
33-
}
35+
3436
// |x| == 1.0
3537
if (x_abs == 0x3c00U) {
3638
fputil::set_errno_if_required(ERANGE);
3739
fputil::raise_except_if_required(FE_DIVBYZERO);
3840
return FPBits::inf(sign).get_val();
39-
} else {
40-
fputil::set_errno_if_required(EDOM);
41-
fputil::raise_except_if_required(FE_INVALID);
42-
return FPBits::quiet_nan().get_val();
4341
}
42+
// |x| > 1.0
43+
fputil::set_errno_if_required(EDOM);
44+
fputil::raise_except_if_required(FE_INVALID);
45+
return FPBits::quiet_nan().get_val();
4446
}
4547

4648
// For |x| less than approximately 0.10
@@ -52,35 +54,25 @@ LLVM_LIBC_FUNCTION(float16, atanhf16, (float16 x)) {
5254
// atanh(x) ≈ x + (1/3)*x^3
5355
if (LIBC_UNLIKELY(x_abs < 0x0100U)) {
5456
return static_cast<float16>(
55-
LIBC_UNLIKELY(x_abs == 0) ? x : (x + 0x1.555556p-2 * x * x * x));
57+
LIBC_UNLIKELY(x_abs == 0) ? x : (x + 0x1.555556p-2f * x * x * x));
5658
}
5759

5860
// For 0x0100U <= |x| <= 0x2e66U:
5961
// Let t = x^2.
6062
// Define P(t) ≈ (1/3)*t + (1/5)*t^2 + (1/7)*t^3 + (1/9)*t^4 + (1/11)*t^5.
61-
// The coefficients below were derived using Sollya:
62-
// > display = hexadecimal;
63-
// > round(1/3, SG, RN);
64-
// > round(1/5, SG, RN);
65-
// > round(1/7, SG, RN);
66-
// > round(1/9, SG, RN);
67-
// > round(1/11, SG, RN);
68-
// This yields:
69-
// 0x1.555556p-2
70-
// 0x1.99999ap-3
71-
// 0x1.24924ap-3
72-
// 0x1.c71c72p-4
73-
// 0x1.745d18p-4f
63+
// Coefficients (from Sollya, RN, hexadecimal):
64+
// 1/3 = 0x1.555556p-2, 1/5 = 0x1.99999ap-3, 1/7 = 0x1.24924ap-3,
65+
// 1/9 = 0x1.c71c72p-4, 1/11 = 0x1.745d18p-4f
7466
// Thus, atanh(x) ≈ x * (1 + P(x^2)).
7567
float xf = x;
7668
float x2 = xf * xf;
7769
float pe = fputil::polyeval(x2, 0.0f, 0x1.555556p-2f, 0x1.99999ap-3f,
7870
0x1.24924ap-3f, 0x1.c71c72p-4f, 0x1.745d18p-4f);
79-
return static_cast<float16>(fputil::multiply_add(xf, pe, xf));
71+
return fputil::cast<float16>(fputil::multiply_add(xf, pe, xf));
8072
}
8173

8274
float xf = x;
83-
return static_cast<float16>(0.5 * log_eval((xf + 1.0) / (xf - 1.0)));
75+
return fputil::cast<float16>(0.5 * log_eval((xf + 1.0) / (xf - 1.0)));
8476
}
8577

8678
} // namespace LIBC_NAMESPACE_DECL

libc/test/src/math/atanhf16_test.cpp

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

99
#include "src/__support/FPUtil/FPBits.h"
10-
#include "src/errno/libc_errno.h"
1110
#include "src/math/atanhf16.h"
1211
#include "test/UnitTest/FPMatcher.h"
1312
#include "test/UnitTest/Test.h"
@@ -17,13 +16,13 @@
1716
using LlvmLibcAtanhf16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
1817
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
1918

20-
// Range for positive numbers: [0, 1)
19+
// Range for positive numbers: [0, +Inf]
2120
static constexpr uint16_t POS_START = 0x0000U;
22-
static constexpr uint16_t POS_STOP = 0x3C00;
21+
static constexpr uint16_t POS_STOP = 0x7C00U;
2322

24-
// Range for negative numbers: (-1, 0]
25-
static constexpr uint16_t NEG_START = 0xBBFFU;
26-
static constexpr uint16_t NEG_STOP = 0x8000U;
23+
// Range for negative numbers: [-Inf, 0]
24+
static constexpr uint16_t NEG_START = 0x8000U;
25+
static constexpr uint16_t NEG_STOP = 0xFC00U;
2726

2827
TEST_F(LlvmLibcAtanhf16Test, PositiveRange) {
2928
for (uint16_t v = POS_START; v < POS_STOP; ++v) {
@@ -34,7 +33,7 @@ TEST_F(LlvmLibcAtanhf16Test, PositiveRange) {
3433
}
3534

3635
TEST_F(LlvmLibcAtanhf16Test, NegativeRange) {
37-
for (uint16_t v = NEG_START; v >= NEG_STOP; --v) {
36+
for (uint16_t v = NEG_START; v <= NEG_STOP; ++v) {
3837
float16 x = FPBits(v).get_val();
3938
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Atanh, x,
4039
LIBC_NAMESPACE::atanhf16(x), 0.5);

libc/test/src/math/smoke/atanhf16_test.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ TEST_F(LlvmLibcAtanhf16Test, SpecialNumbers) {
2222
EXPECT_FP_EQ_ALL_ROUNDING(zero, LIBC_NAMESPACE::atanhf16(zero));
2323
EXPECT_MATH_ERRNO(0);
2424

25-
EXPECT_FP_EQ_ALL_ROUNDING(
26-
-0.0f,
27-
LIBC_NAMESPACE::atanhf16(LIBC_NAMESPACE::fputil::cast<float16>(-0.0f)));
25+
EXPECT_FP_EQ_ALL_ROUNDING(neg_zero, LIBC_NAMESPACE::atanhf16(neg_zero));
2826
EXPECT_MATH_ERRNO(0);
2927

3028
EXPECT_FP_EQ_WITH_EXCEPTION(

0 commit comments

Comments
 (0)