Back to index

glibc  2.9
ldbl2mpn.c
Go to the documentation of this file.
00001 /* Copyright (C) 1995,1996,1997,1998,2002,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 <math.h>
00025 #include <stdlib.h>
00026 
00027 /* Convert a `long double' in IEEE854 standard double-precision format to a
00028    multi-precision integer representing the significand scaled up by its
00029    number of bits (64 for long double) and an integral power of two
00030    (MPN frexpl). */
00031 
00032 mp_size_t
00033 __mpn_extract_long_double (mp_ptr res_ptr, mp_size_t size,
00034                         int *expt, int *is_neg,
00035                         long double value)
00036 {
00037   union ieee854_long_double u;
00038   u.d = value;
00039 
00040   *is_neg = u.ieee.negative;
00041   *expt = (int) u.ieee.exponent - IEEE854_LONG_DOUBLE_BIAS;
00042 
00043 #if BITS_PER_MP_LIMB == 32
00044   res_ptr[0] = u.ieee.mantissa1; /* Low-order 32 bits of fraction.  */
00045   res_ptr[1] = u.ieee.mantissa0; /* High-order 32 bits.  */
00046   #define N 2
00047 #elif BITS_PER_MP_LIMB == 64
00048   /* Hopefully the compiler will combine the two bitfield extracts
00049      and this composition into just the original quadword extract.  */
00050   res_ptr[0] = ((mp_limb_t) u.ieee.mantissa0 << 32) | u.ieee.mantissa1;
00051   #define N 1
00052 #else
00053   #error "mp_limb size " BITS_PER_MP_LIMB "not accounted for"
00054 #endif
00055 
00056   if (u.ieee.exponent == 0)
00057     {
00058       /* A biased exponent of zero is a special case.
00059         Either it is a zero or it is a denormal number.  */
00060       if (res_ptr[0] == 0 && res_ptr[N - 1] == 0) /* Assumes N<=2.  */
00061        /* It's zero.  */
00062        *expt = 0;
00063       else
00064        {
00065           /* It is a denormal number, meaning it has no implicit leading
00066             one bit, and its exponent is in fact the format minimum.  */
00067          int cnt;
00068 
00069          if (res_ptr[N - 1] != 0)
00070            {
00071              count_leading_zeros (cnt, res_ptr[N - 1]);
00072              if (cnt != 0)
00073               {
00074 #if N == 2
00075                  res_ptr[N - 1] = res_ptr[N - 1] << cnt
00076                                 | (res_ptr[0] >> (BITS_PER_MP_LIMB - cnt));
00077                  res_ptr[0] <<= cnt;
00078 #else
00079                  res_ptr[N - 1] <<= cnt;
00080 #endif
00081               }
00082              *expt = LDBL_MIN_EXP - 1 - cnt;
00083            }
00084          else
00085            {
00086              count_leading_zeros (cnt, res_ptr[0]);
00087              res_ptr[N - 1] = res_ptr[0] << cnt;
00088              res_ptr[0] = 0;
00089              *expt = LDBL_MIN_EXP - 1 - BITS_PER_MP_LIMB - cnt;
00090            }
00091        }
00092     }
00093 
00094   return N;
00095 }