3ea56e00bfd5f8be949787119fcef32b90a2f20b
[musl] / src / math / coshl.c
1 /* origin: OpenBSD /usr/src/lib/libm/src/ld80/e_coshl.c */
2 /*
3  * ====================================================
4  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5  *
6  * Developed at SunPro, a Sun Microsystems, Inc. business.
7  * Permission to use, copy, modify, and distribute this
8  * software is freely granted, provided that this notice
9  * is preserved.
10  * ====================================================
11  */
12 /* coshl(x)
13  * Method :
14  * mathematically coshl(x) if defined to be (exp(x)+exp(-x))/2
15  *      1. Replace x by |x| (coshl(x) = coshl(-x)).
16  *      2.
17  *                                                      [ exp(x) - 1 ]^2
18  *          0        <= x <= ln2/2  :  coshl(x) := 1 + -------------------
19  *                                                         2*exp(x)
20  *
21  *                                                 exp(x) +  1/exp(x)
22  *          ln2/2    <= x <= 22     :  coshl(x) := -------------------
23  *                                                         2
24  *          22       <= x <= lnovft :  coshl(x) := expl(x)/2
25  *          lnovft   <= x <= ln2ovft:  coshl(x) := expl(x/2)/2 * expl(x/2)
26  *          ln2ovft  <  x           :  coshl(x) := inf (overflow)
27  *
28  * Special cases:
29  *      coshl(x) is |x| if x is +INF, -INF, or NaN.
30  *      only coshl(0)=1 is exact for finite x.
31  */
32
33 #include "libm.h"
34
35 #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
36 long double coshl(long double x)
37 {
38         return cosh(x);
39 }
40 #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384
41 long double coshl(long double x)
42 {
43         union {
44                 long double f;
45                 struct{uint64_t m; uint16_t se; uint16_t pad;} i;
46         } u = {.f = x};
47         unsigned ex = u.i.se & 0x7fff;
48         long double t;
49         uint32_t mx,lx;
50
51         /* |x| */
52         u.i.se = ex;
53         x = u.f;
54         mx = u.i.m >> 32;
55         lx = u.i.m;
56
57         /* |x| in [0,0.5*ln2], return 1+expm1l(|x|)^2/(2*expl(|x|)) */
58         if (ex < 0x3fff-2 || (ex == 0x3fff-2 && mx < 0xb17217f7)) {
59                 t = expm1l(x);
60                 if (ex < 0x3fff-64)
61                         return 1;
62                 return 1 + t*t/(2*(1+t));
63         }
64
65         /* |x| in [0.5*ln2,22], return (exp(|x|)+1/exp(|x|)/2; */
66         if (ex < 0x3fff+4 || (ex == 0x3fff+4 && mx < 0xb0000000)) {
67                 t = expl(x);
68                 return 0.5*t + 0.5/t;
69         }
70
71         /* |x| in [22, ln(maxdouble)] return 0.5*exp(|x|) */
72         if (ex < 0x3fff+13 || (ex == 0x3fff+13 && mx < 0xb1700000))
73                 return 0.5*expl(x);
74
75         /* |x| in [log(maxdouble), log(2*maxdouble)) */
76         if (ex == 0x3fff+13 && (mx < 0xb174ddc0 ||
77              (mx == 0xb174ddc0 && lx < 0x31aec0eb))) {
78                 t = expl(0.5*x);
79                 return 0.5*t*t;
80         }
81
82         /* |x| >= log(2*maxdouble) or nan */
83         return x*0x1p16383L;
84 }
85 #endif