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

Annotation of OpenXM_contrib/gmp/mpfr/eq.c, Revision 1.1

1.1     ! ohara       1: /* mpfr_eq -- Compare two floats up to a specified bit #.
        !             2:
        !             3: Copyright 1999, 2001 Free Software Foundation, Inc.
        !             4: (Copied from GNU MP, file mpf_eq.)
        !             5:
        !             6: This file is part of the MPFR Library.
        !             7:
        !             8: The MPFR Library is free software; you can redistribute it and/or modify
        !             9: it under the terms of the GNU Lesser General Public License as published by
        !            10: the Free Software Foundation; either version 2.1 of the License, or (at your
        !            11: option) any later version.
        !            12:
        !            13: The MPFR Library is distributed in the hope that it will be useful, but
        !            14: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
        !            15: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
        !            16: License for more details.
        !            17:
        !            18: You should have received a copy of the GNU Lesser General Public License
        !            19: along with the MPFR Library; see the file COPYING.LIB.  If not, write to
        !            20: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
        !            21: MA 02111-1307, USA. */
        !            22:
        !            23: #include <stdio.h>
        !            24: #include "gmp.h"
        !            25: #include "gmp-impl.h"
        !            26: #include "mpfr.h"
        !            27: #include "mpfr-impl.h"
        !            28:
        !            29: int
        !            30: mpfr_eq (mpfr_srcptr u, mpfr_srcptr v, unsigned long int n_bits)
        !            31: {
        !            32:   mp_srcptr up, vp;
        !            33:   mp_size_t usize, vsize, size, i;
        !            34:   mp_exp_t uexp, vexp;
        !            35:   int usign, k;
        !            36:
        !            37:   uexp = MPFR_EXP(u);
        !            38:   vexp = MPFR_EXP(v);
        !            39:
        !            40:   usize = (MPFR_PREC(u)-1)/BITS_PER_MP_LIMB + 1;
        !            41:   vsize = (MPFR_PREC(v)-1)/BITS_PER_MP_LIMB + 1;
        !            42:
        !            43:   usign = MPFR_SIGN(u);
        !            44:
        !            45:   /* 1. Are the signs different?  */
        !            46:   if (usign ==  MPFR_SIGN(v))
        !            47:     {
        !            48:       /* U and V are both non-negative or both negative.  */
        !            49:       if (!MPFR_NOTZERO(u))
        !            50:        return !MPFR_NOTZERO(v);
        !            51:       if (!MPFR_NOTZERO(v))
        !            52:        return !MPFR_NOTZERO(u);
        !            53:
        !            54:       /* Fall out.  */
        !            55:     }
        !            56:   else
        !            57:     {
        !            58:       /* Either U or V is negative, but not both.  */
        !            59:       if (MPFR_NOTZERO(u) || MPFR_NOTZERO(v))
        !            60:        return 0;
        !            61:       else return 1; /* particular case -0 = +0 */
        !            62:     }
        !            63:
        !            64:   /* U and V have the same sign and are both non-zero.  */
        !            65:   if (MPFR_IS_INF(u))
        !            66:     return (MPFR_IS_INF(v) && (usign == MPFR_SIGN(v)));
        !            67:   else if (MPFR_IS_INF(v)) return 0;
        !            68:
        !            69:   if (MPFR_IS_NAN(u) || MPFR_IS_NAN(v)) return 0;
        !            70:
        !            71:   /* 2. Are the exponents different?  */
        !            72:   if (uexp > vexp)
        !            73:     return 0;                  /* ??? handle (uexp = vexp + 1)   */
        !            74:   if (vexp > uexp)
        !            75:     return 0;                  /* ??? handle (vexp = uexp + 1)   */
        !            76:
        !            77:   usize = ABS (usize);
        !            78:   vsize = ABS (vsize);
        !            79:
        !            80:   up = MPFR_MANT(u);
        !            81:   vp = MPFR_MANT(v);
        !            82:
        !            83:   if (usize > vsize)
        !            84:     {
        !            85:       if (vsize * BITS_PER_MP_LIMB < n_bits)
        !            86:        {
        !            87:          k = usize - vsize - 1;
        !            88:          while (k >= 0 && !up[k]) --k;
        !            89:          if (k >= 0)
        !            90:            return 0;           /* surely too different */
        !            91:        }
        !            92:       size = vsize;
        !            93:     }
        !            94:   else if (vsize > usize)
        !            95:     {
        !            96:       if (usize * BITS_PER_MP_LIMB < n_bits)
        !            97:        {
        !            98:          k = vsize - usize - 1;
        !            99:          while (k >= 0 && !vp[k]) --k;
        !           100:          if (k >= 0)
        !           101:            return 0;           /* surely too different */
        !           102:        }
        !           103:       size = usize;
        !           104:     }
        !           105:   else
        !           106:     {
        !           107:       size = usize;
        !           108:     }
        !           109:
        !           110:   if (size > (n_bits + BITS_PER_MP_LIMB - 1) / BITS_PER_MP_LIMB)
        !           111:     size = (n_bits + BITS_PER_MP_LIMB - 1) / BITS_PER_MP_LIMB;
        !           112:
        !           113:   up += usize - size;
        !           114:   vp += vsize - size;
        !           115:
        !           116:   for (i = size - 1; i > 0; i--)
        !           117:     {
        !           118:       if (up[i] != vp[i])
        !           119:        return 0;
        !           120:     }
        !           121:
        !           122:   if (n_bits & (BITS_PER_MP_LIMB - 1))
        !           123:     return (up[i] >> (BITS_PER_MP_LIMB - (n_bits & (BITS_PER_MP_LIMB - 1))) ==
        !           124:            vp[i] >> (BITS_PER_MP_LIMB - (n_bits & (BITS_PER_MP_LIMB - 1))));
        !           125:   else
        !           126:     return (up[i] == vp[i]);
        !           127: }

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