beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpf / cmp_ui.c
blob198eb21e5173fbf222d1c4372f2824542ed0b3b8
1 /* mpf_cmp_ui -- Compare a float with an unsigned integer.
3 Copyright 1993-1995, 1999, 2001, 2002, 2015 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of either:
10 * the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
16 * the GNU General Public License as published by the Free Software
17 Foundation; either version 2 of the License, or (at your option) any
18 later version.
20 or both in parallel, as here.
22 The GNU MP Library is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 for more details.
27 You should have received copies of the GNU General Public License and the
28 GNU Lesser General Public License along with the GNU MP Library. If not,
29 see https://www.gnu.org/licenses/. */
31 #include "gmp.h"
32 #include "gmp-impl.h"
34 int
35 mpf_cmp_ui (mpf_srcptr u, unsigned long int vval) __GMP_NOTHROW
37 mp_srcptr up;
38 mp_size_t usize;
39 mp_exp_t uexp;
40 mp_limb_t ulimb;
42 usize = SIZ (u);
44 /* 1. Is U negative? */
45 if (usize < 0)
46 return -1;
47 /* We rely on usize being non-negative in the code that follows. */
49 if (vval == 0)
50 return usize != 0;
52 /* 2. Are the exponents different (V's exponent == 1)? */
53 uexp = EXP (u);
55 #if GMP_NAIL_BITS != 0
56 if (uexp != 1 + (vval > GMP_NUMB_MAX))
57 return (uexp < 1 + (vval > GMP_NUMB_MAX)) ? -1 : 1;
58 #else
59 if (uexp != 1)
60 return (uexp < 1) ? -1 : 1;
61 #endif
63 up = PTR (u);
65 ASSERT (usize > 0);
66 ulimb = up[--usize];
67 #if GMP_NAIL_BITS != 0
68 if (uexp == 2)
70 if ((ulimb >> GMP_NAIL_BITS) != 0)
71 return 1;
72 ulimb = (ulimb << GMP_NUMB_BITS);
73 if (usize != 0) ulimb |= up[--usize];
75 #endif
77 /* 3. Compare the most significant mantissa limb with V. */
78 if (ulimb != vval)
79 return (ulimb < vval) ? -1 : 1;
81 /* Ignore zeroes at the low end of U. */
82 for (; *up == 0; ++up)
83 --usize;
85 /* 4. Now, if the number of limbs are different, we have a difference
86 since we have made sure the trailing limbs are not zero. */
87 return (usize > 0);