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