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) := inf (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 long double coshl(long double x)
45 struct{uint64_t m; uint16_t se; uint16_t pad;} i;
47 unsigned ex = u.i.se & 0x7fff;
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)) {
62 return 1 + t*t/(2*(1+t));
65 /* |x| in [0.5*ln2,22], return (exp(|x|)+1/exp(|x|)/2; */
66 if (ex < 0x3fff+4 || (ex == 0x3fff+4 && mx < 0xb0000000)) {
71 /* |x| in [22, ln(maxdouble)] return 0.5*exp(|x|) */
72 if (ex < 0x3fff+13 || (ex == 0x3fff+13 && mx < 0xb1700000))
75 /* |x| in [log(maxdouble), log(2*maxdouble)) */
76 if (ex == 0x3fff+13 && (mx < 0xb174ddc0 ||
77 (mx == 0xb174ddc0 && lx < 0x31aec0eb))) {
82 /* |x| >= log(2*maxdouble) or nan */