beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpf / cmp_si.c
blob3bf98211ddb6a3ef40995bf90f034aa336e38b65
1 /* mpf_cmp_si -- Compare a float with a signed integer.
3 Copyright 1993-1995, 1999-2002, 2004, 2012, 2015 Free Software
4 Foundation, Inc.
6 This file is part of the GNU MP Library.
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of either:
11 * the GNU Lesser General Public License as published by the Free
12 Software Foundation; either version 3 of the License, or (at your
13 option) any later version.
17 * the GNU General Public License as published by the Free Software
18 Foundation; either version 2 of the License, or (at your option) any
19 later version.
21 or both in parallel, as here.
23 The GNU MP Library is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 for more details.
28 You should have received copies of the GNU General Public License and the
29 GNU Lesser General Public License along with the GNU MP Library. If not,
30 see https://www.gnu.org/licenses/. */
32 #include "gmp.h"
33 #include "gmp-impl.h"
35 int
36 mpf_cmp_si (mpf_srcptr u, long int vval) __GMP_NOTHROW
38 mp_srcptr up;
39 mp_size_t usize;
40 mp_exp_t uexp;
41 mp_limb_t ulimb;
42 int usign;
43 unsigned long abs_vval;
45 usize = SIZ (u);
47 /* 1. Are the signs different? */
48 if ((usize < 0) == (vval < 0)) /* don't use xor, type size may differ */
50 /* U and V are both non-negative or both negative. */
51 if (usize == 0)
52 /* vval >= 0 */
53 return -(vval != 0);
54 if (vval == 0)
55 /* usize >= 0 */
56 return usize != 0;
57 /* Fall out. */
59 else
61 /* Either U or V is negative, but not both. */
62 return usize >= 0 ? 1 : -1;
65 /* U and V have the same sign and are both non-zero. */
67 /* 2. Are the exponents different (V's exponent == 1)? */
68 uexp = EXP (u);
69 usign = usize >= 0 ? 1 : -1;
70 usize = ABS (usize);
71 abs_vval = ABS_CAST (unsigned long, vval);
73 #if GMP_NAIL_BITS != 0
74 if (uexp != 1 + (abs_vval > GMP_NUMB_MAX))
75 return (uexp < 1 + (abs_vval > GMP_NUMB_MAX)) ? -usign : usign;
76 #else
77 if (uexp != 1)
78 return (uexp < 1) ? -usign : usign;
79 #endif
81 up = PTR (u);
83 ASSERT (usize > 0);
84 ulimb = up[--usize];
85 #if GMP_NAIL_BITS != 0
86 if (uexp == 2)
88 if ((ulimb >> GMP_NAIL_BITS) != 0)
89 return usign;
90 ulimb = (ulimb << GMP_NUMB_BITS);
91 if (usize != 0) ulimb |= up[--usize];
93 #endif
95 /* 3. Compare the most significant mantissa limb with V. */
96 if (ulimb != abs_vval)
97 return (ulimb < abs_vval) ? -usign : usign;
99 /* Ignore zeroes at the low end of U. */
100 for (; *up == 0; ++up)
101 --usize;
103 /* 4. Now, if the number of limbs are different, we have a difference
104 since we have made sure the trailing limbs are not zero. */
105 if (usize > 0)
106 return usign;
108 /* Wow, we got zero even if we tried hard to avoid it. */
109 return 0;