1
/* mpfr_exp2 -- power of 2 function 2^y
3
Copyright 2001, 2002 Free Software Foundation.
5
This file is part of the MPFR Library.
7
The MPFR Library is free software; you can redistribute it and/or modify
8
it under the terms of the GNU Lesser General Public License as published by
9
the Free Software Foundation; either version 2.1 of the License, or (at your
10
option) any later version.
12
The MPFR Library is distributed in the hope that it will be useful, but
13
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15
License for more details.
17
You should have received a copy of the GNU Lesser General Public License
18
along with the MPFR Library; see the file COPYING.LIB. If not, write to
19
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20
MA 02111-1307, USA. */
25
#include "mpfr-impl.h"
27
/* The computation of y=pow(2,z) is done by
33
mpfr_exp2 (mpfr_ptr y, mpfr_srcptr x, mp_rnd_t rnd_mode)
63
return mpfr_set_ui (y, 1, rnd_mode);
65
/* since the smallest representable non-zero float is 1/2*2^__mpfr_emin,
66
if x < __mpfr_emin - 1, the result is either 1/2*2^__mpfr_emin or 0 */
67
if (mpfr_cmp_si_2exp (x, __mpfr_emin - 1, 0) < 0)
68
return mpfr_set_underflow (y, rnd_mode, 1);
72
/* Declaration of the intermediary variable */
75
/* Declaration of the size variable */
76
mp_prec_t Nx = MPFR_PREC(x); /* Precision of input variable */
77
mp_prec_t Ny = MPFR_PREC(y); /* Precision of input variable */
79
mp_prec_t Nt; /* Precision of the intermediary variable */
80
long int err; /* Precision of error */
82
/* compute the precision of intermediary variable */
84
/* the optimal number of bits : see algorithms.ps */
85
Nt = Nt + 5 + _mpfr_ceil_log2 (Nt);
88
/* initialise of intermediary variable */
92
/* First computation */
95
/* reactualisation of the precision */
96
mpfr_set_prec (t, Nt);
97
mpfr_set_prec (te, Nt);
99
/* compute exp(x*ln(2))*/
100
mpfr_const_log2 (t, GMP_RNDU); /* ln(2) */
101
mpfr_mul (te, x, t, GMP_RNDU); /* x*ln(2) */
102
mpfr_exp (t, te, GMP_RNDN); /* exp(x*ln(2))*/
104
/* estimate of the error -- see pow function in algorithms.ps*/
105
err = Nt - (MPFR_EXP(te) + 2);
107
/* actualisation of the precision */
108
Nt += _mpfr_isqrt (Nt) + 10;
110
} while ((err < 0) || !mpfr_can_round (t, err, GMP_RNDN, rnd_mode, Ny));
112
inexact = mpfr_set (y, t, rnd_mode);