Skip to content

Commit 4ad0f21

Browse files
committed
Suggested changes
1 parent c27cf2b commit 4ad0f21

File tree

6 files changed

+53
-37
lines changed

6 files changed

+53
-37
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,6 @@ set(TARGET_LIBM_ENTRYPOINTS
411411
libc.src.math.acoshf
412412
libc.src.math.asinf
413413
libc.src.math.asinhf
414-
libc.src.math.asinhf16
415414
libc.src.math.atan2
416415
libc.src.math.atan2f
417416
libc.src.math.atanf
@@ -653,6 +652,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
653652
list(APPEND TARGET_LIBM_ENTRYPOINTS
654653
# math.h C23 _Float16 entrypoints
655654
libc.src.math.asinf16
655+
libc.src.math.asinhf16
656656
libc.src.math.acosf16
657657
libc.src.math.canonicalizef16
658658
libc.src.math.ceilf16

libc/src/math/asinhf16.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===-- Implementation header for asinhf16 -----------------------*- C++-*-===//
1+
//===-- Implementation header for asinhf16 ----------------------*- C++ -*-===//
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.

libc/src/math/generic/asinhf16.cpp

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,50 @@
1-
//===-- Half-precision asinhf16(x) function--------------------------------===//
1+
//===-- Half-precision asinh(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.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception.
66
//
7-
//
87
//===----------------------------------------------------------------------===//
98

109
#include "src/math/asinhf16.h"
10+
#include "explogxf.h"
11+
#include "hdr/fenv_macros.h"
12+
#include "src/__support/FPUtil/cast.h"
1113
#include "src/__support/FPUtil/except_value_utils.h"
12-
#include "src/__support/FPUtil/generic/sqrt.h"
1314
#include "src/__support/FPUtil/multiply_add.h"
15+
#include "src/__support/FPUtil/sqrt.h"
1416
#include "src/__support/common.h"
1517
#include "src/__support/macros/config.h"
16-
#include "src/__support/macros/properties/types.h"
17-
#include "src/math/generic/explogxf.h"
18+
#include "src/__support/macros/optimization.h"
1819

1920
namespace LIBC_NAMESPACE_DECL {
2021

21-
#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
2222
static constexpr size_t N_EXCEPTS = 8;
2323

2424
static constexpr fputil::ExceptValues<float16, N_EXCEPTS> ASINHF16_EXCEPTS{
2525
{// (input, RZ output, RU offset, RD offset, RN offset)
26-
{0x3769, 0x372A, 1, 0, 1},
27-
{0x3B5B, 0x3A96, 1, 0, 0},
28-
{0x4B1F, 0x42B3, 1, 0, 0},
29-
{0x4C9B, 0x4336, 1, 0, 1},
30-
{0xB769, 0xB72A, 0, 1, 1},
31-
{0xBB5B, 0xBA96, 0, 1, 0},
32-
{0xCB1F, 0xC2B3, 0, 1, 0},
33-
{0xCC9B, 0xC336, 0, 1, 1}}};
34-
#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
26+
27+
// x = 0x1.da4p-2, asinhf16(x) = 0x1.ca8p-2 (RZ)
28+
{0x3769, 0x372a, 1, 0, 1},
29+
// x = 0x1.d6cp-1, asinhf16(x) = 0x1.a58p-1 (RZ)
30+
{0x3b5b, 0x3a96, 1, 0, 0},
31+
// x = 0x1.c7cp+3, asinhf16(x) = 0x1.accp+1 (RZ)
32+
{0x4b1f, 0x42b3, 1, 0, 0},
33+
// x = 0x1.26cp+4, asinhf16(x) = 0x1.cd8p+1 (RZ)
34+
{0x4c9b, 0x4336, 1, 0, 1},
35+
// x = -0x1.da4p-2, asinhf16(x) = -0x1.ca8p-2 (RZ)
36+
{0xb769, 0xb72a, 0, 1, 1},
37+
// x = -0x1.d6cp-1, asinhf16(x) = -0x1.a58p-1 (RZ)
38+
{0xbb5b, 0xba96, 0, 1, 0},
39+
// x = -0x1.c7cp+3, asinhf16(x) = -0x1.accp+1 (RZ)
40+
{0xcb1f, 0xc2b3, 0, 1, 0},
41+
// x = -0x1.26cp+4, asinhf16(x) = -0x1.cd8p+1 (RZ)
42+
{0xcc9b, 0xc336, 0, 1, 1}}};
3543

3644
LLVM_LIBC_FUNCTION(float16, asinhf16, (float16 x)) {
3745
using FPBits = fputil::FPBits<float16>;
3846
FPBits xbits(x);
3947

40-
float x_d = x;
4148
uint16_t x_u = xbits.uintval();
4249
uint16_t x_abs = x_u & 0x7fff;
4350

@@ -50,35 +57,45 @@ LLVM_LIBC_FUNCTION(float16, asinhf16, (float16 x)) {
5057
return x;
5158
}
5259

53-
#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
5460
// Handle exceptional values
5561
if (auto r = ASINHF16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
5662
return r.value();
57-
#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
5863

64+
float xf = x;
5965
const float SIGN[2] = {1.0f, -1.0f};
6066
float x_sign = SIGN[x_u >> 15];
6167

6268
// |x| <= 0.25
6369
if (LIBC_UNLIKELY(x_abs <= 0x3400)) {
64-
if (LIBC_UNLIKELY(x_abs == 0))
65-
return x;
66-
if (LIBC_UNLIKELY((fputil::get_round() == FE_UPWARD) && (x_u >= 0x8401) &&
67-
(x_u <= 0x90E6)))
68-
return static_cast<float16>(x_d + 0x1p-24f);
6970

70-
float x_sq = x_d * x_d;
71+
// when |x| < 0x1.718p-5, asinhf16(x) = x. Adjust by 1 ULP for certain
72+
// rounding types.
73+
if (LIBC_UNLIKELY(x_abs < 0x29c6)) {
74+
if (((fputil::get_round() == FE_UPWARD) ||
75+
(fputil::get_round() == FE_TOWARDZERO)) &&
76+
xf < 0)
77+
return fputil::cast<float16>(xf + 0x1p-24f);
78+
if (((fputil::get_round() == FE_DOWNWARD) ||
79+
(fputil::get_round() == FE_TOWARDZERO)) &&
80+
xf > 0)
81+
return fputil::cast<float16>(xf - 0x1p-24f);
82+
return fputil::cast<float16>(xf);
83+
}
84+
85+
float x_sq = xf * xf;
7186
// Generated by Sollya with:
72-
// > P = fpminimax(asinh(x)/x, [|0, 2, 4, 6, 8|], [|SG...|],[0, 2^-2]);
73-
float p = fputil::polyeval(x_sq, 1.0f, -0x1.555556p-3f, 0x1.3334dep-4f,
74-
-0x1.6f3e2p-5f, 0x1.51d012p-5f);
87+
// > P = fpminimax(asinh(x)/x, [|0, 2, 4, 6, 8|], [|SG...|], [0, 2^-2]);
88+
// The last coefficient 0x1.bd114ep-6f has been changed to 0x1.bd114ep-5f
89+
// for better accuracy.
90+
float p = fputil::polyeval(x_sq, 1.0f, -0x1.555552p-3f, 0x1.332f6ap-4f,
91+
-0x1.6c53dep-5f, 0x1.bd114ep-5f);
7592

76-
return static_cast<float16>(fputil::multiply_add(x_d, p, 0.0f));
93+
return fputil::cast<float16>(xf * p);
7794
}
7895

7996
// General case: asinh(x) = ln(x + sqrt(x^2 + 1))
80-
float sqrt_term = fputil::sqrt<float>(fputil::multiply_add(x_d, x_d, 1.0f));
97+
float sqrt_term = fputil::sqrt<float>(fputil::multiply_add(xf, xf, 1.0f));
8198
return fputil::cast<float16>(
82-
x_sign * log_eval(fputil::multiply_add(x_d, x_sign, sqrt_term)));
99+
x_sign * log_eval(fputil::multiply_add(xf, x_sign, sqrt_term)));
83100
}
84101
} // namespace LIBC_NAMESPACE_DECL

libc/test/src/math/asinhf16_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===-- Exhaustive test for asinhf16---------------------------------------===//
1+
//===-- Exhaustive test for asinhf16 --------------------------------------===//
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.

libc/test/src/math/smoke/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3942,7 +3942,6 @@ add_fp_unittest(
39423942
DEPENDS
39433943
libc.src.errno.errno
39443944
libc.src.math.asinhf16
3945-
libc.src.__support.FPUtil.fp_bits
39463945
)
39473946

39483947
add_fp_unittest(

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===-- Unittests for asinhf16---------------------------------------------===//
1+
//===-- Unittests for asinhf16 --------------------------------------------===//
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.
@@ -21,10 +21,10 @@ TEST_F(LlvmLibcAsinhf16Test, SpecialNumbers) {
2121
EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::asinhf16(sNaN), FE_INVALID);
2222
EXPECT_MATH_ERRNO(0);
2323

24-
EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::asinhf16(0.0f));
24+
EXPECT_FP_EQ(zero, LIBC_NAMESPACE::asinhf16(zero));
2525
EXPECT_MATH_ERRNO(0);
2626

27-
EXPECT_FP_EQ(-0.0f, LIBC_NAMESPACE::asinhf16(-0.0f));
27+
EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::asinhf16(neg_zero));
2828
EXPECT_MATH_ERRNO(0);
2929

3030
EXPECT_FP_EQ(inf, LIBC_NAMESPACE::asinhf16(inf));

0 commit comments

Comments
 (0)