Merge branch 'vendor/TEXINFO'
[dragonfly.git] / contrib / mpfr / get_f.c
1 /* mpfr_get_f -- convert a MPFR number to a GNU MPF number
2
3 Copyright 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by the Arenaire and Cacao projects, INRIA.
5
6 This file is part of the GNU MPFR Library.
7
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.
12
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.
17
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. */
22
23 #include "mpfr-impl.h"
24
25 /* return value is 0 iff no error occurred in the conversion
26    (1 for NaN, +Inf, -Inf that have no equivalent in mpf)
27 */
28 int
29 mpfr_get_f (mpf_ptr x, mpfr_srcptr y, mp_rnd_t rnd_mode)
30 {
31   mp_size_t sx, sy;
32   mp_prec_t precx, precy;
33   mp_limb_t *xp;
34   int sh;
35
36   if (MPFR_UNLIKELY(MPFR_IS_SINGULAR(y)))
37     {
38       if (MPFR_IS_ZERO(y))
39         {
40           mpf_set_ui (x, 0);
41           return 0;
42         }
43       else /* NaN or Inf */
44         return 1;
45     }
46
47   sx = PREC(x); /* number of limbs of the mantissa of x */
48
49   precy = MPFR_PREC(y);
50   precx = (mp_prec_t) sx * BITS_PER_MP_LIMB;
51   sy = MPFR_LIMB_SIZE (y);
52
53   xp = PTR (x);
54
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 */
61     {
62       mp_size_t ds;
63
64       MPFR_ASSERTN (sx >= sy);
65       ds = sx - sy;
66
67       if (sh != 0)
68         {
69           mp_limb_t out;
70           out = mpn_rshift (xp + ds, MPFR_MANT(y), sy, sh);
71           MPFR_ASSERTN (ds > 0 || out == 0);
72           if (ds > 0)
73             xp[--ds] = out;
74         }
75       else
76         MPN_COPY (xp + ds, MPFR_MANT (y), sy);
77       if (ds > 0)
78         MPN_ZERO (xp, ds);
79       EXP(x) = (MPFR_GET_EXP(y) + sh) / BITS_PER_MP_LIMB;
80     }
81   else /* we have to round to precx - sh bits */
82     {
83       mpfr_t z;
84       mp_size_t sz, ds;
85
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);
95       ds = sx - sz;
96       MPFR_ASSERTD (sh >= 0 && ds <= 1);
97       if (sh != 0)
98         {
99           mp_limb_t out;
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);
104         }
105       else
106         MPN_COPY (xp + ds, MPFR_MANT(z), sz);
107       if (ds != 0)
108         xp[0] = 0;
109       EXP(x) = (MPFR_GET_EXP(z) + sh) / BITS_PER_MP_LIMB;
110       mpfr_clear (z);
111     }
112
113   /* set size and sign */
114   SIZ(x) = (MPFR_FROM_SIGN_TO_INT(MPFR_SIGN(y)) < 0) ? -sx : sx;
115
116   return 0;
117 }