xref: /dragonfly/contrib/gmp/mpf/cmp_si.c (revision 73e0051e)
1 /* mpf_cmp_si -- Compare a float with a signed integer.
2 
3 Copyright 1993, 1994, 1995, 1999, 2000, 2001, 2002, 2004 Free Software
4 Foundation, Inc.
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
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
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
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 GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
20 
21 #include "gmp.h"
22 #include "gmp-impl.h"
23 
24 int
25 mpf_cmp_si (mpf_srcptr u, long int vval)
26 {
27   mp_srcptr up;
28   mp_size_t usize;
29   mp_exp_t uexp;
30   mp_limb_t ulimb;
31   int usign;
32 
33   uexp = u->_mp_exp;
34   usize = u->_mp_size;
35 
36   /* 1. Are the signs different?  */
37   if ((usize < 0) == (vval < 0)) /* don't use xor, type size may differ */
38     {
39       /* U and V are both non-negative or both negative.  */
40       if (usize == 0)
41 	/* vval >= 0 */
42 	return -(vval != 0);
43       if (vval == 0)
44 	/* usize >= 0 */
45 	return usize != 0;
46       /* Fall out.  */
47     }
48   else
49     {
50       /* Either U or V is negative, but not both.  */
51       return usize >= 0 ? 1 : -1;
52     }
53 
54   /* U and V have the same sign and are both non-zero.  */
55 
56   usign = usize >= 0 ? 1 : -1;
57   usize = ABS (usize);
58   vval = ABS (vval);
59 
60   /* 2. Are the exponents different (V's exponent == 1)?  */
61 #if GMP_NAIL_BITS != 0
62   if (uexp > 1 + ((unsigned long) vval > GMP_NUMB_MAX))
63     return usign;
64   if (uexp < 1 + ((unsigned long) vval > GMP_NUMB_MAX))
65     return -usign;
66 #else
67   if (uexp > 1)
68     return usign;
69   if (uexp < 1)
70     return -usign;
71 #endif
72 
73   up = u->_mp_d;
74 
75   ulimb = up[usize - 1];
76 #if GMP_NAIL_BITS != 0
77   if (usize >= 2 && uexp == 2)
78     {
79       if ((ulimb >> GMP_NAIL_BITS) != 0)
80 	return usign;
81       ulimb = (ulimb << GMP_NUMB_BITS) | up[usize - 2];
82       usize--;
83     }
84 #endif
85   usize--;
86 
87   /* 3. Compare the most significant mantissa limb with V.  */
88   if (ulimb > (unsigned long) vval)
89     return usign;
90   else if (ulimb < (unsigned long) vval)
91     return -usign;
92 
93   /* Ignore zeroes at the low end of U.  */
94   while (*up == 0)
95     {
96       up++;
97       usize--;
98     }
99 
100   /* 4. Now, if the number of limbs are different, we have a difference
101      since we have made sure the trailing limbs are not zero.  */
102   if (usize > 0)
103     return usign;
104 
105   /* Wow, we got zero even if we tried hard to avoid it.  */
106   return 0;
107 }
108