1 /* origin: OpenBSD /usr/src/lib/libm/src/ld80/e_coshl.c */
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
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
10 * ====================================================
14 * mathematically coshl(x) if defined to be (exp(x)+exp(-x))/2
15 * 1. Replace x by |x| (coshl(x) = coshl(-x)).
18 * 0 <= x <= ln2/2 : coshl(x) := 1 + -------------------
22 * ln2/2 <= x <= 22 : coshl(x) := -------------------
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) := huge*huge (overflow)
29 * coshl(x) is |x| if x is +INF, -INF, or NaN.
30 * only coshl(0)=1 is exact for finite x.
35 #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
36 long double coshl(long double x)
40 #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384
41 static const long double huge = 1.0e4900L;
43 long double coshl(long double x)
49 /* High word of |x|. */
50 GET_LDOUBLE_WORDS(ex, mx, lx, x);
54 if (ex == 0x7fff) return x*x;
56 /* |x| in [0,0.5*ln2], return 1+expm1l(|x|)^2/(2*expl(|x|)) */
57 if (ex < 0x3ffd || (ex == 0x3ffd && mx < 0xb17217f7u)) {
60 if (ex < 0x3fbc) return w; /* cosh(tiny) = 1 */
61 return 1.0+(t*t)/(w+w);
64 /* |x| in [0.5*ln2,22], return (exp(|x|)+1/exp(|x|)/2; */
65 if (ex < 0x4003 || (ex == 0x4003 && mx < 0xb0000000u)) {
70 /* |x| in [22, ln(maxdouble)] return 0.5*exp(|x|) */
71 if (ex < 0x400c || (ex == 0x400c && mx < 0xb1700000u))
72 return 0.5*expl(fabsl(x));
74 /* |x| in [log(maxdouble), log(2*maxdouble)) */
75 if (ex == 0x400c && (mx < 0xb174ddc0u ||
76 (mx == 0xb174ddc0u && lx < 0x31aec0ebu)))
78 w = expl(0.5*fabsl(x));
83 /* |x| >= log(2*maxdouble), cosh(x) overflow */