Home | History | Annotate | Line # | Download | only in src
      1 /* mpfr_log2 -- log base 2
      2 
      3 Copyright 2001-2023 Free Software Foundation, Inc.
      4 Contributed by the AriC and Caramba 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 3 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.LESSER.  If not, see
     20 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
     21 51 Franklin St, Fifth Floor, Boston, 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, mpfr_rnd_t rnd_mode)
     31 {
     32   int inexact;
     33   MPFR_SAVE_EXPO_DECL (expo);
     34 
     35   MPFR_LOG_FUNC
     36     (("a[%Pd]=%.*Rg rnd=%d", mpfr_get_prec (a), mpfr_log_prec, a, rnd_mode),
     37      ("r[%Pd]=%.*Rg inexact=%d", mpfr_get_prec (r), mpfr_log_prec, r,
     38       inexact));
     39 
     40   if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (a)))
     41     {
     42       /* If a is NaN, the result is NaN */
     43       if (MPFR_IS_NAN (a))
     44         {
     45           MPFR_SET_NAN (r);
     46           MPFR_RET_NAN;
     47         }
     48       /* check for infinity before zero */
     49       else if (MPFR_IS_INF (a))
     50         {
     51           if (MPFR_IS_NEG (a))
     52             /* log(-Inf) = NaN */
     53             {
     54               MPFR_SET_NAN (r);
     55               MPFR_RET_NAN;
     56             }
     57           else /* log(+Inf) = +Inf */
     58             {
     59               MPFR_SET_INF (r);
     60               MPFR_SET_POS (r);
     61               MPFR_RET (0);
     62             }
     63         }
     64       else /* a is zero */
     65         {
     66           MPFR_ASSERTD (MPFR_IS_ZERO (a));
     67           MPFR_SET_INF (r);
     68           MPFR_SET_NEG (r);
     69           MPFR_SET_DIVBY0 ();
     70           MPFR_RET (0); /* log2(0) is an exact -infinity */
     71         }
     72     }
     73 
     74   /* If a is negative, the result is NaN */
     75   if (MPFR_UNLIKELY (MPFR_IS_NEG (a)))
     76     {
     77       MPFR_SET_NAN (r);
     78       MPFR_RET_NAN;
     79     }
     80 
     81   /* If a is 1, the result is 0 */
     82   if (MPFR_UNLIKELY (mpfr_cmp_ui (a, 1) == 0))
     83     {
     84       MPFR_SET_ZERO (r);
     85       MPFR_SET_POS (r);
     86       MPFR_RET (0); /* only "normal" case where the result is exact */
     87     }
     88 
     89   /* If a is 2^N, log2(a) is exact*/
     90   if (MPFR_UNLIKELY (mpfr_cmp_ui_2exp (a, 1, MPFR_GET_EXP (a) - 1) == 0))
     91     return mpfr_set_si(r, MPFR_GET_EXP (a) - 1, rnd_mode);
     92 
     93   MPFR_SAVE_EXPO_MARK (expo);
     94 
     95   /* General case */
     96   {
     97     /* Declaration of the intermediary variable */
     98     mpfr_t t, tt;
     99     /* Declaration of the size variable */
    100     mpfr_prec_t Ny = MPFR_PREC(r);              /* target precision */
    101     mpfr_prec_t Nt;                             /* working precision */
    102     mpfr_exp_t err;                             /* error */
    103     MPFR_ZIV_DECL (loop);
    104 
    105     /* compute the precision of intermediary variable */
    106     /* the optimal number of bits : see algorithms.tex */
    107     Nt = Ny + 3 + MPFR_INT_CEIL_LOG2 (Ny);
    108 
    109     /* initialize of intermediary       variable */
    110     mpfr_init2 (t, Nt);
    111     mpfr_init2 (tt, Nt);
    112 
    113     /* First computation of log2 */
    114     MPFR_ZIV_INIT (loop, Nt);
    115     for (;;)
    116       {
    117         /* compute log2 */
    118         mpfr_const_log2(t,MPFR_RNDD); /* log(2) */
    119         mpfr_log(tt,a,MPFR_RNDN);     /* log(a) */
    120         mpfr_div(t,tt,t,MPFR_RNDN); /* log(a)/log(2) */
    121 
    122         /* estimation of the error */
    123         err = Nt-3;
    124         if (MPFR_LIKELY (MPFR_CAN_ROUND (t, err, Ny, rnd_mode)))
    125           break;
    126 
    127         /* actualization of the precision */
    128         MPFR_ZIV_NEXT (loop, Nt);
    129         mpfr_set_prec (t, Nt);
    130         mpfr_set_prec (tt, Nt);
    131       }
    132     MPFR_ZIV_FREE (loop);
    133 
    134     inexact = mpfr_set (r, t, rnd_mode);
    135 
    136     mpfr_clear (t);
    137     mpfr_clear (tt);
    138   }
    139 
    140   MPFR_SAVE_EXPO_FREE (expo);
    141   return mpfr_check_range (r, inexact, rnd_mode);
    142 }
    143