1/*
2 * ====================================================
3 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
4 *
5 * Developed at SunPro, a Sun Microsystems, Inc. business.
6 * Permission to use, copy, modify, and distribute this
7 * software is freely granted, provided that this notice
8 * is preserved.
9 * ====================================================
10 */
11
12/* Changes for 128-bit __float128 are
13   Copyright (C) 2001 Stephen L. Moshier <moshier@na-net.ornl.gov>
14   and are incorporated herein by permission of the author.  The author
15   reserves the right to distribute this material elsewhere under different
16   copying permissions.  These modifications are distributed here under
17   the following terms:
18
19    This library is free software; you can redistribute it and/or
20    modify it under the terms of the GNU Lesser General Public
21    License as published by the Free Software Foundation; either
22    version 2.1 of the License, or (at your option) any later version.
23
24    This library is distributed in the hope that it will be useful,
25    but WITHOUT ANY WARRANTY; without even the implied warranty of
26    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27    Lesser General Public License for more details.
28
29    You should have received a copy of the GNU Lesser General Public
30    License along with this library; if not, write to the Free Software
31    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA */
32
33/* coshq(x)
34 * Method :
35 * mathematically coshq(x) if defined to be (exp(x)+exp(-x))/2
36 *      1. Replace x by |x| (coshq(x) = coshq(-x)).
37 *      2.
38 *                                                      [ exp(x) - 1 ]^2
39 *          0        <= x <= ln2/2  :  coshq(x) := 1 + -------------------
40 *                                                         2*exp(x)
41 *
42 *                                                 exp(x) +  1/exp(x)
43 *          ln2/2    <= x <= 22     :  coshq(x) := -------------------
44 *                                                         2
45 *          22       <= x <= lnovft :  coshq(x) := expq(x)/2
46 *          lnovft   <= x <= ln2ovft:  coshq(x) := expq(x/2)/2 * expq(x/2)
47 *          ln2ovft  <  x           :  coshq(x) := huge*huge (overflow)
48 *
49 * Special cases:
50 *      coshq(x) is |x| if x is +INF, -INF, or NaN.
51 *      only coshq(0)=1 is exact for finite x.
52 */
53
54#include "quadmath-imp.h"
55
56static const __float128 one = 1.0Q, half = 0.5Q, huge = 1.0e4900Q,
57  ovf_thresh = 1.1357216553474703894801348310092223067821E4Q;
58
59__float128
60coshq (__float128 x)
61{
62  __float128 t, w;
63  int32_t ex;
64  ieee854_float128 u;
65
66  u.value = x;
67  ex = u.words32.w0 & 0x7fffffff;
68
69  /* Absolute value of x.  */
70  u.words32.w0 = ex;
71
72  /* x is INF or NaN */
73  if (ex >= 0x7fff0000)
74    return x * x;
75
76  /* |x| in [0,0.5*ln2], return 1+expm1l(|x|)^2/(2*expq(|x|)) */
77  if (ex < 0x3ffd62e4) /* 0.3465728759765625 */
78    {
79      t = expm1q (u.value);
80      w = one + t;
81      if (ex < 0x3fb80000) /* |x| < 2^-116 */
82	return w;		/* cosh(tiny) = 1 */
83
84      return one + (t * t) / (w + w);
85    }
86
87  /* |x| in [0.5*ln2,40], return (exp(|x|)+1/exp(|x|)/2; */
88  if (ex < 0x40044000)
89    {
90      t = expq (u.value);
91      return half * t + half / t;
92    }
93
94  /* |x| in [22, ln(maxdouble)] return half*exp(|x|) */
95  if (ex <= 0x400c62e3) /* 11356.375 */
96    return half * expq (u.value);
97
98  /* |x| in [log(maxdouble), overflowthresold] */
99  if (u.value <= ovf_thresh)
100    {
101      w = expq (half * u.value);
102      t = half * w;
103      return t * w;
104    }
105
106  /* |x| > overflowthresold, cosh(x) overflow */
107  return huge * huge;
108}
109