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 3 common factors. 4 5 Copyright 1993, 1994, 1996, 2000, 2001, 2002, 2003, 2005 Free Software 6 Foundation, Inc. 7 8 This file is part of the GNU MP Library. 9 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. 14 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. 19 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/. */ 22 23 #include "gmp.h" 24 #include "gmp-impl.h" 25 26 int 27 _mpq_cmp_ui (const MP_RAT *op1, unsigned long int num2, unsigned long int den2) 28 { 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; 33 mp_limb_t cy_limb; 34 int cc; 35 TMP_DECL; 36 37 #if GMP_NAIL_BITS != 0 38 if ((num2 | den2) > GMP_NUMB_MAX) 39 { 40 mpq_t op2; 41 mpq_init (op2); 42 mpz_set_ui (mpq_numref (op2), num2); 43 mpz_set_ui (mpq_denref (op2), den2); 44 cc = mpq_cmp (op1, op2); 45 mpq_clear (op2); 46 return cc; 47 } 48 #endif 49 50 /* need canonical sign to get right result */ 51 ASSERT (den1_size > 0); 52 53 if (den2 == 0) 54 DIVIDE_BY_ZERO; 55 56 if (num1_size == 0) 57 return -(num2 != 0); 58 if (num1_size < 0) 59 return num1_size; 60 if (num2 == 0) 61 return num1_size; 62 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. */ 67 return num1_size; 68 if (den1_size > num1_size + 1) 69 /* NUM1 x DEN2 is surely smaller in magnitude than NUM2 x DEN1. */ 70 return -num1_size; 71 72 TMP_MARK; 73 tmp1_ptr = (mp_ptr) TMP_ALLOC ((num1_size + 1) * BYTES_PER_MP_LIMB); 74 tmp2_ptr = (mp_ptr) TMP_ALLOC ((den1_size + 1) * BYTES_PER_MP_LIMB); 75 76 cy_limb = mpn_mul_1 (tmp1_ptr, op1->_mp_num._mp_d, num1_size, 77 (mp_limb_t) den2); 78 tmp1_ptr[num1_size] = cy_limb; 79 tmp1_size = num1_size + (cy_limb != 0); 80 81 cy_limb = mpn_mul_1 (tmp2_ptr, op1->_mp_den._mp_d, den1_size, 82 (mp_limb_t) num2); 83 tmp2_ptr[den1_size] = cy_limb; 84 tmp2_size = den1_size + (cy_limb != 0); 85 86 cc = tmp1_size - tmp2_size != 0 87 ? tmp1_size - tmp2_size : mpn_cmp (tmp1_ptr, tmp2_ptr, tmp1_size); 88 TMP_FREE; 89 return cc; 90 } 91