[BACK]Return to set_q.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / mpf

Diff for /OpenXM_contrib/gmp/mpf/Attic/set_q.c between version 1.1.1.1 and 1.1.1.3

version 1.1.1.1, 2000/01/10 15:35:22 version 1.1.1.3, 2003/08/25 16:06:34
Line 1 
Line 1 
 /* mpf_set_q (mpf_t rop, mpq_t op) -- Convert the rational op to the float rop.  /* mpf_set_q (mpf_t rop, mpq_t op) -- Convert the rational op to the float rop.
   
 Copyright (C) 1996 Free Software Foundation, Inc.  Copyright 1996, 1999, 2001, 2002 Free Software Foundation, Inc.
   
 This file is part of the GNU MP Library.  This file is part of the GNU MP Library.
   
 The GNU MP Library is free software; you can redistribute it and/or modify  The GNU MP Library is free software; you can redistribute it and/or modify
 it under the terms of the GNU Library General Public License as published by  it under the terms of the GNU Lesser General Public License as published by
 the Free Software Foundation; either version 2 of the License, or (at your  the Free Software Foundation; either version 2.1 of the License, or (at your
 option) any later version.  option) any later version.
   
 The GNU MP Library is distributed in the hope that it will be useful, but  The GNU MP Library is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 License for more details.  License for more details.
   
 You should have received a copy of the GNU Library General Public License  You should have received a copy of the GNU Lesser General Public License
 along with the GNU MP Library; see the file COPYING.LIB.  If not, write to  along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,  the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 MA 02111-1307, USA. */  MA 02111-1307, USA. */
Line 23  MA 02111-1307, USA. */
Line 23  MA 02111-1307, USA. */
 #include "gmp-impl.h"  #include "gmp-impl.h"
 #include "longlong.h"  #include "longlong.h"
   
 /* Algorithm:  
    1. Develop >= n bits of src.num / src.den, where n is the number of bits  
       in a double.  This (partial) division will use all bits from the  
       denominator.  
    2. Use the remainder to determine how to round the result.  
    3. Assign the integral result to a temporary double.  
    4. Scale the temporary double, and return the result.  
   
    An alternative algorithm, that would be faster:  
    0. Let n be somewhat larger than the number of significant bits in a double.  
    1. Extract the most significant n bits of the denominator, and an equal  
       number of bits from the numerator.  
    2. Interpret the extracted numbers as integers, call them a and b  
       respectively, and develop n bits of the fractions ((a + 1) / b) and  
       (a / (b + 1)) using mpn_divrem.  
    3. If the computed values are identical UP TO THE POSITION WE CARE ABOUT,  
       we are done.  If they are different, repeat the algorithm from step 1,  
       but first let n = n * 2.  
    4. If we end up using all bits from the numerator and denominator, fall  
       back to the first algorithm above.  
    5. Just to make life harder, The computation of a + 1 and b + 1 above  
       might give carry-out...  Needs special handling.  It might work to  
       subtract 1 in both cases instead.  
 */  
   
 void  void
 #if __STDC__  
 mpf_set_q (mpf_t r, mpq_srcptr q)  mpf_set_q (mpf_t r, mpq_srcptr q)
 #else  
 mpf_set_q (r, q)  
      mpf_t r;  
      mpq_srcptr q;  
 #endif  
 {  {
   mp_ptr np, dp;    mp_ptr np, dp;
   mp_ptr rp;    mp_ptr rp;
   mp_size_t nsize, dsize;    mp_size_t nsize, dsize;
   mp_size_t qsize, rsize;    mp_size_t qsize, rsize;
   mp_size_t sign_quotient;    mp_size_t sign_quotient;
   unsigned normalization_steps;  
   mp_limb_t qlimb;    mp_limb_t qlimb;
   mp_ptr qp;    mp_ptr qp;
   mp_size_t prec;    mp_size_t prec;
Line 107  mpf_set_q (r, q)
Line 75  mpf_set_q (r, q)
   rsize = MAX (nsize, dsize);    rsize = MAX (nsize, dsize);
   rp = (mp_ptr) TMP_ALLOC ((rsize + 1) * BYTES_PER_MP_LIMB);    rp = (mp_ptr) TMP_ALLOC ((rsize + 1) * BYTES_PER_MP_LIMB);
   
   count_leading_zeros (normalization_steps, dp[dsize - 1]);  
   
   /* Normalize the denominator, i.e. make its most significant bit set by    /* Normalize the denominator, i.e. make its most significant bit set by
      shifting it NORMALIZATION_STEPS bits to the left.  Also shift the       shifting it NORMALIZATION_STEPS bits to the left.  Also shift the
      numerator the same number of steps (to keep the quotient the same!).  */       numerator the same number of steps (to keep the quotient the same!).  */
   if (normalization_steps != 0)    if ((dp[dsize - 1] & GMP_NUMB_HIGHBIT) == 0)
     {      {
       mp_ptr tp;        mp_ptr tp;
       mp_limb_t nlimb;        mp_limb_t nlimb;
         unsigned normalization_steps;
   
         count_leading_zeros (normalization_steps, dp[dsize - 1]);
         normalization_steps -= GMP_NAIL_BITS;
   
       /* Shift up the denominator setting the most significant bit of        /* Shift up the denominator setting the most significant bit of
          the most significant limb.  Use temporary storage not to clobber           the most significant limb.  Use temporary storage not to clobber
          the original contents of the denominator.  */           the original contents of the denominator.  */
Line 132  mpf_set_q (r, q)
Line 102  mpf_set_q (r, q)
         }          }
       else        else
         {          {
           nlimb = mpn_lshift (rp, np, nsize, normalization_steps);            nlimb = mpn_lshift (rp, np, rsize, normalization_steps);
         }          }
       if (nlimb != 0)        if (nlimb != 0)
         {          {
           rp[rsize] = nlimb;            rp[rsize] = nlimb;
           rsize++;  
           exp++;            exp++;
             /* Don't just increase rsize, chop off rp at the low end instead.  */
             if (rsize == prec)
               rp++;
             else
               rsize++;
         }          }
     }      }
   else    else
Line 164  mpf_set_q (r, q)
Line 138  mpf_set_q (r, q)
     }      }
   
   EXP (r) = exp;    EXP (r) = exp;
   SIZ (r) = qsize;    SIZ (r) = sign_quotient >= 0 ? qsize : -qsize;
   
   TMP_FREE (marker);    TMP_FREE (marker);
 }  }

Legend:
Removed from v.1.1.1.1  
changed lines
  Added in v.1.1.1.3

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>