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