[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.1     ! maekawa     1: /* mpfr_cmp_ui -- compare a floating-point number with a machine integer
        !             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 <stdio.h>
        !            23: #include "gmp.h"
        !            24: #include "gmp-impl.h"
        !            25: #include "longlong.h"
        !            26: #include "mpfr.h"
        !            27:
        !            28: /* returns a positive value if b>i*2^f,
        !            29:            a negative value if b<i*2^f,
        !            30:            zero if b=i*2^f
        !            31: */
        !            32:
        !            33: int
        !            34: #if __STDC__
        !            35: mpfr_cmp_ui_2exp ( mpfr_srcptr b, unsigned long int i, int f )
        !            36: #else
        !            37: mpfr_cmp_ui_2exp (b, i, f)
        !            38:      mpfr_srcptr b;
        !            39:      unsigned long int i;
        !            40:      int f;
        !            41: #endif
        !            42: {
        !            43:   int e, k, bn; mp_limb_t c, *bp;
        !            44:
        !            45:   if (SIGN(b)<0) return -1;
        !            46:   else if (!NOTZERO(b)) return((i) ? -1 : 0);
        !            47:   else { /* b>0 */
        !            48:     e = EXP(b); /* 2^(e-1) <= b < 2^e */
        !            49:     if (e>f+mp_bits_per_limb) return 1;
        !            50:
        !            51:     c = (mp_limb_t) i;
        !            52:     count_leading_zeros(k, c);
        !            53:     k = f+mp_bits_per_limb - k; /* 2^(k-1) <= i*2^f < 2^k */
        !            54:     if (k!=e) return (e-k);
        !            55:
        !            56:     /* now k=e */
        !            57:     c <<= (f+mp_bits_per_limb-k);
        !            58:     bn = (PREC(b)-1)/mp_bits_per_limb;
        !            59:     bp = MANT(b) + bn;
        !            60:     if (*bp>c) return 1;
        !            61:     else if (*bp<c) return -1;
        !            62:
        !            63:     /* most significant limbs agree, check remaining limbs from b */
        !            64:     while (--bn>=0)
        !            65:       if (*--bp) return 1;
        !            66:     return 0;
        !            67:   }
        !            68: }
        !            69:
        !            70: /* returns a positive value if b>i*2^f,
        !            71:            a negative value if b<i*2^f,
        !            72:            zero if b=i*2^f
        !            73: */
        !            74:
        !            75: int
        !            76: #if __STDC__
        !            77: mpfr_cmp_si_2exp ( mpfr_srcptr b, long int i, int f )
        !            78: #else
        !            79: mpfr_cmp_si_2exp(b, i, f)
        !            80:      mpfr_srcptr b;
        !            81:      long int i;
        !            82:      int f;
        !            83: #endif
        !            84: {
        !            85:   int e, k, bn, si; mp_limb_t c, *bp;
        !            86:
        !            87:   si = (i<0) ? -1 : 1; /* sign of i */
        !            88:   if (SIGN(b)*i<0) return SIGN(b); /* both signs differ */
        !            89:   else if (!NOTZERO(b) || (i==0)) { /* one is zero */
        !            90:     if (i==0) return ((NOTZERO(b)) ? SIGN(b) : 0);
        !            91:     else return si; /* b is zero */
        !            92:
        !            93:   }
        !            94:   else { /* b and i are of same sign */
        !            95:     e = EXP(b); /* 2^(e-1) <= b < 2^e */
        !            96:     if (e>f+mp_bits_per_limb) return si;
        !            97:
        !            98:     c = (mp_limb_t) ((i<0) ? -i : i);
        !            99:     count_leading_zeros(k, c);
        !           100:     k = f+mp_bits_per_limb - k; /* 2^(k-1) <= i*2^f < 2^k */
        !           101:     if (k!=e) return (si*(e-k));
        !           102:
        !           103:     /* now k=e */
        !           104:     c <<= (f+mp_bits_per_limb-k);
        !           105:     bn = (PREC(b)-1)/mp_bits_per_limb;
        !           106:     bp = MANT(b) + bn;
        !           107:     if (*bp>c) return si;
        !           108:     else if (*bp<c) return -si;
        !           109:
        !           110:     /* most significant limbs agree, check remaining limbs from b */
        !           111:     while (--bn>=0)
        !           112:       if (*--bp) return si;
        !           113:     return 0;
        !           114:   }
        !           115: }
        !           116:

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