1 /* mpfr_set_d -- convert a machine double precision float to
2 a multiple precision floating-point number
4 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5 Contributed by the Arenaire and Cacao projects, INRIA.
7 This file is part of the GNU MPFR Library.
9 The GNU MPFR Library is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or (at your
12 option) any later version.
14 The GNU MPFR Library is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with the GNU MPFR Library; see the file COPYING.LIB. If not, write to
21 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
22 MA 02110-1301, USA. */
24 #include <string.h> /* For memcmp if _GMP_IEEE_FLOAT == 0 */
25 #include <float.h> /* For DOUBLE_ISINF and DOUBLE_ISNAN */
27 #define MPFR_NEED_LONGLONG_H
28 #include "mpfr-impl.h"
30 /* extracts the bits of d in rp[0..n-1] where n=ceil(53/BITS_PER_MP_LIMB).
31 Assumes d is neither 0 nor NaN nor Inf. */
33 __gmpfr_extract_double (mp_ptr rp
, double d
)
34 /* e=0 iff BITS_PER_MP_LIMB=32 and rp has only one limb */
38 #if BITS_PER_MP_LIMB == 32
43 1. Should handle Inf and NaN in IEEE specific code.
44 2. Handle Inf and NaN also in default code, to avoid hangs.
45 3. Generalize to handle all BITS_PER_MP_LIMB.
46 4. This lits is incomplete and misspelled.
49 MPFR_ASSERTD(!DOUBLE_ISNAN(d
));
50 MPFR_ASSERTD(!DOUBLE_ISINF(d
));
51 MPFR_ASSERTD(d
!= 0.0);
56 union ieee_double_extract x
;
62 #if BITS_PER_MP_LIMB >= 64
63 manl
= ((MPFR_LIMB_ONE
<< 63)
64 | ((mp_limb_t
) x
.s
.manh
<< 43) | ((mp_limb_t
) x
.s
.manl
<< 11));
66 manh
= (MPFR_LIMB_ONE
<< 31) | (x
.s
.manh
<< 11) | (x
.s
.manl
>> 21);
67 manl
= x
.s
.manl
<< 11;
70 else /* denormalized number */
72 #if BITS_PER_MP_LIMB >= 64
73 manl
= ((mp_limb_t
) x
.s
.manh
<< 43) | ((mp_limb_t
) x
.s
.manl
<< 11);
75 manh
= (x
.s
.manh
<< 11) /* high 21 bits */
76 | (x
.s
.manl
>> 21); /* middle 11 bits */
77 manl
= x
.s
.manl
<< 11; /* low 21 bits */
87 #else /* _GMP_IEEE_FLOATS */
90 /* Unknown (or known to be non-IEEE) double format. */
94 MPFR_ASSERTN (d
* 0.5 != d
);
108 while (d
< (1.0 / 65536.0))
120 d
*= MP_BASE_AS_DOUBLE
;
121 #if BITS_PER_MP_LIMB >= 64
124 manh
= (mp_limb_t
) d
;
125 manl
= (mp_limb_t
) ((d
- manh
) * MP_BASE_AS_DOUBLE
);
129 #endif /* _GMP_IEEE_FLOATS */
131 #if BITS_PER_MP_LIMB >= 64
141 /* End of part included from gmp-2.0.2 */
144 mpfr_set_d (mpfr_ptr r
, double d
, mp_rnd_t rnd_mode
)
150 mp_limb_t tmpmant
[MPFR_LIMBS_PER_DOUBLE
];
151 MPFR_SAVE_EXPO_DECL (expo
);
155 if (MPFR_UNLIKELY(DOUBLE_ISNAN(d
)))
160 else if (MPFR_UNLIKELY(d
== 0))
163 union ieee_double_extract x
;
166 /* set correct sign */
172 #else /* _GMP_IEEE_FLOATS */
175 /* This is to get the sign of zero on non-IEEE hardware
176 Some systems support +0.0, -0.0 and unsigned zero.
177 We can't use d==+0.0 since it should be always true,
178 so we check that the memory representation of d is the
179 same than +0.0. etc */
180 /* FIXME: consider the case where +0.0 or -0.0 may have several
182 double poszero
= +0.0, negzero
= DBL_NEG_ZERO
;
183 if (memcmp(&d
, &poszero
, sizeof(double)) == 0)
185 else if (memcmp(&d
, &negzero
, sizeof(double)) == 0)
191 return 0; /* 0 is exact */
193 else if (MPFR_UNLIKELY(DOUBLE_ISINF(d
)))
200 return 0; /* infinity is exact */
203 /* now d is neither 0, nor NaN nor Inf */
205 MPFR_SAVE_EXPO_MARK (expo
);
207 /* warning: don't use tmp=r here, even if SIZE(r) >= MPFR_LIMBS_PER_DOUBLE,
208 since PREC(r) may be different from PREC(tmp), and then both variables
209 would have same precision in the mpfr_set4 call below. */
210 MPFR_MANT(tmp
) = tmpmant
;
211 MPFR_PREC(tmp
) = IEEE_DBL_MANT_DIG
;
213 signd
= (d
< 0) ? MPFR_SIGN_NEG
: MPFR_SIGN_POS
;
216 /* don't use MPFR_SET_EXP here since the exponent may be out of range */
217 MPFR_EXP(tmp
) = __gmpfr_extract_double (tmpmant
, d
);
220 /* Failed assertion if the stored value is 0 (e.g., if the exponent range
221 has been reduced at the wrong moment and an underflow to 0 occurred).
222 Probably a bug in the C implementation if this happens. */
224 while (tmpmant
[i
] == 0)
227 MPFR_ASSERTN(i
< MPFR_LIMBS_PER_DOUBLE
);
231 /* determine the index i-1 of the most significant non-zero limb
232 and the number k of zero high limbs */
233 i
= MPFR_LIMBS_PER_DOUBLE
;
234 MPN_NORMALIZE_NOT_ZERO(tmpmant
, i
);
235 k
= MPFR_LIMBS_PER_DOUBLE
- i
;
237 count_leading_zeros (cnt
, tmpmant
[i
- 1]);
239 if (MPFR_LIKELY(cnt
!= 0))
240 mpn_lshift (tmpmant
+ k
, tmpmant
, i
, cnt
);
242 MPN_COPY (tmpmant
+ k
, tmpmant
, i
);
244 if (MPFR_UNLIKELY(k
!= 0))
245 MPN_ZERO (tmpmant
, k
);
247 /* don't use MPFR_SET_EXP here since the exponent may be out of range */
248 MPFR_EXP(tmp
) -= (mp_exp_t
) (cnt
+ k
* BITS_PER_MP_LIMB
);
250 /* tmp is exact since PREC(tmp)=53 */
251 inexact
= mpfr_set4 (r
, tmp
, rnd_mode
, signd
);
253 MPFR_SAVE_EXPO_FREE (expo
);
254 return mpfr_check_range (r
, inexact
, rnd_mode
);