Skip to content

Commit 9b30f6b

Browse files
committed
[libc][math] Implement acoshf function correctly rounded to all rounding modes.
Implement acoshf function correctly rounded to all rounding modes. Reviewed By: zimmermann6 Differential Revision: https://reviews.llvm.org/D142781
1 parent 47fbb24 commit 9b30f6b

File tree

16 files changed

+292
-1
lines changed

16 files changed

+292
-1
lines changed

libc/config/darwin/arm/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ set(TARGET_LIBM_ENTRYPOINTS
110110

111111
# math.h entrypoints
112112
libc.src.math.acosf
113+
libc.src.math.acoshf
113114
libc.src.math.asinf
114115
libc.src.math.asinhf
115116
libc.src.math.atanf

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ set(TARGET_LIBM_ENTRYPOINTS
210210

211211
# math.h entrypoints
212212
libc.src.math.acosf
213+
libc.src.math.acoshf
213214
libc.src.math.asin
214215
libc.src.math.asinf
215216
libc.src.math.asinhf

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ set(TARGET_LIBM_ENTRYPOINTS
211211

212212
# math.h entrypoints
213213
libc.src.math.acosf
214+
libc.src.math.acoshf
214215
libc.src.math.asin
215216
libc.src.math.asinf
216217
libc.src.math.asinhf

libc/config/windows/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ set(TARGET_LIBM_ENTRYPOINTS
111111

112112
# math.h entrypoints
113113
libc.src.math.acosf
114+
libc.src.math.acoshf
114115
libc.src.math.asin
115116
libc.src.math.asinf
116117
libc.src.math.asinhf

libc/docs/math.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ Higher Math Functions
122122
<Func> <Func_f> (float) <Func> (double) <Func_l> (long double)
123123
============== ================ =============== ======================
124124
acos :green:`XA`
125-
acosh
125+
acosh :green:`XA`
126126
asin :green:`XA`
127127
asinh :green:`XA`
128128
atan :green:`XA`
@@ -161,6 +161,7 @@ Accuracy of Higher Math Functions
161161
<Func> <Func_f> (float) <Func> (double) <Func_l> (long double)
162162
============== ================ =============== ======================
163163
acos :green:`XA`
164+
acosh :green:`XA`
164165
asin :green:`XA`
165166
asinh :green:`XA`
166167
atan :green:`XA`
@@ -216,6 +217,8 @@ Performance
216217
+==============+===========+===================+===========+===================+=====================================+============+=========================+==============+===============+
217218
| acosf | 24 | 29 | 62 | 77 | :math:`[-1, 1]` | Ryzen 1700 | Ubuntu 22.04 LTS x86_64 | Clang 14.0.0 | FMA |
218219
+--------------+-----------+-------------------+-----------+-------------------+-------------------------------------+------------+-------------------------+--------------+---------------+
220+
| acoshf | 18 | 26 | 73 | 74 | :math:`[1, 21]` | Ryzen 1700 | Ubuntu 22.04 LTS x86_64 | Clang 14.0.0 | FMA |
221+
+--------------+-----------+-------------------+-----------+-------------------+-------------------------------------+------------+-------------------------+--------------+---------------+
219222
| asinf | 23 | 27 | 62 | 62 | :math:`[-1, 1]` | Ryzen 1700 | Ubuntu 22.04 LTS x86_64 | Clang 14.0.0 | FMA |
220223
+--------------+-----------+-------------------+-----------+-------------------+-------------------------------------+------------+-------------------------+--------------+---------------+
221224
| asinhf | 21 | 39 | 77 | 91 | :math:`[-10, 10]` | Ryzen 1700 | Ubuntu 22.04 LTS x86_64 | Clang 14.0.0 | FMA |

libc/spec/stdc.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ def StdC : StandardSpec<"stdc"> {
492492
FunctionSpec<"asin", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
493493
FunctionSpec<"atanf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
494494

495+
FunctionSpec<"acoshf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
495496
FunctionSpec<"asinhf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
496497
FunctionSpec<"atanhf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
497498
]

libc/src/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ add_entrypoint_object(
6565
)
6666

6767
add_math_entrypoint_object(acosf)
68+
add_math_entrypoint_object(acoshf)
6869

6970
add_math_entrypoint_object(asin)
7071
add_math_entrypoint_object(asinf)

libc/src/math/acoshf.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for acoshf ------------------------*- 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_MATH_ACOSHF_H
10+
#define LLVM_LIBC_SRC_MATH_ACOSHF_H
11+
12+
namespace __llvm_libc {
13+
14+
float acoshf(float x);
15+
16+
} // namespace __llvm_libc
17+
18+
#endif // LLVM_LIBC_SRC_MATH_ACOSHF_H

libc/src/math/generic/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,23 @@ add_entrypoint_object(
12961296
-O3
12971297
)
12981298

1299+
add_entrypoint_object(
1300+
acoshf
1301+
SRCS
1302+
acoshf.cpp
1303+
HDRS
1304+
../acoshf.h
1305+
DEPENDS
1306+
.explogxf
1307+
libc.src.__support.FPUtil.fenv_impl
1308+
libc.src.__support.FPUtil.fp_bits
1309+
libc.src.__support.FPUtil.multiply_add
1310+
libc.src.__support.FPUtil.polyeval
1311+
libc.src.__support.FPUtil.sqrt
1312+
COMPILE_OPTIONS
1313+
-O3
1314+
)
1315+
12991316
add_entrypoint_object(
13001317
asinhf
13011318
SRCS

libc/src/math/generic/acoshf.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//===-- Single-precision acosh function -----------------------------------===//
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+
#include "src/math/acoshf.h"
10+
#include "src/__support/FPUtil/FEnvImpl.h"
11+
#include "src/__support/FPUtil/FPBits.h"
12+
#include "src/__support/FPUtil/PolyEval.h"
13+
#include "src/__support/FPUtil/multiply_add.h"
14+
#include "src/__support/FPUtil/sqrt.h"
15+
#include "src/math/generic/common_constants.h"
16+
#include "src/math/generic/explogxf.h"
17+
18+
namespace __llvm_libc {
19+
20+
LLVM_LIBC_FUNCTION(float, acoshf, (float x)) {
21+
using FPBits_t = typename fputil::FPBits<float>;
22+
FPBits_t xbits(x);
23+
uint32_t x_u = xbits.uintval();
24+
25+
if (unlikely(x < 1.0f)) {
26+
// x < 1.
27+
fputil::set_except(FE_INVALID);
28+
return FPBits_t::build_quiet_nan(0);
29+
}
30+
31+
if (unlikely(x_u >= 0x4f8ffb03)) {
32+
// Check for exceptional values.
33+
uint32_t x_abs = x_u & FPBits_t::FloatProp::EXP_MANT_MASK;
34+
if (unlikely(x_abs >= 0x7f80'0000U)) {
35+
// x is +inf or NaN.
36+
return x;
37+
}
38+
39+
// Helper functions to set results for exceptional cases.
40+
auto round_result_slightly_down = [](float r) -> float {
41+
volatile float tmp = r;
42+
tmp = tmp - 0x1.0p-25f;
43+
return tmp;
44+
};
45+
auto round_result_slightly_up = [](float r) -> float {
46+
volatile float tmp = r;
47+
tmp = tmp + 0x1.0p-25f;
48+
return tmp;
49+
};
50+
51+
switch (x_u) {
52+
case 0x4f8ffb03: // x = 0x1.1ff606p32f
53+
return round_result_slightly_up(0x1.6fdd34p4f);
54+
case 0x5c569e88: // x = 0x1.ad3d1p57f
55+
return round_result_slightly_up(0x1.45c146p5f);
56+
case 0x5e68984e: // x = 0x1.d1309cp61f
57+
return round_result_slightly_up(0x1.5c9442p5f);
58+
case 0x655890d3: // x = 0x1.b121a6p75f
59+
return round_result_slightly_down(0x1.a9a3f2p5f);
60+
case 0x6eb1a8ec: // x = 0x1.6351d8p94f
61+
return round_result_slightly_down(0x1.08b512p6f);
62+
case 0x7997f30a: // x = 0x1.2fe614p116f
63+
return round_result_slightly_up(0x1.451436p6f);
64+
}
65+
}
66+
67+
double x_d = static_cast<double>(x);
68+
// acosh(x) = log(x + sqrt(x^2 - 1))
69+
return log_eval(x_d + fputil::sqrt(fputil::multiply_add(x_d, x_d, -1.0)));
70+
}
71+
72+
} // namespace __llvm_libc

libc/test/src/math/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,6 +1485,20 @@ add_fp_unittest(
14851485
libc.src.__support.FPUtil.fp_bits
14861486
)
14871487

1488+
add_fp_unittest(
1489+
acoshf_test
1490+
NEED_MPFR
1491+
SUITE
1492+
libc_math_unittests
1493+
SRCS
1494+
acoshf_test.cpp
1495+
DEPENDS
1496+
libc.include.errno
1497+
libc.src.errno.errno
1498+
libc.src.math.acoshf
1499+
libc.src.__support.FPUtil.fp_bits
1500+
)
1501+
14881502
add_fp_unittest(
14891503
asinf_test
14901504
NEED_MPFR

libc/test/src/math/acoshf_test.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//===-- Unittests for acoshf ----------------------------------------------===//
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+
#include "src/__support/FPUtil/FPBits.h"
10+
#include "src/math/acoshf.h"
11+
#include "utils/MPFRWrapper/MPFRUtils.h"
12+
#include "utils/UnitTest/FPMatcher.h"
13+
#include "utils/UnitTest/Test.h"
14+
#include <math.h>
15+
16+
#include <errno.h>
17+
#include <stdint.h>
18+
19+
using FPBits_t = __llvm_libc::fputil::FPBits<float>;
20+
21+
namespace mpfr = __llvm_libc::testing::mpfr;
22+
23+
DECLARE_SPECIAL_CONSTANTS(float)
24+
25+
TEST(LlvmLibcAcoshfTest, SpecialNumbers) {
26+
errno = 0;
27+
28+
EXPECT_FP_EQ(aNaN, __llvm_libc::acoshf(aNaN));
29+
EXPECT_MATH_ERRNO(0);
30+
31+
EXPECT_FP_EQ(aNaN, __llvm_libc::acoshf(0.0f));
32+
EXPECT_MATH_ERRNO(0);
33+
34+
EXPECT_FP_EQ(0.0f, __llvm_libc::acoshf(1.0f));
35+
EXPECT_MATH_ERRNO(0);
36+
37+
EXPECT_FP_EQ(inf, __llvm_libc::acoshf(inf));
38+
EXPECT_MATH_ERRNO(0);
39+
40+
EXPECT_FP_EQ(aNaN, __llvm_libc::acoshf(neg_inf));
41+
EXPECT_MATH_ERRNO(0);
42+
}
43+
44+
TEST(LlvmLibcAcoshfTest, InFloatRange) {
45+
constexpr uint32_t COUNT = 1000000;
46+
constexpr uint32_t STEP = UINT32_MAX / COUNT;
47+
for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
48+
float x = float(FPBits_t(v));
49+
if (isnan(x) || isinf(x))
50+
continue;
51+
ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Acosh, x,
52+
__llvm_libc::acoshf(x), 0.5);
53+
}
54+
}
55+
56+
TEST(LlvmLibcAcoshfTest, SpecificBitPatterns) {
57+
constexpr int N = 12;
58+
constexpr uint32_t INPUTS[N] = {
59+
0x3f800000, // x = 1.0f
60+
0x45abaf26, // x = 0x1.575e4cp12f
61+
0x49d29048, // x = 0x1.a5209p20f
62+
0x4bdd65a5, // x = 0x1.bacb4ap24f
63+
0x4c803f2c, // x = 0x1.007e58p26f
64+
0x4f8ffb03, // x = 0x1.1ff606p32f
65+
0x5c569e88, // x = 0x1.ad3d1p57f
66+
0x5e68984e, // x = 0x1.d1309cp61f
67+
0x655890d3, // x = 0x1.b121a6p75f
68+
0x65de7ca6, // x = 0x1.bcf94cp76f
69+
0x6eb1a8ec, // x = 0x1.6351d8p94f
70+
0x7997f30a, // x = 0x1.2fe614p116f
71+
};
72+
73+
for (int i = 0; i < N; ++i) {
74+
float x = float(FPBits_t(INPUTS[i]));
75+
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Acosh, x,
76+
__llvm_libc::acoshf(x), 0.5);
77+
}
78+
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,23 @@ add_fp_unittest(
308308
-lpthread
309309
)
310310

311+
add_fp_unittest(
312+
acoshf_test
313+
NO_RUN_POSTBUILD
314+
NEED_MPFR
315+
SUITE
316+
libc_math_exhaustive_tests
317+
SRCS
318+
acoshf_test.cpp
319+
DEPENDS
320+
.exhaustive_test
321+
libc.include.math
322+
libc.src.math.acoshf
323+
libc.src.__support.FPUtil.fp_bits
324+
LINK_LIBRARIES
325+
-lpthread
326+
)
327+
311328
add_fp_unittest(
312329
asinhf_test
313330
NO_RUN_POSTBUILD
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//===-- Exhaustive test for acoshf ----------------------------------------===//
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+
#include "exhaustive_test.h"
10+
#include "src/__support/FPUtil/FPBits.h"
11+
#include "src/math/acoshf.h"
12+
#include "utils/MPFRWrapper/MPFRUtils.h"
13+
14+
#include <thread>
15+
16+
using FPBits = __llvm_libc::fputil::FPBits<float>;
17+
18+
namespace mpfr = __llvm_libc::testing::mpfr;
19+
20+
struct LlvmLibcAcoshfExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
21+
bool check(uint32_t start, uint32_t stop,
22+
mpfr::RoundingMode rounding) override {
23+
mpfr::ForceRoundingMode r(rounding);
24+
uint32_t bits = start;
25+
bool result = true;
26+
do {
27+
FPBits xbits(bits);
28+
float x = float(xbits);
29+
result &= EXPECT_MPFR_MATCH(mpfr::Operation::Acosh, x,
30+
__llvm_libc::acoshf(x), 0.5, rounding);
31+
} while (bits++ < stop);
32+
return result;
33+
}
34+
};
35+
36+
static const int NUM_THREADS = std::thread::hardware_concurrency();
37+
38+
// Range: [1, Inf];
39+
static const uint32_t POS_START = 0x3f80'0000U;
40+
static const uint32_t POS_STOP = 0x7f80'0000U;
41+
42+
TEST_F(LlvmLibcAcoshfExhaustiveTest, PostiveRangeRoundNearestTieToEven) {
43+
test_full_range(POS_START, POS_STOP, mpfr::RoundingMode::Nearest);
44+
}
45+
46+
TEST_F(LlvmLibcAcoshfExhaustiveTest, PostiveRangeRoundUp) {
47+
test_full_range(POS_START, POS_STOP, mpfr::RoundingMode::Upward);
48+
}
49+
50+
TEST_F(LlvmLibcAcoshfExhaustiveTest, PostiveRangeRoundDown) {
51+
test_full_range(POS_START, POS_STOP, mpfr::RoundingMode::Downward);
52+
}
53+
54+
TEST_F(LlvmLibcAcoshfExhaustiveTest, PostiveRangeRoundTowardZero) {
55+
test_full_range(POS_START, POS_STOP, mpfr::RoundingMode::TowardZero);
56+
}

0 commit comments

Comments
 (0)