1/* mpfr_set_f -- set a MPFR number from a GNU MPF number
2
3Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4Contributed by the AriC and Caramel projects, INRIA.
5
6This file is part of the GNU MPFR Library.
7
8The GNU MPFR Library is free software; you can redistribute it and/or modify
9it under the terms of the GNU Lesser General Public License as published by
10the Free Software Foundation; either version 3 of the License, or (at your
11option) any later version.
12
13The GNU MPFR Library is distributed in the hope that it will be useful, but
14WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16License for more details.
17
18You should have received a copy of the GNU Lesser General Public License
19along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
20http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
2151 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22
23#define MPFR_NEED_LONGLONG_H
24#include "mpfr-impl.h"
25
26int
27mpfr_set_f (mpfr_ptr y, mpf_srcptr x, mpfr_rnd_t rnd_mode)
28{
29  mp_limb_t *my, *mx, *tmp;
30  unsigned long cnt, sx, sy;
31  int inexact, carry = 0;
32  MPFR_TMP_DECL(marker);
33
34  sx = ABS(SIZ(x)); /* number of limbs of the mantissa of x */
35
36  if (sx == 0) /* x is zero */
37    {
38      MPFR_SET_ZERO(y);
39      MPFR_SET_POS(y);
40      return 0; /* 0 is exact */
41    }
42
43  if (SIZ(x) * MPFR_FROM_SIGN_TO_INT(MPFR_SIGN(y)) < 0)
44    MPFR_CHANGE_SIGN (y);
45
46  sy = MPFR_LIMB_SIZE (y);
47  my = MPFR_MANT(y);
48  mx = PTR(x);
49
50  count_leading_zeros(cnt, mx[sx - 1]);
51
52  if (sy <= sx) /* we may have to round even when sy = sx */
53    {
54      unsigned long xprec = sx * GMP_NUMB_BITS;
55
56      MPFR_TMP_MARK(marker);
57      tmp = MPFR_TMP_LIMBS_ALLOC (sx);
58      if (cnt)
59        mpn_lshift (tmp, mx, sx, cnt);
60      else
61        /* FIXME: we may avoid the copy here, and directly call mpfr_round_raw
62           on mx instead of tmp */
63        MPN_COPY (tmp, mx, sx);
64      carry = mpfr_round_raw (my, tmp, xprec, (SIZ(x) < 0), MPFR_PREC(y),
65                              rnd_mode, &inexact);
66      if (MPFR_UNLIKELY(carry)) /* result is a power of two */
67        my[sy - 1] = MPFR_LIMB_HIGHBIT;
68      MPFR_TMP_FREE(marker);
69    }
70  else
71    {
72      if (cnt)
73        mpn_lshift (my + sy - sx, mx, sx, cnt);
74      else
75        MPN_COPY (my + sy - sx, mx, sx);
76      MPN_ZERO(my, sy - sx);
77      /* no rounding necessary, since y has a larger mantissa */
78      inexact = 0;
79    }
80
81  /* warning: EXP(x) * GMP_NUMB_BITS may exceed the maximal exponent */
82  if (EXP(x) > 1 + (__gmpfr_emax - 1) / GMP_NUMB_BITS)
83    {
84      /* EXP(x) >= 2 + floor((__gmpfr_emax-1)/GMP_NUMB_BITS)
85         EXP(x) >= 2 + (__gmpfr_emax - GMP_NUMB_BITS) / GMP_NUMB_BITS
86                >= 1 + __gmpfr_emax / GMP_NUMB_BITS
87         EXP(x) * GMP_NUMB_BITS >= __gmpfr_emax + GMP_NUMB_BITS
88         Since 0 <= cnt <= GMP_NUMB_BITS-1, and 0 <= carry <= 1,
89         we have then EXP(x) * GMP_NUMB_BITS - cnt + carry > __gmpfr_emax */
90      return mpfr_overflow (y, rnd_mode, MPFR_SIGN (y));
91    }
92  else
93    {
94      /* Do not use MPFR_SET_EXP as the exponent may be out of range. */
95      MPFR_EXP (y) = EXP (x) * GMP_NUMB_BITS - (mpfr_exp_t) cnt + carry;
96    }
97
98  return mpfr_check_range (y, inexact, rnd_mode);
99}
100