2.9
[glibc/nacl-glibc.git] / sysdeps / ieee754 / ldbl-128 / ldbl2mpn.c
bloba49fac674295f37a3afa5ea9d913a567c771900f
1 /* Copyright (C) 1995,1996,1997,1998,1999,2002,2003
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 IEEE854 quad-precision format to a
29 multi-precision integer representing the significand scaled up by its
30 number of bits (113 for long double) and an integral power of two
31 (MPN 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 ieee854_long_double u;
39 u.d = value;
41 *is_neg = u.ieee.negative;
42 *expt = (int) u.ieee.exponent - IEEE854_LONG_DOUBLE_BIAS;
44 #if BITS_PER_MP_LIMB == 32
45 res_ptr[0] = u.ieee.mantissa3; /* Low-order 32 bits of fraction. */
46 res_ptr[1] = u.ieee.mantissa2;
47 res_ptr[2] = u.ieee.mantissa1;
48 res_ptr[3] = u.ieee.mantissa0; /* High-order 32 bits. */
49 #define N 4
50 #elif BITS_PER_MP_LIMB == 64
51 /* Hopefully the compiler will combine the two bitfield extracts
52 and this composition into just the original quadword extract. */
53 res_ptr[0] = ((mp_limb_t) u.ieee.mantissa2 << 32) | u.ieee.mantissa3;
54 res_ptr[1] = ((mp_limb_t) u.ieee.mantissa0 << 32) | u.ieee.mantissa1;
55 #define N 2
56 #else
57 #error "mp_limb size " BITS_PER_MP_LIMB "not accounted for"
58 #endif
59 /* The format does not fill the last limb. There are some zeros. */
60 #define NUM_LEADING_ZEROS (BITS_PER_MP_LIMB \
61 - (LDBL_MANT_DIG - ((N - 1) * BITS_PER_MP_LIMB)))
63 if (u.ieee.exponent == 0)
65 /* A biased exponent of zero is a special case.
66 Either it is a zero or it is a denormal number. */
67 if (res_ptr[0] == 0 && res_ptr[1] == 0
68 && res_ptr[N - 2] == 0 && res_ptr[N - 1] == 0) /* Assumes N<=4. */
69 /* It's zero. */
70 *expt = 0;
71 else
73 /* It is a denormal number, meaning it has no implicit leading
74 one bit, and its exponent is in fact the format minimum. */
75 int cnt;
77 #if N == 2
78 if (res_ptr[N - 1] != 0)
80 count_leading_zeros (cnt, res_ptr[N - 1]);
81 cnt -= NUM_LEADING_ZEROS;
82 res_ptr[N - 1] = res_ptr[N - 1] << cnt
83 | (res_ptr[0] >> (BITS_PER_MP_LIMB - cnt));
84 res_ptr[0] <<= cnt;
85 *expt = LDBL_MIN_EXP - 1 - cnt;
87 else
89 count_leading_zeros (cnt, res_ptr[0]);
90 if (cnt >= NUM_LEADING_ZEROS)
92 res_ptr[N - 1] = res_ptr[0] << (cnt - NUM_LEADING_ZEROS);
93 res_ptr[0] = 0;
95 else
97 res_ptr[N - 1] = res_ptr[0] >> (NUM_LEADING_ZEROS - cnt);
98 res_ptr[0] <<= BITS_PER_MP_LIMB - (NUM_LEADING_ZEROS - cnt);
100 *expt = LDBL_MIN_EXP - 1
101 - (BITS_PER_MP_LIMB - NUM_LEADING_ZEROS) - cnt;
103 #else
104 int j, k, l;
106 for (j = N - 1; j > 0; j--)
107 if (res_ptr[j] != 0)
108 break;
110 count_leading_zeros (cnt, res_ptr[j]);
111 cnt -= NUM_LEADING_ZEROS;
112 l = N - 1 - j;
113 if (cnt < 0)
115 cnt += BITS_PER_MP_LIMB;
116 l--;
118 if (!cnt)
119 for (k = N - 1; k >= l; k--)
120 res_ptr[k] = res_ptr[k-l];
121 else
123 for (k = N - 1; k > l; k--)
124 res_ptr[k] = res_ptr[k-l] << cnt
125 | res_ptr[k-l-1] >> (BITS_PER_MP_LIMB - cnt);
126 res_ptr[k--] = res_ptr[0] << cnt;
129 for (; k >= 0; k--)
130 res_ptr[k] = 0;
131 *expt = LDBL_MIN_EXP - 1 - l * BITS_PER_MP_LIMB - cnt;
132 #endif
135 else
136 /* Add the implicit leading one bit for a normalized number. */
137 res_ptr[N - 1] |= (mp_limb_t) 1 << (LDBL_MANT_DIG - 1
138 - ((N - 1) * BITS_PER_MP_LIMB));
140 return N;