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
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 IEEE854 quad-precision format to a
00029    multi-precision integer representing the significand scaled up by its
00030    number of bits (113 for long double) and an integral power of two
00031    (MPN 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 ieee854_long_double u;
00039   u.d = value;
00040 
00041   *is_neg = u.ieee.negative;
00042   *expt = (int) u.ieee.exponent - IEEE854_LONG_DOUBLE_BIAS;
00043 
00044 #if BITS_PER_MP_LIMB == 32
00045   res_ptr[0] = u.ieee.mantissa3; /* Low-order 32 bits of fraction.  */
00046   res_ptr[1] = u.ieee.mantissa2;
00047   res_ptr[2] = u.ieee.mantissa1;
00048   res_ptr[3] = u.ieee.mantissa0; /* High-order 32 bits.  */
00049   #define N 4
00050 #elif BITS_PER_MP_LIMB == 64
00051   /* Hopefully the compiler will combine the two bitfield extracts
00052      and this composition into just the original quadword extract.  */
00053   res_ptr[0] = ((mp_limb_t) u.ieee.mantissa2 << 32) | u.ieee.mantissa3;
00054   res_ptr[1] = ((mp_limb_t) u.ieee.mantissa0 << 32) | u.ieee.mantissa1;
00055   #define N 2
00056 #else
00057   #error "mp_limb size " BITS_PER_MP_LIMB "not accounted for"
00058 #endif
00059 /* The format does not fill the last limb.  There are some zeros.  */
00060 #define NUM_LEADING_ZEROS (BITS_PER_MP_LIMB \
00061                         - (LDBL_MANT_DIG - ((N - 1) * BITS_PER_MP_LIMB)))
00062 
00063   if (u.ieee.exponent == 0)
00064     {
00065       /* A biased exponent of zero is a special case.
00066         Either it is a zero or it is a denormal number.  */
00067       if (res_ptr[0] == 0 && res_ptr[1] == 0
00068           && res_ptr[N - 2] == 0 && res_ptr[N - 1] == 0) /* Assumes N<=4.  */
00069        /* It's zero.  */
00070        *expt = 0;
00071       else
00072        {
00073           /* It is a denormal number, meaning it has no implicit leading
00074             one bit, and its exponent is in fact the format minimum.  */
00075          int cnt;
00076 
00077 #if N == 2
00078          if (res_ptr[N - 1] != 0)
00079            {
00080              count_leading_zeros (cnt, res_ptr[N - 1]);
00081              cnt -= NUM_LEADING_ZEROS;
00082              res_ptr[N - 1] = res_ptr[N - 1] << cnt
00083                             | (res_ptr[0] >> (BITS_PER_MP_LIMB - cnt));
00084              res_ptr[0] <<= cnt;
00085              *expt = LDBL_MIN_EXP - 1 - cnt;
00086            }
00087          else
00088            {
00089              count_leading_zeros (cnt, res_ptr[0]);
00090              if (cnt >= NUM_LEADING_ZEROS)
00091               {
00092                 res_ptr[N - 1] = res_ptr[0] << (cnt - NUM_LEADING_ZEROS);
00093                 res_ptr[0] = 0;
00094               }
00095              else
00096               {
00097                 res_ptr[N - 1] = res_ptr[0] >> (NUM_LEADING_ZEROS - cnt);
00098                 res_ptr[0] <<= BITS_PER_MP_LIMB - (NUM_LEADING_ZEROS - cnt);
00099               }
00100              *expt = LDBL_MIN_EXP - 1
00101               - (BITS_PER_MP_LIMB - NUM_LEADING_ZEROS) - cnt;
00102            }
00103 #else
00104          int j, k, l;
00105 
00106          for (j = N - 1; j > 0; j--)
00107            if (res_ptr[j] != 0)
00108              break;
00109 
00110          count_leading_zeros (cnt, res_ptr[j]);
00111          cnt -= NUM_LEADING_ZEROS;
00112          l = N - 1 - j;
00113          if (cnt < 0)
00114            {
00115              cnt += BITS_PER_MP_LIMB;
00116              l--;
00117            }
00118          if (!cnt)
00119            for (k = N - 1; k >= l; k--)
00120              res_ptr[k] = res_ptr[k-l];
00121          else
00122            {
00123              for (k = N - 1; k > l; k--)
00124               res_ptr[k] = res_ptr[k-l] << cnt
00125                           | res_ptr[k-l-1] >> (BITS_PER_MP_LIMB - cnt);
00126              res_ptr[k--] = res_ptr[0] << cnt;
00127            }
00128 
00129          for (; k >= 0; k--)
00130            res_ptr[k] = 0;
00131          *expt = LDBL_MIN_EXP - 1 - l * BITS_PER_MP_LIMB - cnt;
00132 #endif
00133        }
00134     }
00135   else
00136     /* Add the implicit leading one bit for a normalized number.  */
00137     res_ptr[N - 1] |= (mp_limb_t) 1 << (LDBL_MANT_DIG - 1
00138                                    - ((N - 1) * BITS_PER_MP_LIMB));
00139 
00140   return N;
00141 }