2.9
[glibc/nacl-glibc.git] / sysdeps / ieee754 / ldbl-128ibm / ldbl2mpn.c
blob18a2e671ad3effb03464d09baa8a9840d1d1d291
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
18 02111-1307 USA. */
20 #include "gmp.h"
21 #include "gmp-impl.h"
22 #include "longlong.h"
23 #include <ieee754.h>
24 #include <float.h>
25 #include <math.h>
26 #include <stdlib.h>
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
31 frexpl). */
33 mp_size_t
34 __mpn_extract_long_double (mp_ptr res_ptr, mp_size_t size,
35 int *expt, int *is_neg,
36 long double value)
38 union ibm_extended_long_double u;
39 unsigned long long hi, lo;
40 int ediff;
41 u.d = value;
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
49 53rd bit. */
50 if (u.ieee.exponent2 > 0)
52 lo |= 1LL << 52;
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;
57 if (ediff > 53)
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;
68 if (hi == 0LL)
70 /* we have a borrow from the hidden bit, so shift left 1. */
71 hi = 0x0ffffffffffffeLL | (lo >> 51);
72 lo = 0x1fffffffffffffLL & (lo << 1);
73 (*expt)--;
75 else
76 hi--;
78 #if BITS_PER_MP_LIMB == 32
79 /* Combine the mantissas to be contiguous. */
80 res_ptr[0] = lo;
81 res_ptr[1] = (hi << (53 - 32)) | (lo >> 32);
82 res_ptr[2] = hi >> 11;
83 res_ptr[3] = hi >> (32 + 11);
84 #define N 4
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;
89 #define N 2
90 #else
91 #error "mp_limb size " BITS_PER_MP_LIMB "not accounted for"
92 #endif
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. */
103 /* It's zero. */
104 *expt = 0;
105 else
107 /* It is a denormal number, meaning it has no implicit leading
108 one bit, and its exponent is in fact the format minimum. */
109 int cnt;
111 #if N == 2
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));
118 res_ptr[0] <<= cnt;
119 *expt = LDBL_MIN_EXP - 1 - cnt;
121 else
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);
127 res_ptr[0] = 0;
129 else
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;
137 #else
138 int j, k, l;
140 for (j = N - 1; j > 0; j--)
141 if (res_ptr[j] != 0)
142 break;
144 count_leading_zeros (cnt, res_ptr[j]);
145 cnt -= NUM_LEADING_ZEROS;
146 l = N - 1 - j;
147 if (cnt < 0)
149 cnt += BITS_PER_MP_LIMB;
150 l--;
152 if (!cnt)
153 for (k = N - 1; k >= l; k--)
154 res_ptr[k] = res_ptr[k-l];
155 else
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;
163 for (; k >= 0; k--)
164 res_ptr[k] = 0;
165 *expt = LDBL_MIN_EXP - 1 - l * BITS_PER_MP_LIMB - cnt;
166 #endif
169 else
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));
174 return N;