1 /* mpfr_get_f -- convert a MPFR number to a GNU MPF number
3 Copyright 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 #include "mpfr-impl.h"
25 /* return value is 0 iff no error occurred in the conversion
26 (1 for NaN, +Inf, -Inf that have no equivalent in mpf)
29 mpfr_get_f (mpf_ptr x, mpfr_srcptr y, mp_rnd_t rnd_mode)
32 mp_prec_t precx, precy;
36 if (MPFR_UNLIKELY(MPFR_IS_SINGULAR(y)))
47 sx = PREC(x); /* number of limbs of the mantissa of x */
50 precx = (mp_prec_t) sx * BITS_PER_MP_LIMB;
51 sy = MPFR_LIMB_SIZE (y);
55 /* since mpf numbers are represented in base 2^BITS_PER_MP_LIMB,
56 we loose -EXP(y) % BITS_PER_MP_LIMB bits in the most significant limb */
57 sh = MPFR_GET_EXP(y) % BITS_PER_MP_LIMB;
58 sh = sh <= 0 ? - sh : BITS_PER_MP_LIMB - sh;
59 MPFR_ASSERTD (sh >= 0);
60 if (precy + sh <= precx) /* we can copy directly */
64 MPFR_ASSERTN (sx >= sy);
70 out = mpn_rshift (xp + ds, MPFR_MANT(y), sy, sh);
71 MPFR_ASSERTN (ds > 0 || out == 0);
76 MPN_COPY (xp + ds, MPFR_MANT (y), sy);
79 EXP(x) = (MPFR_GET_EXP(y) + sh) / BITS_PER_MP_LIMB;
81 else /* we have to round to precx - sh bits */
86 /* Recall that precx = (mp_prec_t) sx * BITS_PER_MP_LIMB */
87 mpfr_init2 (z, precx - sh);
88 sz = MPFR_LIMB_SIZE (z);
89 mpfr_set (z, y, rnd_mode);
90 /* warning, sh may change due to rounding, but then z is a power of two,
91 thus we can safely ignore its last bit which is 0 */
92 sh = MPFR_GET_EXP(z) % BITS_PER_MP_LIMB;
93 sh = sh <= 0 ? - sh : BITS_PER_MP_LIMB - sh;
94 MPFR_ASSERTD (sx >= sz);
96 MPFR_ASSERTD (sh >= 0 && ds <= 1);
100 out = mpn_rshift (xp + ds, MPFR_MANT(z), sz, sh);
101 /* If sh hasn't changed, it is the number of the non-significant
102 bits in the lowest limb of z. Therefore out == 0. */
103 MPFR_ASSERTD (out == 0);
106 MPN_COPY (xp + ds, MPFR_MANT(z), sz);
109 EXP(x) = (MPFR_GET_EXP(z) + sh) / BITS_PER_MP_LIMB;
113 /* set size and sign */
114 SIZ(x) = (MPFR_FROM_SIGN_TO_INT(MPFR_SIGN(y)) < 0) ? -sx : sx;