Austin Schuh | bb1338c | 2024-06-15 19:31:16 -0700 | [diff] [blame] | 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-impl.h" |
| 32 | |
| 33 | |
| 34 | /* Something like mpq_cmpabs_ui would be more useful for the neg/neg case, |
| 35 | and perhaps a version accepting a parameter to reverse the test, to make |
| 36 | it a tail call here. */ |
| 37 | |
| 38 | int |
| 39 | _mpq_cmp_si (mpq_srcptr q, long n, unsigned long d) |
| 40 | { |
| 41 | /* need canonical sign to get right result */ |
| 42 | ASSERT (SIZ(DEN(q)) > 0); |
| 43 | |
| 44 | if (n >= 0) |
| 45 | return _mpq_cmp_ui (q, n, d); |
| 46 | if (SIZ(NUM(q)) >= 0) |
| 47 | { |
| 48 | return 1; /* >=0 cmp <0 */ |
| 49 | } |
| 50 | else |
| 51 | { |
| 52 | mpq_t qabs; |
| 53 | SIZ(NUM(qabs)) = -SIZ(NUM(q)); |
| 54 | PTR(NUM(qabs)) = PTR(NUM(q)); |
| 55 | SIZ(DEN(qabs)) = SIZ(DEN(q)); |
| 56 | PTR(DEN(qabs)) = PTR(DEN(q)); |
| 57 | |
| 58 | return - _mpq_cmp_ui (qabs, NEG_CAST (unsigned long, n), d); /* <0 cmp <0 */ |
| 59 | } |
| 60 | } |