Merge branch 'vendor/MDOCML'
[dragonfly.git] / contrib / mpfr / log2.c
1 /* mpfr_log2 -- log base 2
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 r=log2(a)
27       r=log2(a)=log(a)/log(2)      */
28
29 int
30 mpfr_log2 (mpfr_ptr r, mpfr_srcptr a, mp_rnd_t rnd_mode)
31 {
32   int inexact;
33   MPFR_SAVE_EXPO_DECL (expo);
34
35   if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (a)))
36     {
37       /* If a is NaN, the result is NaN */
38       if (MPFR_IS_NAN (a))
39         {
40           MPFR_SET_NAN (r);
41           MPFR_RET_NAN;
42         }
43       /* check for infinity before zero */
44       else if (MPFR_IS_INF (a))
45         {
46           if (MPFR_IS_NEG (a))
47             /* log(-Inf) = NaN */
48             {
49               MPFR_SET_NAN (r);
50               MPFR_RET_NAN;
51             }
52           else /* log(+Inf) = +Inf */
53             {
54               MPFR_SET_INF (r);
55               MPFR_SET_POS (r);
56               MPFR_RET (0);
57             }
58         }
59       else /* a is zero */
60         {
61           MPFR_ASSERTD (MPFR_IS_ZERO (a));
62           MPFR_SET_INF (r);
63           MPFR_SET_NEG (r);
64           MPFR_RET (0); /* log2(0) is an exact -infinity */
65         }
66     }
67
68   /* If a is negative, the result is NaN */
69   if (MPFR_UNLIKELY (MPFR_IS_NEG (a)))
70     {
71       MPFR_SET_NAN (r);
72       MPFR_RET_NAN;
73     }
74
75   /* If a is 1, the result is 0 */
76   if (MPFR_UNLIKELY (mpfr_cmp_ui (a, 1) == 0))
77     {
78       MPFR_SET_ZERO (r);
79       MPFR_SET_POS (r);
80       MPFR_RET (0); /* only "normal" case where the result is exact */
81     }
82
83   /* If a is 2^N, log2(a) is exact*/
84   if (MPFR_UNLIKELY (mpfr_cmp_ui_2exp (a, 1, MPFR_GET_EXP (a) - 1) == 0))
85     return mpfr_set_si(r, MPFR_GET_EXP (a) - 1, rnd_mode);
86
87   MPFR_SAVE_EXPO_MARK (expo);
88
89   /* General case */
90   {
91     /* Declaration of the intermediary variable */
92     mpfr_t t, tt;
93     /* Declaration of the size variable */
94     mp_prec_t Ny = MPFR_PREC(r);              /* target precision */
95     mp_prec_t Nt;                             /* working precision */
96     mp_exp_t err;                             /* error */
97     MPFR_ZIV_DECL (loop);
98
99     /* compute the precision of intermediary variable */
100     /* the optimal number of bits : see algorithms.tex */
101     Nt = Ny + 3 + MPFR_INT_CEIL_LOG2 (Ny);
102
103     /* initialise of intermediary       variable */
104     mpfr_init2 (t, Nt);
105     mpfr_init2 (tt, Nt);
106
107     /* First computation of log2 */
108     MPFR_ZIV_INIT (loop, Nt);
109     for (;;)
110       {
111         /* compute log2 */
112         mpfr_const_log2(t,GMP_RNDD); /* log(2) */
113         mpfr_log(tt,a,GMP_RNDN);     /* log(a) */
114         mpfr_div(t,tt,t,GMP_RNDN); /* log(a)/log(2) */
115
116         /* estimation of the error */
117         err = Nt-3;
118         if (MPFR_LIKELY (MPFR_CAN_ROUND (t, err, Ny, rnd_mode)))
119           break;
120
121         /* actualisation of the precision */
122         MPFR_ZIV_NEXT (loop, Nt);
123         mpfr_set_prec (t, Nt);
124         mpfr_set_prec (tt, Nt);
125       }
126     MPFR_ZIV_FREE (loop);
127
128     inexact = mpfr_set (r, t, rnd_mode);
129
130     mpfr_clear (t);
131     mpfr_clear (tt);
132   }
133
134   MPFR_SAVE_EXPO_FREE (expo);
135   return mpfr_check_range (r, inexact, rnd_mode);
136 }