Back to index

glibc  2.9
dbl2mpn.c
Go to the documentation of this file.
00001 /* Copyright (C) 1993,1994,1995,1996,1997,2003 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include "gmp.h"
00020 #include "gmp-impl.h"
00021 #include "longlong.h"
00022 #include <ieee754.h>
00023 #include <float.h>
00024 #include <stdlib.h>
00025 
00026 /* Convert a `double' in IEEE754 standard double-precision format to a
00027    multi-precision integer representing the significand scaled up by its
00028    number of bits (52 for double) and an integral power of two (MPN frexp). */
00029 
00030 mp_size_t
00031 __mpn_extract_double (mp_ptr res_ptr, mp_size_t size,
00032                     int *expt, int *is_neg,
00033                     double value)
00034 {
00035   union ieee754_double u;
00036   u.d = value;
00037 
00038   *is_neg = u.ieee.negative;
00039   *expt = (int) u.ieee.exponent - IEEE754_DOUBLE_BIAS;
00040 
00041 #if BITS_PER_MP_LIMB == 32
00042   res_ptr[0] = u.ieee.mantissa1; /* Low-order 32 bits of fraction.  */
00043   res_ptr[1] = u.ieee.mantissa0; /* High-order 20 bits.  */
00044   #define N 2
00045 #elif BITS_PER_MP_LIMB == 64
00046   /* Hopefully the compiler will combine the two bitfield extracts
00047      and this composition into just the original quadword extract.  */
00048   res_ptr[0] = ((mp_limb_t) u.ieee.mantissa0 << 32) | u.ieee.mantissa1;
00049   #define N 1
00050 #else
00051   #error "mp_limb size " BITS_PER_MP_LIMB "not accounted for"
00052 #endif
00053 /* The format does not fill the last limb.  There are some zeros.  */
00054 #define NUM_LEADING_ZEROS (BITS_PER_MP_LIMB \
00055                         - (DBL_MANT_DIG - ((N - 1) * BITS_PER_MP_LIMB)))
00056 
00057   if (u.ieee.exponent == 0)
00058     {
00059       /* A biased exponent of zero is a special case.
00060         Either it is a zero or it is a denormal number.  */
00061       if (res_ptr[0] == 0 && res_ptr[N - 1] == 0) /* Assumes N<=2.  */
00062        /* It's zero.  */
00063        *expt = 0;
00064       else
00065        {
00066           /* It is a denormal number, meaning it has no implicit leading
00067             one bit, and its exponent is in fact the format minimum.  */
00068          int cnt;
00069 
00070          if (res_ptr[N - 1] != 0)
00071            {
00072              count_leading_zeros (cnt, res_ptr[N - 1]);
00073              cnt -= NUM_LEADING_ZEROS;
00074 #if N == 2
00075              res_ptr[N - 1] = res_ptr[1] << cnt
00076                             | (N - 1)
00077                               * (res_ptr[0] >> (BITS_PER_MP_LIMB - cnt));
00078              res_ptr[0] <<= cnt;
00079 #else
00080              res_ptr[N - 1] <<= cnt;
00081 #endif
00082              *expt = DBL_MIN_EXP - 1 - cnt;
00083            }
00084          else
00085            {
00086              count_leading_zeros (cnt, res_ptr[0]);
00087              if (cnt >= NUM_LEADING_ZEROS)
00088               {
00089                 res_ptr[N - 1] = res_ptr[0] << (cnt - NUM_LEADING_ZEROS);
00090                 res_ptr[0] = 0;
00091               }
00092              else
00093               {
00094                 res_ptr[N - 1] = res_ptr[0] >> (NUM_LEADING_ZEROS - cnt);
00095                 res_ptr[0] <<= BITS_PER_MP_LIMB - (NUM_LEADING_ZEROS - cnt);
00096               }
00097              *expt = DBL_MIN_EXP - 1
00098                     - (BITS_PER_MP_LIMB - NUM_LEADING_ZEROS) - cnt;
00099            }
00100        }
00101     }
00102   else
00103     /* Add the implicit leading one bit for a normalized number.  */
00104     res_ptr[N - 1] |= (mp_limb_t) 1 << (DBL_MANT_DIG - 1
00105                                    - ((N - 1) * BITS_PER_MP_LIMB));
00106 
00107   return N;
00108 }