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