]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/ieee754/dbl-64/e_cosh.c
Optimize libm
[thirdparty/glibc.git] / sysdeps / ieee754 / dbl-64 / e_cosh.c
CommitLineData
0ac5ae23 1/* Optimized by Ulrich Drepper <drepper@gmail.com>, 2011 */
f7eac6eb
RM
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
0ac5ae23 8 * software is freely granted, provided that this notice
f7eac6eb
RM
9 * is preserved.
10 * ====================================================
11 */
12
13#if defined(LIBM_SCCS) && !defined(lint)
14static char rcsid[] = "$NetBSD: e_cosh.c,v 1.7 1995/05/10 20:44:58 jtc Exp $";
15#endif
16
17/* __ieee754_cosh(x)
0ac5ae23 18 * Method :
f7eac6eb 19 * mathematically cosh(x) if defined to be (exp(x)+exp(-x))/2
0ac5ae23
UD
20 * 1. Replace x by |x| (cosh(x) = cosh(-x)).
21 * 2.
22 * [ exp(x) - 1 ]^2
f7eac6eb 23 * 0 <= x <= ln2/2 : cosh(x) := 1 + -------------------
0ac5ae23 24 * 2*exp(x)
f7eac6eb 25 *
0ac5ae23 26 * exp(x) + 1/exp(x)
f7eac6eb 27 * ln2/2 <= x <= 22 : cosh(x) := -------------------
0ac5ae23
UD
28 * 2
29 * 22 <= x <= lnovft : cosh(x) := exp(x)/2
f7eac6eb
RM
30 * lnovft <= x <= ln2ovft: cosh(x) := exp(x/2)/2 * exp(x/2)
31 * ln2ovft < x : cosh(x) := huge*huge (overflow)
32 *
33 * Special cases:
34 * cosh(x) is |x| if x is +INF, -INF, or NaN.
35 * only cosh(0)=1 is exact for finite x.
36 */
37
38#include "math.h"
39#include "math_private.h"
40
f7eac6eb 41static const double one = 1.0, half=0.5, huge = 1.0e300;
f7eac6eb 42
0ac5ae23
UD
43double
44__ieee754_cosh (double x)
45{
f7eac6eb
RM
46 double t,w;
47 int32_t ix;
48 u_int32_t lx;
49
50 /* High word of |x|. */
51 GET_HIGH_WORD(ix,x);
52 ix &= 0x7fffffff;
53
0ac5ae23 54 /* |x| in [0,22] */
f7eac6eb 55 if (ix < 0x40360000) {
0ac5ae23
UD
56 /* |x| in [0,0.5*ln2], return 1+expm1(|x|)^2/(2*exp(|x|)) */
57 if(ix<0x3fd62e43) {
58 t = __expm1(fabs(x));
59 w = one+t;
60 if (ix<0x3c800000) return w; /* cosh(tiny) = 1 */
61 return one+(t*t)/(w+w);
62 }
63
64 /* |x| in [0.5*ln2,22], return (exp(|x|)+1/exp(|x|)/2; */
f7eac6eb
RM
65 t = __ieee754_exp(fabs(x));
66 return half*t+half/t;
67 }
68
69 /* |x| in [22, log(maxdouble)] return half*exp(|x|) */
6796bc80 70 if (ix < 0x40862e42) return half*__ieee754_exp(fabs(x));
f7eac6eb
RM
71
72 /* |x| in [log(maxdouble), overflowthresold] */
73 GET_LOW_WORD(lx,x);
6796bc80 74 if (ix<0x408633ce || ((ix==0x408633ce)&&(lx<=(u_int32_t)0x8fb9f87d))) {
f7eac6eb
RM
75 w = __ieee754_exp(half*fabs(x));
76 t = half*w;
77 return t*w;
78 }
79
0ac5ae23
UD
80 /* x is INF or NaN */
81 if(ix>=0x7ff00000) return x*x;
82
f7eac6eb
RM
83 /* |x| > overflowthresold, cosh(x) overflow */
84 return huge*huge;
85}
0ac5ae23 86strong_alias (__ieee754_cosh, __cosh_finite)