Back to index

glibc  2.9
ldbl2mpn.c
Go to the documentation of this file.
00001 /* Copyright (C) 1995,1996,1997,1998,1999,2002,2003,2006
00002        Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include "gmp.h"
00021 #include "gmp-impl.h"
00022 #include "longlong.h"
00023 #include <ieee754.h>
00024 #include <float.h>
00025 #include <math.h>
00026 #include <stdlib.h>
00027 
00028 /* Convert a `long double' in IBM extended format to a multi-precision
00029    integer representing the significand scaled up by its number of
00030    bits (106 for long double) and an integral power of two (MPN
00031    frexpl). */
00032 
00033 mp_size_t
00034 __mpn_extract_long_double (mp_ptr res_ptr, mp_size_t size,
00035                         int *expt, int *is_neg,
00036                         long double value)
00037 {
00038   union ibm_extended_long_double u;
00039   unsigned long long hi, lo;
00040   int ediff;
00041   u.d = value;
00042 
00043   *is_neg = u.ieee.negative;
00044   *expt = (int) u.ieee.exponent - IBM_EXTENDED_LONG_DOUBLE_BIAS;
00045 
00046   lo = ((long long) u.ieee.mantissa2 << 32) | u.ieee.mantissa3;
00047   hi = ((long long) u.ieee.mantissa0 << 32) | u.ieee.mantissa1;
00048   /* If the lower double is not a denomal or zero then set the hidden
00049      53rd bit.  */
00050   if (u.ieee.exponent2 > 0)
00051     {
00052       lo |= 1LL << 52;
00053 
00054       /* The lower double is normalized separately from the upper.  We may
00055         need to adjust the lower manitissa to reflect this.  */
00056       ediff = u.ieee.exponent - u.ieee.exponent2;
00057       if (ediff > 53)
00058        lo = lo >> (ediff-53);
00059     }
00060   /* The high double may be rounded and the low double reflects the
00061      difference between the long double and the rounded high double
00062      value.  This is indicated by a differnce between the signs of the
00063      high and low doubles.  */
00064   if ((u.ieee.negative != u.ieee.negative2)
00065       && ((u.ieee.exponent2 != 0) && (lo != 0L)))
00066     {
00067       lo = (1ULL << 53) - lo;
00068       if (hi == 0LL)
00069        {
00070          /* we have a borrow from the hidden bit, so shift left 1.  */
00071          hi = 0x0ffffffffffffeLL | (lo >> 51);
00072          lo = 0x1fffffffffffffLL & (lo << 1);
00073          (*expt)--;
00074        }
00075       else
00076        hi--;
00077     }
00078 #if BITS_PER_MP_LIMB == 32
00079   /* Combine the mantissas to be contiguous.  */
00080   res_ptr[0] = lo;
00081   res_ptr[1] = (hi << (53 - 32)) | (lo >> 32);
00082   res_ptr[2] = hi >> 11;
00083   res_ptr[3] = hi >> (32 + 11);
00084   #define N 4
00085 #elif BITS_PER_MP_LIMB == 64
00086   /* Combine the two mantissas to be contiguous.  */
00087   res_ptr[0] = (hi << 53) | lo;
00088   res_ptr[1] = hi >> 11;
00089   #define N 2
00090 #else
00091   #error "mp_limb size " BITS_PER_MP_LIMB "not accounted for"
00092 #endif
00093 /* The format does not fill the last limb.  There are some zeros.  */
00094 #define NUM_LEADING_ZEROS (BITS_PER_MP_LIMB \
00095                         - (LDBL_MANT_DIG - ((N - 1) * BITS_PER_MP_LIMB)))
00096 
00097   if (u.ieee.exponent == 0)
00098     {
00099       /* A biased exponent of zero is a special case.
00100         Either it is a zero or it is a denormal number.  */
00101       if (res_ptr[0] == 0 && res_ptr[1] == 0
00102          && res_ptr[N - 2] == 0 && res_ptr[N - 1] == 0) /* Assumes N<=4.  */
00103        /* It's zero.  */
00104        *expt = 0;
00105       else
00106        {
00107          /* It is a denormal number, meaning it has no implicit leading
00108             one bit, and its exponent is in fact the format minimum.  */
00109          int cnt;
00110 
00111 #if N == 2
00112          if (res_ptr[N - 1] != 0)
00113            {
00114              count_leading_zeros (cnt, res_ptr[N - 1]);
00115              cnt -= NUM_LEADING_ZEROS;
00116              res_ptr[N - 1] = res_ptr[N - 1] << cnt
00117                             | (res_ptr[0] >> (BITS_PER_MP_LIMB - cnt));
00118              res_ptr[0] <<= cnt;
00119              *expt = LDBL_MIN_EXP - 1 - cnt;
00120            }
00121          else
00122            {
00123              count_leading_zeros (cnt, res_ptr[0]);
00124              if (cnt >= NUM_LEADING_ZEROS)
00125               {
00126                 res_ptr[N - 1] = res_ptr[0] << (cnt - NUM_LEADING_ZEROS);
00127                 res_ptr[0] = 0;
00128               }
00129              else
00130               {
00131                 res_ptr[N - 1] = res_ptr[0] >> (NUM_LEADING_ZEROS - cnt);
00132                 res_ptr[0] <<= BITS_PER_MP_LIMB - (NUM_LEADING_ZEROS - cnt);
00133               }
00134              *expt = LDBL_MIN_EXP - 1
00135               - (BITS_PER_MP_LIMB - NUM_LEADING_ZEROS) - cnt;
00136            }
00137 #else
00138          int j, k, l;
00139 
00140          for (j = N - 1; j > 0; j--)
00141            if (res_ptr[j] != 0)
00142              break;
00143 
00144          count_leading_zeros (cnt, res_ptr[j]);
00145          cnt -= NUM_LEADING_ZEROS;
00146          l = N - 1 - j;
00147          if (cnt < 0)
00148            {
00149              cnt += BITS_PER_MP_LIMB;
00150              l--;
00151            }
00152          if (!cnt)
00153            for (k = N - 1; k >= l; k--)
00154              res_ptr[k] = res_ptr[k-l];
00155          else
00156            {
00157              for (k = N - 1; k > l; k--)
00158               res_ptr[k] = res_ptr[k-l] << cnt
00159                           | res_ptr[k-l-1] >> (BITS_PER_MP_LIMB - cnt);
00160              res_ptr[k--] = res_ptr[0] << cnt;
00161            }
00162 
00163          for (; k >= 0; k--)
00164            res_ptr[k] = 0;
00165          *expt = LDBL_MIN_EXP - 1 - l * BITS_PER_MP_LIMB - cnt;
00166 #endif
00167        }
00168     }
00169   else
00170     /* Add the implicit leading one bit for a normalized number.  */
00171     res_ptr[N - 1] |= (mp_limb_t) 1 << (LDBL_MANT_DIG - 1
00172                                    - ((N - 1) * BITS_PER_MP_LIMB));
00173 
00174   return N;
00175 }