xref: /netbsd-src/external/lgpl3/gmp/dist/mpf/cmp_ui.c (revision c38e7cc395b1472a774ff828e46123de44c628e9)
1 /* mpf_cmp_ui -- Compare a float with an unsigned integer.
2 
3 Copyright 1993-1995, 1999, 2001, 2002, 2015 Free Software Foundation, Inc.
4 
5 This file is part of the GNU MP Library.
6 
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it 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 
14 or
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 
20 or both in parallel, as here.
21 
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.
26 
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/.  */
30 
31 #include "gmp.h"
32 #include "gmp-impl.h"
33 
34 int
35 mpf_cmp_ui (mpf_srcptr u, unsigned long int vval) __GMP_NOTHROW
36 {
37   mp_srcptr up;
38   mp_size_t usize;
39   mp_exp_t uexp;
40   mp_limb_t ulimb;
41 
42   usize = SIZ (u);
43 
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.  */
48 
49   if (vval == 0)
50     return usize != 0;
51 
52   /* 2. Are the exponents different (V's exponent == 1)?  */
53   uexp = EXP (u);
54 
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
62 
63   up = PTR (u);
64 
65   ASSERT (usize > 0);
66   ulimb = up[--usize];
67 #if GMP_NAIL_BITS != 0
68   if (uexp == 2)
69     {
70       if ((ulimb >> GMP_NAIL_BITS) != 0)
71 	return 1;
72       ulimb = (ulimb << GMP_NUMB_BITS);
73       if (usize != 0) ulimb |= up[--usize];
74     }
75 #endif
76 
77   /* 3. Compare the most significant mantissa limb with V.  */
78   if (ulimb != vval)
79     return (ulimb < vval) ? -1 : 1;
80 
81   /* Ignore zeroes at the low end of U.  */
82   for (; *up == 0; ++up)
83     --usize;
84 
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);
88 }
89