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