xref: /netbsd-src/external/lgpl3/gmp/dist/mpq/cmp_si.c (revision cef8759bd76c1b621f8eab8faa6f208faabc2e15)
1 /* _mpq_cmp_si -- compare mpq and long/ulong fraction.
2 
3 Copyright 2001, 2013, 2014 Free Software Foundation, Inc.
4 
5 This file is part of the GNU MP Library.
6 
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of either:
9 
10   * the GNU Lesser General Public License as published by the Free
11     Software Foundation; either version 3 of the License, or (at your
12     option) any later version.
13 
14 or
15 
16   * the GNU General Public License as published by the Free Software
17     Foundation; either version 2 of the License, or (at your option) any
18     later version.
19 
20 or both in parallel, as here.
21 
22 The GNU MP Library is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
25 for more details.
26 
27 You should have received copies of the GNU General Public License and the
28 GNU Lesser General Public License along with the GNU MP Library.  If not,
29 see https://www.gnu.org/licenses/.  */
30 
31 #include "gmp.h"
32 #include "gmp-impl.h"
33 
34 
35 /* Something like mpq_cmpabs_ui would be more useful for the neg/neg case,
36    and perhaps a version accepting a parameter to reverse the test, to make
37    it a tail call here.  */
38 
39 int
40 _mpq_cmp_si (mpq_srcptr q, long n, unsigned long d)
41 {
42   /* need canonical sign to get right result */
43   ASSERT (SIZ(DEN(q)) > 0);
44 
45   if (n >= 0)
46     return _mpq_cmp_ui (q, n, d);
47   if (SIZ(NUM(q)) >= 0)
48     {
49       return 1;                                /* >=0 cmp <0 */
50     }
51   else
52     {
53       mpq_t  qabs;
54       SIZ(NUM(qabs)) = -SIZ(NUM(q));
55       PTR(NUM(qabs)) = PTR(NUM(q));
56       SIZ(DEN(qabs)) = SIZ(DEN(q));
57       PTR(DEN(qabs)) = PTR(DEN(q));
58 
59       return - _mpq_cmp_ui (qabs, NEG_CAST (unsigned long, n), d);    /* <0 cmp <0 */
60     }
61 }
62