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 (C) 1991 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 the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 The GNU MP Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with the GNU MP Library; see the file COPYING. If not, write to
20 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
27 mpq_cmp (const MP_RAT *op1, const MP_RAT *op2)
34 mp_size num1_size = op1->num.size;
35 mp_size den1_size = op1->den.size;
36 mp_size num2_size = op2->num.size;
37 mp_size den2_size = op2->den.size;
38 mp_size tmp1_size, tmp2_size;
39 mp_ptr tmp1_ptr, tmp2_ptr;
47 if ((num1_size ^ num2_size) < 0) /* I.e. are the signs different? */
50 num1_sign = num1_size;
51 num1_size = ABS (num1_size);
52 num2_size = ABS (num2_size);
54 tmp1_size = num1_size + den2_size;
55 tmp2_size = num2_size + den1_size;
57 if (tmp1_size != tmp2_size)
58 return (tmp1_size - tmp2_size) ^ num1_sign;
60 tmp1_ptr = (mp_ptr) alloca (tmp1_size * BYTES_PER_MP_LIMB);
61 tmp2_ptr = (mp_ptr) alloca (tmp2_size * BYTES_PER_MP_LIMB);
63 tmp1_size = (num1_size >= den2_size)
64 ? mpn_mul (tmp1_ptr, op1->num.d, num1_size, op2->den.d, den2_size)
65 : mpn_mul (tmp1_ptr, op2->den.d, den2_size, op1->num.d, num1_size);
67 tmp2_size = (num2_size >= den1_size)
68 ? mpn_mul (tmp2_ptr, op2->num.d, num2_size, op1->den.d, den1_size)
69 : mpn_mul (tmp2_ptr, op1->den.d, den1_size, op2->num.d, num2_size);
71 cc = tmp1_size - tmp2_size != 0
72 ? tmp1_size - tmp2_size : mpn_cmp (tmp1_ptr, tmp2_ptr, tmp1_size);
75 return (num1_sign < 0) ? -cc : cc;