1 /* mpq_set_d(mpq_t q, double d) -- Set q to d without rounding.
3 Copyright 2000, 2002, 2003, 2012, 2014 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 either:
10 * the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
16 * the GNU General Public License as published by the Free Software
17 Foundation; either version 2 of the License, or (at your option) any
20 or both in parallel, as here.
22 The GNU MP Library is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 You should have received copies of the GNU General Public License and the
28 GNU Lesser General Public License along with the GNU MP Library. If not,
29 see https://www.gnu.org/licenses/. */
34 #include <float.h> /* for DBL_MAX */
41 #if LIMBS_PER_DOUBLE > 4
46 mpq_set_d (mpq_ptr dest
, double d
)
50 mp_limb_t tp
[LIMBS_PER_DOUBLE
];
55 DOUBLE_NAN_INF_ACTION (d
,
56 __gmp_invalid_operation (),
57 __gmp_invalid_operation ());
62 exp
= __gmp_extract_double (tp
, d
);
64 /* There are two main version of the conversion. The `then' arm handles
65 numbers with a fractional part, while the `else' arm handles integers. */
66 #if LIMBS_PER_DOUBLE == 4
67 if (exp
<= 1 || (exp
== 2 && (tp
[0] | tp
[1]) != 0))
69 #if LIMBS_PER_DOUBLE == 3
70 if (exp
<= 1 || (exp
== 2 && tp
[0] != 0))
72 #if LIMBS_PER_DOUBLE == 2
80 PTR(DEN(dest
))[0] = 1;
84 #if LIMBS_PER_DOUBLE == 4
85 np
= MPZ_NEWALLOC (NUM(dest
), 4);
86 if ((tp
[0] | tp
[1] | tp
[2]) == 0)
87 np
[0] = tp
[3], nn
= 1;
88 else if ((tp
[0] | tp
[1]) == 0)
89 np
[1] = tp
[3], np
[0] = tp
[2], nn
= 2;
91 np
[2] = tp
[3], np
[1] = tp
[2], np
[0] = tp
[1], nn
= 3;
93 np
[3] = tp
[3], np
[2] = tp
[2], np
[1] = tp
[1], np
[0] = tp
[0], nn
= 4;
95 #if LIMBS_PER_DOUBLE == 3
96 np
= MPZ_NEWALLOC (NUM(dest
), 3);
97 if ((tp
[0] | tp
[1]) == 0)
98 np
[0] = tp
[2], nn
= 1;
100 np
[1] = tp
[2], np
[0] = tp
[1], nn
= 2;
102 np
[2] = tp
[2], np
[1] = tp
[1], np
[0] = tp
[0], nn
= 3;
104 #if LIMBS_PER_DOUBLE == 2
105 np
= MPZ_NEWALLOC (NUM(dest
), 2);
107 np
[0] = tp
[1], nn
= 1;
109 np
[1] = tp
[1], np
[0] = tp
[0], nn
= 2;
112 ASSERT (dn
> 0); /* -exp >= -1; nn >= 1*/
113 dp
= MPZ_NEWALLOC (DEN(dest
), dn
);
114 MPN_ZERO (dp
, dn
- 1);
116 count_trailing_zeros (c
, np
[0] | dp
[0]);
119 mpn_rshift (np
, np
, nn
, c
);
120 nn
-= np
[nn
- 1] == 0;
122 dp
[dn
- 1] = CNST_LIMB(1) << (GMP_LIMB_BITS
- c
);
129 np
= MPZ_NEWALLOC (NUM(dest
), nn
);
133 MPN_ZERO (np
, nn
- LIMBS_PER_DOUBLE
);
134 np
+= nn
- LIMBS_PER_DOUBLE
;
136 #if LIMBS_PER_DOUBLE == 2
138 np
[1] = tp
[1], np
[0] = tp
[0];
141 #if LIMBS_PER_DOUBLE == 3
143 np
[2] = tp
[2], np
[1] = tp
[1], np
[0] = tp
[0];
146 np
[1] = tp
[2], np
[0] = tp
[1];
149 #if LIMBS_PER_DOUBLE == 4
151 np
[3] = tp
[3], np
[2] = tp
[2], np
[1] = tp
[1], np
[0] = tp
[0];
154 np
[2] = tp
[3], np
[1] = tp
[2], np
[0] = tp
[1];
157 np
[1] = tp
[3], np
[0] = tp
[2];
164 SIZ(NUM(dest
)) = negative
? -nn
: nn
;