blob: 9d99a9a861c67785ae6db040946cda3b34d572c9 [file] [log] [blame]
Austin Schuhdace2a62020-08-18 10:56:48 -07001/* 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
5Copyright 1993, 1994, 1996, 2000-2003, 2005, 2014, 2018 Free Software
6Foundation, Inc.
7
8This file is part of the GNU MP Library.
9
10The GNU MP Library is free software; you can redistribute it and/or modify
11it under the terms of either:
12
13 * the GNU Lesser General Public License as published by the Free
14 Software Foundation; either version 3 of the License, or (at your
15 option) any later version.
16
17or
18
19 * the GNU General Public License as published by the Free Software
20 Foundation; either version 2 of the License, or (at your option) any
21 later version.
22
23or both in parallel, as here.
24
25The GNU MP Library is distributed in the hope that it will be useful, but
26WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
27or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
28for more details.
29
30You should have received copies of the GNU General Public License and the
31GNU Lesser General Public License along with the GNU MP Library. If not,
32see https://www.gnu.org/licenses/. */
33
34#include "gmp-impl.h"
35
36int
37_mpq_cmp_ui (mpq_srcptr op1, unsigned long int num2, unsigned long int den2)
38{
39 mp_size_t num1_size = SIZ(NUM(op1));
40 mp_size_t den1_size = SIZ(DEN(op1));
41 mp_size_t tmp1_size, tmp2_size;
42 mp_ptr tmp1_ptr, tmp2_ptr;
43 mp_limb_t cy_limb;
44 int cc;
45 TMP_DECL;
46
47#if GMP_NAIL_BITS != 0
48 if ((num2 | den2) > GMP_NUMB_MAX)
49 {
50 mpq_t op2;
51 mpq_init (op2);
52 mpz_set_ui (mpq_numref (op2), num2);
53 mpz_set_ui (mpq_denref (op2), den2);
54 cc = mpq_cmp (op1, op2);
55 mpq_clear (op2);
56 return cc;
57 }
58#endif
59
60 /* need canonical sign to get right result */
61 ASSERT (den1_size > 0);
62
63 if (UNLIKELY (den2 == 0))
64 DIVIDE_BY_ZERO;
65
66 if (num2 == 0)
67 return num1_size;
68 if (num1_size <= 0)
69 return -1;
70
71 /* NUM1 x DEN2 is either TMP1_SIZE limbs or TMP1_SIZE-1 limbs.
72 Same for NUM2 x DEN1 with respect to TMP2_SIZE. */
73 /* If frac2 <= 1 (i.e. num2 <= den2), shortcut with a simpler
74 condition: num1 > den1. Here we only test sizes. */
75 if (num1_size > den1_size + (num2 > den2))
76 /* NUM1 x DEN2 is surely larger in magnitude than NUM2 x DEN1. */
77 return num1_size;
78 if (den1_size > num1_size + (den2 > num2))
79 /* NUM1 x DEN2 is surely smaller in magnitude than NUM2 x DEN1. */
80 return -num1_size;
81
82 TMP_MARK;
83 TMP_ALLOC_LIMBS_2 (tmp1_ptr, num1_size + 1, tmp2_ptr, den1_size + 1);
84
85 cy_limb = mpn_mul_1 (tmp1_ptr, PTR(NUM(op1)), num1_size,
86 (mp_limb_t) den2);
87 tmp1_ptr[num1_size] = cy_limb;
88 tmp1_size = num1_size + (cy_limb != 0);
89
90 cy_limb = mpn_mul_1 (tmp2_ptr, PTR(DEN(op1)), den1_size,
91 (mp_limb_t) num2);
92 tmp2_ptr[den1_size] = cy_limb;
93 tmp2_size = den1_size + (cy_limb != 0);
94
95 cc = tmp1_size - tmp2_size;
96 cc = cc != 0 ? cc : mpn_cmp (tmp1_ptr, tmp2_ptr, tmp1_size);
97 TMP_FREE;
98 return cc;
99}