1 /* mpfr_get_f -- convert a MPFR number to a GNU MPF number
3 Copyright 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramel 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 3 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.LESSER. If not, see
20 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
23 #include "mpfr-impl.h"
25 /* Since MPFR-3.0, return the usual inexact value.
26 The erange flag is set if an error occurred in the conversion
27 (y is NaN, +Inf, or -Inf that have no equivalent in mpf)
30 mpfr_get_f (mpf_ptr x
, mpfr_srcptr y
, mpfr_rnd_t rnd_mode
)
34 mpfr_prec_t precx
, precy
;
38 if (MPFR_UNLIKELY(MPFR_IS_SINGULAR(y
)))
45 else if (MPFR_IS_NAN (y
))
50 else /* y is plus infinity (resp. minus infinity), set x to the maximum
51 value (resp. the minimum value) in precision PREC(x) */
58 /* To this day, [mp_exp_t] and mp_size_t are #defined as the same
60 EXP (x
) = MP_SIZE_T_MAX
;
65 for (i
= 0; i
< sx
; i
++)
66 xp
[i
] = MP_LIMB_T_MAX
;
78 sx
= PREC(x
); /* number of limbs of the mantissa of x */
81 precx
= (mpfr_prec_t
) sx
* GMP_NUMB_BITS
;
82 sy
= MPFR_LIMB_SIZE (y
);
86 /* since mpf numbers are represented in base 2^GMP_NUMB_BITS,
87 we loose -EXP(y) % GMP_NUMB_BITS bits in the most significant limb */
88 sh
= MPFR_GET_EXP(y
) % GMP_NUMB_BITS
;
89 sh
= sh
<= 0 ? - sh
: GMP_NUMB_BITS
- sh
;
90 MPFR_ASSERTD (sh
>= 0);
91 if (precy
+ sh
<= precx
) /* we can copy directly */
95 MPFR_ASSERTN (sx
>= sy
);
101 out
= mpn_rshift (xp
+ ds
, MPFR_MANT(y
), sy
, sh
);
102 MPFR_ASSERTN (ds
> 0 || out
== 0);
107 MPN_COPY (xp
+ ds
, MPFR_MANT (y
), sy
);
110 EXP(x
) = (MPFR_GET_EXP(y
) + sh
) / GMP_NUMB_BITS
;
113 else /* we have to round to precx - sh bits */
118 /* Recall that precx = (mpfr_prec_t) sx * GMP_NUMB_BITS, thus removing
119 sh bits (sh < GMP_NUMB_BITSS) won't reduce the number of limbs. */
120 mpfr_init2 (z
, precx
- sh
);
121 sz
= MPFR_LIMB_SIZE (z
);
122 MPFR_ASSERTN (sx
== sz
);
124 inex
= mpfr_set (z
, y
, rnd_mode
);
125 /* warning, sh may change due to rounding, but then z is a power of two,
126 thus we can safely ignore its last bit which is 0 */
127 sh
= MPFR_GET_EXP(z
) % GMP_NUMB_BITS
;
128 sh
= sh
<= 0 ? - sh
: GMP_NUMB_BITS
- sh
;
129 MPFR_ASSERTD (sh
>= 0);
133 out
= mpn_rshift (xp
, MPFR_MANT(z
), sz
, sh
);
134 /* If sh hasn't changed, it is the number of the non-significant
135 bits in the lowest limb of z. Therefore out == 0. */
136 MPFR_ASSERTD (out
== 0); (void) out
; /* avoid a warning */
139 MPN_COPY (xp
, MPFR_MANT(z
), sz
);
140 EXP(x
) = (MPFR_GET_EXP(z
) + sh
) / GMP_NUMB_BITS
;
144 /* set size and sign */
145 SIZ(x
) = (MPFR_FROM_SIGN_TO_INT(MPFR_SIGN(y
)) < 0) ? -sx
: sx
;