1 /* __gmp_extract_double -- convert from double to array of mp_limb_t.
3 Copyright (C) 1996, 1999, 2000 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 /* Extract a non-negative double in d. */
37 __gmp_extract_double (mp_ptr rp, double d)
39 __gmp_extract_double (rp, d)
50 1. Should handle Inf and NaN in IEEE specific code.
51 2. Handle Inf and NaN also in default code, to avoid hangs.
52 3. Generalize to handle all BITS_PER_MP_LIMB >= 32.
53 4. This lits is incomplete and misspelled.
60 #if BITS_PER_MP_LIMB == 32
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_MP_LIMB == 64
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 manh = ((mp_limb_t) 1 << 31) | (x.s.manh << 11) | (x.s.manl >> 21);
92 manl = x.s.manl << 11;
95 /* Denormalized number. Don't try to be clever about this,
96 since it is not an important case to make fast. */
100 manh = (manh << 1) | (manl >> 31);
104 while ((mp_limb_signed_t) manh >= 0);
107 exp -= 1022; /* Remove IEEE bias. */
111 /* Unknown (or known to be non-IEEE) double format. */
120 d *= (1.0 / 65536.0);
131 while (d < (1.0 / 65536.0))
143 d *= MP_BASE_AS_DOUBLE;
144 #if BITS_PER_MP_LIMB == 64
148 manl = (d - manh) * MP_BASE_AS_DOUBLE;
153 sc = (unsigned) exp % BITS_PER_MP_LIMB;
155 /* We add something here to get rounding right. */
156 exp = (exp + 2048) / BITS_PER_MP_LIMB - 2048 / BITS_PER_MP_LIMB + 1;
158 #if BITS_PER_MP_LIMB == 64
161 rp[1] = manl >> (BITS_PER_MP_LIMB - sc);
173 rp[2] = manh >> (BITS_PER_MP_LIMB - sc);
174 rp[1] = (manl >> (BITS_PER_MP_LIMB - sc)) | (manh << sc);