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

Annotation of OpenXM_contrib/gmp/mpfr/cmp_ui.c, Revision 1.1.1.2

1.1       maekawa     1: /* mpfr_cmp_ui -- compare a floating-point number with a machine integer
                      2:
1.1.1.2 ! ohara       3: Copyright 1999, 2001, 2002 Free Software Foundation, Inc.
1.1       maekawa     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
1.1.1.2 ! ohara       8: it under the terms of the GNU Lesser General Public License as published by
        !             9: the Free Software Foundation; either version 2.1 of the License, or (at your
1.1       maekawa    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
1.1.1.2 ! ohara      14: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
1.1       maekawa    15: License for more details.
                     16:
1.1.1.2 ! ohara      17: You should have received a copy of the GNU Lesser General Public License
1.1       maekawa    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 <stdio.h>
                     23: #include "gmp.h"
                     24: #include "gmp-impl.h"
                     25: #include "longlong.h"
                     26: #include "mpfr.h"
1.1.1.2 ! ohara      27: #include "mpfr-impl.h"
1.1       maekawa    28:
                     29: /* returns a positive value if b>i*2^f,
                     30:            a negative value if b<i*2^f,
                     31:            zero if b=i*2^f
                     32: */
                     33:
                     34: int
1.1.1.2 ! ohara      35: mpfr_cmp_ui_2exp (mpfr_srcptr b, unsigned long int i, int f)
1.1       maekawa    36: {
1.1.1.2 ! ohara      37:   int e, k, bn;
        !            38:   mp_limb_t c, *bp;
1.1       maekawa    39:
1.1.1.2 ! ohara      40:   MPFR_ASSERTN(!MPFR_IS_NAN(b));
        !            41:
        !            42:   if (MPFR_IS_INF(b))
        !            43:     return MPFR_SIGN(b);
        !            44:
        !            45:   /* now b is neither NaN nor +/-Infinity */
        !            46:   if (MPFR_IS_ZERO(b))
        !            47:     return i ? -1 : 0;
        !            48:   else if (MPFR_SIGN(b) < 0)
        !            49:     return -1;
        !            50:   /* now b>0 */
        !            51:   else if (i == 0)
        !            52:     return 1;
        !            53:   else
        !            54:     { /* b>0, i>0 */
        !            55:       e = MPFR_EXP(b); /* 2^(e-1) <= b < 2^e */
        !            56:       if (e > f + BITS_PER_MP_LIMB)
        !            57:         return 1;
1.1       maekawa    58:
                     59:     c = (mp_limb_t) i;
                     60:     count_leading_zeros(k, c);
1.1.1.2 ! ohara      61:     k = f + BITS_PER_MP_LIMB - k; /* 2^(k-1) <= i*2^f < 2^k */
        !            62:     if (k != e)
        !            63:       return e - k;
1.1       maekawa    64:
                     65:     /* now k=e */
1.1.1.2 ! ohara      66:     c <<= f + BITS_PER_MP_LIMB - k;
        !            67:     bn = (MPFR_PREC(b) - 1) / BITS_PER_MP_LIMB;
        !            68:     bp = MPFR_MANT(b) + bn;
        !            69:     if (*bp > c)
        !            70:       return 1;
        !            71:     if (*bp < c)
        !            72:       return -1;
1.1       maekawa    73:
                     74:     /* most significant limbs agree, check remaining limbs from b */
1.1.1.2 ! ohara      75:     while (--bn >= 0)
        !            76:       if (*--bp)
        !            77:         return 1;
1.1       maekawa    78:     return 0;
1.1.1.2 ! ohara      79:     }
1.1       maekawa    80: }
                     81:
                     82: /* returns a positive value if b>i*2^f,
                     83:            a negative value if b<i*2^f,
                     84:            zero if b=i*2^f
                     85: */
                     86:
                     87: int
1.1.1.2 ! ohara      88: mpfr_cmp_si_2exp (mpfr_srcptr b, long int i, int f)
1.1       maekawa    89: {
1.1.1.2 ! ohara      90:   int e, k, bn, si;
        !            91:   mp_limb_t c, *bp;
1.1       maekawa    92:
1.1.1.2 ! ohara      93:   MPFR_ASSERTN(!MPFR_IS_NAN(b));
1.1       maekawa    94:
1.1.1.2 ! ohara      95:   si = i < 0 ? -1 : 1; /* sign of i */
        !            96:   if (MPFR_IS_INF(b) || (MPFR_NOTZERO(b) && MPFR_SIGN(b) != si))
        !            97:     return MPFR_SIGN(b);
        !            98:   /* both signs differ */
        !            99:   else if (MPFR_IS_ZERO(b) || i == 0)
        !           100:     return i == 0 ? (MPFR_IS_ZERO(b) ? 0 : MPFR_SIGN(b)) : -si;
        !           101:   else
        !           102:     { /* b and i are of same sign */
        !           103:       e = MPFR_EXP(b); /* 2^(e-1) <= b < 2^e */
        !           104:       if (e > f + BITS_PER_MP_LIMB)
        !           105:         return si;
        !           106:
        !           107:       c = i < 0 ? - (mp_limb_t) i : (mp_limb_t) i;
        !           108:       count_leading_zeros(k, c);
        !           109:       k = f + BITS_PER_MP_LIMB - k; /* 2^(k-1) <= i*2^f < 2^k */
        !           110:       if (k != e)
        !           111:         return si * (e - k);
        !           112:
        !           113:       /* now k = e */
        !           114:       c <<= f + BITS_PER_MP_LIMB - k;
        !           115:       bn = (MPFR_PREC(b) - 1) / BITS_PER_MP_LIMB;
        !           116:       bp = MPFR_MANT(b) + bn;
        !           117:       if (*bp > c)
        !           118:         return si;
        !           119:       if (*bp < c)
        !           120:         return -si;
        !           121:
        !           122:       /* most significant limbs agree, check remaining limbs from b */
        !           123:       while (--bn >= 0)
        !           124:         if (*--bp)
        !           125:           return si;
        !           126:       return 0;
        !           127:     }
1.1       maekawa   128: }

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