Skip to content
This repository was archived by the owner on Jan 22, 2024. It is now read-only.

Commit 8677b96

Browse files
lntueskatrak
authored andcommitted
[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 87b5637 commit 8677b96

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

0 commit comments

Comments
 (0)