xref: /aosp_15_r20/external/arm-optimized-routines/pl/math/acosh_3u.c (revision 412f47f9e737e10ed5cc46ec6a8d7fa2264f8a14)
1*412f47f9SXin Li /*
2*412f47f9SXin Li  * Double-precision acosh(x) function.
3*412f47f9SXin Li  *
4*412f47f9SXin Li  * Copyright (c) 2022-2023, Arm Limited.
5*412f47f9SXin Li  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*412f47f9SXin Li  */
7*412f47f9SXin Li 
8*412f47f9SXin Li #include "math_config.h"
9*412f47f9SXin Li #include "pl_sig.h"
10*412f47f9SXin Li #include "pl_test.h"
11*412f47f9SXin Li 
12*412f47f9SXin Li #define Ln2 (0x1.62e42fefa39efp-1)
13*412f47f9SXin Li #define MinusZero (0x8000000000000000)
14*412f47f9SXin Li #define SquareLim (0x5fe0000000000000) /* asuint64(0x1.0p511).  */
15*412f47f9SXin Li #define Two (0x4000000000000000)       /* asuint64(2.0).  */
16*412f47f9SXin Li 
17*412f47f9SXin Li double
18*412f47f9SXin Li optr_aor_log_f64 (double);
19*412f47f9SXin Li 
20*412f47f9SXin Li double
21*412f47f9SXin Li log1p (double);
22*412f47f9SXin Li 
23*412f47f9SXin Li /* acosh approximation using a variety of approaches on different intervals:
24*412f47f9SXin Li 
25*412f47f9SXin Li    acosh(x) = ln(x + sqrt(x * x - 1)).
26*412f47f9SXin Li 
27*412f47f9SXin Li    x >= 2^511: We cannot square x without overflow. For huge x, sqrt(x*x - 1) is
28*412f47f9SXin Li    close enough to x that we can calculate the result by ln(2x) == ln(x) +
29*412f47f9SXin Li    ln(2). The greatest observed error in this region is 0.98 ULP:
30*412f47f9SXin Li    acosh(0x1.1b9bf42923d1dp+853) got 0x1.28066a11a7c7fp+9
31*412f47f9SXin Li 				want 0x1.28066a11a7c8p+9.
32*412f47f9SXin Li 
33*412f47f9SXin Li    x > 2: Calculate the result directly using definition of acosh(x). Greatest
34*412f47f9SXin Li    observed error in this region is 1.33 ULP:
35*412f47f9SXin Li    acosh(0x1.1e45d14bfcfa2p+1) got 0x1.71a06f50c34b5p+0
36*412f47f9SXin Li 			      want 0x1.71a06f50c34b6p+0.
37*412f47f9SXin Li 
38*412f47f9SXin Li    0 <= x <= 2: Calculate the result using log1p. For x < 1, acosh(x) is
39*412f47f9SXin Li    undefined. For 1 <= x <= 2, the largest observed error is 2.69 ULP:
40*412f47f9SXin Li    acosh(0x1.073528248093p+0) got 0x1.e4d9bd20684f3p-3
41*412f47f9SXin Li 			     want 0x1.e4d9bd20684f6p-3.  */
42*412f47f9SXin Li double
acosh(double x)43*412f47f9SXin Li acosh (double x)
44*412f47f9SXin Li {
45*412f47f9SXin Li   uint64_t ix = asuint64 (x);
46*412f47f9SXin Li 
47*412f47f9SXin Li   if (unlikely (ix >= MinusZero))
48*412f47f9SXin Li     return __math_invalid (x);
49*412f47f9SXin Li 
50*412f47f9SXin Li   if (unlikely (ix >= SquareLim))
51*412f47f9SXin Li     return optr_aor_log_f64 (x) + Ln2;
52*412f47f9SXin Li 
53*412f47f9SXin Li   if (ix >= Two)
54*412f47f9SXin Li     return optr_aor_log_f64 (x + sqrt (x * x - 1));
55*412f47f9SXin Li 
56*412f47f9SXin Li   double xm1 = x - 1;
57*412f47f9SXin Li   return log1p (xm1 + sqrt (2 * xm1 + xm1 * xm1));
58*412f47f9SXin Li }
59*412f47f9SXin Li 
60*412f47f9SXin Li PL_SIG (S, D, 1, acosh, 1.0, 10.0)
61*412f47f9SXin Li PL_TEST_ULP (acosh, 2.19)
62*412f47f9SXin Li PL_TEST_INTERVAL (acosh, 0, 1, 10000)
63*412f47f9SXin Li PL_TEST_INTERVAL (acosh, 1, 2, 100000)
64*412f47f9SXin Li PL_TEST_INTERVAL (acosh, 2, 0x1p511, 100000)
65*412f47f9SXin Li PL_TEST_INTERVAL (acosh, 0x1p511, inf, 100000)
66*412f47f9SXin Li PL_TEST_INTERVAL (acosh, -0, -inf, 10000)
67