HAMMER VFS - Minor bug (caught by assertion panic)
[dragonfly.git] / contrib / mpfr / log1p.c
blobc3a20377051abd658e21b5bd1487c56bd5880a46
1 /* mpfr_log1p -- Compute log(1+x)
3 Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by the Arenaire and Cacao projects, INRIA.
6 This file is part of the GNU MPFR Library.
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.
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.
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. */
23 #define MPFR_NEED_LONGLONG_H
24 #include "mpfr-impl.h"
26 /* The computation of log1p is done by
27 log1p(x)=log(1+x) */
29 int
30 mpfr_log1p (mpfr_ptr y, mpfr_srcptr x, mp_rnd_t rnd_mode)
32 int comp, inexact;
33 mp_exp_t ex;
34 MPFR_SAVE_EXPO_DECL (expo);
36 if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (x)))
38 if (MPFR_IS_NAN (x))
40 MPFR_SET_NAN (y);
41 MPFR_RET_NAN;
43 /* check for inf or -inf (result is not defined) */
44 else if (MPFR_IS_INF (x))
46 if (MPFR_IS_POS (x))
48 MPFR_SET_INF (y);
49 MPFR_SET_POS (y);
50 MPFR_RET (0);
52 else
54 MPFR_SET_NAN (y);
55 MPFR_RET_NAN;
58 else /* x is zero */
60 MPFR_ASSERTD (MPFR_IS_ZERO (x));
61 MPFR_SET_ZERO (y); /* log1p(+/- 0) = +/- 0 */
62 MPFR_SET_SAME_SIGN (y, x);
63 MPFR_RET (0);
67 ex = MPFR_GET_EXP (x);
68 if (ex < 0) /* -0.5 < x < 0.5 */
70 /* For x > 0, abs(log(1+x)-x) < x^2/2.
71 For x > -0.5, abs(log(1+x)-x) < x^2. */
72 if (MPFR_IS_POS (x))
73 MPFR_FAST_COMPUTE_IF_SMALL_INPUT (y, x, - ex - 1, 0, 0, rnd_mode, {});
74 else
75 MPFR_FAST_COMPUTE_IF_SMALL_INPUT (y, x, - ex, 0, 1, rnd_mode, {});
78 comp = mpfr_cmp_si (x, -1);
79 /* log1p(x) is undefined for x < -1 */
80 if (MPFR_UNLIKELY(comp <= 0))
82 if (comp == 0)
83 /* x=0: log1p(-1)=-inf (division by zero) */
85 MPFR_SET_INF (y);
86 MPFR_SET_NEG (y);
87 MPFR_RET (0);
89 MPFR_SET_NAN (y);
90 MPFR_RET_NAN;
93 MPFR_SAVE_EXPO_MARK (expo);
95 /* General case */
97 /* Declaration of the intermediary variable */
98 mpfr_t t;
99 /* Declaration of the size variable */
100 mp_prec_t Ny = MPFR_PREC(y); /* target precision */
101 mp_prec_t Nt; /* working precision */
102 mp_exp_t err; /* error */
103 MPFR_ZIV_DECL (loop);
105 /* compute the precision of intermediary variable */
106 /* the optimal number of bits : see algorithms.tex */
107 Nt = Ny + MPFR_INT_CEIL_LOG2 (Ny) + 6;
109 /* if |x| is smaller than 2^(-e), we will loose about e bits
110 in log(1+x) */
111 if (MPFR_EXP(x) < 0)
112 Nt += -MPFR_EXP(x);
114 /* initialise of intermediary variable */
115 mpfr_init2 (t, Nt);
117 /* First computation of log1p */
118 MPFR_ZIV_INIT (loop, Nt);
119 for (;;)
121 /* compute log1p */
122 inexact = mpfr_add_ui (t, x, 1, GMP_RNDN); /* 1+x */
123 /* if inexact = 0, then t = x+1, and the result is simply log(t) */
124 if (inexact == 0)
126 inexact = mpfr_log (y, t, rnd_mode);
127 goto end;
129 mpfr_log (t, t, GMP_RNDN); /* log(1+x) */
131 /* the error is bounded by (1/2+2^(1-EXP(t))*ulp(t) (cf algorithms.tex)
132 if EXP(t)>=2, then error <= ulp(t)
133 if EXP(t)<=1, then error <= 2^(2-EXP(t))*ulp(t) */
134 err = Nt - MAX (0, 2 - MPFR_GET_EXP (t));
136 if (MPFR_LIKELY (MPFR_CAN_ROUND (t, err, Ny, rnd_mode)))
137 break;
139 /* increase the precision */
140 MPFR_ZIV_NEXT (loop, Nt);
141 mpfr_set_prec (t, Nt);
143 inexact = mpfr_set (y, t, rnd_mode);
145 end:
146 MPFR_ZIV_FREE (loop);
147 mpfr_clear (t);
150 MPFR_SAVE_EXPO_FREE (expo);
151 return mpfr_check_range (y, inexact, rnd_mode);