1*627f7eb2Smrg /* Copyright (C) 1995,1996,1997,1998,1999,2002,2003
2*627f7eb2Smrg Free Software Foundation, Inc.
3*627f7eb2Smrg This file is part of the GNU C Library.
4*627f7eb2Smrg
5*627f7eb2Smrg The GNU C Library is free software; you can redistribute it and/or
6*627f7eb2Smrg modify it under the terms of the GNU Lesser General Public
7*627f7eb2Smrg License as published by the Free Software Foundation; either
8*627f7eb2Smrg version 2.1 of the License, or (at your option) any later version.
9*627f7eb2Smrg
10*627f7eb2Smrg The GNU C Library is distributed in the hope that it will be useful,
11*627f7eb2Smrg but WITHOUT ANY WARRANTY; without even the implied warranty of
12*627f7eb2Smrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13*627f7eb2Smrg Lesser General Public License for more details.
14*627f7eb2Smrg
15*627f7eb2Smrg You should have received a copy of the GNU Lesser General Public
16*627f7eb2Smrg License along with the GNU C Library; if not, write to the Free
17*627f7eb2Smrg Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18*627f7eb2Smrg 02111-1307 USA. */
19*627f7eb2Smrg
20*627f7eb2Smrg #include <float.h>
21*627f7eb2Smrg #include <math.h>
22*627f7eb2Smrg #include <stdlib.h>
23*627f7eb2Smrg #include "gmp-impl.h"
24*627f7eb2Smrg
25*627f7eb2Smrg /* Convert a `__float128' in IEEE854 quad-precision format to a
26*627f7eb2Smrg multi-precision integer representing the significand scaled up by its
27*627f7eb2Smrg number of bits (113 for long double) and an integral power of two
28*627f7eb2Smrg (MPN frexpl). */
29*627f7eb2Smrg
30*627f7eb2Smrg mp_size_t
mpn_extract_flt128(mp_ptr res_ptr,mp_size_t size,int * expt,int * is_neg,__float128 value)31*627f7eb2Smrg mpn_extract_flt128 (mp_ptr res_ptr, mp_size_t size,
32*627f7eb2Smrg int *expt, int *is_neg,
33*627f7eb2Smrg __float128 value)
34*627f7eb2Smrg {
35*627f7eb2Smrg ieee854_float128 u;
36*627f7eb2Smrg u.value = value;
37*627f7eb2Smrg
38*627f7eb2Smrg *is_neg = u.ieee.negative;
39*627f7eb2Smrg *expt = (int) u.ieee.exponent - IEEE854_FLOAT128_BIAS;
40*627f7eb2Smrg
41*627f7eb2Smrg #if BITS_PER_MP_LIMB == 32
42*627f7eb2Smrg res_ptr[0] = u.ieee.mantissa3; /* Low-order 32 bits of fraction. */
43*627f7eb2Smrg res_ptr[1] = u.ieee.mantissa2;
44*627f7eb2Smrg res_ptr[2] = u.ieee.mantissa1;
45*627f7eb2Smrg res_ptr[3] = u.ieee.mantissa0; /* High-order 32 bits. */
46*627f7eb2Smrg #define N 4
47*627f7eb2Smrg #elif BITS_PER_MP_LIMB == 64
48*627f7eb2Smrg res_ptr[0] = ((mp_limb_t) u.ieee.mantissa2 << 32) | u.ieee.mantissa3;
49*627f7eb2Smrg res_ptr[1] = ((mp_limb_t) u.ieee.mantissa0 << 32) | u.ieee.mantissa1;
50*627f7eb2Smrg #define N 2
51*627f7eb2Smrg #else
52*627f7eb2Smrg #error "mp_limb size " BITS_PER_MP_LIMB "not accounted for"
53*627f7eb2Smrg #endif
54*627f7eb2Smrg /* The format does not fill the last limb. There are some zeros. */
55*627f7eb2Smrg #define NUM_LEADING_ZEROS (BITS_PER_MP_LIMB \
56*627f7eb2Smrg - (FLT128_MANT_DIG - ((N - 1) * BITS_PER_MP_LIMB)))
57*627f7eb2Smrg
58*627f7eb2Smrg if (u.ieee.exponent == 0)
59*627f7eb2Smrg {
60*627f7eb2Smrg /* A biased exponent of zero is a special case.
61*627f7eb2Smrg Either it is a zero or it is a denormal number. */
62*627f7eb2Smrg if (res_ptr[0] == 0 && res_ptr[1] == 0
63*627f7eb2Smrg && res_ptr[N - 2] == 0 && res_ptr[N - 1] == 0) /* Assumes N<=4. */
64*627f7eb2Smrg /* It's zero. */
65*627f7eb2Smrg *expt = 0;
66*627f7eb2Smrg else
67*627f7eb2Smrg {
68*627f7eb2Smrg /* It is a denormal number, meaning it has no implicit leading
69*627f7eb2Smrg one bit, and its exponent is in fact the format minimum. */
70*627f7eb2Smrg int cnt;
71*627f7eb2Smrg
72*627f7eb2Smrg #if N == 2
73*627f7eb2Smrg if (res_ptr[N - 1] != 0)
74*627f7eb2Smrg {
75*627f7eb2Smrg count_leading_zeros (cnt, res_ptr[N - 1]);
76*627f7eb2Smrg cnt -= NUM_LEADING_ZEROS;
77*627f7eb2Smrg res_ptr[N - 1] = res_ptr[N - 1] << cnt
78*627f7eb2Smrg | (res_ptr[0] >> (BITS_PER_MP_LIMB - cnt));
79*627f7eb2Smrg res_ptr[0] <<= cnt;
80*627f7eb2Smrg *expt = FLT128_MIN_EXP - 1 - cnt;
81*627f7eb2Smrg }
82*627f7eb2Smrg else
83*627f7eb2Smrg {
84*627f7eb2Smrg count_leading_zeros (cnt, res_ptr[0]);
85*627f7eb2Smrg if (cnt >= NUM_LEADING_ZEROS)
86*627f7eb2Smrg {
87*627f7eb2Smrg res_ptr[N - 1] = res_ptr[0] << (cnt - NUM_LEADING_ZEROS);
88*627f7eb2Smrg res_ptr[0] = 0;
89*627f7eb2Smrg }
90*627f7eb2Smrg else
91*627f7eb2Smrg {
92*627f7eb2Smrg res_ptr[N - 1] = res_ptr[0] >> (NUM_LEADING_ZEROS - cnt);
93*627f7eb2Smrg res_ptr[0] <<= BITS_PER_MP_LIMB - (NUM_LEADING_ZEROS - cnt);
94*627f7eb2Smrg }
95*627f7eb2Smrg *expt = FLT128_MIN_EXP - 1
96*627f7eb2Smrg - (BITS_PER_MP_LIMB - NUM_LEADING_ZEROS) - cnt;
97*627f7eb2Smrg }
98*627f7eb2Smrg #else
99*627f7eb2Smrg int j, k, l;
100*627f7eb2Smrg
101*627f7eb2Smrg for (j = N - 1; j > 0; j--)
102*627f7eb2Smrg if (res_ptr[j] != 0)
103*627f7eb2Smrg break;
104*627f7eb2Smrg
105*627f7eb2Smrg count_leading_zeros (cnt, res_ptr[j]);
106*627f7eb2Smrg cnt -= NUM_LEADING_ZEROS;
107*627f7eb2Smrg l = N - 1 - j;
108*627f7eb2Smrg if (cnt < 0)
109*627f7eb2Smrg {
110*627f7eb2Smrg cnt += BITS_PER_MP_LIMB;
111*627f7eb2Smrg l--;
112*627f7eb2Smrg }
113*627f7eb2Smrg if (!cnt)
114*627f7eb2Smrg for (k = N - 1; k >= l; k--)
115*627f7eb2Smrg res_ptr[k] = res_ptr[k-l];
116*627f7eb2Smrg else
117*627f7eb2Smrg {
118*627f7eb2Smrg for (k = N - 1; k > l; k--)
119*627f7eb2Smrg res_ptr[k] = res_ptr[k-l] << cnt
120*627f7eb2Smrg | res_ptr[k-l-1] >> (BITS_PER_MP_LIMB - cnt);
121*627f7eb2Smrg res_ptr[k--] = res_ptr[0] << cnt;
122*627f7eb2Smrg }
123*627f7eb2Smrg
124*627f7eb2Smrg for (; k >= 0; k--)
125*627f7eb2Smrg res_ptr[k] = 0;
126*627f7eb2Smrg *expt = FLT128_MIN_EXP - 1 - l * BITS_PER_MP_LIMB - cnt;
127*627f7eb2Smrg #endif
128*627f7eb2Smrg }
129*627f7eb2Smrg }
130*627f7eb2Smrg else
131*627f7eb2Smrg /* Add the implicit leading one bit for a normalized number. */
132*627f7eb2Smrg res_ptr[N - 1] |= (mp_limb_t) 1 << (FLT128_MANT_DIG - 1
133*627f7eb2Smrg - ((N - 1) * BITS_PER_MP_LIMB));
134*627f7eb2Smrg
135*627f7eb2Smrg return N;
136*627f7eb2Smrg }
137