Upgrade MPFR from 2.4.1 to 2.4.2-p3 on the vendor branch.
[dragonfly.git] / contrib / mpfr / cosh.c
1 /* mpfr_cosh -- hyperbolic cosine
2
3 Copyright 2001, 2002, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by the Arenaire and Cacao projects, INRIA.
5
6 This file is part of the GNU MPFR Library.
7
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or (at your
11 option) any later version.
12
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16 License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LIB.  If not, write to
20 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 MA 02110-1301, USA. */
22
23 #define MPFR_NEED_LONGLONG_H
24 #include "mpfr-impl.h"
25
26 /* The computation of cosh is done by    *
27  *  cosh= 1/2[e^(x)+e^(-x)]              */
28
29 int
30 mpfr_cosh (mpfr_ptr y, mpfr_srcptr xt , mp_rnd_t rnd_mode)
31 {
32   mpfr_t x;
33   int inexact;
34   MPFR_SAVE_EXPO_DECL (expo);
35
36   MPFR_LOG_FUNC (("x[%#R]=%R rnd=%d", xt, xt, rnd_mode),
37                  ("y[%#R]=%R inexact=%d", y, y, inexact));
38
39   if (MPFR_UNLIKELY(MPFR_IS_SINGULAR(xt)))
40     {
41       if (MPFR_IS_NAN(xt))
42         {
43           MPFR_SET_NAN(y);
44           MPFR_RET_NAN;
45         }
46       else if (MPFR_IS_INF(xt))
47         {
48           MPFR_SET_INF(y);
49           MPFR_SET_POS(y);
50           MPFR_RET(0);
51         }
52       else
53         {
54           MPFR_ASSERTD(MPFR_IS_ZERO(xt));
55           return mpfr_set_ui (y, 1, rnd_mode); /* cosh(0) = 1 */
56         }
57     }
58
59   MPFR_SAVE_EXPO_MARK (expo);
60
61   /* cosh(x) = 1+x^2/2 + ... <= 1+x^2 for x <= 2.9828...,
62      thus the error < 2^(2*EXP(x)). If x >= 1, then EXP(x) >= 1,
63      thus the following will always fail. */
64   MPFR_FAST_COMPUTE_IF_SMALL_INPUT (y, __gmpfr_one, -2 * MPFR_GET_EXP (xt), 0,
65                                     1, rnd_mode, inexact = _inexact; goto end);
66
67   MPFR_TMP_INIT_ABS(x, xt);
68   /* General case */
69   {
70     /* Declaration of the intermediary variable */
71     mpfr_t t, te;
72     /* Declaration of the size variable */
73     mp_prec_t Ny = MPFR_PREC(y);   /* Precision of output variable */
74     mp_prec_t Nt;                  /* Precision of the intermediary variable */
75     long int err;                  /* Precision of error */
76     MPFR_ZIV_DECL (loop);
77     MPFR_GROUP_DECL (group);
78
79     /* compute the precision of intermediary variable */
80     /* The optimal number of bits : see algorithms.tex */
81     Nt = Ny + 3 + MPFR_INT_CEIL_LOG2 (Ny);
82
83     /* initialise of intermediary variables */
84     MPFR_GROUP_INIT_2 (group, Nt, t, te);
85
86     /* First computation of cosh */
87     MPFR_ZIV_INIT (loop, Nt);
88     for (;;)
89       {
90         MPFR_BLOCK_DECL (flags);
91
92         /* Compute cosh */
93         MPFR_BLOCK (flags, mpfr_exp (te, x, GMP_RNDD));  /* exp(x) */
94         /* exp can overflow (but not underflow since x>0) */
95         if (MPFR_OVERFLOW (flags))
96           /* cosh(x) > exp(x), cosh(x) underflows too */
97           {
98             inexact = mpfr_overflow (y, rnd_mode, MPFR_SIGN_POS);
99             MPFR_SAVE_EXPO_UPDATE_FLAGS (expo, MPFR_FLAGS_OVERFLOW);
100             break;
101           }
102         mpfr_ui_div (t, 1, te, GMP_RNDU);   /* 1/exp(x) */
103         mpfr_add (t, te, t, GMP_RNDU);      /* exp(x) + 1/exp(x)*/
104         mpfr_div_2ui (t, t, 1, GMP_RNDN);   /* 1/2(exp(x) + 1/exp(x))*/
105
106         /* Estimation of the error */
107         err = Nt - 3;
108         /* Check if we can round */
109         if (MPFR_LIKELY (MPFR_CAN_ROUND (t, err, Ny, rnd_mode)))
110           {
111             inexact = mpfr_set (y, t, rnd_mode);
112             break;
113           }
114
115         /* Actualisation of the precision */
116         MPFR_ZIV_NEXT (loop, Nt);
117         MPFR_GROUP_REPREC_2 (group, Nt, t, te);
118       }
119     MPFR_ZIV_FREE (loop);
120     MPFR_GROUP_CLEAR (group);
121   }
122
123  end:
124   MPFR_SAVE_EXPO_FREE (expo);
125   return mpfr_check_range (y, inexact, rnd_mode);
126 }