1 /* Copyright (C) 1995,1996,1997,1998,1999,2002,2003,2006
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 /* Convert a `long double' in IBM extended format to a multi-precision
29 integer representing the significand scaled up by its number of
30 bits (106 for long double) and an integral power of two (MPN
34 __mpn_extract_long_double (mp_ptr res_ptr
, mp_size_t size
,
35 int *expt
, int *is_neg
,
38 union ibm_extended_long_double u
;
39 unsigned long long hi
, lo
;
43 *is_neg
= u
.ieee
.negative
;
44 *expt
= (int) u
.ieee
.exponent
- IBM_EXTENDED_LONG_DOUBLE_BIAS
;
46 lo
= ((long long) u
.ieee
.mantissa2
<< 32) | u
.ieee
.mantissa3
;
47 hi
= ((long long) u
.ieee
.mantissa0
<< 32) | u
.ieee
.mantissa1
;
48 /* If the lower double is not a denomal or zero then set the hidden
50 if (u
.ieee
.exponent2
> 0)
54 /* The lower double is normalized separately from the upper. We may
55 need to adjust the lower manitissa to reflect this. */
56 ediff
= u
.ieee
.exponent
- u
.ieee
.exponent2
;
58 lo
= lo
>> (ediff
-53);
60 /* The high double may be rounded and the low double reflects the
61 difference between the long double and the rounded high double
62 value. This is indicated by a differnce between the signs of the
63 high and low doubles. */
64 if ((u
.ieee
.negative
!= u
.ieee
.negative2
)
65 && ((u
.ieee
.exponent2
!= 0) && (lo
!= 0L)))
67 lo
= (1ULL << 53) - lo
;
70 /* we have a borrow from the hidden bit, so shift left 1. */
71 hi
= 0x0ffffffffffffeLL
| (lo
>> 51);
72 lo
= 0x1fffffffffffffLL
& (lo
<< 1);
78 #if BITS_PER_MP_LIMB == 32
79 /* Combine the mantissas to be contiguous. */
81 res_ptr
[1] = (hi
<< (53 - 32)) | (lo
>> 32);
82 res_ptr
[2] = hi
>> 11;
83 res_ptr
[3] = hi
>> (32 + 11);
85 #elif BITS_PER_MP_LIMB == 64
86 /* Combine the two mantissas to be contiguous. */
87 res_ptr
[0] = (hi
<< 53) | lo
;
88 res_ptr
[1] = hi
>> 11;
91 #error "mp_limb size " BITS_PER_MP_LIMB "not accounted for"
93 /* The format does not fill the last limb. There are some zeros. */
94 #define NUM_LEADING_ZEROS (BITS_PER_MP_LIMB \
95 - (LDBL_MANT_DIG - ((N - 1) * BITS_PER_MP_LIMB)))
97 if (u
.ieee
.exponent
== 0)
99 /* A biased exponent of zero is a special case.
100 Either it is a zero or it is a denormal number. */
101 if (res_ptr
[0] == 0 && res_ptr
[1] == 0
102 && res_ptr
[N
- 2] == 0 && res_ptr
[N
- 1] == 0) /* Assumes N<=4. */
107 /* It is a denormal number, meaning it has no implicit leading
108 one bit, and its exponent is in fact the format minimum. */
112 if (res_ptr
[N
- 1] != 0)
114 count_leading_zeros (cnt
, res_ptr
[N
- 1]);
115 cnt
-= NUM_LEADING_ZEROS
;
116 res_ptr
[N
- 1] = res_ptr
[N
- 1] << cnt
117 | (res_ptr
[0] >> (BITS_PER_MP_LIMB
- cnt
));
119 *expt
= LDBL_MIN_EXP
- 1 - cnt
;
123 count_leading_zeros (cnt
, res_ptr
[0]);
124 if (cnt
>= NUM_LEADING_ZEROS
)
126 res_ptr
[N
- 1] = res_ptr
[0] << (cnt
- NUM_LEADING_ZEROS
);
131 res_ptr
[N
- 1] = res_ptr
[0] >> (NUM_LEADING_ZEROS
- cnt
);
132 res_ptr
[0] <<= BITS_PER_MP_LIMB
- (NUM_LEADING_ZEROS
- cnt
);
134 *expt
= LDBL_MIN_EXP
- 1
135 - (BITS_PER_MP_LIMB
- NUM_LEADING_ZEROS
) - cnt
;
140 for (j
= N
- 1; j
> 0; j
--)
144 count_leading_zeros (cnt
, res_ptr
[j
]);
145 cnt
-= NUM_LEADING_ZEROS
;
149 cnt
+= BITS_PER_MP_LIMB
;
153 for (k
= N
- 1; k
>= l
; k
--)
154 res_ptr
[k
] = res_ptr
[k
-l
];
157 for (k
= N
- 1; k
> l
; k
--)
158 res_ptr
[k
] = res_ptr
[k
-l
] << cnt
159 | res_ptr
[k
-l
-1] >> (BITS_PER_MP_LIMB
- cnt
);
160 res_ptr
[k
--] = res_ptr
[0] << cnt
;
165 *expt
= LDBL_MIN_EXP
- 1 - l
* BITS_PER_MP_LIMB
- cnt
;
170 /* Add the implicit leading one bit for a normalized number. */
171 res_ptr
[N
- 1] |= (mp_limb_t
) 1 << (LDBL_MANT_DIG
- 1
172 - ((N
- 1) * BITS_PER_MP_LIMB
));