Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/musl/src/math/cosh.c
4397 views
1
#include "libm.h"
2
3
/* cosh(x) = (exp(x) + 1/exp(x))/2
4
* = 1 + 0.5*(exp(x)-1)*(exp(x)-1)/exp(x)
5
* = 1 + x*x/2 + o(x^4)
6
*/
7
double __cdecl cosh(double x)
8
{
9
union {double f; uint64_t i;} u = {.f = x};
10
uint64_t sign = u.i & 0x8000000000000000ULL;
11
uint32_t w;
12
double t;
13
14
/* |x| */
15
u.i &= (uint64_t)-1/2;
16
x = u.f;
17
w = u.i >> 32;
18
19
/* |x| < log(2) */
20
if (w < 0x3fe62e42) {
21
if (w < 0x3ff00000 - (26<<20)) {
22
/* raise inexact if x!=0 */
23
FORCE_EVAL(x + 0x1p120f);
24
return 1;
25
}
26
t = expm1(x);
27
return 1 + t*t/(2*(1+t));
28
}
29
30
/* |x| < log(DBL_MAX) */
31
if (w < 0x40862e42) {
32
t = exp(x);
33
/* note: if x>log(0x1p26) then the 1/t is not needed */
34
return 0.5*(t + 1/t);
35
}
36
37
/* |x| > log(DBL_MAX) or nan */
38
/* note: the result is stored to handle overflow */
39
if (w > 0x7ff00000) {
40
u.i |= sign | 0x0008000000000000ULL;
41
return u.f;
42
}
43
t = __expo2(x, 1.0);
44
return t;
45
}
46
47