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