1 /* mpq_cmp_ui(u,vn,vd) -- Compare U with Vn/Vd. Return positive, zero, or
2 negative based on if U > V, U == V, or U < V. Vn and Vd may have
5 Copyright 1993, 1994, 1996, 2000, 2001, 2002, 2003, 2005 Free Software
8 This file is part of the GNU MP Library.
10 The GNU MP Library is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or (at your
13 option) any later version.
15 The GNU MP Library is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 License for more details.
20 You should have received a copy of the GNU Lesser General Public License
21 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
27 _mpq_cmp_ui (const MP_RAT *op1, unsigned long int num2, unsigned long int den2)
29 mp_size_t num1_size = op1->_mp_num._mp_size;
30 mp_size_t den1_size = op1->_mp_den._mp_size;
31 mp_size_t tmp1_size, tmp2_size;
32 mp_ptr tmp1_ptr, tmp2_ptr;
37 #if GMP_NAIL_BITS != 0
38 if ((num2 | den2) > GMP_NUMB_MAX)
42 mpz_set_ui (mpq_numref (op2), num2);
43 mpz_set_ui (mpq_denref (op2), den2);
44 cc = mpq_cmp (op1, op2);
50 /* need canonical sign to get right result */
51 ASSERT (den1_size > 0);
63 /* NUM1 x DEN2 is either TMP1_SIZE limbs or TMP1_SIZE-1 limbs.
64 Same for NUM1 x DEN1 with respect to TMP2_SIZE. */
65 if (num1_size > den1_size + 1)
66 /* NUM1 x DEN2 is surely larger in magnitude than NUM2 x DEN1. */
68 if (den1_size > num1_size + 1)
69 /* NUM1 x DEN2 is surely smaller in magnitude than NUM2 x DEN1. */
73 tmp1_ptr = TMP_ALLOC_LIMBS (num1_size + 1);
74 tmp2_ptr = TMP_ALLOC_LIMBS (den1_size + 1);
76 cy_limb = mpn_mul_1 (tmp1_ptr, op1->_mp_num._mp_d, num1_size,
78 tmp1_ptr[num1_size] = cy_limb;
79 tmp1_size = num1_size + (cy_limb != 0);
81 cy_limb = mpn_mul_1 (tmp2_ptr, op1->_mp_den._mp_d, den1_size,
83 tmp2_ptr[den1_size] = cy_limb;
84 tmp2_size = den1_size + (cy_limb != 0);
86 cc = tmp1_size - tmp2_size != 0
87 ? tmp1_size - tmp2_size : mpn_cmp (tmp1_ptr, tmp2_ptr, tmp1_size);