xref: /haiku/src/system/libroot/posix/glibc/arch/generic/dbl2mpn.c (revision c1437015ddbea841cbd0e154b416199886c83624)
1*c1437015SIngo Weinhold /* Copyright (C) 1993, 1994, 1995, 1996, 1997 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 "longlong.h"
22*c1437015SIngo Weinhold #include <ieee754.h>
23*c1437015SIngo Weinhold #include <float.h>
24*c1437015SIngo Weinhold #include <stdlib.h>
25*c1437015SIngo Weinhold 
26*c1437015SIngo Weinhold /* Convert a `double' in IEEE754 standard double-precision format to a
27*c1437015SIngo Weinhold    multi-precision integer representing the significand scaled up by its
28*c1437015SIngo Weinhold    number of bits (52 for double) and an integral power of two (MPN frexp). */
29*c1437015SIngo Weinhold 
30*c1437015SIngo Weinhold mp_size_t
__mpn_extract_double(mp_ptr res_ptr,mp_size_t size,int * expt,int * is_neg,double value)31*c1437015SIngo Weinhold __mpn_extract_double (mp_ptr res_ptr, mp_size_t size,
32*c1437015SIngo Weinhold 		      int *expt, int *is_neg,
33*c1437015SIngo Weinhold 		      double value)
34*c1437015SIngo Weinhold {
35*c1437015SIngo Weinhold   union ieee754_double u;
36*c1437015SIngo Weinhold   u.d = value;
37*c1437015SIngo Weinhold 
38*c1437015SIngo Weinhold   *is_neg = u.ieee.negative;
39*c1437015SIngo Weinhold   *expt = (int) u.ieee.exponent - IEEE754_DOUBLE_BIAS;
40*c1437015SIngo Weinhold 
41*c1437015SIngo Weinhold #if BITS_PER_MP_LIMB == 32
42*c1437015SIngo Weinhold   res_ptr[0] = u.ieee.mantissa1; /* Low-order 32 bits of fraction.  */
43*c1437015SIngo Weinhold   res_ptr[1] = u.ieee.mantissa0; /* High-order 20 bits.  */
44*c1437015SIngo Weinhold   #define N 2
45*c1437015SIngo Weinhold #elif BITS_PER_MP_LIMB == 64
46*c1437015SIngo Weinhold   /* Hopefully the compiler will combine the two bitfield extracts
47*c1437015SIngo Weinhold      and this composition into just the original quadword extract.  */
48*c1437015SIngo Weinhold   res_ptr[0] = ((unsigned long int) u.ieee.mantissa0 << 32) | u.ieee.mantissa1;
49*c1437015SIngo Weinhold   #define N 1
50*c1437015SIngo Weinhold #else
51*c1437015SIngo Weinhold   #error "mp_limb size " BITS_PER_MP_LIMB "not accounted for"
52*c1437015SIngo Weinhold #endif
53*c1437015SIngo Weinhold /* The format does not fill the last limb.  There are some zeros.  */
54*c1437015SIngo Weinhold #define NUM_LEADING_ZEROS (BITS_PER_MP_LIMB \
55*c1437015SIngo Weinhold 			   - (DBL_MANT_DIG - ((N - 1) * BITS_PER_MP_LIMB)))
56*c1437015SIngo Weinhold 
57*c1437015SIngo Weinhold   if (u.ieee.exponent == 0)
58*c1437015SIngo Weinhold     {
59*c1437015SIngo Weinhold       /* A biased exponent of zero is a special case.
60*c1437015SIngo Weinhold 	 Either it is a zero or it is a denormal number.  */
61*c1437015SIngo Weinhold       if (res_ptr[0] == 0 && res_ptr[N - 1] == 0) /* Assumes N<=2.  */
62*c1437015SIngo Weinhold 	/* It's zero.  */
63*c1437015SIngo Weinhold 	*expt = 0;
64*c1437015SIngo Weinhold       else
65*c1437015SIngo Weinhold 	{
66*c1437015SIngo Weinhold           /* It is a denormal number, meaning it has no implicit leading
67*c1437015SIngo Weinhold 	     one bit, and its exponent is in fact the format minimum.  */
68*c1437015SIngo Weinhold 	  int cnt;
69*c1437015SIngo Weinhold 
70*c1437015SIngo Weinhold 	  if (res_ptr[N - 1] != 0)
71*c1437015SIngo Weinhold 	    {
72*c1437015SIngo Weinhold 	      count_leading_zeros (cnt, res_ptr[N - 1]);
73*c1437015SIngo Weinhold 	      cnt -= NUM_LEADING_ZEROS;
74*c1437015SIngo Weinhold #if N == 2
75*c1437015SIngo Weinhold 	      res_ptr[N - 1] = res_ptr[1] << cnt
76*c1437015SIngo Weinhold 			       | (N - 1)
77*c1437015SIngo Weinhold 			         * (res_ptr[0] >> (BITS_PER_MP_LIMB - cnt));
78*c1437015SIngo Weinhold 	      res_ptr[0] <<= cnt;
79*c1437015SIngo Weinhold #else
80*c1437015SIngo Weinhold 	      res_ptr[N - 1] <<= cnt;
81*c1437015SIngo Weinhold #endif
82*c1437015SIngo Weinhold 	      *expt = DBL_MIN_EXP - 1 - cnt;
83*c1437015SIngo Weinhold 	    }
84*c1437015SIngo Weinhold 	  else
85*c1437015SIngo Weinhold 	    {
86*c1437015SIngo Weinhold 	      count_leading_zeros (cnt, res_ptr[0]);
87*c1437015SIngo Weinhold 	      if (cnt >= NUM_LEADING_ZEROS)
88*c1437015SIngo Weinhold 		{
89*c1437015SIngo Weinhold 		  res_ptr[N - 1] = res_ptr[0] << (cnt - NUM_LEADING_ZEROS);
90*c1437015SIngo Weinhold 		  res_ptr[0] = 0;
91*c1437015SIngo Weinhold 		}
92*c1437015SIngo Weinhold 	      else
93*c1437015SIngo Weinhold 		{
94*c1437015SIngo Weinhold 		  res_ptr[N - 1] = res_ptr[0] >> (NUM_LEADING_ZEROS - cnt);
95*c1437015SIngo Weinhold 		  res_ptr[0] <<= BITS_PER_MP_LIMB - (NUM_LEADING_ZEROS - cnt);
96*c1437015SIngo Weinhold 		}
97*c1437015SIngo Weinhold 	      *expt = DBL_MIN_EXP - 1
98*c1437015SIngo Weinhold 		      - (BITS_PER_MP_LIMB - NUM_LEADING_ZEROS) - cnt;
99*c1437015SIngo Weinhold 	    }
100*c1437015SIngo Weinhold 	}
101*c1437015SIngo Weinhold     }
102*c1437015SIngo Weinhold   else
103*c1437015SIngo Weinhold     /* Add the implicit leading one bit for a normalized number.  */
104*c1437015SIngo Weinhold     res_ptr[N - 1] |= 1L << (DBL_MANT_DIG - 1 - ((N - 1) * BITS_PER_MP_LIMB));
105*c1437015SIngo Weinhold 
106*c1437015SIngo Weinhold   return N;
107*c1437015SIngo Weinhold }
108