1 /* double mpz_get_d (mpz_t src) -- Return the double approximation to SRC.
3 Copyright (C) 1996, 1997, 2000 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or (at your
10 option) any later version.
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser General Public License
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. */
29 mpn_zero_p (mp_ptr p, mp_size_t n)
38 for (i = 0; i < n; i++)
50 mpz_get_d (mpz_srcptr src)
77 res = MP_BASE_AS_DOUBLE * qp[size - 1] + qp[size - 2];
81 count_leading_zeros (cnt, qp[size - 1]);
83 #if BITS_PER_MP_LIMB == 32
91 hz = (qp[size - 1] << cnt) | (qp[size - 2] >> BITS_PER_MP_LIMB - cnt);
92 lz = (qp[size - 2] << cnt) | (qp[size - 3] >> BITS_PER_MP_LIMB - cnt);
95 /* Take bits from less significant limbs, but only if they may affect
97 if ((lz & 0x7ff) == 0x400)
100 lz += ((qp[size - 3] << cnt) != 0 || ! mpn_zero_p (qp, size - 3));
102 lz += (! mpn_zero_p (qp, size - 2));
105 res = MP_BASE_AS_DOUBLE * hz + lz;
106 res = __gmp_scale2 (res, (size - 2) * BITS_PER_MP_LIMB - cnt);
108 #if BITS_PER_MP_LIMB == 64
112 hz = (qp[size - 1] << cnt) | (qp[size - 2] >> BITS_PER_MP_LIMB - cnt);
114 if ((hz & 0x7ff) == 0x400)
117 hz += ((qp[size - 2] << cnt) != 0 || ! mpn_zero_p (qp, size - 2));
119 hz += (! mpn_zero_p (qp, size - 1));
123 res = __gmp_scale2 (res, (size - 1) * BITS_PER_MP_LIMB - cnt);
127 return negative ? -res : res;