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

Annotation of OpenXM_contrib/gmp/mpfr/sqrt.c, Revision 1.1.1.1

1.1       maekawa     1: /* mpfr_sqrt -- square root of a floating-point number
                      2:
                      3: Copyright (C) 1999 PolKA project, Inria Lorraine and Loria
                      4:
                      5: This file is part of the MPFR Library.
                      6:
                      7: The MPFR 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 MPFR 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 MPFR 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 <math.h>
                     23: #include <stdio.h>
                     24: #include <stdlib.h>
                     25: #include "gmp.h"
                     26: #include "gmp-impl.h"
                     27: #include "mpfr.h"
                     28: #include "longlong.h"
                     29:
                     30: /* #define DEBUG */
                     31:
                     32: int
                     33: mpfr_sqrt (mpfr_ptr r, mpfr_srcptr u, unsigned char rnd_mode)
                     34: {
                     35:   mp_ptr up, rp, tmp, remp;
                     36:   mp_size_t usize, rrsize;
                     37:   mp_size_t rsize;
                     38:   mp_size_t prec, err;
                     39:   mp_limb_t q_limb;
                     40:   long rw, nw, k;
                     41:   int exact = 0;
                     42:   unsigned long cc = 0;
                     43:   char can_round = 0;
                     44:   TMP_DECL (marker); TMP_DECL(marker0);
                     45:
                     46:   if (FLAG_NAN(u) || SIGN(u) == -1) { SET_NAN(r); return 0; }
                     47:
                     48:   prec = PREC(r);
                     49:
                     50:   if (!NOTZERO(u))
                     51:     {
                     52:       EXP(r) = 0;
                     53:       MPN_ZERO(MANT(r), ABSSIZE(r));
                     54:       return 1;
                     55:     }
                     56:
                     57:   up = MANT(u);
                     58:
                     59: #ifdef DEBUG
                     60:       printf("Entering square root : ");
                     61:       for(k = usize - 1; k >= 0; k--) { printf("%lu ", up[k]); }
                     62:       printf(".\n");
                     63: #endif
                     64:
                     65:   /* Compare the mantissas */
                     66:
                     67:   usize = (PREC(u) - 1)/BITS_PER_MP_LIMB + 1;
                     68:   rsize = ((PREC(r) + 2 + (EXP(u) & 1))/BITS_PER_MP_LIMB + 1) << 1;
                     69:   rrsize = (PREC(r) + 2 + (EXP(u) & 1))/BITS_PER_MP_LIMB + 1;
                     70:   /* One extra bit is needed in order to get the square root with enough
                     71:      precision ; take one extra bit for rrsize in order to solve more
                     72:      easily the problem of rounding to nearest.
                     73:      Need to have 2*rrsize = rsize...
                     74:      Take one extra bit if the exponent of u is odd since we shall have
                     75:      to shift then.
                     76:   */
                     77:
                     78:   TMP_MARK(marker0);
                     79:   if (EXP(u) & 1) /* Shift u one bit to the right */
                     80:     {
                     81:       if (PREC(u) & (BITS_PER_MP_LIMB - 1))
                     82:        {
                     83:          up = TMP_ALLOC(usize*BYTES_PER_MP_LIMB);
                     84:          mpn_rshift(up, u->_mp_d, usize, 1);
                     85:        }
                     86:       else
                     87:        {
                     88:          up = TMP_ALLOC((usize + 1)*BYTES_PER_MP_LIMB);
                     89:          if (mpn_rshift(up + 1, u->_mp_d, ABSSIZE(u), 1))
                     90:            up [0] = ((mp_limb_t) 1) << (BITS_PER_MP_LIMB - 1);
                     91:          else up[0] = 0;
                     92:          usize++;
                     93:        }
                     94:     }
                     95:
                     96:   EXP(r) = ((EXP(u) + (EXP(u) & 1)) / 2) ;
                     97:
                     98:   do
                     99:     {
                    100:       TMP_MARK (marker);
                    101:
                    102:       err = rsize*BITS_PER_MP_LIMB;
                    103:       if (rsize < usize) { err--; }
                    104:       if (err > rrsize * BITS_PER_MP_LIMB)
                    105:        { err = rrsize * BITS_PER_MP_LIMB; }
                    106:
                    107:       tmp = (mp_ptr) TMP_ALLOC (rsize * BYTES_PER_MP_LIMB);
                    108:       rp = (mp_ptr) TMP_ALLOC (rrsize * BYTES_PER_MP_LIMB);
                    109:       remp = (mp_ptr) TMP_ALLOC (rsize * BYTES_PER_MP_LIMB);
                    110:
                    111:       if (usize >= rsize) {
                    112:        MPN_COPY (tmp, up + usize - rsize, rsize);
                    113:       }
                    114:       else {
                    115:        MPN_COPY (tmp + rsize - usize, up, usize);
                    116:        MPN_ZERO (tmp, rsize - usize);
                    117:       }
                    118:
                    119:       /* Do the real job */
                    120:
                    121: #ifdef DEBUG
                    122:       printf("Taking the sqrt of : ");
                    123:       for(k = rsize - 1; k >= 0; k--) {
                    124:        printf("+%lu*2^%lu",tmp[k],k*mp_bits_per_limb); }
                    125:       printf(".\n");
                    126: #endif
                    127:
                    128:       q_limb = kara_sqrtrem (rp, remp, tmp, rsize);
                    129:
                    130: #ifdef DEBUG
                    131:       printf("The result is : \n");
                    132:       printf("sqrt : ");
                    133:       for(k = rrsize - 1; k >= 0; k--) { printf("%lu ", rp[k]); }
                    134:       printf("(q_limb = %lu)\n", q_limb);
                    135: #endif
                    136:
                    137:       can_round = (mpfr_can_round_raw(rp, rrsize, 1, err,
                    138:                                      GMP_RNDZ, rnd_mode, PREC(r)));
                    139:
                    140:       /* If we used all the limbs of both the dividend and the divisor,
                    141:         then we have the correct RNDZ rounding */
                    142:
                    143:       if (!can_round && (rsize < 2*usize))
                    144:        {
                    145: #ifdef DEBUG
                    146:          printf("Increasing the precision.\n");
                    147: #endif
                    148:          TMP_FREE(marker);
                    149:        }
                    150:     }
                    151:   while (!can_round && (rsize < 2*usize)
                    152:         && (rsize += 2) && (rrsize ++));
                    153:
                    154:
                    155:   /* This part may be deplaced upper to avoid a few mpfr_can_round_raw */
                    156:   /* when the square root is exact. It is however very unprobable that */
                    157:   /* it would improve the behaviour of the present code on average.    */
                    158:
                    159:   if (!q_limb) /* possibly exact */
                    160:     {
                    161:       /* if we have taken into account the whole of up */
                    162:       for (k = usize - rsize - 1; k >= 0; k ++)
                    163:        if (up[k]) break;
                    164:
                    165:       if (k < 0) { exact = 1; goto fin; }
                    166:     }
                    167:
                    168:   if (can_round)
                    169:     {
                    170:       cc = mpfr_round_raw(rp, rp, err, 0, PREC(r), rnd_mode);
                    171:       rrsize = (PREC(r) - 1)/BITS_PER_MP_LIMB + 1;
                    172:     }
                    173:   else
                    174:     /* Use the return value of sqrtrem to decide of the rounding         */
                    175:     /* Note that at this point the sqrt has been computed                */
                    176:     /* EXACTLY. If rounding = GMP_RNDZ, do nothing [comes from           */
                    177:     /* the fact that the exact square root can end with a bunch of ones, */
                    178:     /* and in that case we indeed cannot round if we do not know that    */
                    179:     /* the computation was exact.                                        */
                    180:     switch (rnd_mode)
                    181:       {
                    182:       case GMP_RNDZ :
                    183:       case GMP_RNDD : break;
                    184:
                    185:       case GMP_RNDN :
                    186:        /* Not in the situation ...0 111111 */
                    187:        rw = (PREC(r) + 1) & (BITS_PER_MP_LIMB - 1);
                    188:        if (rw) { rw = BITS_PER_MP_LIMB - rw; nw = 0; } else nw = 1;
                    189:        if ((rp[nw] >> rw) & 1 &&                     /* Not 0111111111 */
                    190:            (q_limb ||                                /* Nonzero remainder */
                    191:            (rw ? (rp[nw] >> (rw + 1)) & 1 :
                    192:             (rp[nw] >> (BITS_PER_MP_LIMB - 1)) & 1))) /* or even rounding */
                    193:          cc = mpn_add_1(rp + nw, rp + nw, rrsize, ((mp_limb_t)1) << rw);
                    194:        break;
                    195:
                    196:       case GMP_RNDU :
                    197:        if (q_limb)
                    198:          cc = mpn_add_1(rp, rp, rrsize, 1 << (BITS_PER_MP_LIMB -
                    199:                                               (PREC(r) &
                    200:                                                (BITS_PER_MP_LIMB - 1))));
                    201:       }
                    202:
                    203:   if (cc) {
                    204:     mpn_rshift(rp, rp, rrsize, 1);
                    205:     rp[rrsize-1] |= (mp_limb_t) 1 << (BITS_PER_MP_LIMB-1);
                    206:     r->_mp_exp++;
                    207:   }
                    208:
                    209:  fin:
                    210:   rsize = rrsize;
                    211:   rrsize = (PREC(r) - 1)/BITS_PER_MP_LIMB + 1;
                    212:   MPN_COPY(r->_mp_d, rp + rsize - rrsize, rrsize);
                    213:
                    214:   if (PREC(r) & (BITS_PER_MP_LIMB - 1))
                    215:     MANT(r) [0] &= ~(((mp_limb_t)1 << (BITS_PER_MP_LIMB -
                    216:                                   (PREC(r) & (BITS_PER_MP_LIMB - 1)))) - 1) ;
                    217:
                    218:   TMP_FREE(marker0); TMP_FREE (marker);
                    219:   return exact;
                    220: }

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