14a238c70SJohn Marino /* mpfr_get_f -- convert a MPFR number to a GNU MPF number
24a238c70SJohn Marino
3*ab6d115fSJohn Marino Copyright 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4*ab6d115fSJohn Marino Contributed by the AriC and Caramel projects, INRIA.
54a238c70SJohn Marino
64a238c70SJohn Marino This file is part of the GNU MPFR Library.
74a238c70SJohn Marino
84a238c70SJohn Marino The GNU MPFR Library is free software; you can redistribute it and/or modify
94a238c70SJohn Marino it under the terms of the GNU Lesser General Public License as published by
104a238c70SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
114a238c70SJohn Marino option) any later version.
124a238c70SJohn Marino
134a238c70SJohn Marino The GNU MPFR Library is distributed in the hope that it will be useful, but
144a238c70SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
154a238c70SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
164a238c70SJohn Marino License for more details.
174a238c70SJohn Marino
184a238c70SJohn Marino You should have received a copy of the GNU Lesser General Public License
194a238c70SJohn Marino along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
204a238c70SJohn Marino http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
214a238c70SJohn Marino 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
224a238c70SJohn Marino
234a238c70SJohn Marino #include "mpfr-impl.h"
244a238c70SJohn Marino
254a238c70SJohn Marino /* Since MPFR-3.0, return the usual inexact value.
264a238c70SJohn Marino The erange flag is set if an error occurred in the conversion
274a238c70SJohn Marino (y is NaN, +Inf, or -Inf that have no equivalent in mpf)
284a238c70SJohn Marino */
294a238c70SJohn Marino int
mpfr_get_f(mpf_ptr x,mpfr_srcptr y,mpfr_rnd_t rnd_mode)304a238c70SJohn Marino mpfr_get_f (mpf_ptr x, mpfr_srcptr y, mpfr_rnd_t rnd_mode)
314a238c70SJohn Marino {
324a238c70SJohn Marino int inex;
334a238c70SJohn Marino mp_size_t sx, sy;
344a238c70SJohn Marino mpfr_prec_t precx, precy;
354a238c70SJohn Marino mp_limb_t *xp;
364a238c70SJohn Marino int sh;
374a238c70SJohn Marino
384a238c70SJohn Marino if (MPFR_UNLIKELY(MPFR_IS_SINGULAR(y)))
394a238c70SJohn Marino {
404a238c70SJohn Marino if (MPFR_IS_ZERO(y))
414a238c70SJohn Marino {
424a238c70SJohn Marino mpf_set_ui (x, 0);
434a238c70SJohn Marino return 0;
444a238c70SJohn Marino }
454a238c70SJohn Marino else if (MPFR_IS_NAN (y))
464a238c70SJohn Marino {
474a238c70SJohn Marino MPFR_SET_ERANGE ();
484a238c70SJohn Marino return 0;
494a238c70SJohn Marino }
504a238c70SJohn Marino else /* y is plus infinity (resp. minus infinity), set x to the maximum
514a238c70SJohn Marino value (resp. the minimum value) in precision PREC(x) */
524a238c70SJohn Marino {
534a238c70SJohn Marino int i;
544a238c70SJohn Marino mp_limb_t *xp;
554a238c70SJohn Marino
564a238c70SJohn Marino MPFR_SET_ERANGE ();
574a238c70SJohn Marino
584a238c70SJohn Marino /* To this day, [mp_exp_t] and mp_size_t are #defined as the same
594a238c70SJohn Marino type */
604a238c70SJohn Marino EXP (x) = MP_SIZE_T_MAX;
614a238c70SJohn Marino
624a238c70SJohn Marino sx = PREC (x);
634a238c70SJohn Marino SIZ (x) = sx;
64*ab6d115fSJohn Marino xp = PTR (x);
654a238c70SJohn Marino for (i = 0; i < sx; i++)
664a238c70SJohn Marino xp[i] = MP_LIMB_T_MAX;
674a238c70SJohn Marino
684a238c70SJohn Marino if (MPFR_IS_POS (y))
694a238c70SJohn Marino return -1;
704a238c70SJohn Marino else
714a238c70SJohn Marino {
724a238c70SJohn Marino mpf_neg (x, x);
734a238c70SJohn Marino return +1;
744a238c70SJohn Marino }
754a238c70SJohn Marino }
764a238c70SJohn Marino }
774a238c70SJohn Marino
784a238c70SJohn Marino sx = PREC(x); /* number of limbs of the mantissa of x */
794a238c70SJohn Marino
804a238c70SJohn Marino precy = MPFR_PREC(y);
814a238c70SJohn Marino precx = (mpfr_prec_t) sx * GMP_NUMB_BITS;
824a238c70SJohn Marino sy = MPFR_LIMB_SIZE (y);
834a238c70SJohn Marino
844a238c70SJohn Marino xp = PTR (x);
854a238c70SJohn Marino
864a238c70SJohn Marino /* since mpf numbers are represented in base 2^GMP_NUMB_BITS,
874a238c70SJohn Marino we loose -EXP(y) % GMP_NUMB_BITS bits in the most significant limb */
884a238c70SJohn Marino sh = MPFR_GET_EXP(y) % GMP_NUMB_BITS;
894a238c70SJohn Marino sh = sh <= 0 ? - sh : GMP_NUMB_BITS - sh;
904a238c70SJohn Marino MPFR_ASSERTD (sh >= 0);
914a238c70SJohn Marino if (precy + sh <= precx) /* we can copy directly */
924a238c70SJohn Marino {
934a238c70SJohn Marino mp_size_t ds;
944a238c70SJohn Marino
954a238c70SJohn Marino MPFR_ASSERTN (sx >= sy);
964a238c70SJohn Marino ds = sx - sy;
974a238c70SJohn Marino
984a238c70SJohn Marino if (sh != 0)
994a238c70SJohn Marino {
1004a238c70SJohn Marino mp_limb_t out;
1014a238c70SJohn Marino out = mpn_rshift (xp + ds, MPFR_MANT(y), sy, sh);
1024a238c70SJohn Marino MPFR_ASSERTN (ds > 0 || out == 0);
1034a238c70SJohn Marino if (ds > 0)
1044a238c70SJohn Marino xp[--ds] = out;
1054a238c70SJohn Marino }
1064a238c70SJohn Marino else
1074a238c70SJohn Marino MPN_COPY (xp + ds, MPFR_MANT (y), sy);
1084a238c70SJohn Marino if (ds > 0)
1094a238c70SJohn Marino MPN_ZERO (xp, ds);
1104a238c70SJohn Marino EXP(x) = (MPFR_GET_EXP(y) + sh) / GMP_NUMB_BITS;
1114a238c70SJohn Marino inex = 0;
1124a238c70SJohn Marino }
1134a238c70SJohn Marino else /* we have to round to precx - sh bits */
1144a238c70SJohn Marino {
1154a238c70SJohn Marino mpfr_t z;
1164a238c70SJohn Marino mp_size_t sz;
1174a238c70SJohn Marino
1184a238c70SJohn Marino /* Recall that precx = (mpfr_prec_t) sx * GMP_NUMB_BITS, thus removing
1194a238c70SJohn Marino sh bits (sh < GMP_NUMB_BITSS) won't reduce the number of limbs. */
1204a238c70SJohn Marino mpfr_init2 (z, precx - sh);
1214a238c70SJohn Marino sz = MPFR_LIMB_SIZE (z);
1224a238c70SJohn Marino MPFR_ASSERTN (sx == sz);
1234a238c70SJohn Marino
1244a238c70SJohn Marino inex = mpfr_set (z, y, rnd_mode);
1254a238c70SJohn Marino /* warning, sh may change due to rounding, but then z is a power of two,
1264a238c70SJohn Marino thus we can safely ignore its last bit which is 0 */
1274a238c70SJohn Marino sh = MPFR_GET_EXP(z) % GMP_NUMB_BITS;
1284a238c70SJohn Marino sh = sh <= 0 ? - sh : GMP_NUMB_BITS - sh;
1294a238c70SJohn Marino MPFR_ASSERTD (sh >= 0);
1304a238c70SJohn Marino if (sh != 0)
1314a238c70SJohn Marino {
1324a238c70SJohn Marino mp_limb_t out;
1334a238c70SJohn Marino out = mpn_rshift (xp, MPFR_MANT(z), sz, sh);
1344a238c70SJohn Marino /* If sh hasn't changed, it is the number of the non-significant
1354a238c70SJohn Marino bits in the lowest limb of z. Therefore out == 0. */
1364a238c70SJohn Marino MPFR_ASSERTD (out == 0); (void) out; /* avoid a warning */
1374a238c70SJohn Marino }
1384a238c70SJohn Marino else
1394a238c70SJohn Marino MPN_COPY (xp, MPFR_MANT(z), sz);
1404a238c70SJohn Marino EXP(x) = (MPFR_GET_EXP(z) + sh) / GMP_NUMB_BITS;
1414a238c70SJohn Marino mpfr_clear (z);
1424a238c70SJohn Marino }
1434a238c70SJohn Marino
1444a238c70SJohn Marino /* set size and sign */
1454a238c70SJohn Marino SIZ(x) = (MPFR_FROM_SIGN_TO_INT(MPFR_SIGN(y)) < 0) ? -sx : sx;
1464a238c70SJohn Marino
1474a238c70SJohn Marino return inex;
1484a238c70SJohn Marino }
149