1 /*
2 * Double-precision sinh(x) function.
3 *
4 * Copyright (c) 2022-2024, Arm Limited.
5 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6 */
7
8 #include "math_config.h"
9 #include "test_sig.h"
10 #include "test_defs.h"
11 #include "exp_inline.h"
12
13 #define AbsMask 0x7fffffffffffffff
14 #define Half 0x3fe0000000000000
15 /* 0x1.62e42fefa39fp+9, above which using expm1 results in NaN. */
16 #define OFlowBound 0x40862e42fefa39f0
17
18 /* Approximation for double-precision sinh(x) using expm1.
19 sinh(x) = (exp(x) - exp(-x)) / 2.
20 The greatest observed error is 2.57 ULP:
21 __v_sinh(0x1.9fb1d49d1d58bp-2) got 0x1.ab34e59d678dcp-2
22 want 0x1.ab34e59d678d9p-2. */
23 double
sinh(double x)24 sinh (double x)
25 {
26 uint64_t ix = asuint64 (x);
27 uint64_t iax = ix & AbsMask;
28 double ax = asdouble (iax);
29 uint64_t sign = ix & ~AbsMask;
30 double halfsign = asdouble (Half | sign);
31
32 if (unlikely (iax >= OFlowBound))
33 {
34 /* Special values and overflow. */
35 if (unlikely (iax > 0x7ff0000000000000))
36 return __math_invalidf (x);
37 /* expm1 overflows a little before sinh. We have to fill this
38 gap by using a different algorithm, in this case we use a
39 double-precision exp helper. For large x sinh(x) is dominated
40 by exp(x), however we cannot compute exp without overflow
41 either. We use the identity: exp(a) = (exp(a / 2)) ^ 2
42 to compute sinh(x) ~= (exp(|x| / 2)) ^ 2 / 2 for x > 0
43 ~= (exp(|x| / 2)) ^ 2 / -2 for x < 0. */
44 double e = exp_inline (ax / 2, 0);
45 return (e * halfsign) * e;
46 }
47
48 /* Use expm1f to retain acceptable precision for small numbers.
49 Let t = e^(|x|) - 1. */
50 double t = expm1 (ax);
51 /* Then sinh(x) = (t + t / (t + 1)) / 2 for x > 0
52 (t + t / (t + 1)) / -2 for x < 0. */
53 return (t + t / (t + 1)) * halfsign;
54 }
55
56 TEST_SIG (S, D, 1, sinh, -10.0, 10.0)
57 TEST_ULP (sinh, 2.08)
58 TEST_SYM_INTERVAL (sinh, 0, 0x1p-51, 100)
59 TEST_SYM_INTERVAL (sinh, 0x1p-51, 0x1.62e42fefa39fp+9, 100000)
60 TEST_SYM_INTERVAL (sinh, 0x1.62e42fefa39fp+9, inf, 1000)
61