1*c1437015SIngo Weinhold /* Copyright (C) 1995,1996,1997,2002,2003 Free Software Foundation, Inc.
2*c1437015SIngo Weinhold This file is part of the GNU C Library.
3*c1437015SIngo Weinhold
4*c1437015SIngo Weinhold The GNU C Library is free software; you can redistribute it and/or
5*c1437015SIngo Weinhold modify it under the terms of the GNU Lesser General Public
6*c1437015SIngo Weinhold License as published by the Free Software Foundation; either
7*c1437015SIngo Weinhold version 2.1 of the License, or (at your option) any later version.
8*c1437015SIngo Weinhold
9*c1437015SIngo Weinhold The GNU C Library is distributed in the hope that it will be useful,
10*c1437015SIngo Weinhold but WITHOUT ANY WARRANTY; without even the implied warranty of
11*c1437015SIngo Weinhold MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12*c1437015SIngo Weinhold Lesser General Public License for more details.
13*c1437015SIngo Weinhold
14*c1437015SIngo Weinhold You should have received a copy of the GNU Lesser General Public
15*c1437015SIngo Weinhold License along with the GNU C Library; if not, write to the Free
16*c1437015SIngo Weinhold Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17*c1437015SIngo Weinhold 02111-1307 USA. */
18*c1437015SIngo Weinhold
19*c1437015SIngo Weinhold #include "gmp.h"
20*c1437015SIngo Weinhold #include "gmp-impl.h"
21*c1437015SIngo Weinhold #include <ieee754.h>
22*c1437015SIngo Weinhold #include <float.h>
23*c1437015SIngo Weinhold
24*c1437015SIngo Weinhold /* Convert a multi-precision integer of the needed number of bits (53 for
25*c1437015SIngo Weinhold double) and an integral power of two to a `double' in IEEE754 double-
26*c1437015SIngo Weinhold precision format. */
27*c1437015SIngo Weinhold
28*c1437015SIngo Weinhold double
__mpn_construct_double(mp_srcptr frac_ptr,int expt,int negative)29*c1437015SIngo Weinhold __mpn_construct_double (mp_srcptr frac_ptr, int expt, int negative)
30*c1437015SIngo Weinhold {
31*c1437015SIngo Weinhold union ieee754_double u;
32*c1437015SIngo Weinhold
33*c1437015SIngo Weinhold u.ieee.negative = negative;
34*c1437015SIngo Weinhold u.ieee.exponent = expt + IEEE754_DOUBLE_BIAS;
35*c1437015SIngo Weinhold #if BITS_PER_MP_LIMB == 32
36*c1437015SIngo Weinhold u.ieee.mantissa1 = frac_ptr[0];
37*c1437015SIngo Weinhold u.ieee.mantissa0 = frac_ptr[1] & (((mp_limb_t) 1
38*c1437015SIngo Weinhold << (DBL_MANT_DIG - 32)) - 1);
39*c1437015SIngo Weinhold #elif BITS_PER_MP_LIMB == 64
40*c1437015SIngo Weinhold u.ieee.mantissa1 = frac_ptr[0] & (((mp_limb_t) 1 << 32) - 1);
41*c1437015SIngo Weinhold u.ieee.mantissa0 = (frac_ptr[0] >> 32) & (((mp_limb_t) 1
42*c1437015SIngo Weinhold << (DBL_MANT_DIG - 32)) - 1);
43*c1437015SIngo Weinhold #else
44*c1437015SIngo Weinhold #error "mp_limb size " BITS_PER_MP_LIMB "not accounted for"
45*c1437015SIngo Weinhold #endif
46*c1437015SIngo Weinhold
47*c1437015SIngo Weinhold return u.d;
48*c1437015SIngo Weinhold }
49