Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
Contributed by the Arenaire and Cacao projects, INRIA.
This file is part of the MPFR Library.
The MPFR Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version.
The MPFR Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the MPFR Library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
MA 02110-1301, USA. */
#define MPFR_NEED_LONGLONG_H
#include "mpfr-impl.h"
if we want p bits of the result,
pi
log(a) ~ ------------ - m log 2
2 AG(1,4/s)
where s = x 2^m > 2^(p/2)
More precisely, if F(x) = int(1/sqrt(1-(1-x^2)*sin(t)^2), t=0..PI/2),
then for s>=1.26 we have log(s) < F(4/s) < log(s)*(1+4/s^2)
from which we deduce pi/2/AG(1,4/s)*(1-4/s^2) < log(s) < pi/2/AG(1,4/s)
so the relative error 4/s^2 is < 4/2^p i.e. 4 ulps.
*/
int
mpfr_log (mpfr_ptr r, mpfr_srcptr a, mp_rnd_t rnd_mode)
{
int inexact;
mp_prec_t p, q;
mpfr_t tmp1, tmp2;
mp_limb_t *tmp1p, *tmp2p;
MPFR_SAVE_EXPO_DECL (expo);
MPFR_ZIV_DECL (loop);
MPFR_TMP_DECL(marker);
MPFR_LOG_FUNC (("a[%#R]=%R rnd=%d", a, a, rnd_mode),
("r[%#R]=%R inexact=%d", r, r, inexact));
if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (a)))
{
if (MPFR_IS_NAN (a))
{
MPFR_SET_NAN (r);
MPFR_RET_NAN;
}
else if (MPFR_IS_INF (a))
{
if (MPFR_IS_NEG (a))
{
MPFR_SET_NAN (r);
MPFR_RET_NAN;
}
else
{
MPFR_SET_INF (r);
MPFR_SET_POS (r);
MPFR_RET (0);
}
}
else
{
MPFR_ASSERTD (MPFR_IS_ZERO (a));
MPFR_SET_INF (r);
MPFR_SET_NEG (r);
MPFR_RET (0);
}
}
else if (MPFR_UNLIKELY (MPFR_IS_NEG (a)))
{
MPFR_SET_NAN (r);
MPFR_RET_NAN;
}
else if (MPFR_UNLIKELY (MPFR_GET_EXP (a) == 1 && mpfr_cmp_ui (a, 1) == 0))
{
MPFR_SET_ZERO (r);
MPFR_SET_POS (r);
MPFR_RET (0);
}
q = MPFR_PREC (r);
p = q + 5 + 2 * MPFR_INT_CEIL_LOG2 (q);
m=q; while (m) { p++; m >>= 1; } */
p += BITS_PER_MP_LIMB - (p%BITS_PER_MP_LIMB); */
MPFR_TMP_MARK(marker);
MPFR_SAVE_EXPO_MARK (expo);
MPFR_ZIV_INIT (loop, p);
for (;;)
{
mp_size_t size;
long m;
mp_exp_t cancel;
m = (p + 1) / 2 - MPFR_GET_EXP (a) + 1;
size = (p-1)/BITS_PER_MP_LIMB+1;
MPFR_TMP_INIT (tmp1p, tmp1, p, size);
MPFR_TMP_INIT (tmp2p, tmp2, p, size);
mpfr_mul_2si (tmp2, a, m, GMP_RNDN);
mpfr_div (tmp1, __gmpfr_four, tmp2, GMP_RNDN);
mpfr_agm (tmp2, __gmpfr_one, tmp1, GMP_RNDN);
mpfr_mul_2ui (tmp2, tmp2, 1, GMP_RNDN);
mpfr_const_pi (tmp1, GMP_RNDN);
mpfr_div (tmp2, tmp1, tmp2, GMP_RNDN);
mpfr_const_log2 (tmp1, GMP_RNDN);
mpfr_mul_si (tmp1, tmp1, m, GMP_RNDN);
mpfr_sub (tmp1, tmp2, tmp1, GMP_RNDN);
if (MPFR_LIKELY (MPFR_IS_PURE_FP (tmp1) && MPFR_IS_PURE_FP (tmp2)))
{
cancel = MPFR_GET_EXP (tmp2) - MPFR_GET_EXP (tmp1);
MPFR_LOG_MSG (("canceled bits=%ld\n", cancel));
MPFR_LOG_VAR (tmp1);
if (MPFR_UNLIKELY (cancel < 0))
cancel = 0;
4 ulps from the 4/s^2 second order term,
plus the canceled bits */
if (MPFR_LIKELY (MPFR_CAN_ROUND (tmp1, p-cancel-4, q, rnd_mode)))
break;
too low; in particular, the increment must be positive even
if cancel = 0 (can this occur?). */
p += cancel >= 8 ? cancel : 8;
}
else
{
with it. */
p += 32;
}
MPFR_ZIV_NEXT (loop, p);
}
MPFR_ZIV_FREE (loop);
inexact = mpfr_set (r, tmp1, rnd_mode);
MPFR_TMP_FREE(marker);
MPFR_SAVE_EXPO_FREE (expo);
return mpfr_check_range (r, inexact, rnd_mode);
}