1
/* mpq_set_d(mpq_t q, double d) -- Set q to d without rounding.
3
Copyright 2000, 2002 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. */
26
#if BITS_PER_MP_LIMB != 32 && BITS_PER_MP_LIMB != 64
31
mpq_set_d (mpq_ptr dest, double d)
35
mp_limb_t tp[LIMBS_PER_DOUBLE];
43
exp = __gmp_extract_double (tp, d);
45
/* There are two main version of the conversion. The `then' arm handles
46
things that have a fractional part, while the `else' part handles
48
#if BITS_PER_MP_LIMB == 32
49
if (exp <= 1 || (exp == 2 && tp[0] != 0))
56
SIZ(&(dest->_mp_num)) = 0;
57
SIZ(&(dest->_mp_den)) = 1;
58
PTR(&(dest->_mp_den))[0] = 1;
63
if (dest->_mp_num._mp_alloc < 3)
64
_mpz_realloc (&(dest->_mp_num), 3);
65
np = PTR(&(dest->_mp_num));
66
#if BITS_PER_MP_LIMB == 32
67
if ((tp[0] | tp[1]) == 0)
68
np[0] = tp[2], nn = 1;
70
np[1] = tp[2], np[0] = tp[1], nn = 2;
72
np[2] = tp[2], np[1] = tp[1], np[0] = tp[0], nn = 3;
75
np[0] = tp[1], nn = 1;
77
np[1] = tp[1], np[0] = tp[0], nn = 2;
80
if (dest->_mp_den._mp_alloc < dn)
81
_mpz_realloc (&(dest->_mp_den), dn);
82
dp = PTR(&(dest->_mp_den));
83
MPN_ZERO (dp, dn - 1);
85
count_trailing_zeros (c, np[0] | dp[0]);
88
mpn_rshift (np, np, nn, c);
89
nn -= np[nn - 1] == 0;
90
mpn_rshift (dp, dp, dn, c);
91
dn -= dp[dn - 1] == 0;
93
SIZ(&(dest->_mp_den)) = dn;
94
SIZ(&(dest->_mp_num)) = negative ? -nn : nn;
99
if (dest->_mp_num._mp_alloc < nn)
100
_mpz_realloc (&(dest->_mp_num), nn);
101
np = PTR(&(dest->_mp_num));
102
#if BITS_PER_MP_LIMB == 32
106
MPN_ZERO (np, nn - 3);
110
np[2] = tp[2], np[1] = tp[1], np[0] = tp[0];
113
np[1] = tp[2], np[0] = tp[1];
120
MPN_ZERO (np, nn - 2);
124
np[1] = tp[1], np[0] = tp[0];
128
dp = PTR(&(dest->_mp_den));
130
SIZ(&(dest->_mp_den)) = 1;
131
SIZ(&(dest->_mp_num)) = negative ? -nn : nn;