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

Annotation of OpenXM_contrib/gmp/mpf/set_q.c, Revision 1.1.1.1

1.1       maekawa     1: /* mpf_set_q (mpf_t rop, mpq_t op) -- Convert the rational op to the float rop.
                      2:
                      3: Copyright (C) 1996 Free Software Foundation, Inc.
                      4:
                      5: This file is part of the GNU MP Library.
                      6:
                      7: The GNU MP Library is free software; you can redistribute it and/or modify
                      8: it under the terms of the GNU Library General Public License as published by
                      9: the Free Software Foundation; either version 2 of the License, or (at your
                     10: option) any later version.
                     11:
                     12: The GNU MP Library is distributed in the hope that it will be useful, but
                     13: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
                     14: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
                     15: License for more details.
                     16:
                     17: You should have received a copy of the GNU Library General Public License
                     18: along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
                     19: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
                     20: MA 02111-1307, USA. */
                     21:
                     22: #include "gmp.h"
                     23: #include "gmp-impl.h"
                     24: #include "longlong.h"
                     25:
                     26: /* Algorithm:
                     27:    1. Develop >= n bits of src.num / src.den, where n is the number of bits
                     28:       in a double.  This (partial) division will use all bits from the
                     29:       denominator.
                     30:    2. Use the remainder to determine how to round the result.
                     31:    3. Assign the integral result to a temporary double.
                     32:    4. Scale the temporary double, and return the result.
                     33:
                     34:    An alternative algorithm, that would be faster:
                     35:    0. Let n be somewhat larger than the number of significant bits in a double.
                     36:    1. Extract the most significant n bits of the denominator, and an equal
                     37:       number of bits from the numerator.
                     38:    2. Interpret the extracted numbers as integers, call them a and b
                     39:       respectively, and develop n bits of the fractions ((a + 1) / b) and
                     40:       (a / (b + 1)) using mpn_divrem.
                     41:    3. If the computed values are identical UP TO THE POSITION WE CARE ABOUT,
                     42:       we are done.  If they are different, repeat the algorithm from step 1,
                     43:       but first let n = n * 2.
                     44:    4. If we end up using all bits from the numerator and denominator, fall
                     45:       back to the first algorithm above.
                     46:    5. Just to make life harder, The computation of a + 1 and b + 1 above
                     47:       might give carry-out...  Needs special handling.  It might work to
                     48:       subtract 1 in both cases instead.
                     49: */
                     50:
                     51: void
                     52: #if __STDC__
                     53: mpf_set_q (mpf_t r, mpq_srcptr q)
                     54: #else
                     55: mpf_set_q (r, q)
                     56:      mpf_t r;
                     57:      mpq_srcptr q;
                     58: #endif
                     59: {
                     60:   mp_ptr np, dp;
                     61:   mp_ptr rp;
                     62:   mp_size_t nsize, dsize;
                     63:   mp_size_t qsize, rsize;
                     64:   mp_size_t sign_quotient;
                     65:   unsigned normalization_steps;
                     66:   mp_limb_t qlimb;
                     67:   mp_ptr qp;
                     68:   mp_size_t prec;
                     69:   mp_exp_t exp;
                     70:   TMP_DECL (marker);
                     71:
                     72:   nsize = SIZ (&q->_mp_num);
                     73:   dsize = SIZ (&q->_mp_den);
                     74:
                     75:   if (nsize == 0)
                     76:     {
                     77:       SIZ (r) = 0;
                     78:       EXP (r) = 0;
                     79:       return;
                     80:     }
                     81:
                     82:   prec = PREC (r) + 1;
                     83:
                     84:   TMP_MARK (marker);
                     85:
                     86:   qp = PTR (r);
                     87:
                     88:   sign_quotient = nsize ^ dsize;
                     89:   nsize = ABS (nsize);
                     90:   dsize = ABS (dsize);
                     91:   np = PTR (&q->_mp_num);
                     92:   dp = PTR (&q->_mp_den);
                     93:
                     94:   exp = nsize - dsize;
                     95:
                     96:   if (nsize > prec)
                     97:     {
                     98:       np += nsize - prec;
                     99:       nsize = prec;
                    100:     }
                    101:   if (dsize > prec)
                    102:     {
                    103:       dp += dsize - prec;
                    104:       dsize = prec;
                    105:     }
                    106:
                    107:   rsize = MAX (nsize, dsize);
                    108:   rp = (mp_ptr) TMP_ALLOC ((rsize + 1) * BYTES_PER_MP_LIMB);
                    109:
                    110:   count_leading_zeros (normalization_steps, dp[dsize - 1]);
                    111:
                    112:   /* Normalize the denominator, i.e. make its most significant bit set by
                    113:      shifting it NORMALIZATION_STEPS bits to the left.  Also shift the
                    114:      numerator the same number of steps (to keep the quotient the same!).  */
                    115:   if (normalization_steps != 0)
                    116:     {
                    117:       mp_ptr tp;
                    118:       mp_limb_t nlimb;
                    119:
                    120:       /* Shift up the denominator setting the most significant bit of
                    121:         the most significant limb.  Use temporary storage not to clobber
                    122:         the original contents of the denominator.  */
                    123:       tp = (mp_ptr) TMP_ALLOC (dsize * BYTES_PER_MP_LIMB);
                    124:       mpn_lshift (tp, dp, dsize, normalization_steps);
                    125:       dp = tp;
                    126:
                    127:       if (rsize != nsize)
                    128:        {
                    129:          MPN_ZERO (rp, rsize - nsize);
                    130:          nlimb = mpn_lshift (rp + (rsize - nsize),
                    131:                              np, nsize, normalization_steps);
                    132:        }
                    133:       else
                    134:        {
                    135:          nlimb = mpn_lshift (rp, np, nsize, normalization_steps);
                    136:        }
                    137:       if (nlimb != 0)
                    138:        {
                    139:          rp[rsize] = nlimb;
                    140:          rsize++;
                    141:          exp++;
                    142:        }
                    143:     }
                    144:   else
                    145:     {
                    146:       if (rsize != nsize)
                    147:        {
                    148:          MPN_ZERO (rp, rsize - nsize);
                    149:          MPN_COPY (rp + (rsize - nsize), np, nsize);
                    150:        }
                    151:       else
                    152:        {
                    153:          MPN_COPY (rp, np, rsize);
                    154:        }
                    155:     }
                    156:
                    157:   qlimb = mpn_divrem (qp, prec - 1 - (rsize - dsize), rp, rsize, dp, dsize);
                    158:   qsize = prec - 1;
                    159:   if (qlimb)
                    160:     {
                    161:       qp[qsize] = qlimb;
                    162:       qsize++;
                    163:       exp++;
                    164:     }
                    165:
                    166:   EXP (r) = exp;
                    167:   SIZ (r) = qsize;
                    168:
                    169:   TMP_FREE (marker);
                    170: }

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