1
/* __gmp_extract_double -- convert from double to array of mp_limb_t.
3
Copyright 1996, 1999, 2000, 2001, 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
#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 ((mp_limb_signed_t) manl >= 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 ((mp_limb_signed_t) manh >= 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. */
124
d *= (1.0 / 65536.0);
135
while (d < (1.0 / 65536.0))
147
d *= (4.0 * ((unsigned long int) 1 << (BITS_PER_PART - 2)));
148
#if BITS_PER_PART == 64
152
manl = (d - manh) * (4.0 * ((unsigned long int) 1 << (BITS_PER_PART - 2)));
157
/* Up until here, we have ignored the actual limb size. Remains
158
to split manh,,manl into an array of LIMBS_PER_DOUBLE limbs.
161
sc = (unsigned) (exp + 64 * GMP_NUMB_BITS) % GMP_NUMB_BITS;
163
/* We add something here to get rounding right. */
164
exp = (exp + 64 * GMP_NUMB_BITS) / GMP_NUMB_BITS - 64 * GMP_NUMB_BITS / GMP_NUMB_BITS + 1;
166
#if LIMBS_PER_DOUBLE == 2
167
#if GMP_NAIL_BITS == 0
170
rp[1] = manl >> (GMP_LIMB_BITS - sc);
180
if (sc > GMP_NAIL_BITS)
182
rp[1] = manl >> (GMP_LIMB_BITS - sc);
183
rp[0] = (manl << (sc - GMP_NAIL_BITS)) & GMP_NUMB_MASK;
189
rp[1] = manl >> GMP_NAIL_BITS;
190
rp[0] = (manl << GMP_NUMB_BITS - GMP_NAIL_BITS) & GMP_NUMB_MASK;
195
rp[1] = manl >> (GMP_LIMB_BITS - sc);
196
rp[0] = (manl >> (GMP_NAIL_BITS - sc)) & GMP_NUMB_MASK;
202
#if LIMBS_PER_DOUBLE == 3
203
#if GMP_NAIL_BITS == 0
206
rp[2] = manh >> (GMP_LIMB_BITS - sc);
207
rp[1] = (manh << sc) | (manl >> (GMP_LIMB_BITS - sc));
218
if (sc > GMP_NAIL_BITS)
220
rp[2] = (manh >> (GMP_LIMB_BITS - sc));
221
rp[1] = ((manh << (sc - GMP_NAIL_BITS)) |
222
(manl >> (GMP_LIMB_BITS - sc + GMP_NAIL_BITS))) & GMP_NUMB_MASK;
223
if (sc >= 2 * GMP_NAIL_BITS)
224
rp[0] = (manl << sc - 2 * GMP_NAIL_BITS) & GMP_NUMB_MASK;
226
rp[0] = manl >> (2 * GMP_NAIL_BITS - sc) & GMP_NUMB_MASK;
232
rp[2] = manh >> GMP_NAIL_BITS;
233
rp[1] = ((manh << GMP_NUMB_BITS - GMP_NAIL_BITS) | (manl >> 2 * GMP_NAIL_BITS)) & GMP_NUMB_MASK;
239
rp[2] = (manh >> (GMP_LIMB_BITS - sc));
240
rp[1] = (manh >> (GMP_NAIL_BITS - sc)) & GMP_NUMB_MASK;
241
rp[0] = ((manh << (GMP_NUMB_BITS - GMP_NAIL_BITS + sc))
242
| (manl >> (GMP_LIMB_BITS - (GMP_NUMB_BITS - GMP_NAIL_BITS + sc)))) & GMP_NUMB_MASK;
248
#if LIMBS_PER_DOUBLE > 3
249
/* Insert code for splitting manh,,manl into LIMBS_PER_DOUBLE
250
mp_limb_t's at rp. */
253
/* This is not perfect, and would fail for GMP_LIMB_BITS == 16.
254
The ASSERT_ALWAYS should catch the problematic cases. */
255
ASSERT_ALWAYS ((manl << sc) == 0);
256
manl = (manh << sc) | (manl >> (GMP_LIMB_BITS - sc));
257
manh = manh >> (GMP_LIMB_BITS - sc);
261
for (i = LIMBS_PER_DOUBLE - 1; i >= 0; i--)
263
rp[i] = manh >> (BITS_PER_LONGINT - GMP_LIMB_BITS);
264
manh = ((manh << GMP_LIMB_BITS)
265
| (manl >> (BITS_PER_LONGINT - GMP_LIMB_BITS)));
266
manl = manl << GMP_LIMB_BITS;