1 /* mpfr_cmp_ui_2exp -- compare a floating-point number with an unsigned
2 machine integer multiplied by a power of 2
3
4 Copyright 1999, 2001-2004, 2006-2023 Free Software Foundation, Inc.
5 Contributed by the AriC and Caramba projects, INRIA.
6
7 This file is part of the GNU MPFR Library.
8
9 The GNU MPFR Library is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
13
14 The GNU MPFR Library is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
21 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
22 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
23
24 #define MPFR_NEED_LONGLONG_H
25 #include "mpfr-impl.h"
26
27 /* returns a positive value if b > i*2^f,
28 a negative value if b < i*2^f,
29 zero if b = i*2^f.
30 b must not be NaN
31 */
32
33 int
mpfr_cmp_ui_2exp(mpfr_srcptr b,unsigned long int i,mpfr_exp_t f)34 mpfr_cmp_ui_2exp (mpfr_srcptr b, unsigned long int i, mpfr_exp_t f)
35 {
36 if (MPFR_UNLIKELY( MPFR_IS_SINGULAR(b) ))
37 {
38 if (MPFR_IS_NAN (b))
39 {
40 MPFR_SET_ERANGEFLAG ();
41 return 0;
42 }
43 else if (MPFR_IS_INF(b))
44 return MPFR_INT_SIGN (b);
45 else /* since b cannot be NaN, b=0 here */
46 return i != 0 ? -1 : 0;
47 }
48
49 if (MPFR_IS_NEG (b))
50 return -1;
51 /* now b > 0 */
52 else if (MPFR_UNLIKELY(i == 0))
53 return 1;
54 else /* b > 0, i > 0 */
55 #ifdef MPFR_LONG_WITHIN_LIMB
56 {
57 mpfr_exp_t e;
58 int k;
59 mp_size_t bn;
60 mp_limb_t c, *bp;
61
62 /* i must be representable in a mp_limb_t */
63 MPFR_ASSERTN(i == (mp_limb_t) i);
64
65 e = MPFR_GET_EXP (b); /* 2^(e-1) <= b < 2^e */
66 if (e <= f)
67 return -1;
68 if (f < MPFR_EMAX_MAX - GMP_NUMB_BITS &&
69 e > f + GMP_NUMB_BITS)
70 return 1;
71
72 /* now f < e <= f + GMP_NUMB_BITS */
73 c = (mp_limb_t) i;
74 count_leading_zeros(k, c);
75 if ((int) (e - f) > GMP_NUMB_BITS - k)
76 return 1;
77 if ((int) (e - f) < GMP_NUMB_BITS - k)
78 return -1;
79
80 /* now b and i*2^f have the same exponent */
81 c <<= k;
82 bn = (MPFR_PREC(b) - 1) / GMP_NUMB_BITS;
83 bp = MPFR_MANT(b);
84 if (bp[bn] > c)
85 return 1;
86 if (bp[bn] < c)
87 return -1;
88
89 /* most significant limbs agree, check remaining limbs from b */
90 while (bn > 0)
91 if (bp[--bn] != 0)
92 return 1;
93 return 0;
94 }
95 #else
96 {
97 mpfr_t uu;
98 int ret;
99 MPFR_SAVE_EXPO_DECL (expo);
100
101 mpfr_init2 (uu, sizeof (unsigned long) * CHAR_BIT);
102 /* Warning: i*2^f might be outside the current exponent range! */
103 MPFR_SAVE_EXPO_MARK (expo);
104 mpfr_set_ui_2exp (uu, i, f, MPFR_RNDZ);
105 MPFR_SAVE_EXPO_FREE (expo);
106 ret = mpfr_cmp (b, uu);
107 mpfr_clear (uu);
108 return ret;
109 }
110 #endif /* MPFR_LONG_WITHIN_LIMB */
111 }
112
113 #undef mpfr_cmp_ui
114 int
mpfr_cmp_ui(mpfr_srcptr b,unsigned long int i)115 mpfr_cmp_ui (mpfr_srcptr b, unsigned long int i)
116 {
117 return mpfr_cmp_ui_2exp (b, i, 0);
118 }
119