beta-0.89.2
[luatex.git] / source / libs / mpfr / mpfr-3.1.3 / src / set_f.c
blobb6c9f5f12d21a68de6494af7bdf4960fce1d38dc
1 /* mpfr_set_f -- set a MPFR number from a GNU MPF number
3 Copyright 1999-2015 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 #define MPFR_NEED_LONGLONG_H
24 #include "mpfr-impl.h"
26 int
27 mpfr_set_f (mpfr_ptr y, mpf_srcptr x, mpfr_rnd_t rnd_mode)
29 mp_limb_t *my, *mx, *tmp;
30 unsigned long cnt, sx, sy;
31 int inexact, carry = 0;
32 MPFR_TMP_DECL(marker);
34 sx = ABS(SIZ(x)); /* number of limbs of the mantissa of x */
36 if (sx == 0) /* x is zero */
38 MPFR_SET_ZERO(y);
39 MPFR_SET_POS(y);
40 return 0; /* 0 is exact */
43 if (SIZ(x) * MPFR_FROM_SIGN_TO_INT(MPFR_SIGN(y)) < 0)
44 MPFR_CHANGE_SIGN (y);
46 sy = MPFR_LIMB_SIZE (y);
47 my = MPFR_MANT(y);
48 mx = PTR(x);
50 count_leading_zeros(cnt, mx[sx - 1]);
52 if (sy <= sx) /* we may have to round even when sy = sx */
54 unsigned long xprec = sx * GMP_NUMB_BITS;
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);
70 else
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;
81 /* warning: EXP(x) * GMP_NUMB_BITS may exceed the maximal exponent */
82 if (EXP(x) > 1 + (__gmpfr_emax - 1) / GMP_NUMB_BITS)
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));
92 else
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;
98 return mpfr_check_range (y, inexact, rnd_mode);