1 /* Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
18
19 #if 0 /* vstr */
20 #include "gmp.h"
21 #include "gmp-impl.h"
22 #include "longlong.h"
23 #include <ieee754.h>
24 #include <float.h>
25 #include <stdlib.h>
26
27 /* Convert a `double' in IEEE754 standard double-precision format to a
28 multi-precision integer representing the significand scaled up by its
29 number of bits (52 for double) and an integral power of two (MPN frexp). */
30
31 #endif /* vstr */
32 static
33 mp_size_t
__mpn_extract_double(mp_ptr res_ptr,mp_size_t size,int * expt,int * is_neg,double value)34 __mpn_extract_double (mp_ptr res_ptr,
35 mp_size_t size __attribute__((unused)),
36 int *expt, int *is_neg,
37 double value)
38 {
39 union ieee754_double u;
40 u.d = value;
41
42 *is_neg = u.ieee.negative;
43 *expt = (int) u.ieee.exponent - IEEE754_DOUBLE_BIAS;
44
45 #if BITS_PER_MP_LIMB == 32
46 res_ptr[0] = u.ieee.mantissa1; /* Low-order 32 bits of fraction. */
47 res_ptr[1] = u.ieee.mantissa0; /* High-order 20 bits. */
48 #define N 2
49 #elif BITS_PER_MP_LIMB == 64
50 /* Hopefully the compiler will combine the two bitfield extracts
51 and this composition into just the original quadword extract. */
52 res_ptr[0] = ((mp_limb_t) u.ieee.mantissa0 << 32) | u.ieee.mantissa1;
53 #define N 1
54 #else
55 #error "mp_limb size " BITS_PER_MP_LIMB "not accounted for"
56 #endif
57 /* The format does not fill the last limb. There are some zeros. */
58 #define NUM_LEADING_ZEROS (BITS_PER_MP_LIMB \
59 - (DBL_MANT_DIG - ((N - 1) * BITS_PER_MP_LIMB)))
60
61 if (u.ieee.exponent == 0)
62 {
63 /* A biased exponent of zero is a special case.
64 Either it is a zero or it is a denormal number. */
65 if (res_ptr[0] == 0 && res_ptr[N - 1] == 0) /* Assumes N<=2. */
66 /* It's zero. */
67 *expt = 0;
68 else
69 {
70 /* It is a denormal number, meaning it has no implicit leading
71 one bit, and its exponent is in fact the format minimum. */
72 int cnt;
73
74 if (res_ptr[N - 1] != 0)
75 {
76 count_leading_zeros (cnt, res_ptr[N - 1]);
77 cnt -= NUM_LEADING_ZEROS;
78 #if N == 2
79 res_ptr[N - 1] = res_ptr[1] << cnt
80 | (N - 1)
81 * (res_ptr[0] >> (BITS_PER_MP_LIMB - cnt));
82 res_ptr[0] <<= cnt;
83 #else
84 res_ptr[N - 1] <<= cnt;
85 #endif
86 *expt = DBL_MIN_EXP - 1 - cnt;
87 }
88 else
89 {
90 count_leading_zeros (cnt, res_ptr[0]);
91 if (cnt >= NUM_LEADING_ZEROS)
92 {
93 res_ptr[N - 1] = res_ptr[0] << (cnt - NUM_LEADING_ZEROS);
94 res_ptr[0] = 0;
95 }
96 else
97 {
98 res_ptr[N - 1] = res_ptr[0] >> (NUM_LEADING_ZEROS - cnt);
99 res_ptr[0] <<= BITS_PER_MP_LIMB - (NUM_LEADING_ZEROS - cnt);
100 }
101 *expt = DBL_MIN_EXP - 1
102 - (BITS_PER_MP_LIMB - NUM_LEADING_ZEROS) - cnt;
103 }
104 }
105 }
106 else
107 /* Add the implicit leading one bit for a normalized number. */
108 res_ptr[N - 1] |= (mp_limb_t) 1 << (DBL_MANT_DIG - 1
109 - ((N - 1) * BITS_PER_MP_LIMB));
110
111 return N;
112 }
113