1 /* __gmp_extract_double -- convert from double to array of mp_limb_t.
3 Copyright 1996, 1999, 2000, 2001, 2002, 2006 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., 51 Franklin Street, Fifth Floor, Boston,
20 MA 02110-1301, USA. */
26 #undef _GMP_IEEE_FLOATS
29 #ifndef _GMP_IEEE_FLOATS
30 #define _GMP_IEEE_FLOATS 0
33 #define BITS_IN_MANTISSA 53
35 /* Extract a non-negative double in d. */
38 __gmp_extract_double (mp_ptr rp, double d)
42 #ifdef _LONG_LONG_LIMB
43 #define BITS_PER_PART 64 /* somewhat bogus */
44 unsigned long long int manl;
46 #define BITS_PER_PART GMP_LIMB_BITS
47 unsigned long int manh, manl;
52 1. Should handle Inf and NaN in IEEE specific code.
53 2. Handle Inf and NaN also in default code, to avoid hangs.
54 3. Generalize to handle all GMP_LIMB_BITS >= 32.
55 4. This lits is incomplete and misspelled.
62 MPN_ZERO (rp, LIMBS_PER_DOUBLE);
68 #if defined (__alpha) && __GNUC__ == 2 && __GNUC_MINOR__ == 8
69 /* Work around alpha-specific bug in GCC 2.8.x. */
72 union ieee_double_extract x;
75 #if BITS_PER_PART == 64 /* generalize this to BITS_PER_PART > BITS_IN_MANTISSA */
76 manl = (((mp_limb_t) 1 << 63)
77 | ((mp_limb_t) x.s.manh << 43) | ((mp_limb_t) x.s.manl << 11));
80 /* Denormalized number. Don't try to be clever about this,
81 since it is not an important case to make fast. */
88 while ((manl & GMP_LIMB_HIGHBIT) == 0);
91 #if BITS_PER_PART == 32
92 manh = ((mp_limb_t) 1 << 31) | (x.s.manh << 11) | (x.s.manl >> 21);
93 manl = x.s.manl << 11;
96 /* Denormalized number. Don't try to be clever about this,
97 since it is not an important case to make fast. */
101 manh = (manh << 1) | (manl >> 31);
105 while ((manh & GMP_LIMB_HIGHBIT) == 0);
108 #if BITS_PER_PART != 32 && BITS_PER_PART != 64
109 You need to generalize the code above to handle this.
111 exp -= 1022; /* Remove IEEE bias. */
115 /* Unknown (or known to be non-IEEE) double format. */
119 ASSERT_ALWAYS (d * 0.5 != d);
123 d *= (1.0 / 65536.0);
134 while (d < (1.0 / 65536.0))
146 d *= (4.0 * ((unsigned long int) 1 << (BITS_PER_PART - 2)));
147 #if BITS_PER_PART == 64
151 manl = (d - manh) * (4.0 * ((unsigned long int) 1 << (BITS_PER_PART - 2)));
156 /* Up until here, we have ignored the actual limb size. Remains
157 to split manh,,manl into an array of LIMBS_PER_DOUBLE limbs.
160 sc = (unsigned) (exp + 64 * GMP_NUMB_BITS) % GMP_NUMB_BITS;
162 /* We add something here to get rounding right. */
163 exp = (exp + 64 * GMP_NUMB_BITS) / GMP_NUMB_BITS - 64 * GMP_NUMB_BITS / GMP_NUMB_BITS + 1;
165 #if LIMBS_PER_DOUBLE == 2
166 #if GMP_NAIL_BITS == 0
169 rp[1] = manl >> (GMP_LIMB_BITS - sc);
179 if (sc > GMP_NAIL_BITS)
181 rp[1] = manl >> (GMP_LIMB_BITS - sc);
182 rp[0] = (manl << (sc - GMP_NAIL_BITS)) & GMP_NUMB_MASK;
188 rp[1] = manl >> GMP_NAIL_BITS;
189 rp[0] = (manl << GMP_NUMB_BITS - GMP_NAIL_BITS) & GMP_NUMB_MASK;
194 rp[1] = manl >> (GMP_LIMB_BITS - sc);
195 rp[0] = (manl >> (GMP_NAIL_BITS - sc)) & GMP_NUMB_MASK;
201 #if LIMBS_PER_DOUBLE == 3
202 #if GMP_NAIL_BITS == 0
205 rp[2] = manh >> (GMP_LIMB_BITS - sc);
206 rp[1] = (manh << sc) | (manl >> (GMP_LIMB_BITS - sc));
217 if (sc > GMP_NAIL_BITS)
219 rp[2] = (manh >> (GMP_LIMB_BITS - sc));
220 rp[1] = ((manh << (sc - GMP_NAIL_BITS)) |
221 (manl >> (GMP_LIMB_BITS - sc + GMP_NAIL_BITS))) & GMP_NUMB_MASK;
222 if (sc >= 2 * GMP_NAIL_BITS)
223 rp[0] = (manl << sc - 2 * GMP_NAIL_BITS) & GMP_NUMB_MASK;
225 rp[0] = manl >> (2 * GMP_NAIL_BITS - sc) & GMP_NUMB_MASK;
231 rp[2] = manh >> GMP_NAIL_BITS;
232 rp[1] = ((manh << GMP_NUMB_BITS - GMP_NAIL_BITS) | (manl >> 2 * GMP_NAIL_BITS)) & GMP_NUMB_MASK;
238 rp[2] = (manh >> (GMP_LIMB_BITS - sc));
239 rp[1] = (manh >> (GMP_NAIL_BITS - sc)) & GMP_NUMB_MASK;
240 rp[0] = ((manh << (GMP_NUMB_BITS - GMP_NAIL_BITS + sc))
241 | (manl >> (GMP_LIMB_BITS - (GMP_NUMB_BITS - GMP_NAIL_BITS + sc)))) & GMP_NUMB_MASK;
247 #if LIMBS_PER_DOUBLE > 3
252 for (i = LIMBS_PER_DOUBLE - 1; i >= 0; i--)
254 rp[i] = manh >> (BITS_PER_ULONG - GMP_NUMB_BITS);
255 manh = ((manh << GMP_NUMB_BITS)
256 | (manl >> (BITS_PER_ULONG - GMP_NUMB_BITS)));
257 manl = manl << GMP_NUMB_BITS;
265 rp[LIMBS_PER_DOUBLE - 1] = (manh >> (GMP_LIMB_BITS - sc));
266 manh = (manh << sc) | (manl >> (GMP_LIMB_BITS - sc));
268 for (i = LIMBS_PER_DOUBLE - 2; i >= 0; i--)
270 rp[i] = manh >> (BITS_PER_ULONG - GMP_NUMB_BITS);
271 manh = ((manh << GMP_NUMB_BITS)
272 | (manl >> (BITS_PER_ULONG - GMP_NUMB_BITS)));
273 manl = manl << GMP_NUMB_BITS;