xref: /dflybsd-src/contrib/gmp/mpq/cmp_ui.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino /* mpq_cmp_ui(u,vn,vd) -- Compare U with Vn/Vd.  Return positive, zero, or
286d7f5d3SJohn Marino    negative based on if U > V, U == V, or U < V.  Vn and Vd may have
386d7f5d3SJohn Marino    common factors.
486d7f5d3SJohn Marino 
586d7f5d3SJohn Marino Copyright 1993, 1994, 1996, 2000, 2001, 2002, 2003, 2005 Free Software
686d7f5d3SJohn Marino Foundation, Inc.
786d7f5d3SJohn Marino 
886d7f5d3SJohn Marino This file is part of the GNU MP Library.
986d7f5d3SJohn Marino 
1086d7f5d3SJohn Marino The GNU MP Library is free software; you can redistribute it and/or modify
1186d7f5d3SJohn Marino it under the terms of the GNU Lesser General Public License as published by
1286d7f5d3SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
1386d7f5d3SJohn Marino option) any later version.
1486d7f5d3SJohn Marino 
1586d7f5d3SJohn Marino The GNU MP Library is distributed in the hope that it will be useful, but
1686d7f5d3SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1786d7f5d3SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
1886d7f5d3SJohn Marino License for more details.
1986d7f5d3SJohn Marino 
2086d7f5d3SJohn Marino You should have received a copy of the GNU Lesser General Public License
2186d7f5d3SJohn Marino along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
2286d7f5d3SJohn Marino 
2386d7f5d3SJohn Marino #include "gmp.h"
2486d7f5d3SJohn Marino #include "gmp-impl.h"
2586d7f5d3SJohn Marino 
2686d7f5d3SJohn Marino int
_mpq_cmp_ui(const MP_RAT * op1,unsigned long int num2,unsigned long int den2)2786d7f5d3SJohn Marino _mpq_cmp_ui (const MP_RAT *op1, unsigned long int num2, unsigned long int den2)
2886d7f5d3SJohn Marino {
2986d7f5d3SJohn Marino   mp_size_t num1_size = op1->_mp_num._mp_size;
3086d7f5d3SJohn Marino   mp_size_t den1_size = op1->_mp_den._mp_size;
3186d7f5d3SJohn Marino   mp_size_t tmp1_size, tmp2_size;
3286d7f5d3SJohn Marino   mp_ptr tmp1_ptr, tmp2_ptr;
3386d7f5d3SJohn Marino   mp_limb_t cy_limb;
3486d7f5d3SJohn Marino   int cc;
3586d7f5d3SJohn Marino   TMP_DECL;
3686d7f5d3SJohn Marino 
3786d7f5d3SJohn Marino #if GMP_NAIL_BITS != 0
3886d7f5d3SJohn Marino   if ((num2 | den2) > GMP_NUMB_MAX)
3986d7f5d3SJohn Marino     {
4086d7f5d3SJohn Marino       mpq_t op2;
4186d7f5d3SJohn Marino       mpq_init (op2);
4286d7f5d3SJohn Marino       mpz_set_ui (mpq_numref (op2), num2);
4386d7f5d3SJohn Marino       mpz_set_ui (mpq_denref (op2), den2);
4486d7f5d3SJohn Marino       cc = mpq_cmp (op1, op2);
4586d7f5d3SJohn Marino       mpq_clear (op2);
4686d7f5d3SJohn Marino       return cc;
4786d7f5d3SJohn Marino     }
4886d7f5d3SJohn Marino #endif
4986d7f5d3SJohn Marino 
5086d7f5d3SJohn Marino   /* need canonical sign to get right result */
5186d7f5d3SJohn Marino   ASSERT (den1_size > 0);
5286d7f5d3SJohn Marino 
5386d7f5d3SJohn Marino   if (den2 == 0)
5486d7f5d3SJohn Marino     DIVIDE_BY_ZERO;
5586d7f5d3SJohn Marino 
5686d7f5d3SJohn Marino   if (num1_size == 0)
5786d7f5d3SJohn Marino     return -(num2 != 0);
5886d7f5d3SJohn Marino   if (num1_size < 0)
5986d7f5d3SJohn Marino     return num1_size;
6086d7f5d3SJohn Marino   if (num2 == 0)
6186d7f5d3SJohn Marino     return num1_size;
6286d7f5d3SJohn Marino 
6386d7f5d3SJohn Marino   /* NUM1 x DEN2 is either TMP1_SIZE limbs or TMP1_SIZE-1 limbs.
6486d7f5d3SJohn Marino      Same for NUM1 x DEN1 with respect to TMP2_SIZE.  */
6586d7f5d3SJohn Marino   if (num1_size > den1_size + 1)
6686d7f5d3SJohn Marino     /* NUM1 x DEN2 is surely larger in magnitude than NUM2 x DEN1.  */
6786d7f5d3SJohn Marino     return num1_size;
6886d7f5d3SJohn Marino   if (den1_size > num1_size + 1)
6986d7f5d3SJohn Marino     /* NUM1 x DEN2 is surely smaller in magnitude than NUM2 x DEN1.  */
7086d7f5d3SJohn Marino     return -num1_size;
7186d7f5d3SJohn Marino 
7286d7f5d3SJohn Marino   TMP_MARK;
7386d7f5d3SJohn Marino   tmp1_ptr = TMP_ALLOC_LIMBS (num1_size + 1);
7486d7f5d3SJohn Marino   tmp2_ptr = TMP_ALLOC_LIMBS (den1_size + 1);
7586d7f5d3SJohn Marino 
7686d7f5d3SJohn Marino   cy_limb = mpn_mul_1 (tmp1_ptr, op1->_mp_num._mp_d, num1_size,
7786d7f5d3SJohn Marino                        (mp_limb_t) den2);
7886d7f5d3SJohn Marino   tmp1_ptr[num1_size] = cy_limb;
7986d7f5d3SJohn Marino   tmp1_size = num1_size + (cy_limb != 0);
8086d7f5d3SJohn Marino 
8186d7f5d3SJohn Marino   cy_limb = mpn_mul_1 (tmp2_ptr, op1->_mp_den._mp_d, den1_size,
8286d7f5d3SJohn Marino                        (mp_limb_t) num2);
8386d7f5d3SJohn Marino   tmp2_ptr[den1_size] = cy_limb;
8486d7f5d3SJohn Marino   tmp2_size = den1_size + (cy_limb != 0);
8586d7f5d3SJohn Marino 
8686d7f5d3SJohn Marino   cc = tmp1_size - tmp2_size != 0
8786d7f5d3SJohn Marino     ? tmp1_size - tmp2_size : mpn_cmp (tmp1_ptr, tmp2_ptr, tmp1_size);
8886d7f5d3SJohn Marino   TMP_FREE;
8986d7f5d3SJohn Marino   return cc;
9086d7f5d3SJohn Marino }
91