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

Annotation of OpenXM_contrib/gmp/mpf/cmp_si.c, Revision 1.1.1.3

1.1       maekawa     1: /* mpf_cmp_si -- Compare a float with a signed integer.
                      2:
1.1.1.3 ! ohara       3: Copyright 1993, 1994, 1995, 1999, 2000, 2001, 2002 Free Software Foundation,
        !             4: Inc.
1.1       maekawa     5:
                      6: This file is part of the GNU MP Library.
                      7:
                      8: The GNU MP Library is free software; you can redistribute it and/or modify
1.1.1.2   maekawa     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
1.1       maekawa    11: option) any later version.
                     12:
                     13: The GNU MP Library is distributed in the hope that it will be useful, but
                     14: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1.1.1.2   maekawa    15: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
1.1       maekawa    16: License for more details.
                     17:
1.1.1.2   maekawa    18: You should have received a copy of the GNU Lesser General Public License
1.1       maekawa    19: along with the GNU MP 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 "gmp.h"
                     24: #include "gmp-impl.h"
                     25:
                     26: int
1.1.1.3 ! ohara      27: mpf_cmp_si (mpf_srcptr u, long int vval)
1.1       maekawa    28: {
                     29:   mp_srcptr up;
                     30:   mp_size_t usize;
                     31:   mp_exp_t uexp;
1.1.1.3 ! ohara      32:   mp_limb_t ulimb;
1.1       maekawa    33:   int usign;
                     34:
                     35:   uexp = u->_mp_exp;
                     36:   usize = u->_mp_size;
                     37:
                     38:   /* 1. Are the signs different?  */
1.1.1.3 ! ohara      39:   if ((usize < 0) == (vval < 0)) /* don't use xor, type size may differ */
1.1       maekawa    40:     {
                     41:       /* U and V are both non-negative or both negative.  */
                     42:       if (usize == 0)
1.1.1.3 ! ohara      43:        /* vval >= 0 */
        !            44:        return -(vval != 0);
        !            45:       if (vval == 0)
1.1       maekawa    46:        /* usize >= 0 */
                     47:        return usize != 0;
                     48:       /* Fall out.  */
                     49:     }
                     50:   else
                     51:     {
                     52:       /* Either U or V is negative, but not both.  */
                     53:       return usize >= 0 ? 1 : -1;
                     54:     }
                     55:
                     56:   /* U and V have the same sign and are both non-zero.  */
                     57:
                     58:   usign = usize >= 0 ? 1 : -1;
1.1.1.3 ! ohara      59:   usize = ABS (usize);
        !            60:   vval = ABS (vval);
1.1       maekawa    61:
                     62:   /* 2. Are the exponents different (V's exponent == 1)?  */
1.1.1.3 ! ohara      63: #if GMP_NAIL_BITS != 0
        !            64:   if (uexp > 1 + (vval > GMP_NUMB_MAX))
        !            65:     return usign;
        !            66:   if (uexp < 1 + (vval > GMP_NUMB_MAX))
        !            67:     return -usign;
        !            68: #else
1.1       maekawa    69:   if (uexp > 1)
                     70:     return usign;
                     71:   if (uexp < 1)
                     72:     return -usign;
1.1.1.3 ! ohara      73: #endif
1.1       maekawa    74:
                     75:   up = u->_mp_d;
                     76:
1.1.1.3 ! ohara      77:   ulimb = up[usize - 1];
        !            78: #if GMP_NAIL_BITS != 0
        !            79:   if (usize >= 2 && uexp == 2)
        !            80:     {
        !            81:       if ((ulimb >> GMP_NAIL_BITS) != 0)
        !            82:        return 1;
        !            83:       ulimb = (ulimb << GMP_NUMB_BITS) | up[usize - 2];
        !            84:       usize--;
        !            85:     }
        !            86: #endif
        !            87:   usize--;
        !            88:
1.1.1.2   maekawa    89:   /* 3. Compare the most significant mantissa limb with V.  */
1.1.1.3 ! ohara      90:   if (ulimb > (unsigned long) vval)
1.1.1.2   maekawa    91:     return usign;
1.1.1.3 ! ohara      92:   else if (ulimb < (unsigned long) vval)
1.1.1.2   maekawa    93:     return -usign;
                     94:
                     95:   /* Ignore zeroes at the low end of U.  */
1.1       maekawa    96:   while (*up == 0)
                     97:     {
                     98:       up++;
                     99:       usize--;
                    100:     }
                    101:
1.1.1.2   maekawa   102:   /* 4. Now, if the number of limbs are different, we have a difference
1.1       maekawa   103:      since we have made sure the trailing limbs are not zero.  */
1.1.1.3 ! ohara     104:   if (usize > 0)
1.1       maekawa   105:     return usign;
                    106:
                    107:   /* Wow, we got zero even if we tried hard to avoid it.  */
                    108:   return 0;
                    109: }

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