beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpq / cmp.c
blob1758b78a6031a3768458283ffb98fb66d7bbea10
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
19 later version.
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
26 for more details.
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/. */
32 #include "gmp.h"
33 #include "gmp-impl.h"
34 #include "longlong.h"
36 static int
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);
43 int op2_is_int;
44 mp_limb_t d1h, d2h;
45 mp_size_t tmp1_size, tmp2_size;
46 mp_ptr tmp1_ptr, tmp2_ptr;
47 mp_size_t num1_sign;
48 int cc;
49 TMP_DECL;
51 /* need canonical signs to get right result */
52 ASSERT (den1_size > 0);
53 ASSERT (den2_size > 0);
55 if (num1_size == 0)
56 return -num2_size;
57 if (num2_size == 0)
58 return num1_size;
59 if ((num1_size ^ num2_size) < 0) /* I.e. are the signs different? */
60 return num1_size;
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); */
72 int cmp;
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. */
93 return num1_sign;
94 if (tmp2_size + op2_is_int > tmp1_size + 1)
95 /* NUM1 x DEN2 is surely smaller in magnitude than NUM2 x DEN1. */
96 return -num1_sign;
98 /* 2. Same, but compare the number of significant bits. */
100 int cnt1, cnt2;
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)
112 return num1_sign;
113 if (bits2 + op2_is_int > bits1 + 1)
114 return -num1_sign;
117 /* 3. Finally, cross multiply and compare. */
119 TMP_MARK;
120 if (op2_is_int)
122 tmp2_ptr = TMP_ALLOC_LIMBS (tmp2_size);
123 tmp1_ptr = PTR(NUM(op1));
124 --tmp1_size;
126 else
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);
134 else
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);
144 else
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);
152 TMP_FREE;
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);