source: trunk/third/gmp/mpf/cmp_si.c @ 18191

Revision 18191, 2.7 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18190, which included commits to RCS files with non-trunk default branches.
Line 
1/* mpf_cmp_si -- Compare a float with a signed integer.
2
3Copyright 1993, 1994, 1995, 1999, 2000, 2001, 2002 Free Software Foundation,
4Inc.
5
6This file is part of the GNU MP Library.
7
8The GNU MP Library is free software; you can redistribute it and/or modify
9it under the terms of the GNU Lesser General Public License as published by
10the Free Software Foundation; either version 2.1 of the License, or (at your
11option) any later version.
12
13The GNU MP Library is distributed in the hope that it will be useful, but
14WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16License for more details.
17
18You should have received a copy of the GNU Lesser General Public License
19along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
20the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21MA 02111-1307, USA. */
22
23#include "gmp.h"
24#include "gmp-impl.h"
25
26int
27mpf_cmp_si (mpf_srcptr u, long int vval)
28{
29  mp_srcptr up;
30  mp_size_t usize;
31  mp_exp_t uexp;
32  mp_limb_t ulimb;
33  int usign;
34
35  uexp = u->_mp_exp;
36  usize = u->_mp_size;
37
38  /* 1. Are the signs different?  */
39  if ((usize < 0) == (vval < 0)) /* don't use xor, type size may differ */
40    {
41      /* U and V are both non-negative or both negative.  */
42      if (usize == 0)
43        /* vval >= 0 */
44        return -(vval != 0);
45      if (vval == 0)
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;
59  usize = ABS (usize);
60  vval = ABS (vval);
61
62  /* 2. Are the exponents different (V's exponent == 1)?  */
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
69  if (uexp > 1)
70    return usign;
71  if (uexp < 1)
72    return -usign;
73#endif
74
75  up = u->_mp_d;
76
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
89  /* 3. Compare the most significant mantissa limb with V.  */
90  if (ulimb > (unsigned long) vval)
91    return usign;
92  else if (ulimb < (unsigned long) vval)
93    return -usign;
94
95  /* Ignore zeroes at the low end of U.  */
96  while (*up == 0)
97    {
98      up++;
99      usize--;
100    }
101
102  /* 4. Now, if the number of limbs are different, we have a difference
103     since we have made sure the trailing limbs are not zero.  */
104  if (usize > 0)
105    return usign;
106
107  /* Wow, we got zero even if we tried hard to avoid it.  */
108  return 0;
109}
Note: See TracBrowser for help on using the repository browser.