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

Annotation of OpenXM_contrib/gmp/mpf/add.c, Revision 1.1

1.1     ! maekawa     1: /* mpf_add -- Add two floats.
        !             2:
        !             3: Copyright (C) 1993, 1994, 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:
        !            25: void
        !            26: #if __STDC__
        !            27: mpf_add (mpf_ptr r, mpf_srcptr u, mpf_srcptr v)
        !            28: #else
        !            29: mpf_add (r, u, v)
        !            30:      mpf_ptr r;
        !            31:      mpf_srcptr u;
        !            32:      mpf_srcptr v;
        !            33: #endif
        !            34: {
        !            35:   mp_srcptr up, vp;
        !            36:   mp_ptr rp, tp;
        !            37:   mp_size_t usize, vsize, rsize;
        !            38:   mp_size_t prec;
        !            39:   mp_exp_t uexp;
        !            40:   mp_size_t ediff;
        !            41:   mp_limb_t cy;
        !            42:   int negate;
        !            43:   TMP_DECL (marker);
        !            44:
        !            45:   usize = u->_mp_size;
        !            46:   vsize = v->_mp_size;
        !            47:
        !            48:   /* Handle special cases that don't work in generic code below.  */
        !            49:   if (usize == 0)
        !            50:     {
        !            51:       mpf_set (r, v);
        !            52:       return;
        !            53:     }
        !            54:   if (vsize == 0)
        !            55:     {
        !            56:       mpf_set (r, u);
        !            57:       return;
        !            58:     }
        !            59:
        !            60:   /* If signs of U and V are different, perform subtraction.  */
        !            61:   if ((usize ^ vsize) < 0)
        !            62:     {
        !            63:       __mpf_struct v_negated;
        !            64:       v_negated._mp_size = -vsize;
        !            65:       v_negated._mp_exp = v->_mp_exp;
        !            66:       v_negated._mp_d = v->_mp_d;
        !            67:       mpf_sub (r, u, &v_negated);
        !            68:       return;
        !            69:     }
        !            70:
        !            71:   TMP_MARK (marker);
        !            72:
        !            73:   /* Signs are now known to be the same.  */
        !            74:   negate = usize < 0;
        !            75:
        !            76:   /* Make U be the operand with the largest exponent.  */
        !            77:   if (u->_mp_exp < v->_mp_exp)
        !            78:     {
        !            79:       mpf_srcptr t;
        !            80:       t = u; u = v; v = t;
        !            81:       usize = u->_mp_size;
        !            82:       vsize = v->_mp_size;
        !            83:     }
        !            84:
        !            85:   usize = ABS (usize);
        !            86:   vsize = ABS (vsize);
        !            87:   up = u->_mp_d;
        !            88:   vp = v->_mp_d;
        !            89:   rp = r->_mp_d;
        !            90:   prec = r->_mp_prec;
        !            91:   uexp = u->_mp_exp;
        !            92:   ediff = u->_mp_exp - v->_mp_exp;
        !            93:
        !            94:   /* If U extends beyond PREC, ignore the part that does.  */
        !            95:   if (usize > prec)
        !            96:     {
        !            97:       up += usize - prec;
        !            98:       usize = prec;
        !            99:     }
        !           100:
        !           101:   /* If V extends beyond PREC, ignore the part that does.
        !           102:      Note that this may make vsize negative.  */
        !           103:   if (vsize + ediff > prec)
        !           104:     {
        !           105:       vp += vsize + ediff - prec;
        !           106:       vsize = prec - ediff;
        !           107:     }
        !           108:
        !           109: #if 0
        !           110:   /* Locate the least significant non-zero limb in (the needed parts
        !           111:      of) U and V, to simplify the code below.  */
        !           112:   while (up[0] == 0)
        !           113:     up++, usize--;
        !           114:   while (vp[0] == 0)
        !           115:     vp++, vsize--;
        !           116: #endif
        !           117:
        !           118:   /* Allocate temp space for the result.  Allocate
        !           119:      just vsize + ediff later???  */
        !           120:   tp = (mp_ptr) TMP_ALLOC (prec * BYTES_PER_MP_LIMB);
        !           121:
        !           122:   if (ediff >= prec)
        !           123:     {
        !           124:       /* V completely cancelled.  */
        !           125:       if (tp != up)
        !           126:        MPN_COPY (rp, up, usize);
        !           127:       rsize = usize;
        !           128:     }
        !           129:   else
        !           130:     {
        !           131:       /* uuuu     |  uuuu     |  uuuu     |  uuuu     |  uuuu    */
        !           132:       /* vvvvvvv  |  vv       |    vvvvv  |    v      |       vv */
        !           133:
        !           134:       if (usize > ediff)
        !           135:        {
        !           136:          /* U and V partially overlaps.  */
        !           137:          if (vsize + ediff <= usize)
        !           138:            {
        !           139:              /* uuuu     */
        !           140:              /*   v      */
        !           141:              mp_size_t size;
        !           142:              size = usize - ediff - vsize;
        !           143:              MPN_COPY (tp, up, size);
        !           144:              cy = mpn_add (tp + size, up + size, usize - size, vp, vsize);
        !           145:              rsize = usize;
        !           146:            }
        !           147:          else
        !           148:            {
        !           149:              /* uuuu     */
        !           150:              /*   vvvvv  */
        !           151:              mp_size_t size;
        !           152:              size = vsize + ediff - usize;
        !           153:              MPN_COPY (tp, vp, size);
        !           154:              cy = mpn_add (tp + size, up, usize, vp + size, usize - ediff);
        !           155:              rsize = vsize + ediff;
        !           156:            }
        !           157:        }
        !           158:       else
        !           159:        {
        !           160:          /* uuuu     */
        !           161:          /*      vv  */
        !           162:          mp_size_t size;
        !           163:          size = vsize + ediff - usize;
        !           164:          MPN_COPY (tp, vp, vsize);
        !           165:          MPN_ZERO (tp + vsize, ediff - usize);
        !           166:          MPN_COPY (tp + size, up, usize);
        !           167:          cy = 0;
        !           168:          rsize = size + usize;
        !           169:        }
        !           170:
        !           171:       MPN_COPY (rp, tp, rsize);
        !           172:       rp[rsize] = cy;
        !           173:       rsize += cy;
        !           174:       uexp += cy;
        !           175:     }
        !           176:
        !           177:   r->_mp_size = negate ? -rsize : rsize;
        !           178:   r->_mp_exp = uexp;
        !           179:   TMP_FREE (marker);
        !           180: }

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