1 /* mpq_cmp(u,v) -- Compare U, V. Return positive, zero, or negative
2 based on if U > V, U == V, or U < V.
4 Copyright 1991, 1994, 1996, 2001, 2002, 2005, 2015 Free Software 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
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
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/. */
37 mpq_cmp_numden (mpq_srcptr op1
, mpz_srcptr num_op2
, mpz_srcptr den_op2
)
39 mp_size_t num1_size
= SIZ(NUM(op1
));
40 mp_size_t den1_size
= SIZ(DEN(op1
));
41 mp_size_t num2_size
= SIZ(num_op2
);
42 mp_size_t den2_size
= SIZ(den_op2
);
45 mp_size_t tmp1_size
, tmp2_size
;
46 mp_ptr tmp1_ptr
, tmp2_ptr
;
51 /* need canonical signs to get right result */
52 ASSERT (den1_size
> 0);
53 ASSERT (den2_size
> 0);
59 if ((num1_size
^ num2_size
) < 0) /* I.e. are the signs different? */
62 num1_sign
= num1_size
;
63 num1_size
= ABS (num1_size
);
65 /* THINK: Does storing d1h and d2h make sense? */
66 d1h
= PTR(DEN(op1
))[den1_size
- 1];
67 d2h
= PTR(den_op2
)[den2_size
- 1];
68 op2_is_int
= (den2_size
| d2h
) == 1;
69 if (op2_is_int
== (den1_size
| d1h
)) /* Both ops are integers */
70 /* return mpz_cmp (NUM (op1), num_op2); */
74 if (num1_sign
!= num2_size
)
75 return num1_sign
- num2_size
;
77 cmp
= mpn_cmp (PTR(NUM(op1
)), PTR(num_op2
), num1_size
);
78 return (num1_sign
> 0 ? cmp
: -cmp
);
81 num2_size
= ABS (num2_size
);
83 tmp1_size
= num1_size
+ den2_size
;
84 tmp2_size
= num2_size
+ den1_size
;
86 /* 1. Check to see if we can tell which operand is larger by just looking at
87 the number of limbs. */
89 /* NUM1 x DEN2 is either TMP1_SIZE limbs or TMP1_SIZE-1 limbs.
90 Same for NUM1 x DEN1 with respect to TMP2_SIZE. */
91 if (tmp1_size
> tmp2_size
+ 1)
92 /* NUM1 x DEN2 is surely larger in magnitude than NUM2 x DEN1. */
94 if (tmp2_size
+ op2_is_int
> tmp1_size
+ 1)
95 /* NUM1 x DEN2 is surely smaller in magnitude than NUM2 x DEN1. */
98 /* 2. Same, but compare the number of significant bits. */
101 mp_bitcnt_t bits1
, bits2
;
103 count_leading_zeros (cnt1
, PTR(NUM(op1
))[num1_size
- 1]);
104 count_leading_zeros (cnt2
, d2h
);
105 bits1
= (mp_bitcnt_t
) tmp1_size
* GMP_NUMB_BITS
- cnt1
- cnt2
+ 2 * GMP_NAIL_BITS
;
107 count_leading_zeros (cnt1
, PTR(num_op2
)[num2_size
- 1]);
108 count_leading_zeros (cnt2
, d1h
);
109 bits2
= (mp_bitcnt_t
) tmp2_size
* GMP_NUMB_BITS
- cnt1
- cnt2
+ 2 * GMP_NAIL_BITS
;
111 if (bits1
> bits2
+ 1)
113 if (bits2
+ op2_is_int
> bits1
+ 1)
117 /* 3. Finally, cross multiply and compare. */
122 tmp2_ptr
= TMP_ALLOC_LIMBS (tmp2_size
);
123 tmp1_ptr
= PTR(NUM(op1
));
128 TMP_ALLOC_LIMBS_2 (tmp1_ptr
,tmp1_size
, tmp2_ptr
,tmp2_size
);
130 if (num1_size
>= den2_size
)
131 tmp1_size
-= 0 == mpn_mul (tmp1_ptr
,
132 PTR(NUM(op1
)), num1_size
,
133 PTR(den_op2
), den2_size
);
135 tmp1_size
-= 0 == mpn_mul (tmp1_ptr
,
136 PTR(den_op2
), den2_size
,
137 PTR(NUM(op1
)), num1_size
);
140 if (num2_size
>= den1_size
)
141 tmp2_size
-= 0 == mpn_mul (tmp2_ptr
,
142 PTR(num_op2
), num2_size
,
143 PTR(DEN(op1
)), den1_size
);
145 tmp2_size
-= 0 == mpn_mul (tmp2_ptr
,
146 PTR(DEN(op1
)), den1_size
,
147 PTR(num_op2
), num2_size
);
150 cc
= tmp1_size
- tmp2_size
!= 0
151 ? tmp1_size
- tmp2_size
: mpn_cmp (tmp1_ptr
, tmp2_ptr
, tmp1_size
);
153 return num1_sign
< 0 ? -cc
: cc
;
157 mpq_cmp (mpq_srcptr op1
, mpq_srcptr op2
)
159 return mpq_cmp_numden (op1
, NUM(op2
), DEN(op2
));
163 mpq_cmp_z (mpq_srcptr op1
, mpz_srcptr op2
)
165 const static mp_limb_t one
= 1;
166 const static mpz_t den
= MPZ_ROINIT_N ((mp_limb_t
*) &one
, 1);
168 return mpq_cmp_numden (op1
, op2
, den
);