Merge branch 'vendor/GCC44'
[dragonfly.git] / contrib / mpfr / asinh.c
1 /* mpfr_asinh -- inverse hyperbolic sine
2
3 Copyright 2001, 2002, 2003, 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 asinh is done by  *
27  *    asinh = ln(x + sqrt(x^2 + 1))     */
28
29 int
30 mpfr_asinh (mpfr_ptr y, mpfr_srcptr x, mp_rnd_t rnd_mode)
31 {
32   int inexact;
33   int signx, neg;
34   mp_prec_t Ny, Nt;
35   mpfr_t t; /* auxiliary variables */
36   mp_exp_t err;
37   MPFR_SAVE_EXPO_DECL (expo);
38   MPFR_ZIV_DECL (loop);
39
40   MPFR_LOG_FUNC (("x[%#R]=%R rnd=%d", x, x, rnd_mode),
41                  ("y[%#R]=%R inexact=%d", y, y, inexact));
42
43   if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (x)))
44     {
45       if (MPFR_IS_NAN (x))
46         {
47           MPFR_SET_NAN (y);
48           MPFR_RET_NAN;
49         }
50       else if (MPFR_IS_INF (x))
51         {
52           MPFR_SET_INF (y);
53           MPFR_SET_SAME_SIGN (y, x);
54           MPFR_RET (0);
55         }
56       else /* x is necessarily 0 */
57         {
58           MPFR_ASSERTD (MPFR_IS_ZERO (x));
59           MPFR_SET_ZERO (y);   /* asinh(0) = 0 */
60           MPFR_SET_SAME_SIGN (y, x);
61           MPFR_RET (0);
62         }
63     }
64
65   /* asinh(x) = x - x^3/6 + ... so the error is < 2^(3*EXP(x)-2) */
66   MPFR_FAST_COMPUTE_IF_SMALL_INPUT (y, x, -2 * MPFR_GET_EXP (x), 2, 0,
67                                     rnd_mode, {});
68
69   Ny = MPFR_PREC (y);   /* Precision of output variable */
70
71   signx = MPFR_SIGN (x);
72   neg = MPFR_IS_NEG (x);
73
74   /* General case */
75
76   /* compute the precision of intermediary variable */
77   /* the optimal number of bits : see algorithms.tex */
78   Nt = Ny + 4 + MPFR_INT_CEIL_LOG2 (Ny);
79
80   MPFR_SAVE_EXPO_MARK (expo);
81
82   /* initialize intermediary variables */
83   mpfr_init2 (t, Nt);
84
85   /* First computation of asinh */
86   MPFR_ZIV_INIT (loop, Nt);
87   for (;;)
88     {
89       /* compute asinh */
90       mpfr_mul (t, x, x, GMP_RNDD);                    /* x^2 */
91       mpfr_add_ui (t, t, 1, GMP_RNDD);                 /* x^2+1 */
92       mpfr_sqrt (t, t, GMP_RNDN);                      /* sqrt(x^2+1) */
93       (neg ? mpfr_sub : mpfr_add) (t, t, x, GMP_RNDN); /* sqrt(x^2+1)+x */
94       mpfr_log (t, t, GMP_RNDN);                       /* ln(sqrt(x^2+1)+x)*/
95
96       if (MPFR_LIKELY (MPFR_IS_PURE_FP (t)))
97         {
98           /* error estimate -- see algorithms.tex */
99           err = Nt - (MAX (4 - MPFR_GET_EXP (t), 0) + 1);
100           if (MPFR_LIKELY (MPFR_IS_ZERO (t)
101                            || MPFR_CAN_ROUND (t, err, Ny, rnd_mode)))
102             break;
103         }
104
105       /* actualisation of the precision */
106       MPFR_ZIV_NEXT (loop, Nt);
107       mpfr_set_prec (t, Nt);
108     }
109   MPFR_ZIV_FREE (loop);
110
111   inexact = mpfr_set4 (y, t, rnd_mode, signx);
112
113   mpfr_clear (t);
114
115   MPFR_SAVE_EXPO_FREE (expo);
116   return mpfr_check_range (y, inexact, rnd_mode);
117 }