xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/experimental/cosh_2u.c (revision f3087bef11543b42e0d69b708f367097a4118d24)
1 /*
2  * Double-precision cosh(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 SpecialBound                                                          \
15   0x40861da04cbafe44 /* 0x1.61da04cbafe44p+9, above which exp overflows.  */
16 
17 static double
specialcase(double x,uint64_t iax)18 specialcase (double x, uint64_t iax)
19 {
20   if (iax == 0x7ff0000000000000)
21     return INFINITY;
22   if (iax > 0x7ff0000000000000)
23     return __math_invalid (x);
24   /* exp overflows above SpecialBound. At this magnitude cosh(x) is dominated
25      by exp(x), so we can approximate cosh(x) by (exp(|x|/2)) ^ 2 / 2.  */
26   double t = exp_inline (asdouble (iax) / 2, 0);
27   return (0.5 * t) * t;
28 }
29 
30 /* Approximation for double-precision cosh(x).
31    cosh(x) = (exp(x) + exp(-x)) / 2.
32    The greatest observed error is in the special region, 1.93 ULP:
33    cosh(0x1.628af341989dap+9) got 0x1.fdf28623ef921p+1021
34 			     want 0x1.fdf28623ef923p+1021.
35 
36    The greatest observed error in the non-special region is 1.03 ULP:
37    cosh(0x1.502cd8e56ab3bp+0) got 0x1.fe54962842d0ep+0
38 			     want 0x1.fe54962842d0fp+0.  */
39 double
cosh(double x)40 cosh (double x)
41 {
42   uint64_t ix = asuint64 (x);
43   uint64_t iax = ix & AbsMask;
44 
45   /* exp overflows a little bit before cosh, so use special-case handler for
46      the gap, as well as special values.  */
47   if (unlikely (iax >= SpecialBound))
48     return specialcase (x, iax);
49 
50   double ax = asdouble (iax);
51   /* Use double-precision exp helper to calculate exp(x), then:
52      cosh(x) = exp(|x|) / 2 + 1 / (exp(|x| * 2).  */
53   double t = exp_inline (ax, 0);
54   return 0.5 * t + 0.5 / t;
55 }
56 
57 TEST_SIG (S, D, 1, cosh, -10.0, 10.0)
58 TEST_ULP (cosh, 1.43)
59 TEST_SYM_INTERVAL (cosh, 0, 0x1.61da04cbafe44p+9, 100000)
60 TEST_SYM_INTERVAL (cosh, 0x1.61da04cbafe44p+9, 0x1p10, 1000)
61 TEST_SYM_INTERVAL (cosh, 0x1p10, inf, 100)
62