1
1
/* double mpz_get_d (mpz_t src) -- Return the double approximation to SRC.
3
Copyright 1996, 1997, 2000, 2001, 2002 Free Software Foundation, Inc.
3
Copyright 1996, 1997, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
5
5
This file is part of the GNU MP Library.
17
17
You should have received a copy of the GNU Lesser General Public License
18
18
along with the GNU MP Library; see the file COPYING.LIB. If not, write to
19
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20
MA 02111-1307, USA. */
19
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
MA 02110-1301, USA. */
23
23
#include "gmp-impl.h"
27
/* FIXME: Would prefer to inline this on all compilers, not just those with
30
mpn_zero_p (mp_srcptr p, mp_size_t n)
34
for (i = 0; i < n; i++)
43
mpz_get_d (mpz_srcptr src)
26
mpz_get_d (mpz_srcptr z)
31
if (UNLIKELY (size == 0))
66
res = MP_BASE_AS_DOUBLE * qp[size - 1] + qp[size - 2];
70
count_leading_zeros (cnt, qp[size - 1]);
73
#if BITS_PER_MP_LIMB == 32
81
hz = ((qp[size - 1] << cnt) | (qp[size - 2] >> GMP_NUMB_BITS - cnt)) & GMP_NUMB_MASK;
82
lz = ((qp[size - 2] << cnt) | (qp[size - 3] >> GMP_NUMB_BITS - cnt)) & GMP_NUMB_MASK;
85
/* Take bits from less significant limbs, but only if they may affect
87
if ((lz & 0x7ff) == 0x400)
90
lz += (((qp[size - 3] << cnt) & GMP_NUMB_MASK) != 0
91
|| ! mpn_zero_p (qp, size - 3));
93
lz += (! mpn_zero_p (qp, size - 2));
96
res = MP_BASE_AS_DOUBLE * hz + lz;
97
res = __gmp_scale2 (res, (size - 2) * GMP_NUMB_BITS - cnt);
99
#if BITS_PER_MP_LIMB == 64
103
hz = ((qp[size - 1] << cnt) | (qp[size - 2] >> GMP_NUMB_BITS - cnt)) & GMP_NUMB_MASK;
105
if ((hz & 0x7ff) == 0x400)
108
hz += (((qp[size - 2] << cnt) & GMP_NUMB_MASK) != 0
109
|| ! mpn_zero_p (qp, size - 2));
111
hz += (! mpn_zero_p (qp, size - 1));
115
res = __gmp_scale2 (res, (size - 1) * GMP_NUMB_BITS - cnt);
119
return negative ? -res : res;
34
return mpn_get_d (PTR (z), ABS (size), size, 0L);